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