20 #define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
22 int svd(
int m,
int n,
int withu,
int withv,
double eps,
double tol,
25 int svd_nlm(
int m,
int n,
int withu,
int withv,
double eps,
double tol,
28 int svd_mln(
int m,
int n,
int withu,
int withv,
double eps,
double tol,
75 cerr <<
"index error in singval_decomp" <<
endl;
88 int k =
svd(m,n,1,1,eps,tol,a,w,u,v);
91 cerr <<
"Error in singval_decomp in iteration " << k <<
endl;
118 int svd(
int m,
int n,
int withu,
int withv,
double eps,
double tol,
134 int qlb=q.indexmin();
144 k =
svd_nlm(m,n,withu,withv,eps,tol,a,q,u,v);
146 k =
svd_mln(m,n,withu,withv,eps,tol,a,q,u,v);
170 int svd_mln(
int m,
int n,
int withu,
int withv,
double eps,
double tol,
178 int i,j,k,l,l1,iter,retval;
179 double c,f,g,h,s,
x,y,z;
185 double* e = (
double*)calloc((
size_t)n,
sizeof(double));
201 s += (u[j][i]*u[j][i]);
216 s += (u[k][i] * u[k][j]);
221 u[k][j] += (f * u[k][i]);
232 s += (u[i][j] * u[i][j]);
251 s += (u[j][k] * u[i][k]);
255 u[j][k] += (s * e[k]);
288 s += (u[i][k] * v[k][j]);
292 v[k][j] += (s * v[k][i]);
298 v[i][j] = v[j][i] = 0.0;
309 for (i=
min(m,n)-1;i>=0;i--) {
319 s += (u[k][i] * u[k][j]);
322 u[k][j] += (f * u[k][i]);
337 for (k=n-1;k>=0;k--) {
341 if (
fabs(e[l]) <=
eps)
goto test_f_convergence;
342 if (
fabs(q[l-1]) <=
eps)
goto cancellation;
353 if (
fabs(f) <=
eps)
goto test_f_convergence;
355 h = q[i] =
sqrt(f*f + g*g);
362 u[j][l1] = y * c + z * s;
363 u[j][i] = -y * s + z * c;
369 if (l == k)
goto convergence;
381 f = ((y-z)*(y+z) + (g-h)*(g+h)) / (2*h*y);
383 f = ((x-z)*(x+z) + h*(y/((f<0)?(f-g):(f+g))-h))/
x;
386 for (i=l+1;i<=k;i++) {
391 e[i-1] = z =
sqrt(f*f+h*h);
402 v[j][i-1] = x * c + z * s;
403 v[j][i] = -x * s + z * c;
406 q[i-1] = z =
sqrt(f*f + h*h);
415 u[j][i-1] = y * c + z * s;
416 u[j][i] = -y * s + z * c;
423 goto test_f_splitting;
451 int svd_nlm(
int m,
int n,
int withu,
int withv,
double eps,
double tol,
459 int i,j,k,l,l1,iter,retval;
460 double c,f,g,h,s,
x,y,z;
465 double* e = (
double *)calloc((
size_t)n,
sizeof(double));
478 s += (u[j][i]*u[j][i]);
493 s += (u[k][i] * u[k][j]);
498 u[k][j] += (f * u[k][i]);
506 s += (u[i][j] * u[i][j]);
525 s += (u[j][k] * u[i][k]);
529 u[j][k] += (s * e[k]);
559 s += (u[i][k] * v[k][j]);
563 v[k][j] += (s * v[k][i]);
569 v[i][j] = v[j][i] = 0.0;
579 for (i=n-1;i>=0;i--) {
589 s += (u[k][i] * u[k][j]);
592 u[k][j] += (f * u[k][i]);
607 for (k=n-1;k>=0;k--) {
611 if (
fabs(e[l]) <=
eps)
goto test_f_convergence;
612 if (
fabs(q[l-1]) <=
eps)
goto cancellation;
623 if (
fabs(f) <=
eps)
goto test_f_convergence;
625 h = q[i] =
sqrt(f*f + g*g);
632 u[j][l1] = y * c + z * s;
633 u[j][i] = -y * s + z * c;
639 if (l == k)
goto convergence;
651 f = ((y-z)*(y+z) + (g-h)*(g+h)) / (2*h*y);
653 f = ((x-z)*(x+z) + h*(y/((f<0)?(f-g):(f+g))-h))/
x;
656 for (i=l+1;i<=k;i++) {
661 e[i-1] = z =
sqrt(f*f+h*h);
672 v[j][i-1] = x * c + z * s;
673 v[j][i] = -x * s + z * c;
676 q[i-1] = z =
sqrt(f*f + h*h);
685 u[j][i-1] = y * c + z * s;
686 u[j][i] = -y * s + z * c;
693 goto test_f_splitting;
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Vector of double precision numbers.
df1_two_variable fabs(const df1_two_variable &x)
sing_val_decomp singval_decomp(const dmatrix &_a)
Singular value decomposition.
prnstream & endl(prnstream &)
int svd_nlm(int m, int n, int withu, int withv, double eps, double tol, const dmatrix &aa, const dvector &_q, const dmatrix &_u, const dmatrix &_v)
Singular value decomposition.
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Description not yet available.
Description not yet available.
void rowshift(int min)
Changes the range of valid indices for the rows.
int svd(int m, int n, int withu, int withv, double eps, double tol, const dmatrix &a, const dvector &_q, const dmatrix &_u, const dmatrix &_v)
Singular value decomposition.
void colshift(int min)
Description not yet available.
int svd_mln(int m, int n, int withu, int withv, double eps, double tol, const dmatrix &aa, const dvector &_q, const dmatrix &_u, const dmatrix &_v)
Singular value decomposition.