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