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, () -> {
51              new SemiDefinitePositiveCholeskyDecomposition(MatrixUtils.createRealMatrix(new double[3][2]));
52          });
53      }
54  
55      /** test negative definite matrix */
56      @Test
57      void testNotPositiveDefinite() {
58          assertThrows(MathIllegalArgumentException.class, () -> {
59              new SemiDefinitePositiveCholeskyDecomposition(MatrixUtils.createRealMatrix(new double[][]{
60                  {-14, 11, 13, 15, 24},
61                  {11, 34, 13, 8, 25},
62                  {-13, 13, 14, 15, 21},
63                  {15, 8, -15, 18, 23},
64                  {24, 25, 21, 23, -45}
65              }));
66          });
67      }
68  
69      /** test A = LLT */
70      @Test
71      void testAEqualLLT() {
72          RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
73          SemiDefinitePositiveCholeskyDecomposition llt = new SemiDefinitePositiveCholeskyDecomposition(matrix);
74          RealMatrix l  = llt.getL();
75          RealMatrix lt = llt.getLT();
76          double norm = l.multiply(lt).subtract(matrix).getNorm1();
77          assertEquals(0, norm, 1.0e-15);
78      }
79  
80      /** test that L is lower triangular */
81      @Test
82      void testLLowerTriangular() {
83          RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
84          RealMatrix l = new SemiDefinitePositiveCholeskyDecomposition(matrix).getL();
85          for (int i = 0; i < l.getRowDimension(); i++) {
86              for (int j = i + 1; j < l.getColumnDimension(); j++) {
87                  assertEquals(0.0, l.getEntry(i, j), 0.0);
88              }
89          }
90      }
91  
92      /** test that LT is transpose of L */
93      @Test
94      void testLTTransposed() {
95          RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
96          SemiDefinitePositiveCholeskyDecomposition llt = new SemiDefinitePositiveCholeskyDecomposition(matrix);
97          RealMatrix l  = llt.getL();
98          RealMatrix lt = llt.getLT();
99          double norm = l.subtract(lt.transpose()).getNorm1();
100         assertEquals(0, norm, 1.0e-15);
101     }
102 
103 }