View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.linear;
19  
20  import org.hipparchus.exception.MathIllegalArgumentException;
21  import org.junit.jupiter.api.Test;
22  
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  
26  class SemiDefinitePositiveCholeskyDecompositionTest {
27  
28      private double[][] testData = new double[][] {
29          {  1,  2,   4,   7,  11 },
30          {  2, 13,  23,  38,  58 },
31          {  4, 23,  77, 122, 182 },
32          {  7, 38, 122, 294, 430 },
33          { 11, 58, 182, 430, 855 }
34      };
35  
36      /** test dimensions */
37      @Test
38      void testDimensions() {
39          SemiDefinitePositiveCholeskyDecomposition llt =
40              new SemiDefinitePositiveCholeskyDecomposition(MatrixUtils.createRealMatrix(testData));
41          assertEquals(testData.length, llt.getL().getRowDimension());
42          assertEquals(testData.length, llt.getL().getColumnDimension());
43          assertEquals(testData.length, llt.getLT().getRowDimension());
44          assertEquals(testData.length, llt.getLT().getColumnDimension());
45      }
46  
47      /** test non-square matrix */
48      @Test
49      void testNonSquare() {
50          assertThrows(MathIllegalArgumentException.class, () -> new SemiDefinitePositiveCholeskyDecomposition(MatrixUtils.createRealMatrix(new double[3][2])));
51      }
52  
53      /** test negative definite matrix */
54      @Test
55      void testNotPositiveDefinite() {
56          assertThrows(MathIllegalArgumentException.class, () -> new SemiDefinitePositiveCholeskyDecomposition(MatrixUtils.createRealMatrix(new double[][]{
57              {-14, 11, 13, 15, 24},
58              {11, 34, 13, 8, 25},
59              {-13, 13, 14, 15, 21},
60              {15, 8, -15, 18, 23},
61              {24, 25, 21, 23, -45}
62          })));
63      }
64  
65      /** test A = LLT */
66      @Test
67      void testAEqualLLT() {
68          RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
69          SemiDefinitePositiveCholeskyDecomposition llt = new SemiDefinitePositiveCholeskyDecomposition(matrix);
70          RealMatrix l  = llt.getL();
71          RealMatrix lt = llt.getLT();
72          double norm = l.multiply(lt).subtract(matrix).getNorm1();
73          assertEquals(0, norm, 1.0e-15);
74      }
75  
76      /** test that L is lower triangular */
77      @Test
78      void testLLowerTriangular() {
79          RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
80          RealMatrix l = new SemiDefinitePositiveCholeskyDecomposition(matrix).getL();
81          for (int i = 0; i < l.getRowDimension(); i++) {
82              for (int j = i + 1; j < l.getColumnDimension(); j++) {
83                  assertEquals(0.0, l.getEntry(i, j), 0.0);
84              }
85          }
86      }
87  
88      /** test that LT is transpose of L */
89      @Test
90      void testLTTransposed() {
91          RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
92          SemiDefinitePositiveCholeskyDecomposition llt = new SemiDefinitePositiveCholeskyDecomposition(matrix);
93          RealMatrix l  = llt.getL();
94          RealMatrix lt = llt.getLT();
95          double norm = l.subtract(lt.transpose()).getNorm1();
96          assertEquals(0, norm, 1.0e-15);
97      }
98  
99  }