33 eps = std::numeric_limits< Float >::epsilon();
34 if( !decompose() )
return false;
35 if( !reorder() )
return false;
37 tsh = 0.5*grt_sqrt(m+n+1.)*w[0]*eps;
45 if(b.size() != m || x.size() != n){
49 tsh = (thresh >= 0. ? thresh : 0.5*grt_sqrt(m+n+1.)*w[0]*eps);
53 for (i=0;i<m;i++) s += u[i][j]*b[i];
60 for (jj=0;jj<n;jj++) s += v[j][jj]*tmp[jj];
73 for (i=0;i<n;i++) xx[i] = b[i][j];
74 solveVector(xx,xx,thresh);
75 for (i=0;i<n;i++) x[i][j] = xx[i];
79 UINT SVD::rank(Float thresh) {
81 tsh = (thresh >= 0. ? thresh : 0.5*grt_sqrt(m+n+1.)*w[0]*eps);
82 for (j=0;j<n;j++) if (w[j] > tsh) nr++;
86 UINT SVD::nullity(Float thresh) {
88 tsh = (thresh >= 0. ? thresh : 0.5*grt_sqrt(m+n+1.)*w[0]*eps);
89 for (j=0;j<n;j++)
if (w[j] <= tsh) nn++;
98 for (i=0;i<m;i++) rnge[i][nr] = u[i][j];
110 for (jj=0;jj<n;jj++) nullsp[jj][nn] = v[jj][j];
117 Float SVD::inv_condition() {
118 return (w[0] <= 0. || w[n-1] <= 0.) ? 0. : w[n-1]/w[0];
121 bool SVD::decompose() {
123 int i,its,j,jj,k,l,nm,N,M;
124 Float anorm,c,f,g,h,s,scale,x,y,z;
126 g = scale = anorm = 0.0;
135 for (k=i;k<M;k++) scale += fabs(u[k][i]);
142 g = -SIGN(grt_sqrt(s),f);
145 for (j=l-1;j<N;j++) {
146 for (s=0.0,k=i;k<M;k++) s += u[k][i] * u[k][j];
148 for (k=i;k<M;k++) u[k][j] += f * u[k][i];
150 for (k=i;k<M;k++) u[k][i] *= scale;
155 if (i+1 <= M && i+1 != N) {
156 for (k=l-1;k<N;k++) scale += fabs(u[i][k]);
158 for (k=l-1;k<N;k++) {
160 s += u[i][k]*u[i][k];
163 g = -SIGN(grt_sqrt(s),f);
166 for (k=l-1;k<N;k++) rv1[k]=u[i][k]/h;
167 for (j=l-1;j<M;j++) {
168 for (s=0.0,k=l-1;k<N;k++) s += u[j][k]*u[i][k];
169 for (k=l-1;k<N;k++) u[j][k] += s*rv1[k];
171 for (k=l-1;k<N;k++) u[i][k]*= scale;
174 anorm=MAX(anorm,(fabs(w[i])+fabs(rv1[i])));
176 for (i=N-1;i>=0;i--) {
180 v[j][i]=u[i][j]/u[i][l]/g;
182 for (s=0.0,k=l;k<N;k++) s += u[i][k]*v[k][j];
183 for (k=l;k<N;k++) v[k][j] += s*v[k][i];
186 for (j=l;j<N;j++) v[i][j]=v[j][i]=0.0;
192 for (i=MIN(M,N)-1;i>=0;i--) {
195 for (j=l;j<N;j++) u[i][j]=0.0;
199 for (s=0.0,k=l;k<M;k++) s += u[k][i]*u[k][j];
201 for (k=i;k<M;k++) u[k][j] += f*u[k][i];
203 for (j=i;j<M;j++) u[j][i] *= g;
204 }
else for (j=i;j<M;j++) u[j][i] =0.0;
207 for (k=N-1;k>=0;k--) {
208 for (its=0;its<MAX_NUM_SVD_ITER;its++) {
212 if (l == 0 || fabs(rv1[l]) <= eps*anorm) {
216 if (fabs(w[nm]) <= eps*anorm)
break;
221 for (i=l;i<k+1;i++) {
224 if (fabs(f) <= eps*anorm)
break;
243 for (j=0;j<N;j++) v[j][k] = -v[j][k];
247 if (its == MAX_NUM_SVD_ITER-1){
255 f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y);
257 f=((x-z)*(x+z)+h*((y/(f+SIGN(g,f)))-h))/x;
259 for (j=l;j<=nm;j++) {
273 for (jj=0;jj<N;jj++) {
288 for (jj=0;jj<M;jj++) {
304 bool SVD::reorder() {
309 do { inc *= 3; inc++; }
while (inc <= n);
312 for (i=inc;i<n;i++) {
314 for (k=0;k<m;k++) su[k] = u[k][i];
315 for (k=0;k<n;k++) sv[k] = v[k][i];
317 while (w[j-inc] < sw) {
319 for (k=0;k<m;k++) u[k][j] = u[k][j-inc];
320 for (k=0;k<n;k++) v[k][j] = v[k][j-inc];
325 for (k=0;k<m;k++) u[k][j] = su[k];
326 for (k=0;k<n;k++) v[k][j] = sv[k];
331 for (i=0;i<m;i++)
if (u[i][k] < 0.) s++;
332 for (j=0;j<n;j++)
if (v[j][k] < 0.) s++;
334 for (i=0;i<m;i++) u[i][k] = -u[i][k];
335 for (j=0;j<n;j++) v[j][k] = -v[j][k];
341 Float SVD::pythag(
const Float a,
const Float b) {
344 return (absa > absb ? absa*grt_sqrt(1.0+grt_sqr(absb/absa)) : (absb == 0.0 ? 0.0 : absb*grt_sqrt(1.0+grt_sqr(absa/absb))));
virtual bool resize(const unsigned int size)
unsigned int getNumRows() const
unsigned int getNumCols() const
virtual bool resize(const unsigned int r, const unsigned int c)