View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.linear;
24  
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  
30  public class CholeskySolverTest {
31  
32      private double[][] testData = new double[][] {
33              {  1,  2,   4,   7,  11 },
34              {  2, 13,  23,  38,  58 },
35              {  4, 23,  77, 122, 182 },
36              {  7, 38, 122, 294, 430 },
37              { 11, 58, 182, 430, 855 }
38      };
39  
40      /** test solve dimension errors */
41      @Test
42      public void testSolveDimensionErrors() {
43          DecompositionSolver solver =
44              new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData)).getSolver();
45          RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
46          try {
47              solver.solve(b);
48              Assert.fail("an exception should have been thrown");
49          } catch (MathIllegalArgumentException iae) {
50              // expected behavior
51          }
52          try {
53              solver.solve(b.getColumnVector(0));
54              Assert.fail("an exception should have been thrown");
55          } catch (MathIllegalArgumentException iae) {
56              // expected behavior
57          }
58          try {
59              solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
60              Assert.fail("an exception should have been thrown");
61          } catch (MathIllegalArgumentException iae) {
62              // expected behavior
63          }
64      }
65  
66      /** test solve */
67      @Test
68      public void testSolve() {
69          DecompositionSolver solver =
70              new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData)).getSolver();
71          RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
72                  {   78,  -13,    1 },
73                  {  414,  -62,   -1 },
74                  { 1312, -202,  -37 },
75                  { 2989, -542,  145 },
76                  { 5510, -1465, 201 }
77          });
78          RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] {
79                  { 1,  0,  1 },
80                  { 0,  1,  1 },
81                  { 2,  1, -4 },
82                  { 2,  2,  2 },
83                  { 5, -3,  0 }
84          });
85  
86          // using RealMatrix
87          Assert.assertEquals(0, solver.solve(b).subtract(xRef).getNorm1(), 1.0e-13);
88  
89          // using ArrayRealVector
90          for (int i = 0; i < b.getColumnDimension(); ++i) {
91              Assert.assertEquals(0,
92                           solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
93                           1.0e-13);
94          }
95  
96          // using RealVector with an alternate implementation
97          for (int i = 0; i < b.getColumnDimension(); ++i) {
98              ArrayRealVectorTest.RealVectorTestImpl v =
99                  new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i));
100             Assert.assertEquals(0,
101                          solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
102                          1.0e-13);
103         }
104 
105     }
106 
107     /** test determinant */
108     @Test
109     public void testDeterminant() {
110         Assert.assertEquals(7290000.0, getDeterminant(MatrixUtils.createRealMatrix(testData)), 1.0e-15);
111     }
112 
113     private double getDeterminant(RealMatrix m) {
114         return new CholeskyDecomposition(m).getDeterminant();
115     }
116 
117 }