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.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  import static org.junit.jupiter.api.Assertions.fail;
30  
31  
32  class CholeskySolverTest {
33  
34      private double[][] testData = new double[][] {
35              {  1,  2,   4,   7,  11 },
36              {  2, 13,  23,  38,  58 },
37              {  4, 23,  77, 122, 182 },
38              {  7, 38, 122, 294, 430 },
39              { 11, 58, 182, 430, 855 }
40      };
41  
42      /** test solve dimension errors */
43      @Test
44      void testSolveDimensionErrors() {
45          DecompositionSolver solver =
46              new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData)).getSolver();
47          RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
48          try {
49              solver.solve(b);
50              fail("an exception should have been thrown");
51          } catch (MathIllegalArgumentException iae) {
52              // expected behavior
53          }
54          try {
55              solver.solve(b.getColumnVector(0));
56              fail("an exception should have been thrown");
57          } catch (MathIllegalArgumentException iae) {
58              // expected behavior
59          }
60          try {
61              solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
62              fail("an exception should have been thrown");
63          } catch (MathIllegalArgumentException iae) {
64              // expected behavior
65          }
66      }
67  
68      /** test solve */
69      @Test
70      void testSolve() {
71          DecompositionSolver solver =
72              new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData)).getSolver();
73          RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
74                  {   78,  -13,    1 },
75                  {  414,  -62,   -1 },
76                  { 1312, -202,  -37 },
77                  { 2989, -542,  145 },
78                  { 5510, -1465, 201 }
79          });
80          RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] {
81                  { 1,  0,  1 },
82                  { 0,  1,  1 },
83                  { 2,  1, -4 },
84                  { 2,  2,  2 },
85                  { 5, -3,  0 }
86          });
87  
88          // using RealMatrix
89          assertEquals(0, solver.solve(b).subtract(xRef).getNorm1(), 1.0e-13);
90  
91          // using ArrayRealVector
92          for (int i = 0; i < b.getColumnDimension(); ++i) {
93              assertEquals(0,
94                           solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
95                           1.0e-13);
96          }
97  
98          // using RealVector with an alternate implementation
99          for (int i = 0; i < b.getColumnDimension(); ++i) {
100             ArrayRealVectorTest.RealVectorTestImpl v =
101                 new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i));
102             assertEquals(0,
103                          solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
104                          1.0e-13);
105         }
106 
107     }
108 
109     /** test determinant */
110     @Test
111     void testDeterminant() {
112         assertEquals(7290000.0, getDeterminant(MatrixUtils.createRealMatrix(testData)), 1.0e-15);
113     }
114 
115     private double getDeterminant(RealMatrix m) {
116         return new CholeskyDecomposition(m).getDeterminant();
117     }
118 
119 }