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