OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
svd.cpp
Go to the documentation of this file.
1 //$$svd.cpp singular value decomposition
2 
3 // Copyright (C) 1991,2,3,4,5: R B Davies
4 // Updated 17 July, 1995
5 
6 #define WANT_MATH
7 
8 #include <ossim/matrix/include.h>
10 #include <ossim/matrix/newmatrm.h>
11 #include <ossim/matrix/precisio.h>
12 
13 #ifdef use_namespace
14 namespace NEWMAT {
15 #endif
16 
17 #ifdef DO_REPORT
18 #define REPORT { static ExeCounter ExeCount(__LINE__,15); ++ExeCount; }
19 #else
20 #define REPORT {}
21 #endif
22 
23 
24 
25 
26 void SVD(const Matrix& A, DiagonalMatrix& Q, Matrix& U, Matrix& V,
27  bool withU, bool withV)
28 // from Wilkinson and Reinsch: "Handbook of Automatic Computation"
29 {
30  REPORT
31  Tracer trace("SVD");
32  Real eps = FloatingPointPrecision::Epsilon();
34 
35  int m = A.Nrows(); int n = A.Ncols();
36  if (m<n)
37  Throw(ProgramException("Want no. Rows >= no. Cols", A));
38  if (withV && &U == &V)
39  Throw(ProgramException("Need different matrices for U and V", U, V));
40  U = A; Real g = 0.0; Real f,h; Real x = 0.0; int i;
41  RowVector E(n); RectMatrixRow EI(E,0); Q.ReSize(n);
42  RectMatrixCol UCI(U,0); RectMatrixRow URI(U,0,1,n-1);
43 
44  if (n) for (i=0;;)
45  {
46  EI.First() = g; Real ei = g; EI.Right(); Real s = UCI.SumSquare();
47  if (s<tol) { REPORT Q.element(i) = 0.0; }
48  else
49  {
50  REPORT
51  f = UCI.First(); g = -sign(sqrt(s), f); h = f*g-s; UCI.First() = f-g;
52  Q.element(i) = g; RectMatrixCol UCJ = UCI; int j=n-i;
53  while (--j) { UCJ.Right(); UCJ.AddScaled(UCI, (UCI*UCJ)/h); }
54  }
55 
56  s = URI.SumSquare();
57  if (s<tol) { REPORT g = 0.0; }
58  else
59  {
60  REPORT
61  f = URI.First(); g = -sign(sqrt(s), f); URI.First() = f-g;
62  EI.Divide(URI,f*g-s); RectMatrixRow URJ = URI; int j=m-i;
63  while (--j) { URJ.Down(); URJ.AddScaled(EI, URI*URJ); }
64  }
65 
66  Real y = fabs(Q.element(i)) + fabs(ei); if (x<y) { REPORT x = y; }
67  if (++i == n) { REPORT break; }
68  UCI.DownDiag(); URI.DownDiag();
69  }
70 
71  if (withV)
72  {
73  REPORT
74  V.ReSize(n,n); V = 0.0; RectMatrixCol VCI(V,n-1,n-1,1);
75  if (n) { VCI.First() = 1.0; g=E.element(n-1); if (n!=1) URI.UpDiag(); }
76  for (i=n-2; i>=0; i--)
77  {
78  VCI.Left();
79  if (g!=0.0)
80  {
81  VCI.Divide(URI, URI.First()*g); int j = n-i;
82  RectMatrixCol VCJ = VCI;
83  while (--j) { VCJ.Right(); VCJ.AddScaled( VCI, (URI*VCJ) ); }
84  }
85  VCI.Zero(); VCI.Up(); VCI.First() = 1.0; g=E.element(i);
86  if (i==0) break;
87  URI.UpDiag();
88  }
89  }
90 
91  if (withU)
92  {
93  REPORT
94  for (i=n-1; i>=0; i--)
95  {
96  g = Q.element(i); URI.Reset(U,i,i+1,n-i-1); URI.Zero();
97  if (g!=0.0)
98  {
99  h=UCI.First()*g; int j=n-i; RectMatrixCol UCJ = UCI;
100  while (--j)
101  {
102  UCJ.Right(); UCI.Down(); UCJ.Down(); Real s = UCI*UCJ;
103  UCI.Up(); UCJ.Up(); UCJ.AddScaled(UCI,s/h);
104  }
105  UCI.Divide(g);
106  }
107  else UCI.Zero();
108  UCI.First() += 1.0;
109  if (i==0) break;
110  UCI.UpDiag();
111  }
112  }
113 
114  eps *= x;
115  for (int k=n-1; k>=0; k--)
116  {
117  Real z = -FloatingPointPrecision::Maximum(); // to keep Gnu happy
118  Real y; int limit = 50; int l = 0;
119  while (limit--)
120  {
121  Real c, s; int i; int l1=k; bool tfc=false;
122  for (l=k; l>=0; l--)
123  {
124 // if (fabs(E.element(l))<=eps) goto test_f_convergence;
125  if (fabs(E.element(l))<=eps) { REPORT tfc=true; break; }
126  if (fabs(Q.element(l-1))<=eps) { REPORT l1=l; break; }
127  REPORT
128  }
129  if (!tfc)
130  {
131  REPORT
132  l=l1; l1=l-1; s = -1.0; c = 0.0;
133  for (i=l; i<=k; i++)
134  {
135  f = - s * E.element(i); E.element(i) *= c;
136 // if (fabs(f)<=eps) goto test_f_convergence;
137  if (fabs(f)<=eps) { REPORT break; }
138  g = Q.element(i); h = pythag(g,f,c,s); Q.element(i) = h;
139  if (withU)
140  {
141  REPORT
142  RectMatrixCol UCI(U,i); RectMatrixCol UCJ(U,l1);
143  ComplexScale(UCJ, UCI, c, s);
144  }
145  }
146  }
147 // test_f_convergence: z = Q.element(k); if (l==k) goto convergence;
148  z = Q.element(k); if (l==k) { REPORT break; }
149 
150  x = Q.element(l); y = Q.element(k-1);
151  g = E.element(k-1); h = E.element(k);
152  f = ((y-z)*(y+z) + (g-h)*(g+h)) / (2*h*y);
153  if (f>1) { REPORT g = f * sqrt(1 + square(1/f)); }
154  else if (f<-1) { REPORT g = -f * sqrt(1 + square(1/f)); }
155  else { REPORT g = sqrt(f*f + 1); }
156  { REPORT f = ((x-z)*(x+z) + h*(y / ((f<0.0) ? f-g : f+g)-h)) / x; }
157 
158  c = 1.0; s = 1.0;
159  for (i=l+1; i<=k; i++)
160  {
161  g = E.element(i); y = Q.element(i); h = s*g; g *= c;
162  z = pythag(f,h,c,s); E.element(i-1) = z;
163  f = x*c + g*s; g = -x*s + g*c; h = y*s; y *= c;
164  if (withV)
165  {
166  REPORT
167  RectMatrixCol VCI(V,i); RectMatrixCol VCJ(V,i-1);
168  ComplexScale(VCI, VCJ, c, s);
169  }
170  z = pythag(f,h,c,s); Q.element(i-1) = z;
171  f = c*g + s*y; x = -s*g + c*y;
172  if (withU)
173  {
174  REPORT
175  RectMatrixCol UCI(U,i); RectMatrixCol UCJ(U,i-1);
176  ComplexScale(UCI, UCJ, c, s);
177  }
178  }
179  E.element(l) = 0.0; E.element(k) = f; Q.element(k) = x;
180  }
181  if (l!=k) { Throw(ConvergenceException(A)); }
182 // convergence:
183  if (z < 0.0)
184  {
185  REPORT
186  Q.element(k) = -z;
187  if (withV) { RectMatrixCol VCI(V,k); VCI.Negate(); }
188  }
189  }
190  if (withU & withV) SortSV(Q, U, V);
191  else if (withU) SortSV(Q, U);
192  else if (withV) SortSV(Q, V);
193  else SortDescending(Q);
194 }
195 
196 void SVD(const Matrix& A, DiagonalMatrix& D)
197 { REPORT Matrix U; SVD(A, D, U, U, false, false); }
198 
199 
200 
201 #ifdef use_namespace
202 }
203 #endif
204 
Real SumSquare() const
Definition: newmatrm.cpp:52
ossim_uint32 x
double Real
Definition: include.h:57
Real sign(Real x, Real y)
Definition: newmatrm.h:108
void SVD(const Matrix &A, DiagonalMatrix &Q, Matrix &U, Matrix &V, bool withU, bool withV)
Definition: svd.cpp:26
Real Maximum(const BaseMatrix &B)
Definition: newmat.h:1766
ossim_uint32 y
#define REPORT
Definition: svd.cpp:20
Real & element(int, int)
Definition: newmat6.cpp:725
T square(T x)
Definition: ossimCommon.h:334
void Up()
Definition: newmatrm.h:72
#define A(r, c)
Real pythag(Real f, Real g, Real &c, Real &s)
Definition: newmatrm.cpp:186
void Reset(const Matrix &, int, int, int)
Definition: newmatrm.cpp:25
virtual void ReSize(int, int)
Definition: newmat4.cpp:233
void ComplexScale(RectMatrixCol &U, RectMatrixCol &V, Real x, Real y)
Definition: newmatrm.cpp:132
os2<< "> n<< " > nendobj n
void Left()
Definition: newmatrm.h:73
void Divide(const RectMatrixRowCol &, Real)
Definition: newmatrm.cpp:94
Definition: newmat.h:543
void UpDiag()
Definition: newmatrm.h:42
Real & First()
Definition: newmatrm.h:40
void ReSize(int)
Definition: newmat4.cpp:256
void DownDiag()
Definition: newmatrm.h:41
void AddScaled(const RectMatrixRowCol &, Real)
Definition: newmatrm.cpp:79
void SortDescending(GeneralMatrix &)
Definition: sort.cpp:45
void Right()
Definition: newmatrm.h:57
Real Minimum(const BaseMatrix &B)
Definition: newmat.h:1767
void Down()
Definition: newmatrm.h:56
void SortSV(DiagonalMatrix &D, Matrix &U, bool ascending=false)
Definition: sort.cpp:190
void Right()
Definition: newmatrm.h:71
Real & element(int)
Definition: newmat6.cpp:769
void Down()
Definition: newmatrm.h:70