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