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