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, () -> {
51 new SemiDefinitePositiveCholeskyDecomposition(MatrixUtils.createRealMatrix(new double[3][2]));
52 });
53 }
54
55
56 @Test
57 void testNotPositiveDefinite() {
58 assertThrows(MathIllegalArgumentException.class, () -> {
59 new SemiDefinitePositiveCholeskyDecomposition(MatrixUtils.createRealMatrix(new double[][]{
60 {-14, 11, 13, 15, 24},
61 {11, 34, 13, 8, 25},
62 {-13, 13, 14, 15, 21},
63 {15, 8, -15, 18, 23},
64 {24, 25, 21, 23, -45}
65 }));
66 });
67 }
68
69
70 @Test
71 void testAEqualLLT() {
72 RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
73 SemiDefinitePositiveCholeskyDecomposition llt = new SemiDefinitePositiveCholeskyDecomposition(matrix);
74 RealMatrix l = llt.getL();
75 RealMatrix lt = llt.getLT();
76 double norm = l.multiply(lt).subtract(matrix).getNorm1();
77 assertEquals(0, norm, 1.0e-15);
78 }
79
80
81 @Test
82 void testLLowerTriangular() {
83 RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
84 RealMatrix l = new SemiDefinitePositiveCholeskyDecomposition(matrix).getL();
85 for (int i = 0; i < l.getRowDimension(); i++) {
86 for (int j = i + 1; j < l.getColumnDimension(); j++) {
87 assertEquals(0.0, l.getEntry(i, j), 0.0);
88 }
89 }
90 }
91
92
93 @Test
94 void testLTTransposed() {
95 RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
96 SemiDefinitePositiveCholeskyDecomposition llt = new SemiDefinitePositiveCholeskyDecomposition(matrix);
97 RealMatrix l = llt.getL();
98 RealMatrix lt = llt.getLT();
99 double norm = l.subtract(lt.transpose()).getNorm1();
100 assertEquals(0, norm, 1.0e-15);
101 }
102
103 }