52 int i = 0, j = 0, k = 0;
57 int nct = min(m - 1, n);
58 int nrt = max(0, min(n - 2, m));
60 for (k = 0; k < max(nct, nrt); k++) {
66 for (i = k; i < m; i++) {
67 s(k) = hypot(s(k), A(i, k));
70 if (A(k, k) < 0.0) { s(k) = -s(k); }
71 for (i = k; i < m; i++) {
78 for (j = k + 1; j < n; j++) {
79 if ((k < nct) && (s(k) != 0.0)) {
83 for (i = k; i < m; i++) {
84 t += A(i, k) * A(i, j);
87 for (i = k; i < m; i++) {
88 A(i, j) += t * A(i, k);
97 if (wantu & (k < nct)) {
101 for (i = k; i < m; i++) {
110 for (i = k + 1; i < n; i++) {
111 e(k) = hypot(e(k), e(i));
114 if (e(k + 1) < 0.0) { e(k) = -e(k); }
115 for (i = k + 1; i < n; i++) {
121 if ((k + 1 < m) & (e(k) != 0.0)) {
124 for (i = k + 1; i < m; i++) {
127 for (j = k + 1; j < n; j++) {
128 for (i = k + 1; i < m; i++) {
129 work(i) += e(j) * A(i, j);
132 for (j = k + 1; j < n; j++) {
133 Real t(-e(j) / e(k + 1));
134 for (i = k + 1; i < m; i++) {
135 A(i, j) += t * work(i);
143 for (i = k + 1; i < n; i++) {
152 int p = min(n, m + 1);
153 if (nct < n) { s(nct) = A(nct, nct); }
154 if (m < p) { s(p - 1) = 0.0; }
155 if (nrt + 1 < p) { e(nrt) = A(nrt, p - 1); }
161 for (j = nct; j < nu; j++) {
162 for (i = 0; i < m; i++) {
167 for (k = nct - 1; k >= 0; k--) {
169 for (j = k + 1; j < nu; j++) {
171 for (i = k; i < m; i++) {
172 t += U(i, k) * U(i, j);
175 for (i = k; i < m; i++) {
176 U(i, j) += t * U(i, k);
179 for (i = k; i < m; i++) {
182 U(k, k) = 1.0 + U(k, k);
183 for (i = 0; i < k - 1; i++) {
187 for (i = 0; i < m; i++) {
198 for (k = n - 1; k >= 0; k--) {
199 if ((k < nrt) & (e(k) != 0.0)) {
200 for (j = k + 1; j < nu; j++) {
202 for (i = k + 1; i < n; i++) {
203 t += V(i, k) * V(i, j);
205 t = -t / V(k + 1, k);
206 for (i = k + 1; i < n; i++) {
207 V(i, j) += t * V(i, k);
211 for (i = 0; i < n; i++) {
222 Real eps(pow(2.0, -52.0));
239 for (k = p - 2; k >= -1; k--) {
240 if (k == -1) {
break; }
241 if (abs(e(k)) <= eps * (abs(s(k)) + abs(s(k + 1)))) {
250 for (ks = p - 1; ks >= k; ks--) {
251 if (ks == k) {
break; }
252 Real t((ks != p ? abs(e(ks)) : 0.) +
253 (ks != k + 1 ? abs(e(ks - 1)) : 0.));
254 if (abs(s(ks)) <= eps * t) {
261 }
else if (ks == p - 1) {
278 for (j = p - 2; j >= k; j--) {
279 Real t(hypot(s(j), f));
285 e(j - 1) = cs * e(j - 1);
288 for (i = 0; i < n; i++) {
289 t = cs * V(i, j) + sn * V(i, p - 1);
290 V(i, p - 1) = -sn * V(i, j) + cs * V(i, p - 1);
302 for (j = k; j < p; j++) {
303 Real t(hypot(s(j), f));
310 for (i = 0; i < m; i++) {
311 t = cs * U(i, j) + sn * U(i, k - 1);
312 U(i, k - 1) = -sn * U(i, j) + cs * U(i, k - 1);
325 max(max(max(max(abs(s(p - 1)), abs(s(p - 2))), abs(e(p - 2))),
328 Real sp = s(p - 1) / scale;
329 Real spm1 = s(p - 2) / scale;
330 Real epm1 = e(p - 2) / scale;
331 Real sk = s(k) / scale;
332 Real ek = e(k) / scale;
333 Real b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2.0;
334 Real c = (sp * epm1) * (sp * epm1);
336 if ((b != 0.0) || (c != 0.0)) {
337 shift = sqrt(b * b + c);
338 if (b < 0.0) { shift = -shift; }
339 shift = c / (b + shift);
341 Real f = (sk + sp) * (sk - sp) + shift;
346 for (j = k; j < p - 1; j++) {
347 Real t = hypot(f, g);
350 if (j != k) { e(j - 1) = t; }
351 f = cs * s(j) + sn * e(j);
352 e(j) = cs * e(j) - sn * s(j);
354 s(j + 1) = cs * s(j + 1);
356 for (i = 0; i < n; i++) {
357 t = cs * V(i, j) + sn * V(i, j + 1);
358 V(i, j + 1) = -sn * V(i, j) + cs * V(i, j + 1);
366 f = cs * e(j) + sn * s(j + 1);
367 s(j + 1) = -sn * e(j) + cs * s(j + 1);
369 e(j + 1) = cs * e(j + 1);
370 if (wantu && (j < m - 1)) {
371 for (i = 0; i < m; i++) {
372 t = cs * U(i, j) + sn * U(i, j + 1);
373 U(i, j + 1) = -sn * U(i, j) + cs * U(i, j + 1);
388 s(k) = (s(k) < 0.0 ? -s(k) : 0.0);
390 for (i = 0; i <= pp; i++) {
399 if (s(k) >= s(k + 1)) {
break; }
403 if (wantv && (k < n - 1)) {
404 for (i = 0; i < n; i++) {
406 V(i, k + 1) = V(i, k);
410 if (wantu && (k < m - 1)) {
411 for (i = 0; i < m; i++) {
413 U(i, k + 1) = U(i, k);
427 int minm = min(m + 1, n);
431 for (
int i = 0; i < m; i++)
432 for (
int j = 0; j < minm; j++)
447 for (
int i = 0; i < n; i++) {
448 for (
int j = 0; j < n; j++) {
459 Real
cond() {
return s(0) / s(min(m, n) - 1); }
465 Real eps = pow(2.0, -52.0);
466 Real tol = max(m, n) * s(0) * eps;
468 for (
int i = 0; i < s.dim(); i++) {
469 if (s(i) > tol) { r++; }