1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.linear;
18
19 import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
20 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
21 import org.apache.commons.math4.core.jdkmath.JdkMath;
22 import org.apache.commons.numbers.core.Precision;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class SingularValueDecomposition {
54
55 private static final double EPS = 0x1.0p-52;
56
57 private static final double TINY = 0x1.0p-966;
58
59 private final double[] singularValues;
60
61 private final int m;
62
63 private final int n;
64
65 private final boolean transposed;
66
67 private final RealMatrix cachedU;
68
69 private RealMatrix cachedUt;
70
71 private RealMatrix cachedS;
72
73 private final RealMatrix cachedV;
74
75 private RealMatrix cachedVt;
76
77
78
79
80 private final double tol;
81
82
83
84
85
86
87 public SingularValueDecomposition(final RealMatrix matrix) {
88 final double[][] A;
89
90
91 if (matrix.getRowDimension() < matrix.getColumnDimension()) {
92 transposed = true;
93 A = matrix.transpose().getData();
94 m = matrix.getColumnDimension();
95 n = matrix.getRowDimension();
96 } else {
97 transposed = false;
98 A = matrix.getData();
99 m = matrix.getRowDimension();
100 n = matrix.getColumnDimension();
101 }
102
103 singularValues = new double[n];
104 final double[][] U = new double[m][n];
105 final double[][] V = new double[n][n];
106 final double[] e = new double[n];
107 final double[] work = new double[m];
108
109
110 final int nct = JdkMath.min(m - 1, n);
111 final int nrt = JdkMath.max(0, n - 2);
112 for (int k = 0; k < JdkMath.max(nct, nrt); k++) {
113 if (k < nct) {
114
115
116
117 singularValues[k] = 0;
118 for (int i = k; i < m; i++) {
119 singularValues[k] = JdkMath.hypot(singularValues[k], A[i][k]);
120 }
121 if (singularValues[k] != 0) {
122 if (A[k][k] < 0) {
123 singularValues[k] = -singularValues[k];
124 }
125 for (int i = k; i < m; i++) {
126 A[i][k] /= singularValues[k];
127 }
128 A[k][k] += 1;
129 }
130 singularValues[k] = -singularValues[k];
131 }
132 for (int j = k + 1; j < n; j++) {
133 if (k < nct &&
134 singularValues[k] != 0) {
135
136 double t = 0;
137 for (int i = k; i < m; i++) {
138 t += A[i][k] * A[i][j];
139 }
140 t = -t / A[k][k];
141 for (int i = k; i < m; i++) {
142 A[i][j] += t * A[i][k];
143 }
144 }
145
146
147 e[j] = A[k][j];
148 }
149 if (k < nct) {
150
151
152 for (int i = k; i < m; i++) {
153 U[i][k] = A[i][k];
154 }
155 }
156 if (k < nrt) {
157
158
159
160 e[k] = 0;
161 for (int i = k + 1; i < n; i++) {
162 e[k] = JdkMath.hypot(e[k], e[i]);
163 }
164 if (e[k] != 0) {
165 if (e[k + 1] < 0) {
166 e[k] = -e[k];
167 }
168 for (int i = k + 1; i < n; i++) {
169 e[i] /= e[k];
170 }
171 e[k + 1] += 1;
172 }
173 e[k] = -e[k];
174 if (k + 1 < m &&
175 e[k] != 0) {
176
177 for (int i = k + 1; i < m; i++) {
178 work[i] = 0;
179 }
180 for (int j = k + 1; j < n; j++) {
181 for (int i = k + 1; i < m; i++) {
182 work[i] += e[j] * A[i][j];
183 }
184 }
185 for (int j = k + 1; j < n; j++) {
186 final double t = -e[j] / e[k + 1];
187 for (int i = k + 1; i < m; i++) {
188 A[i][j] += t * work[i];
189 }
190 }
191 }
192
193
194
195 for (int i = k + 1; i < n; i++) {
196 V[i][k] = e[i];
197 }
198 }
199 }
200
201 int p = n;
202 if (nct < n) {
203 singularValues[nct] = A[nct][nct];
204 }
205 if (m < p) {
206 singularValues[p - 1] = 0;
207 }
208 if (nrt + 1 < p) {
209 e[nrt] = A[nrt][p - 1];
210 }
211 e[p - 1] = 0;
212
213
214 for (int j = nct; j < n; j++) {
215 for (int i = 0; i < m; i++) {
216 U[i][j] = 0;
217 }
218 U[j][j] = 1;
219 }
220 for (int k = nct - 1; k >= 0; k--) {
221 if (singularValues[k] != 0) {
222 for (int j = k + 1; j < n; j++) {
223 double t = 0;
224 for (int i = k; i < m; i++) {
225 t += U[i][k] * U[i][j];
226 }
227 t = -t / U[k][k];
228 for (int i = k; i < m; i++) {
229 U[i][j] += t * U[i][k];
230 }
231 }
232 for (int i = k; i < m; i++) {
233 U[i][k] = -U[i][k];
234 }
235 U[k][k] = 1 + U[k][k];
236 for (int i = 0; i < k - 1; i++) {
237 U[i][k] = 0;
238 }
239 } else {
240 for (int i = 0; i < m; i++) {
241 U[i][k] = 0;
242 }
243 U[k][k] = 1;
244 }
245 }
246
247
248 for (int k = n - 1; k >= 0; k--) {
249 if (k < nrt &&
250 e[k] != 0) {
251 for (int j = k + 1; j < n; j++) {
252 double t = 0;
253 for (int i = k + 1; i < n; i++) {
254 t += V[i][k] * V[i][j];
255 }
256 t = -t / V[k + 1][k];
257 for (int i = k + 1; i < n; i++) {
258 V[i][j] += t * V[i][k];
259 }
260 }
261 }
262 for (int i = 0; i < n; i++) {
263 V[i][k] = 0;
264 }
265 V[k][k] = 1;
266 }
267
268
269 final int pp = p - 1;
270 while (p > 0) {
271 int k;
272 int kase;
273
274
275
276
277
278
279
280
281
282 for (k = p - 2; k >= 0; k--) {
283 final double threshold
284 = TINY + EPS * (JdkMath.abs(singularValues[k]) +
285 JdkMath.abs(singularValues[k + 1]));
286
287
288
289
290
291
292
293 if (!(JdkMath.abs(e[k]) > threshold)) {
294 e[k] = 0;
295 break;
296 }
297 }
298
299 if (k == p - 2) {
300 kase = 4;
301 } else {
302 int ks;
303 for (ks = p - 1; ks >= k; ks--) {
304 if (ks == k) {
305 break;
306 }
307 final double t = (ks != p ? JdkMath.abs(e[ks]) : 0) +
308 (ks != k + 1 ? JdkMath.abs(e[ks - 1]) : 0);
309 if (JdkMath.abs(singularValues[ks]) <= TINY + EPS * t) {
310 singularValues[ks] = 0;
311 break;
312 }
313 }
314 if (ks == k) {
315 kase = 3;
316 } else if (ks == p - 1) {
317 kase = 1;
318 } else {
319 kase = 2;
320 k = ks;
321 }
322 }
323 k++;
324
325 double f;
326 switch (kase) {
327
328 case 1:
329 f = e[p - 2];
330 e[p - 2] = 0;
331 for (int j = p - 2; j >= k; j--) {
332 double t = JdkMath.hypot(singularValues[j], f);
333 final double cs = singularValues[j] / t;
334 final double sn = f / t;
335 singularValues[j] = t;
336 if (j != k) {
337 f = -sn * e[j - 1];
338 e[j - 1] = cs * e[j - 1];
339 }
340
341 for (int i = 0; i < n; i++) {
342 t = cs * V[i][j] + sn * V[i][p - 1];
343 V[i][p - 1] = -sn * V[i][j] + cs * V[i][p - 1];
344 V[i][j] = t;
345 }
346 }
347 break;
348
349 case 2:
350 f = e[k - 1];
351 e[k - 1] = 0;
352 for (int j = k; j < p; j++) {
353 double t = JdkMath.hypot(singularValues[j], f);
354 final double cs = singularValues[j] / t;
355 final double sn = f / t;
356 singularValues[j] = t;
357 f = -sn * e[j];
358 e[j] = cs * e[j];
359
360 for (int i = 0; i < m; i++) {
361 t = cs * U[i][j] + sn * U[i][k - 1];
362 U[i][k - 1] = -sn * U[i][j] + cs * U[i][k - 1];
363 U[i][j] = t;
364 }
365 }
366 break;
367
368 case 3:
369
370 final double maxPm1Pm2 = JdkMath.max(JdkMath.abs(singularValues[p - 1]),
371 JdkMath.abs(singularValues[p - 2]));
372 final double scale = JdkMath.max(JdkMath.max(JdkMath.max(maxPm1Pm2,
373 JdkMath.abs(e[p - 2])),
374 JdkMath.abs(singularValues[k])),
375 JdkMath.abs(e[k]));
376 final double sp = singularValues[p - 1] / scale;
377 final double spm1 = singularValues[p - 2] / scale;
378 final double epm1 = e[p - 2] / scale;
379 final double sk = singularValues[k] / scale;
380 final double ek = e[k] / scale;
381 final double b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2.0;
382 final double c = (sp * epm1) * (sp * epm1);
383 double shift = 0;
384 if (b != 0 ||
385 c != 0) {
386 shift = JdkMath.sqrt(b * b + c);
387 if (b < 0) {
388 shift = -shift;
389 }
390 shift = c / (b + shift);
391 }
392 f = (sk + sp) * (sk - sp) + shift;
393 double g = sk * ek;
394
395 for (int j = k; j < p - 1; j++) {
396 double t = JdkMath.hypot(f, g);
397 double cs = f / t;
398 double sn = g / t;
399 if (j != k) {
400 e[j - 1] = t;
401 }
402 f = cs * singularValues[j] + sn * e[j];
403 e[j] = cs * e[j] - sn * singularValues[j];
404 g = sn * singularValues[j + 1];
405 singularValues[j + 1] = cs * singularValues[j + 1];
406
407 for (int i = 0; i < n; i++) {
408 t = cs * V[i][j] + sn * V[i][j + 1];
409 V[i][j + 1] = -sn * V[i][j] + cs * V[i][j + 1];
410 V[i][j] = t;
411 }
412 t = JdkMath.hypot(f, g);
413 cs = f / t;
414 sn = g / t;
415 singularValues[j] = t;
416 f = cs * e[j] + sn * singularValues[j + 1];
417 singularValues[j + 1] = -sn * e[j] + cs * singularValues[j + 1];
418 g = sn * e[j + 1];
419 e[j + 1] = cs * e[j + 1];
420 if (j < m - 1) {
421 for (int i = 0; i < m; i++) {
422 t = cs * U[i][j] + sn * U[i][j + 1];
423 U[i][j + 1] = -sn * U[i][j] + cs * U[i][j + 1];
424 U[i][j] = t;
425 }
426 }
427 }
428 e[p - 2] = f;
429 break;
430
431 default:
432
433 if (singularValues[k] <= 0) {
434 singularValues[k] = singularValues[k] < 0 ? -singularValues[k] : 0;
435
436 for (int i = 0; i <= pp; i++) {
437 V[i][k] = -V[i][k];
438 }
439 }
440
441 while (k < pp) {
442 if (singularValues[k] >= singularValues[k + 1]) {
443 break;
444 }
445 double t = singularValues[k];
446 singularValues[k] = singularValues[k + 1];
447 singularValues[k + 1] = t;
448 if (k < n - 1) {
449 for (int i = 0; i < n; i++) {
450 t = V[i][k + 1];
451 V[i][k + 1] = V[i][k];
452 V[i][k] = t;
453 }
454 }
455 if (k < m - 1) {
456 for (int i = 0; i < m; i++) {
457 t = U[i][k + 1];
458 U[i][k + 1] = U[i][k];
459 U[i][k] = t;
460 }
461 }
462 k++;
463 }
464 p--;
465 break;
466 }
467 }
468
469
470 tol = JdkMath.max(m * singularValues[0] * EPS,
471 JdkMath.sqrt(Precision.SAFE_MIN));
472
473 if (!transposed) {
474 cachedU = MatrixUtils.createRealMatrix(U);
475 cachedV = MatrixUtils.createRealMatrix(V);
476 } else {
477 cachedU = MatrixUtils.createRealMatrix(V);
478 cachedV = MatrixUtils.createRealMatrix(U);
479 }
480 }
481
482
483
484
485
486
487
488 public RealMatrix getU() {
489
490 return cachedU;
491 }
492
493
494
495
496
497
498
499 public RealMatrix getUT() {
500 if (cachedUt == null) {
501 cachedUt = getU().transpose();
502 }
503
504 return cachedUt;
505 }
506
507
508
509
510
511
512
513 public RealMatrix getS() {
514 if (cachedS == null) {
515
516 cachedS = MatrixUtils.createRealDiagonalMatrix(singularValues);
517 }
518 return cachedS;
519 }
520
521
522
523
524
525
526
527 public double[] getSingularValues() {
528 return singularValues.clone();
529 }
530
531
532
533
534
535
536
537 public RealMatrix getV() {
538
539 return cachedV;
540 }
541
542
543
544
545
546
547
548 public RealMatrix getVT() {
549 if (cachedVt == null) {
550 cachedVt = getV().transpose();
551 }
552
553 return cachedVt;
554 }
555
556
557
558
559
560
561
562
563
564
565
566
567 public RealMatrix getCovariance(final double minSingularValue) {
568
569 final int p = singularValues.length;
570 int dimension = 0;
571 while (dimension < p &&
572 singularValues[dimension] >= minSingularValue) {
573 ++dimension;
574 }
575
576 if (dimension == 0) {
577 throw new NumberIsTooLargeException(LocalizedFormats.TOO_LARGE_CUTOFF_SINGULAR_VALUE,
578 minSingularValue, singularValues[0], true);
579 }
580
581 final double[][] data = new double[dimension][p];
582 getVT().walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() {
583
584 @Override
585 public void visit(final int row, final int column,
586 final double value) {
587 data[row][column] = value / singularValues[row];
588 }
589 }, 0, dimension - 1, 0, p - 1);
590
591 RealMatrix jv = new Array2DRowRealMatrix(data, false);
592 return jv.transpose().multiply(jv);
593 }
594
595
596
597
598
599
600
601
602 public double getNorm() {
603 return singularValues[0];
604 }
605
606
607
608
609
610 public double getConditionNumber() {
611 return singularValues[0] / singularValues[n - 1];
612 }
613
614
615
616
617
618
619
620
621 public double getInverseConditionNumber() {
622 return singularValues[n - 1] / singularValues[0];
623 }
624
625
626
627
628
629
630
631
632
633 public int getRank() {
634 int r = 0;
635 for (int i = 0; i < singularValues.length; i++) {
636 if (singularValues[i] > tol) {
637 r++;
638 }
639 }
640 return r;
641 }
642
643
644
645
646
647 public DecompositionSolver getSolver() {
648 return new Solver(singularValues, getUT(), getV(), getRank() == m, tol);
649 }
650
651
652 private static final class Solver implements DecompositionSolver {
653
654 private final RealMatrix pseudoInverse;
655
656 private final boolean nonSingular;
657
658
659
660
661
662
663
664
665
666
667 private Solver(final double[] singularValues, final RealMatrix uT,
668 final RealMatrix v, final boolean nonSingular, final double tol) {
669 final double[][] suT = uT.getData();
670 for (int i = 0; i < singularValues.length; ++i) {
671 final double a;
672 if (singularValues[i] > tol) {
673 a = 1 / singularValues[i];
674 } else {
675 a = 0;
676 }
677 final double[] suTi = suT[i];
678 for (int j = 0; j < suTi.length; ++j) {
679 suTi[j] *= a;
680 }
681 }
682 pseudoInverse = v.multiply(new Array2DRowRealMatrix(suT, false));
683 this.nonSingular = nonSingular;
684 }
685
686
687
688
689
690
691
692
693
694
695
696
697 @Override
698 public RealVector solve(final RealVector b) {
699 return pseudoInverse.operate(b);
700 }
701
702
703
704
705
706
707
708
709
710
711
712
713
714 @Override
715 public RealMatrix solve(final RealMatrix b) {
716 return pseudoInverse.multiply(b);
717 }
718
719
720
721
722
723
724 @Override
725 public boolean isNonSingular() {
726 return nonSingular;
727 }
728
729
730
731
732
733
734 @Override
735 public RealMatrix getInverse() {
736 return pseudoInverse;
737 }
738 }
739 }