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