1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.linear;
24
25 import org.junit.jupiter.api.Test;
26
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29
30 class RectangularCholeskyDecompositionTest {
31
32 @Test
33 void testDecomposition3x3() {
34
35 RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
36 { 1, 9, 9 },
37 { 9, 225, 225 },
38 { 9, 225, 625 }
39 });
40
41 RectangularCholeskyDecomposition d =
42 new RectangularCholeskyDecomposition(m, 1.0e-6);
43
44
45
46
47 assertEquals(0.8, d.getRootMatrix().getEntry(0, 2), 1.0e-15);
48 assertEquals(25.0, d.getRootMatrix().getEntry(2, 0), 1.0e-15);
49 assertEquals(0.0, d.getRootMatrix().getEntry(2, 2), 1.0e-15);
50
51 RealMatrix root = d.getRootMatrix();
52 RealMatrix rebuiltM = root.multiplyTransposed(root);
53 assertEquals(0.0, m.subtract(rebuiltM).getNorm1(), 1.0e-15);
54
55 }
56
57 @Test
58 void testFullRank() {
59
60 RealMatrix base = MatrixUtils.createRealMatrix(new double[][] {
61 { 0.1159548705, 0., 0., 0. },
62 { 0.0896442724, 0.1223540781, 0., 0. },
63 { 0.0852155322, 4.558668e-3, 0.1083577299, 0. },
64 { 0.0905486674, 0.0213768077, 0.0128878333, 0.1014155693 }
65 });
66
67 RealMatrix m = base.multiplyTransposed(base);
68
69 RectangularCholeskyDecomposition d =
70 new RectangularCholeskyDecomposition(m, 1.0e-10);
71
72 RealMatrix root = d.getRootMatrix();
73 RealMatrix rebuiltM = root.multiply(root.transpose());
74 assertEquals(0.0, m.subtract(rebuiltM).getNorm1(), 1.0e-15);
75
76
77
78 assertTrue(root.subtract(base).getNorm1() > 0.25);
79
80 }
81
82 @Test
83 void testMath789() {
84
85 final RealMatrix m1 = MatrixUtils.createRealMatrix(new double[][]{
86 {0.013445532, 0.010394690, 0.009881156, 0.010499559},
87 {0.010394690, 0.023006616, 0.008196856, 0.010732709},
88 {0.009881156, 0.008196856, 0.019023866, 0.009210099},
89 {0.010499559, 0.010732709, 0.009210099, 0.019107243}
90 });
91 composeAndTest(m1, 4);
92
93 final RealMatrix m2 = MatrixUtils.createRealMatrix(new double[][]{
94 {0.0, 0.0, 0.0, 0.0, 0.0},
95 {0.0, 0.013445532, 0.010394690, 0.009881156, 0.010499559},
96 {0.0, 0.010394690, 0.023006616, 0.008196856, 0.010732709},
97 {0.0, 0.009881156, 0.008196856, 0.019023866, 0.009210099},
98 {0.0, 0.010499559, 0.010732709, 0.009210099, 0.019107243}
99 });
100 composeAndTest(m2, 4);
101
102 final RealMatrix m3 = MatrixUtils.createRealMatrix(new double[][]{
103 {0.013445532, 0.010394690, 0.0, 0.009881156, 0.010499559},
104 {0.010394690, 0.023006616, 0.0, 0.008196856, 0.010732709},
105 {0.0, 0.0, 0.0, 0.0, 0.0},
106 {0.009881156, 0.008196856, 0.0, 0.019023866, 0.009210099},
107 {0.010499559, 0.010732709, 0.0, 0.009210099, 0.019107243}
108 });
109 composeAndTest(m3, 4);
110
111 }
112
113 private void composeAndTest(RealMatrix m, int expectedRank) {
114 RectangularCholeskyDecomposition r = new RectangularCholeskyDecomposition(m);
115 assertEquals(expectedRank, r.getRank());
116 RealMatrix root = r.getRootMatrix();
117 RealMatrix rebuiltMatrix = root.multiplyTransposed(root);
118 assertEquals(0.0, m.subtract(rebuiltMatrix).getNorm1(), 1.0e-16);
119 }
120
121 }