1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  package org.apache.commons.math4.legacy.linear;
19  
20  import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
21  import org.junit.Assert;
22  import org.junit.Test;
23  
24  
25  public class CholeskySolverTest {
26  
27      private double[][] testData = new double[][] {
28              {  1,  2,   4,   7,  11 },
29              {  2, 13,  23,  38,  58 },
30              {  4, 23,  77, 122, 182 },
31              {  7, 38, 122, 294, 430 },
32              { 11, 58, 182, 430, 855 }
33      };
34  
35      
36      @Test
37      public void testSolveDimensionErrors() {
38          DecompositionSolver solver =
39              new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData)).getSolver();
40          RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
41          try {
42              solver.solve(b);
43              Assert.fail("an exception should have been thrown");
44          } catch (MathIllegalArgumentException iae) {
45              
46          }
47          try {
48              solver.solve(b.getColumnVector(0));
49              Assert.fail("an exception should have been thrown");
50          } catch (MathIllegalArgumentException iae) {
51              
52          }
53          try {
54              solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
55              Assert.fail("an exception should have been thrown");
56          } catch (MathIllegalArgumentException iae) {
57              
58          }
59      }
60  
61      
62      @Test
63      public void testSolve() {
64          DecompositionSolver solver =
65              new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData)).getSolver();
66          RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
67                  {   78,  -13,    1 },
68                  {  414,  -62,   -1 },
69                  { 1312, -202,  -37 },
70                  { 2989, -542,  145 },
71                  { 5510, -1465, 201 }
72          });
73          RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] {
74                  { 1,  0,  1 },
75                  { 0,  1,  1 },
76                  { 2,  1, -4 },
77                  { 2,  2,  2 },
78                  { 5, -3,  0 }
79          });
80  
81          
82          Assert.assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), 1.0e-13);
83  
84          
85          for (int i = 0; i < b.getColumnDimension(); ++i) {
86              Assert.assertEquals(0,
87                           solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
88                           1.0e-13);
89          }
90  
91          
92          for (int i = 0; i < b.getColumnDimension(); ++i) {
93              ArrayRealVectorTest.RealVectorTestImpl v =
94                  new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i));
95              Assert.assertEquals(0,
96                           solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
97                           1.0e-13);
98          }
99      }
100 
101     
102     @Test
103     public void testDeterminant() {
104         Assert.assertEquals(7290000.0, getDeterminant(MatrixUtils.createRealMatrix(testData)), 1.0e-15);
105     }
106 
107     private double getDeterminant(RealMatrix m) {
108         return new CholeskyDecomposition(m).getDeterminant();
109     }
110 }