20 #define GRT_DLL_EXPORTS 34 eps = std::numeric_limits< Float >::epsilon();
35 if( !decompose() )
return false;
36 if( !reorder() )
return false;
38 tsh = 0.5*grt_sqrt(m+n+1.)*w[0]*eps;
46 if(b.size() != m || x.size() != n){
50 tsh = (thresh >= 0. ? thresh : 0.5*grt_sqrt(m+n+1.)*w[0]*eps);
54 for (i=0;i<m;i++) s += u[i][j]*b[i];
61 for (jj=0;jj<n;jj++) s += v[j][jj]*tmp[jj];
74 for (i=0;i<n;i++) xx[i] = b[i][j];
75 solveVector(xx,xx,thresh);
76 for (i=0;i<n;i++) x[i][j] = xx[i];
80 UINT SVD::rank(Float thresh) {
82 tsh = (thresh >= 0. ? thresh : 0.5*grt_sqrt(m+n+1.)*w[0]*eps);
83 for (j=0;j<n;j++) if (w[j] > tsh) nr++;
87 UINT SVD::nullity(Float thresh) {
89 tsh = (thresh >= 0. ? thresh : 0.5*grt_sqrt(m+n+1.)*w[0]*eps);
90 for (j=0;j<n;j++)
if (w[j] <= tsh) nn++;
99 for (i=0;i<m;i++) rnge[i][nr] = u[i][j];
111 for (jj=0;jj<n;jj++) nullsp[jj][nn] = v[jj][j];
118 Float SVD::inv_condition() {
119 return (w[0] <= 0. || w[n-1] <= 0.) ? 0. : w[n-1]/w[0];
122 bool SVD::decompose() {
124 int i,its,j,jj,k,l,nm,N,M;
125 Float anorm,c,f,g,h,s,scale,x,y,z;
127 g = scale = anorm = 0.0;
136 for (k=i;k<M;k++) scale += fabs(u[k][i]);
143 g = -SIGN(grt_sqrt(s),f);
146 for (j=l-1;j<N;j++) {
147 for (s=0.0,k=i;k<M;k++) s += u[k][i] * u[k][j];
149 for (k=i;k<M;k++) u[k][j] += f * u[k][i];
151 for (k=i;k<M;k++) u[k][i] *= scale;
156 if (i+1 <= M && i+1 != N) {
157 for (k=l-1;k<N;k++) scale += fabs(u[i][k]);
159 for (k=l-1;k<N;k++) {
161 s += u[i][k]*u[i][k];
164 g = -SIGN(grt_sqrt(s),f);
167 for (k=l-1;k<N;k++) rv1[k]=u[i][k]/h;
168 for (j=l-1;j<M;j++) {
169 for (s=0.0,k=l-1;k<N;k++) s += u[j][k]*u[i][k];
170 for (k=l-1;k<N;k++) u[j][k] += s*rv1[k];
172 for (k=l-1;k<N;k++) u[i][k]*= scale;
175 anorm=grt_max(anorm,(fabs(w[i])+fabs(rv1[i])));
177 for (i=N-1;i>=0;i--) {
181 v[j][i]=u[i][j]/u[i][l]/g;
183 for (s=0.0,k=l;k<N;k++) s += u[i][k]*v[k][j];
184 for (k=l;k<N;k++) v[k][j] += s*v[k][i];
187 for (j=l;j<N;j++) v[i][j]=v[j][i]=0.0;
193 for (i=grt_min(M,N)-1;i>=0;i--) {
196 for (j=l;j<N;j++) u[i][j]=0.0;
200 for (s=0.0,k=l;k<M;k++) s += u[k][i]*u[k][j];
202 for (k=i;k<M;k++) u[k][j] += f*u[k][i];
204 for (j=i;j<M;j++) u[j][i] *= g;
205 }
else for (j=i;j<M;j++) u[j][i] =0.0;
208 for (k=N-1;k>=0;k--) {
209 for (its=0;its<MAX_NUM_SVD_ITER;its++) {
213 if (l == 0 || fabs(rv1[l]) <= eps*anorm) {
217 if (fabs(w[nm]) <= eps*anorm)
break;
222 for (i=l;i<k+1;i++) {
225 if (fabs(f) <= eps*anorm)
break;
244 for (j=0;j<N;j++) v[j][k] = -v[j][k];
248 if (its == MAX_NUM_SVD_ITER-1){
256 f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y);
258 f=((x-z)*(x+z)+h*((y/(f+SIGN(g,f)))-h))/x;
260 for (j=l;j<=nm;j++) {
274 for (jj=0;jj<N;jj++) {
289 for (jj=0;jj<M;jj++) {
305 bool SVD::reorder() {
310 do { inc *= 3; inc++; }
while (inc <= n);
313 for (i=inc;i<n;i++) {
315 for (k=0;k<m;k++) su[k] = u[k][i];
316 for (k=0;k<n;k++) sv[k] = v[k][i];
318 while (w[j-inc] < sw) {
320 for (k=0;k<m;k++) u[k][j] = u[k][j-inc];
321 for (k=0;k<n;k++) v[k][j] = v[k][j-inc];
326 for (k=0;k<m;k++) u[k][j] = su[k];
327 for (k=0;k<n;k++) v[k][j] = sv[k];
332 for (i=0;i<m;i++)
if (u[i][k] < 0.) s++;
333 for (j=0;j<n;j++)
if (v[j][k] < 0.) s++;
335 for (i=0;i<m;i++) u[i][k] = -u[i][k];
336 for (j=0;j<n;j++) v[j][k] = -v[j][k];
342 Float SVD::pythag(
const Float a,
const Float b) {
345 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)