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.hipparchus.exception.LocalizedCoreFormats;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27 import org.junit.jupiter.api.Test;
28
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30 import static org.junit.jupiter.api.Assertions.assertSame;
31 import static org.junit.jupiter.api.Assertions.assertThrows;
32 import static org.junit.jupiter.api.Assertions.fail;
33
34 class CholeskyDecompositionTest {
35
36 private double[][] testData = new double[][] {
37 { 1, 2, 4, 7, 11 },
38 { 2, 13, 23, 38, 58 },
39 { 4, 23, 77, 122, 182 },
40 { 7, 38, 122, 294, 430 },
41 { 11, 58, 182, 430, 855 }
42 };
43
44
45 @Test
46 void testDimensions() {
47 CholeskyDecomposition llt =
48 new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData));
49 assertEquals(testData.length, llt.getL().getRowDimension());
50 assertEquals(testData.length, llt.getL().getColumnDimension());
51 assertEquals(testData.length, llt.getLT().getRowDimension());
52 assertEquals(testData.length, llt.getLT().getColumnDimension());
53 }
54
55
56 @Test
57 void testNonSquare() {
58 assertThrows(MathIllegalArgumentException.class, () -> new CholeskyDecomposition(MatrixUtils.createRealMatrix(new double[3][2])));
59 }
60
61
62 @Test
63 void testNotSymmetricMatrixException() {
64 assertThrows(MathIllegalArgumentException.class, () -> {
65 double[][] changed = testData.clone();
66 changed[0][changed[0].length - 1] += 1.0e-5;
67 new CholeskyDecomposition(MatrixUtils.createRealMatrix(changed));
68 });
69 }
70
71
72 @Test
73 void testNotPositiveDefinite() {
74 assertThrows(MathIllegalArgumentException.class, () -> new CholeskyDecomposition(MatrixUtils.createRealMatrix(new double[][]{
75 {14, 11, 13, 15, 24},
76 {11, 34, 13, 8, 25},
77 {13, 13, 14, 15, 21},
78 {15, 8, 15, 18, 23},
79 {24, 25, 21, 23, 45}
80 })));
81 }
82
83 @Test
84 void testMath274() {
85 try {
86 new CholeskyDecomposition(MatrixUtils.createRealMatrix(new double[][] {
87 { 0.40434286, -0.09376327, 0.30328980, 0.04909388 },
88 {-0.09376327, 0.10400408, 0.07137959, 0.04762857 },
89 { 0.30328980, 0.07137959, 0.30458776, 0.04882449 },
90 { 0.04909388, 0.04762857, 0.04882449, 0.07543265 }
91
92 }));
93 fail("an exception should have been thrown");
94 } catch (MathIllegalArgumentException miae) {
95 assertEquals(LocalizedCoreFormats.NOT_POSITIVE_DEFINITE_MATRIX,
96 miae.getSpecifier());
97 }
98 }
99
100 @Test
101 void testDecomposer() {
102 new CholeskyDecomposer(1.0e-15, -0.2).
103 decompose(MatrixUtils.createRealMatrix(new double[][] {
104 { 0.40434286, -0.09376327, 0.30328980, 0.04909388 },
105 {-0.09376327, 0.10400408, 0.07137959, 0.04762857 },
106 { 0.30328980, 0.07137959, 0.30458776, 0.04882449 },
107 { 0.04909388, 0.04762857, 0.04882449, 0.07543265 }
108
109 }));
110 }
111
112
113 @Test
114 void testAEqualLLT() {
115 RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
116 CholeskyDecomposition llt = new CholeskyDecomposition(matrix);
117 RealMatrix l = llt.getL();
118 RealMatrix lt = llt.getLT();
119 double norm = l.multiply(lt).subtract(matrix).getNorm1();
120 assertEquals(0, norm, 1.0e-15);
121 assertEquals(matrix.getRowDimension(), llt.getSolver().getRowDimension());
122 assertEquals(matrix.getColumnDimension(), llt.getSolver().getColumnDimension());
123 }
124
125
126 @Test
127 void testLLowerTriangular() {
128 RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
129 RealMatrix l = new CholeskyDecomposition(matrix).getL();
130 for (int i = 0; i < l.getRowDimension(); i++) {
131 for (int j = i + 1; j < l.getColumnDimension(); j++) {
132 assertEquals(0.0, l.getEntry(i, j), 0.0);
133 }
134 }
135 }
136
137
138 @Test
139 void testLTTransposed() {
140 RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
141 CholeskyDecomposition llt = new CholeskyDecomposition(matrix);
142 RealMatrix l = llt.getL();
143 RealMatrix lt = llt.getLT();
144 double norm = l.subtract(lt.transpose()).getNorm1();
145 assertEquals(0, norm, 1.0e-15);
146 }
147
148
149 @Test
150 void testMatricesValues() {
151 RealMatrix lRef = MatrixUtils.createRealMatrix(new double[][] {
152 { 1, 0, 0, 0, 0 },
153 { 2, 3, 0, 0, 0 },
154 { 4, 5, 6, 0, 0 },
155 { 7, 8, 9, 10, 0 },
156 { 11, 12, 13, 14, 15 }
157 });
158 CholeskyDecomposition llt =
159 new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData));
160
161
162 RealMatrix l = llt.getL();
163 assertEquals(0, l.subtract(lRef).getNorm1(), 1.0e-13);
164 RealMatrix lt = llt.getLT();
165 assertEquals(0, lt.subtract(lRef.transpose()).getNorm1(), 1.0e-13);
166
167
168 assertSame(l, llt.getL());
169 assertSame(lt, llt.getLT());
170 }
171 }