Homepage Demos Overview Downloads Tutorials Reference
Credits

svd.cpp

Go to the documentation of this file.
00001 //$$svd.cpp                           singular value decomposition
00002 
00003 // Copyright (C) 1991,2,3,4,5: R B Davies
00004 // Updated 17 July, 1995
00005 
00006 #define WANT_MATH
00007 
00008 #include "include.h"
00009 #include "newmatap.h"
00010 #include "newmatrm.h"
00011 #include "precisio.h"
00012 
00013 #ifdef use_namespace
00014 namespace NEWMAT {
00015 #endif
00016 
00017 #ifdef DO_REPORT
00018 #define REPORT { static ExeCounter ExeCount(__LINE__,15); ++ExeCount; }
00019 #else
00020 #define REPORT {}
00021 #endif
00022 
00023 
00024 
00025 
00026 void SVD(const Matrix& A, DiagonalMatrix& Q, Matrix& U, Matrix& V,
00027    bool withU, bool withV)
00028 // from Wilkinson and Reinsch: "Handbook of Automatic Computation"
00029 {
00030    REPORT
00031    Tracer trace("SVD");
00032    Real eps = FloatingPointPrecision::Epsilon();
00033    Real tol = FloatingPointPrecision::Minimum()/eps;
00034 
00035    int m = A.Nrows(); int n = A.Ncols();
00036    if (m<n)
00037       Throw(ProgramException("Want no. Rows >= no. Cols", A));
00038    if (withV && &U == &V)
00039       Throw(ProgramException("Need different matrices for U and V", U, V));
00040    U = A; Real g = 0.0; Real f,h; Real x = 0.0; int i;
00041    RowVector E(n); RectMatrixRow EI(E,0); Q.ReSize(n);
00042    RectMatrixCol UCI(U,0); RectMatrixRow URI(U,0,1,n-1);
00043 
00044    if (n) for (i=0;;)
00045    {
00046       EI.First() = g; Real ei = g; EI.Right(); Real s = UCI.SumSquare();
00047       if (s<tol) { REPORT Q.element(i) = 0.0; }
00048       else
00049       {
00050          REPORT
00051          f = UCI.First(); g = -sign(sqrt(s), f); h = f*g-s; UCI.First() = f-g;
00052          Q.element(i) = g; RectMatrixCol UCJ = UCI; int j=n-i;
00053          while (--j) { UCJ.Right(); UCJ.AddScaled(UCI, (UCI*UCJ)/h); }
00054       }
00055 
00056       s = URI.SumSquare();
00057       if (s<tol) { REPORT g = 0.0; }
00058       else
00059       {
00060          REPORT
00061          f = URI.First(); g = -sign(sqrt(s), f); URI.First() = f-g;
00062          EI.Divide(URI,f*g-s); RectMatrixRow URJ = URI; int j=m-i;
00063          while (--j) { URJ.Down(); URJ.AddScaled(EI, URI*URJ); }
00064       }
00065 
00066       Real y = fabs(Q.element(i)) + fabs(ei); if (x<y) { REPORT x = y; }
00067       if (++i == n) { REPORT break; }
00068       UCI.DownDiag(); URI.DownDiag();
00069    }
00070 
00071    if (withV)
00072    {
00073       REPORT
00074       V.ReSize(n,n); V = 0.0; RectMatrixCol VCI(V,n-1,n-1,1);
00075       if (n) { VCI.First() = 1.0; g=E.element(n-1); if (n!=1) URI.UpDiag(); }
00076       for (i=n-2; i>=0; i--)
00077       {
00078          VCI.Left();
00079          if (g!=0.0)
00080          {
00081             VCI.Divide(URI, URI.First()*g); int j = n-i;
00082             RectMatrixCol VCJ = VCI;
00083             while (--j) { VCJ.Right(); VCJ.AddScaled( VCI, (URI*VCJ) ); }
00084          }
00085          VCI.Zero(); VCI.Up(); VCI.First() = 1.0; g=E.element(i);
00086          if (i==0) break;
00087          URI.UpDiag();
00088       }
00089    }
00090 
00091    if (withU)
00092    {
00093       REPORT
00094       for (i=n-1; i>=0; i--)
00095       {
00096          g = Q.element(i); URI.Reset(U,i,i+1,n-i-1); URI.Zero();
00097          if (g!=0.0)
00098          {
00099             h=UCI.First()*g; int j=n-i; RectMatrixCol UCJ = UCI;
00100             while (--j)
00101             {
00102                UCJ.Right(); UCI.Down(); UCJ.Down(); Real s = UCI*UCJ;
00103                UCI.Up(); UCJ.Up(); UCJ.AddScaled(UCI,s/h);
00104             }
00105             UCI.Divide(g);
00106          }
00107          else UCI.Zero();
00108          UCI.First() += 1.0;
00109          if (i==0) break;
00110          UCI.UpDiag();
00111       }
00112    }
00113 
00114    eps *= x;
00115    for (int k=n-1; k>=0; k--)
00116    {
00117       Real z = -FloatingPointPrecision::Maximum(); // to keep Gnu happy
00118       Real y; int limit = 50; int l = 0;
00119       while (limit--)
00120       {
00121          Real c, s; int i; int l1=k; bool tfc=false;
00122          for (l=k; l>=0; l--)
00123          {
00124 //          if (fabs(E.element(l))<=eps) goto test_f_convergence;
00125             if (fabs(E.element(l))<=eps) { REPORT tfc=true; break; }
00126             if (fabs(Q.element(l-1))<=eps) { REPORT l1=l; break; }
00127             REPORT
00128          }
00129          if (!tfc)
00130          {
00131             REPORT
00132             l=l1; l1=l-1; s = -1.0; c = 0.0;
00133             for (i=l; i<=k; i++)
00134             {
00135                f = - s * E.element(i); E.element(i) *= c;
00136 //             if (fabs(f)<=eps) goto test_f_convergence;
00137                if (fabs(f)<=eps) { REPORT break; }
00138                g = Q.element(i); h = pythag(g,f,c,s); Q.element(i) = h;
00139                if (withU)
00140                {
00141                   REPORT
00142                   RectMatrixCol UCI(U,i); RectMatrixCol UCJ(U,l1);
00143                   ComplexScale(UCJ, UCI, c, s);
00144                }
00145             }
00146          }
00147 //       test_f_convergence: z = Q.element(k); if (l==k) goto convergence;
00148          z = Q.element(k);  if (l==k) { REPORT break; }
00149 
00150          x = Q.element(l); y = Q.element(k-1);
00151          g = E.element(k-1); h = E.element(k);
00152          f = ((y-z)*(y+z) + (g-h)*(g+h)) / (2*h*y);
00153          if (f>1)         { REPORT g = f * sqrt(1 + square(1/f)); }
00154          else if (f<-1)   { REPORT g = -f * sqrt(1 + square(1/f)); }
00155          else             { REPORT g = sqrt(f*f + 1); }
00156             { REPORT f = ((x-z)*(x+z) + h*(y / ((f<0.0) ? f-g : f+g)-h)) / x; }
00157 
00158          c = 1.0; s = 1.0;
00159          for (i=l+1; i<=k; i++)
00160          {
00161             g = E.element(i); y = Q.element(i); h = s*g; g *= c;
00162             z = pythag(f,h,c,s); E.element(i-1) = z;
00163             f = x*c + g*s; g = -x*s + g*c; h = y*s; y *= c;
00164             if (withV)
00165             {
00166                REPORT
00167                RectMatrixCol VCI(V,i); RectMatrixCol VCJ(V,i-1);
00168                ComplexScale(VCI, VCJ, c, s);
00169             }
00170             z = pythag(f,h,c,s); Q.element(i-1) = z;
00171             f = c*g + s*y; x = -s*g + c*y;
00172             if (withU)
00173             {
00174                REPORT
00175                RectMatrixCol UCI(U,i); RectMatrixCol UCJ(U,i-1);
00176                ComplexScale(UCI, UCJ, c, s);
00177             }
00178          }
00179          E.element(l) = 0.0; E.element(k) = f; Q.element(k) = x;
00180       }
00181       if (l!=k) { Throw(ConvergenceException(A)); }
00182 // convergence:
00183       if (z < 0.0)
00184       {
00185          REPORT
00186          Q.element(k) = -z;
00187          if (withV) { RectMatrixCol VCI(V,k); VCI.Negate(); }
00188       }
00189    }
00190    if (withU & withV) SortSV(Q, U, V);
00191    else if (withU) SortSV(Q, U);
00192    else if (withV) SortSV(Q, V);
00193    else SortDescending(Q);
00194 }
00195 
00196 void SVD(const Matrix& A, DiagonalMatrix& D)
00197 { REPORT Matrix U; SVD(A, D, U, U, false, false); }
00198 
00199 
00200 
00201 #ifdef use_namespace
00202 }
00203 #endif
00204 

newmat11b
Generated Tue Nov 23 16:35:40 2004 by Doxygen 1.3.9.1