59 int nu = std::min(m,
n);
73 int nct = std::min(m-1,
n);
74 int nrt = std::max(0,std::min(
n-2,m));
75 for (k = 0; k < std::max(nct,nrt); k++) {
82 for (i = k; i < m; i++) {
83 s[k] =
hypot(s[k],A[i][k]);
89 for (i = k; i < m; i++) {
96 for (j = k+1; j <
n; j++) {
97 if ((k < nct) && (s[k] != 0.0)) {
102 for (i = k; i < m; i++) {
103 t += A[i][k]*A[i][j];
106 for (i = k; i < m; i++) {
107 A[i][j] += t*A[i][k];
116 if (wantu & (k < nct)) {
121 for (i = k; i < m; i++) {
131 for (i = k+1; i <
n; i++) {
138 for (i = k+1; i <
n; i++) {
144 if ((k+1 < m) & (
e[k] != 0.0)) {
148 for (i = k+1; i < m; i++) {
151 for (j = k+1; j <
n; j++) {
152 for (i = k+1; i < m; i++) {
153 work[i] +=
e[j]*A[i][j];
156 for (j = k+1; j <
n; j++) {
157 double t = -
e[j]/
e[k+1];
158 for (i = k+1; i < m; i++) {
159 A[i][j] += t*work[i];
168 for (i = k+1; i <
n; i++) {
177 int p = std::min(
n,m+1);
179 s[nct] = A[nct][nct];
185 e[nrt] = A[nrt][p-1];
192 for (j = nct; j < nu; j++) {
193 for (i = 0; i < m; i++) {
198 for (k = nct-1; k >= 0; k--) {
200 for (j = k+1; j < nu; j++) {
202 for (i = k; i < m; i++) {
203 t += U[i][k]*U[i][j];
206 for (i = k; i < m; i++) {
207 U[i][j] += t*U[i][k];
210 for (i = k; i < m; i++ ) {
213 U[k][k] = 1.0 + U[k][k];
214 for (i = 0; i < k-1; i++) {
218 for (i = 0; i < m; i++) {
229 for (k =
n-1; k >= 0; k--) {
230 if ((k < nrt) & (
e[k] != 0.0)) {
231 for (j = k+1; j < nu; j++) {
233 for (i = k+1; i <
n; i++) {
234 t +=
V[i][k]*
V[i][j];
237 for (i = k+1; i <
n; i++) {
238 V[i][j] += t*
V[i][k];
242 for (i = 0; i <
n; i++) {
253 double eps = pow(2.0,-52.0);
270 for (k = p-2; k >= -1; k--) {
274 if (abs(
e[k]) <= eps*(abs(s[k]) + abs(s[k+1]))) {
283 for (ks = p-1; ks >= k; ks--) {
287 double t = (ks != p ? abs(
e[ks]) : 0.) +
288 (ks != k+1 ? abs(
e[ks-1]) : 0.);
289 if (abs(s[ks]) <= eps*t) {
296 }
else if (ks == p-1) {
314 for (j = p-2; j >= k; j--) {
315 double t =
hypot(s[j],f);
324 for (i = 0; i <
n; i++) {
325 t = cs*
V[i][j] + sn*
V[i][p-1];
326 V[i][p-1] = -sn*
V[i][j] + cs*
V[i][p-1];
339 for (j = k; j < p; j++) {
340 double t =
hypot(s[j],f);
347 for (i = 0; i < m; i++) {
348 t = cs*U[i][j] + sn*U[i][k-1];
349 U[i][k-1] = -sn*U[i][j] + cs*U[i][k-1];
363 double scale = std::max(std::max(std::max(std::max(
364 abs(s[p-1]),abs(s[p-2])),abs(
e[p-2])),
365 abs(s[k])),abs(
e[k]));
366 double sp = s[p-1]/scale;
367 double spm1 = s[p-2]/scale;
368 double epm1 =
e[p-2]/scale;
369 double sk = s[k]/scale;
370 double ek =
e[k]/scale;
371 double b = ((spm1 + sp)*(spm1 - sp) + epm1*epm1)/2.0;
372 double c = (sp*epm1)*(sp*epm1);
374 if ((
b != 0.0) || (c != 0.0)) {
375 shift = sqrt(
b*
b + c);
379 shift = c/(
b + shift);
381 double f = (sk + sp)*(sk - sp) + shift;
386 for (j = k; j < p-1; j++) {
387 double t =
hypot(f,g);
393 f = cs*s[j] + sn*
e[j];
394 e[j] = cs*
e[j] - sn*s[j];
398 for (i = 0; i <
n; i++) {
399 t = cs*
V[i][j] + sn*
V[i][j+1];
400 V[i][j+1] = -sn*
V[i][j] + cs*
V[i][j+1];
408 f = cs*
e[j] + sn*s[j+1];
409 s[j+1] = -sn*
e[j] + cs*s[j+1];
412 if (wantu && (j < m-1)) {
413 for (i = 0; i < m; i++) {
414 t = cs*U[i][j] + sn*U[i][j+1];
415 U[i][j+1] = -sn*U[i][j] + cs*U[i][j+1];
432 s[k] = (s[k] < 0.0 ? -s[k] : 0.0);
434 for (i = 0; i <= pp; i++) {
443 if (s[k] >= s[k+1]) {
449 if (wantv && (k <
n-1)) {
450 for (i = 0; i <
n; i++) {
451 t =
V[i][k+1];
V[i][k+1] =
V[i][k];
V[i][k] = t;
454 if (wantu && (k < m-1)) {
455 for (i = 0; i < m; i++) {
456 t = U[i][k+1]; U[i][k+1] = U[i][k]; U[i][k] = t;