MOOS 0.2375
|
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 static Real pythag(Real f, Real g, Real& c, Real& s) 00025 // return z=sqrt(f*f+g*g), c=f/z, s=g/z 00026 // set c=1,s=0 if z==0 00027 // avoid floating point overflow or divide by zero 00028 { 00029 if (f==0 && g==0) { c=1.0; s=0.0; return 0.0; } 00030 Real af = f>=0 ? f : -f; 00031 Real ag = g>=0 ? g : -g; 00032 if (ag<af) 00033 { 00034 REPORT 00035 Real h = g/f; Real sq = sqrt(1.0+h*h); 00036 if (f<0) sq = -sq; // make return value non-negative 00037 c = 1.0/sq; s = h/sq; return sq*f; 00038 } 00039 else 00040 { 00041 REPORT 00042 Real h = f/g; Real sq = sqrt(1.0+h*h); 00043 if (g<0) sq = -sq; 00044 s = 1.0/sq; c = h/sq; return sq*g; 00045 } 00046 } 00047 00048 00049 void SVD(const Matrix& A, DiagonalMatrix& Q, Matrix& U, Matrix& V, 00050 bool withU, bool withV) 00051 // from Wilkinson and Reinsch: "Handbook of Automatic Computation" 00052 { 00053 REPORT 00054 Tracer trace("SVD"); 00055 Real eps = FloatingPointPrecision::Epsilon(); 00056 Real tol = FloatingPointPrecision::Minimum()/eps; 00057 00058 int m = A.Nrows(); int n = A.Ncols(); 00059 if (m<n) 00060 Throw(ProgramException("Want no. Rows >= no. Cols", A)); 00061 if (withV && &U == &V) 00062 Throw(ProgramException("Need different matrices for U and V", U, V)); 00063 U = A; Real g = 0.0; Real f,h; Real x = 0.0; int i; 00064 RowVector E(n); RectMatrixRow EI(E,0); Q.ReSize(n); 00065 RectMatrixCol UCI(U,0); RectMatrixRow URI(U,0,1,n-1); 00066 00067 if (n) for (i=0;;) 00068 { 00069 EI.First() = g; Real ei = g; EI.Right(); Real s = UCI.SumSquare(); 00070 if (s<tol) { REPORT Q.element(i) = 0.0; } 00071 else 00072 { 00073 REPORT 00074 f = UCI.First(); g = -sign(sqrt(s), f); h = f*g-s; UCI.First() = f-g; 00075 Q.element(i) = g; RectMatrixCol UCJ = UCI; int j=n-i; 00076 while (--j) { UCJ.Right(); UCJ.AddScaled(UCI, (UCI*UCJ)/h); } 00077 } 00078 00079 s = URI.SumSquare(); 00080 if (s<tol) { REPORT g = 0.0; } 00081 else 00082 { 00083 REPORT 00084 f = URI.First(); g = -sign(sqrt(s), f); URI.First() = f-g; 00085 EI.Divide(URI,f*g-s); RectMatrixRow URJ = URI; int j=m-i; 00086 while (--j) { URJ.Down(); URJ.AddScaled(EI, URI*URJ); } 00087 } 00088 00089 Real y = fabs(Q.element(i)) + fabs(ei); if (x<y) { REPORT x = y; } 00090 if (++i == n) { REPORT break; } 00091 UCI.DownDiag(); URI.DownDiag(); 00092 } 00093 00094 if (withV) 00095 { 00096 REPORT 00097 V.ReSize(n,n); V = 0.0; RectMatrixCol VCI(V,n-1,n-1,1); 00098 if (n) { VCI.First() = 1.0; g=E.element(n-1); if (n!=1) URI.UpDiag(); } 00099 for (i=n-2; i>=0; i--) 00100 { 00101 VCI.Left(); 00102 if (g!=0.0) 00103 { 00104 VCI.Divide(URI, URI.First()*g); int j = n-i; 00105 RectMatrixCol VCJ = VCI; 00106 while (--j) { VCJ.Right(); VCJ.AddScaled( VCI, (URI*VCJ) ); } 00107 } 00108 VCI.Zero(); VCI.Up(); VCI.First() = 1.0; g=E.element(i); 00109 if (i==0) break; 00110 URI.UpDiag(); 00111 } 00112 } 00113 00114 if (withU) 00115 { 00116 REPORT 00117 for (i=n-1; i>=0; i--) 00118 { 00119 g = Q.element(i); URI.Reset(U,i,i+1,n-i-1); URI.Zero(); 00120 if (g!=0.0) 00121 { 00122 h=UCI.First()*g; int j=n-i; RectMatrixCol UCJ = UCI; 00123 while (--j) 00124 { 00125 UCJ.Right(); UCI.Down(); UCJ.Down(); Real s = UCI*UCJ; 00126 UCI.Up(); UCJ.Up(); UCJ.AddScaled(UCI,s/h); 00127 } 00128 UCI.Divide(g); 00129 } 00130 else UCI.Zero(); 00131 UCI.First() += 1.0; 00132 if (i==0) break; 00133 UCI.UpDiag(); 00134 } 00135 } 00136 00137 eps *= x; 00138 for (int k=n-1; k>=0; k--) 00139 { 00140 Real z = -FloatingPointPrecision::Maximum(); // to keep Gnu happy 00141 Real y; int limit = 50; int l = 0; 00142 while (limit--) 00143 { 00144 Real c, s; int i; int l1=k; bool tfc=false; 00145 for (l=k; l>=0; l--) 00146 { 00147 // if (fabs(E.element(l))<=eps) goto test_f_convergence; 00148 if (fabs(E.element(l))<=eps) { REPORT tfc=true; break; } 00149 if (fabs(Q.element(l-1))<=eps) { REPORT l1=l; break; } 00150 REPORT 00151 } 00152 if (!tfc) 00153 { 00154 REPORT 00155 l=l1; l1=l-1; s = -1.0; c = 0.0; 00156 for (i=l; i<=k; i++) 00157 { 00158 f = - s * E.element(i); E.element(i) *= c; 00159 // if (fabs(f)<=eps) goto test_f_convergence; 00160 if (fabs(f)<=eps) { REPORT break; } 00161 g = Q.element(i); h = pythag(g,f,c,s); Q.element(i) = h; 00162 if (withU) 00163 { 00164 REPORT 00165 RectMatrixCol UCI(U,i); RectMatrixCol UCJ(U,l1); 00166 ComplexScale(UCJ, UCI, c, s); 00167 } 00168 } 00169 } 00170 // test_f_convergence: z = Q.element(k); if (l==k) goto convergence; 00171 z = Q.element(k); if (l==k) { REPORT break; } 00172 00173 x = Q.element(l); y = Q.element(k-1); 00174 g = E.element(k-1); h = E.element(k); 00175 f = ((y-z)*(y+z) + (g-h)*(g+h)) / (2*h*y); 00176 if (f>1) { REPORT g = f * sqrt(1 + square(1/f)); } 00177 else if (f<-1) { REPORT g = -f * sqrt(1 + square(1/f)); } 00178 else { REPORT g = sqrt(f*f + 1); } 00179 { REPORT f = ((x-z)*(x+z) + h*(y / ((f<0.0) ? f-g : f+g)-h)) / x; } 00180 00181 c = 1.0; s = 1.0; 00182 for (i=l+1; i<=k; i++) 00183 { 00184 g = E.element(i); y = Q.element(i); h = s*g; g *= c; 00185 z = pythag(f,h,c,s); E.element(i-1) = z; 00186 f = x*c + g*s; g = -x*s + g*c; h = y*s; y *= c; 00187 if (withV) 00188 { 00189 REPORT 00190 RectMatrixCol VCI(V,i); RectMatrixCol VCJ(V,i-1); 00191 ComplexScale(VCI, VCJ, c, s); 00192 } 00193 z = pythag(f,h,c,s); Q.element(i-1) = z; 00194 f = c*g + s*y; x = -s*g + c*y; 00195 if (withU) 00196 { 00197 REPORT 00198 RectMatrixCol UCI(U,i); RectMatrixCol UCJ(U,i-1); 00199 ComplexScale(UCI, UCJ, c, s); 00200 } 00201 } 00202 E.element(l) = 0.0; E.element(k) = f; Q.element(k) = x; 00203 } 00204 if (l!=k) { Throw(ConvergenceException(A)); } 00205 // convergence: 00206 if (z < 0.0) 00207 { 00208 REPORT 00209 Q.element(k) = -z; 00210 if (withV) { RectMatrixCol VCI(V,k); VCI.Negate(); } 00211 } 00212 } 00213 if (withU & withV) SortSV(Q, U, V); 00214 else if (withU) SortSV(Q, U); 00215 else if (withV) SortSV(Q, V); 00216 else SortDescending(Q); 00217 } 00218 00219 void SVD(const Matrix& A, DiagonalMatrix& D) 00220 { REPORT Matrix U; SVD(A, D, U, U, false, false); } 00221 00222 00223 00224 #ifdef use_namespace 00225 } 00226 #endif 00227