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    *      http://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  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      /** test solve dimension errors */
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              // expected behavior
46          }
47          try {
48              solver.solve(b.getColumnVector(0));
49              Assert.fail("an exception should have been thrown");
50          } catch (MathIllegalArgumentException iae) {
51              // expected behavior
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              // expected behavior
58          }
59      }
60  
61      /** test solve */
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          // using RealMatrix
82          Assert.assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), 1.0e-13);
83  
84          // using ArrayRealVector
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          // using RealVector with an alternate implementation
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     /** test determinant */
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 }