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.junit.Assert;
26 import org.junit.Test;
27
28 public class RectangularCholeskyDecompositionTest {
29
30 @Test
31 public void testDecomposition3x3() {
32
33 RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
34 { 1, 9, 9 },
35 { 9, 225, 225 },
36 { 9, 225, 625 }
37 });
38
39 RectangularCholeskyDecomposition d =
40 new RectangularCholeskyDecomposition(m, 1.0e-6);
41
42
43
44
45 Assert.assertEquals(0.8, d.getRootMatrix().getEntry(0, 2), 1.0e-15);
46 Assert.assertEquals(25.0, d.getRootMatrix().getEntry(2, 0), 1.0e-15);
47 Assert.assertEquals(0.0, d.getRootMatrix().getEntry(2, 2), 1.0e-15);
48
49 RealMatrix root = d.getRootMatrix();
50 RealMatrix rebuiltM = root.multiplyTransposed(root);
51 Assert.assertEquals(0.0, m.subtract(rebuiltM).getNorm1(), 1.0e-15);
52
53 }
54
55 @Test
56 public void testFullRank() {
57
58 RealMatrix base = MatrixUtils.createRealMatrix(new double[][] {
59 { 0.1159548705, 0., 0., 0. },
60 { 0.0896442724, 0.1223540781, 0., 0. },
61 { 0.0852155322, 4.558668e-3, 0.1083577299, 0. },
62 { 0.0905486674, 0.0213768077, 0.0128878333, 0.1014155693 }
63 });
64
65 RealMatrix m = base.multiplyTransposed(base);
66
67 RectangularCholeskyDecomposition d =
68 new RectangularCholeskyDecomposition(m, 1.0e-10);
69
70 RealMatrix root = d.getRootMatrix();
71 RealMatrix rebuiltM = root.multiply(root.transpose());
72 Assert.assertEquals(0.0, m.subtract(rebuiltM).getNorm1(), 1.0e-15);
73
74
75
76 Assert.assertTrue(root.subtract(base).getNorm1() > 0.25);
77
78 }
79
80 @Test
81 public void testMath789() {
82
83 final RealMatrix m1 = MatrixUtils.createRealMatrix(new double[][]{
84 {0.013445532, 0.010394690, 0.009881156, 0.010499559},
85 {0.010394690, 0.023006616, 0.008196856, 0.010732709},
86 {0.009881156, 0.008196856, 0.019023866, 0.009210099},
87 {0.010499559, 0.010732709, 0.009210099, 0.019107243}
88 });
89 composeAndTest(m1, 4);
90
91 final RealMatrix m2 = MatrixUtils.createRealMatrix(new double[][]{
92 {0.0, 0.0, 0.0, 0.0, 0.0},
93 {0.0, 0.013445532, 0.010394690, 0.009881156, 0.010499559},
94 {0.0, 0.010394690, 0.023006616, 0.008196856, 0.010732709},
95 {0.0, 0.009881156, 0.008196856, 0.019023866, 0.009210099},
96 {0.0, 0.010499559, 0.010732709, 0.009210099, 0.019107243}
97 });
98 composeAndTest(m2, 4);
99
100 final RealMatrix m3 = MatrixUtils.createRealMatrix(new double[][]{
101 {0.013445532, 0.010394690, 0.0, 0.009881156, 0.010499559},
102 {0.010394690, 0.023006616, 0.0, 0.008196856, 0.010732709},
103 {0.0, 0.0, 0.0, 0.0, 0.0},
104 {0.009881156, 0.008196856, 0.0, 0.019023866, 0.009210099},
105 {0.010499559, 0.010732709, 0.0, 0.009210099, 0.019107243}
106 });
107 composeAndTest(m3, 4);
108
109 }
110
111 private void composeAndTest(RealMatrix m, int expectedRank) {
112 RectangularCholeskyDecomposition r = new RectangularCholeskyDecomposition(m);
113 Assert.assertEquals(expectedRank, r.getRank());
114 RealMatrix root = r.getRootMatrix();
115 RealMatrix rebuiltMatrix = root.multiplyTransposed(root);
116 Assert.assertEquals(0.0, m.subtract(rebuiltMatrix).getNorm1(), 1.0e-16);
117 }
118
119 }