70 int nrt = max(0,min(n-2,m));
71 for (k = 0; k < max(nct,nrt); k++) {
78 for (i = k; i < m; i++) {
79 s[k] =
hypot(s[k],A[i][k]);
85 for (i = k; i < m; i++) {
92 for (j = k+1; j < n; j++) {
93 if ((k < nct) && (s[k] != 0.0)) {
98 for (i = k; i < m; i++) {
102 for (i = k; i < m; i++) {
103 A[i][j] += t*A[i][k];
112 if (wantu & (k < nct)) {
117 for (i = k; i < m; i++) {
127 for (i = k+1; i < n; i++) {
128 e[k] =
hypot(e[k],e[i]);
134 for (i = k+1; i < n; i++) {
140 if ((k+1 < m) & (e[k] != 0.0)) {
144 for (i = k+1; i < m; i++) {
147 for (j = k+1; j < n; j++) {
148 for (i = k+1; i < m; i++) {
149 work[i] += e[j]*A[i][j];
152 for (j = k+1; j < n; j++) {
153 double t = -e[j]/e[k+1];
154 for (i = k+1; i < m; i++) {
155 A[i][j] += t*work[i];
164 for (i = k+1; i < n; i++) {
175 s[nct] = A[nct][nct];
181 e[nrt] = A[nrt][p-1];
188 for (j = nct; j < nu; j++) {
189 for (i = 0; i < m; i++) {
194 for (k = nct-1; k >= 0; k--) {
196 for (j = k+1; j < nu; j++) {
198 for (i = k; i < m; i++) {
199 t += U[i][k]*U[i][j];
202 for (i = k; i < m; i++) {
203 U[i][j] += t*U[i][k];
206 for (i = k; i < m; i++ ) {
209 U[k][k] = 1.0 + U[k][k];
210 for (i = 0; i < k-1; i++) {
214 for (i = 0; i < m; i++) {
225 for (k = n-1; k >= 0; k--) {
226 if ((k < nrt) & (e[k] != 0.0)) {
227 for (j = k+1; j < nu; j++) {
229 for (i = k+1; i < n; i++) {
230 t += V[i][k]*V[i][j];
233 for (i = k+1; i < n; i++) {
234 V[i][j] += t*V[i][k];
238 for (i = 0; i < n; i++) {
249 double eps = pow(2.0,-52.0);
266 for (k = p-2; k >= -1; k--) {
270 if (abs(e[k]) <= eps*(abs(s[k]) + abs(s[k+1]))) {
279 for (ks = p-1; ks >= k; ks--) {
283 double t = (ks != p ? abs(e[ks]) : 0.) +
284 (ks != k+1 ? abs(e[ks-1]) : 0.);
285 if (abs(s[ks]) <= eps*t) {
292 }
else if (ks == p-1) {
310 for (j = p-2; j >= k; j--) {
311 double t =
hypot(s[j],f);
320 for (i = 0; i < n; i++) {
321 t = cs*V[i][j] + sn*V[i][p-1];
322 V[i][p-1] = -sn*V[i][j] + cs*V[i][p-1];
335 for (j = k; j < p; j++) {
336 double t =
hypot(s[j],f);
343 for (i = 0; i < m; i++) {
344 t = cs*U[i][j] + sn*U[i][k-1];
345 U[i][k-1] = -sn*U[i][j] + cs*U[i][k-1];
359 double scale = max(max(max(max(
360 abs(s[p-1]),abs(s[p-2])),abs(e[p-2])),
361 abs(s[k])),abs(e[k]));
362 double sp = s[p-1]/scale;
363 double spm1 = s[p-2]/scale;
364 double epm1 = e[p-2]/scale;
365 double sk = s[k]/scale;
366 double ek = e[k]/scale;
367 double b = ((spm1 + sp)*(spm1 - sp) + epm1*epm1)/2.0;
368 double c = (sp*epm1)*(sp*epm1);
370 if ((b != 0.0) || (c != 0.0)) {
371 shift = sqrt(b*b + c);
375 shift = c/(b + shift);
377 double f = (sk + sp)*(sk - sp) + shift;
382 for (j = k; j < p-1; j++) {
383 double t =
hypot(f,g);
389 f = cs*s[j] + sn*e[j];
390 e[j] = cs*e[j] - sn*s[j];
394 for (i = 0; i < n; i++) {
395 t = cs*V[i][j] + sn*V[i][j+1];
396 V[i][j+1] = -sn*V[i][j] + cs*V[i][j+1];
404 f = cs*e[j] + sn*s[j+1];
405 s[j+1] = -sn*e[j] + cs*s[j+1];
408 if (wantu && (j < m-1)) {
409 for (i = 0; i < m; i++) {
410 t = cs*U[i][j] + sn*U[i][j+1];
411 U[i][j+1] = -sn*U[i][j] + cs*U[i][j+1];
428 s[k] = (s[k] < 0.0 ? -s[k] : 0.0);
430 for (i = 0; i <= pp; i++) {
439 if (s[k] >= s[k+1]) {
445 if (wantv && (k < n-1)) {
446 for (i = 0; i < n; i++) {
447 t = V[i][k+1]; V[i][k+1] = V[i][k]; V[i][k] = t;
450 if (wantu && (k < m-1)) {
451 for (i = 0; i < m; i++) {
452 t = U[i][k+1]; U[i][k+1] = U[i][k]; U[i][k] = t;
468 int minm = min(m+1,n);
472 for (
int i=0; i<m; i++)
473 for (
int j=0; j<minm; j++)
498 for (
int i = 0; i < n; i++) {
499 for (
int j = 0; j < n; j++) {
515 return s[0]/s[min(m,n)-1];
524 double eps = pow(2.0,-52.0);
525 double tol = max(m,n)*s[0]*eps;
527 for (
int i = 0; i < s.
dim(); i++) {
Definition: jama_svd.h:40
int rank()
Definition: jama_svd.h:522
SVD(const Array2D< Real > &Arg)
Definition: jama_svd.h:50
double cond()
Definition: jama_svd.h:514
int m
Definition: jama_svd.h:45
void getU(Array2D< Real > &A)
Definition: jama_svd.h:466
void getSingularValues(Array1D< Real > &x)
Definition: jama_svd.h:487
void getS(Array2D< Real > &A)
Definition: jama_svd.h:496
void getV(Array2D< Real > &A)
Definition: jama_svd.h:480
double norm2()
Definition: jama_svd.h:508
Array2D< Real > U
Definition: jama_svd.h:43
Array1D< Real > s
Definition: jama_svd.h:44
int dim() const
Definition: tnt_array1d.h:221
int dim1() const
Definition: tnt_array2d.h:231
int dim2() const
Definition: tnt_array2d.h:234
Array2D copy() const
Definition: tnt_array2d.h:180
Definition: jama_cholesky.h:9
Definition: tnt_array1d.h:36
Real hypot(const Real &a, const Real &b)
Definition: tnt_math_utils.h:18
float Real
Definition: types.h:69