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.LocalizedCoreFormats;
26  import org.hipparchus.exception.MathIllegalArgumentException;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  public class CholeskyDecompositionTest {
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 dimensions */
41      @Test
42      public void testDimensions() {
43          CholeskyDecomposition llt =
44              new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData));
45          Assert.assertEquals(testData.length, llt.getL().getRowDimension());
46          Assert.assertEquals(testData.length, llt.getL().getColumnDimension());
47          Assert.assertEquals(testData.length, llt.getLT().getRowDimension());
48          Assert.assertEquals(testData.length, llt.getLT().getColumnDimension());
49      }
50  
51      /** test non-square matrix */
52      @Test(expected = MathIllegalArgumentException.class)
53      public void testNonSquare() {
54          new CholeskyDecomposition(MatrixUtils.createRealMatrix(new double[3][2]));
55      }
56  
57      /** test non-symmetric matrix */
58      @Test(expected = MathIllegalArgumentException.class)
59      public void testNotSymmetricMatrixException() {
60          double[][] changed = testData.clone();
61          changed[0][changed[0].length - 1] += 1.0e-5;
62          new CholeskyDecomposition(MatrixUtils.createRealMatrix(changed));
63      }
64  
65      /** test non positive definite matrix */
66      @Test(expected = MathIllegalArgumentException.class)
67      public void testNotPositiveDefinite() {
68          new CholeskyDecomposition(MatrixUtils.createRealMatrix(new double[][] {
69                  { 14, 11, 13, 15, 24 },
70                  { 11, 34, 13, 8,  25 },
71                  { 13, 13, 14, 15, 21 },
72                  { 15, 8,  15, 18, 23 },
73                  { 24, 25, 21, 23, 45 }
74          }));
75      }
76  
77      @Test
78      public void testMath274() {
79          try {
80              new CholeskyDecomposition(MatrixUtils.createRealMatrix(new double[][] {
81                  { 0.40434286, -0.09376327, 0.30328980, 0.04909388 },
82                  {-0.09376327,  0.10400408, 0.07137959, 0.04762857 },
83                  { 0.30328980,  0.07137959, 0.30458776, 0.04882449 },
84                  { 0.04909388,  0.04762857, 0.04882449, 0.07543265 }
85  
86              }));
87              Assert.fail("an exception should have been thrown");
88          } catch (MathIllegalArgumentException miae) {
89              Assert.assertEquals(LocalizedCoreFormats.NOT_POSITIVE_DEFINITE_MATRIX,
90                                  miae.getSpecifier());
91          }
92      }
93  
94      @Test
95      public void testDecomposer() {
96          new CholeskyDecomposer(1.0e-15, -0.2).
97          decompose(MatrixUtils.createRealMatrix(new double[][] {
98              { 0.40434286, -0.09376327, 0.30328980, 0.04909388 },
99              {-0.09376327,  0.10400408, 0.07137959, 0.04762857 },
100             { 0.30328980,  0.07137959, 0.30458776, 0.04882449 },
101             { 0.04909388,  0.04762857, 0.04882449, 0.07543265 }
102 
103         }));
104     }
105 
106     /** test A = LLT */
107     @Test
108     public void testAEqualLLT() {
109         RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
110         CholeskyDecomposition llt = new CholeskyDecomposition(matrix);
111         RealMatrix l  = llt.getL();
112         RealMatrix lt = llt.getLT();
113         double norm = l.multiply(lt).subtract(matrix).getNorm1();
114         Assert.assertEquals(0, norm, 1.0e-15);
115         Assert.assertEquals(matrix.getRowDimension(),    llt.getSolver().getRowDimension());
116         Assert.assertEquals(matrix.getColumnDimension(), llt.getSolver().getColumnDimension());
117     }
118 
119     /** test that L is lower triangular */
120     @Test
121     public void testLLowerTriangular() {
122         RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
123         RealMatrix l = new CholeskyDecomposition(matrix).getL();
124         for (int i = 0; i < l.getRowDimension(); i++) {
125             for (int j = i + 1; j < l.getColumnDimension(); j++) {
126                 Assert.assertEquals(0.0, l.getEntry(i, j), 0.0);
127             }
128         }
129     }
130 
131     /** test that LT is transpose of L */
132     @Test
133     public void testLTTransposed() {
134         RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
135         CholeskyDecomposition llt = new CholeskyDecomposition(matrix);
136         RealMatrix l  = llt.getL();
137         RealMatrix lt = llt.getLT();
138         double norm = l.subtract(lt.transpose()).getNorm1();
139         Assert.assertEquals(0, norm, 1.0e-15);
140     }
141 
142     /** test matrices values */
143     @Test
144     public void testMatricesValues() {
145         RealMatrix lRef = MatrixUtils.createRealMatrix(new double[][] {
146                 {  1,  0,  0,  0,  0 },
147                 {  2,  3,  0,  0,  0 },
148                 {  4,  5,  6,  0,  0 },
149                 {  7,  8,  9, 10,  0 },
150                 { 11, 12, 13, 14, 15 }
151         });
152        CholeskyDecomposition llt =
153             new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData));
154 
155         // check values against known references
156         RealMatrix l = llt.getL();
157         Assert.assertEquals(0, l.subtract(lRef).getNorm1(), 1.0e-13);
158         RealMatrix lt = llt.getLT();
159         Assert.assertEquals(0, lt.subtract(lRef.transpose()).getNorm1(), 1.0e-13);
160 
161         // check the same cached instance is returned the second time
162         Assert.assertTrue(l  == llt.getL());
163         Assert.assertTrue(lt == llt.getLT());
164     }
165 }