1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.hipparchus.linear;
19
20 import org.hipparchus.exception.MathIllegalArgumentException;
21 import org.junit.jupiter.api.Test;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25
26 class SemiDefinitePositiveCholeskyDecompositionTest {
27
28 private double[][] testData = new double[][] {
29 { 1, 2, 4, 7, 11 },
30 { 2, 13, 23, 38, 58 },
31 { 4, 23, 77, 122, 182 },
32 { 7, 38, 122, 294, 430 },
33 { 11, 58, 182, 430, 855 }
34 };
35
36
37 @Test
38 void testDimensions() {
39 SemiDefinitePositiveCholeskyDecomposition llt =
40 new SemiDefinitePositiveCholeskyDecomposition(MatrixUtils.createRealMatrix(testData));
41 assertEquals(testData.length, llt.getL().getRowDimension());
42 assertEquals(testData.length, llt.getL().getColumnDimension());
43 assertEquals(testData.length, llt.getLT().getRowDimension());
44 assertEquals(testData.length, llt.getLT().getColumnDimension());
45 }
46
47
48 @Test
49 void testNonSquare() {
50 assertThrows(MathIllegalArgumentException.class, () -> new SemiDefinitePositiveCholeskyDecomposition(MatrixUtils.createRealMatrix(new double[3][2])));
51 }
52
53
54 @Test
55 void testNotPositiveDefinite() {
56 assertThrows(MathIllegalArgumentException.class, () -> new SemiDefinitePositiveCholeskyDecomposition(MatrixUtils.createRealMatrix(new double[][]{
57 {-14, 11, 13, 15, 24},
58 {11, 34, 13, 8, 25},
59 {-13, 13, 14, 15, 21},
60 {15, 8, -15, 18, 23},
61 {24, 25, 21, 23, -45}
62 })));
63 }
64
65
66 @Test
67 void testAEqualLLT() {
68 RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
69 SemiDefinitePositiveCholeskyDecomposition llt = new SemiDefinitePositiveCholeskyDecomposition(matrix);
70 RealMatrix l = llt.getL();
71 RealMatrix lt = llt.getLT();
72 double norm = l.multiply(lt).subtract(matrix).getNorm1();
73 assertEquals(0, norm, 1.0e-15);
74 }
75
76
77 @Test
78 void testLLowerTriangular() {
79 RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
80 RealMatrix l = new SemiDefinitePositiveCholeskyDecomposition(matrix).getL();
81 for (int i = 0; i < l.getRowDimension(); i++) {
82 for (int j = i + 1; j < l.getColumnDimension(); j++) {
83 assertEquals(0.0, l.getEntry(i, j), 0.0);
84 }
85 }
86 }
87
88
89 @Test
90 void testLTTransposed() {
91 RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
92 SemiDefinitePositiveCholeskyDecomposition llt = new SemiDefinitePositiveCholeskyDecomposition(matrix);
93 RealMatrix l = llt.getL();
94 RealMatrix lt = llt.getLT();
95 double norm = l.subtract(lt.transpose()).getNorm1();
96 assertEquals(0, norm, 1.0e-15);
97 }
98
99 }