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    *      http://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.apache.commons.math4.legacy.linear;
19  
20  import org.junit.Test;
21  import org.junit.Assert;
22  
23  public class CholeskyDecompositionTest {
24  
25      private double[][] testData = new double[][] {
26              {  1,  2,   4,   7,  11 },
27              {  2, 13,  23,  38,  58 },
28              {  4, 23,  77, 122, 182 },
29              {  7, 38, 122, 294, 430 },
30              { 11, 58, 182, 430, 855 }
31      };
32  
33      /** test dimensions */
34      @Test
35      public void testDimensions() {
36          CholeskyDecomposition llt =
37              new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData));
38          Assert.assertEquals(testData.length, llt.getL().getRowDimension());
39          Assert.assertEquals(testData.length, llt.getL().getColumnDimension());
40          Assert.assertEquals(testData.length, llt.getLT().getRowDimension());
41          Assert.assertEquals(testData.length, llt.getLT().getColumnDimension());
42      }
43  
44      /** test non-square matrix */
45      @Test(expected = NonSquareMatrixException.class)
46      public void testNonSquare() {
47          new CholeskyDecomposition(MatrixUtils.createRealMatrix(new double[3][2]));
48      }
49  
50      /** test non-symmetric matrix */
51      @Test(expected = NonSymmetricMatrixException.class)
52      public void testNotSymmetricMatrixException() {
53          double[][] changed = testData.clone();
54          changed[0][changed[0].length - 1] += 1.0e-5;
55          new CholeskyDecomposition(MatrixUtils.createRealMatrix(changed));
56      }
57  
58      /** test non positive definite matrix */
59      @Test(expected = NonPositiveDefiniteMatrixException.class)
60      public void testNotPositiveDefinite() {
61          new CholeskyDecomposition(MatrixUtils.createRealMatrix(new double[][] {
62                  { 14, 11, 13, 15, 24 },
63                  { 11, 34, 13, 8,  25 },
64                  { 13, 13, 14, 15, 21 },
65                  { 15, 8,  15, 18, 23 },
66                  { 24, 25, 21, 23, 45 }
67          }));
68      }
69  
70      @Test(expected = NonPositiveDefiniteMatrixException.class)
71      public void testMath274() {
72          new CholeskyDecomposition(MatrixUtils.createRealMatrix(new double[][] {
73                  { 0.40434286, -0.09376327, 0.30328980, 0.04909388 },
74                  {-0.09376327,  0.10400408, 0.07137959, 0.04762857 },
75                  { 0.30328980,  0.07137959, 0.30458776, 0.04882449 },
76                  { 0.04909388,  0.04762857, 0.04882449, 0.07543265 }
77          }));
78      }
79  
80      /** test A = LLT */
81      @Test
82      public void testAEqualLLT() {
83          RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
84          CholeskyDecomposition llt = new CholeskyDecomposition(matrix);
85          RealMatrix l  = llt.getL();
86          RealMatrix lt = llt.getLT();
87          double norm = l.multiply(lt).subtract(matrix).getNorm();
88          Assert.assertEquals(0, norm, 1.0e-15);
89      }
90  
91      /** test that L is lower triangular */
92      @Test
93      public void testLLowerTriangular() {
94          RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
95          RealMatrix l = new CholeskyDecomposition(matrix).getL();
96          for (int i = 0; i < l.getRowDimension(); i++) {
97              for (int j = i + 1; j < l.getColumnDimension(); j++) {
98                  Assert.assertEquals(0.0, l.getEntry(i, j), 0.0);
99              }
100         }
101     }
102 
103     /** test that LT is transpose of L */
104     @Test
105     public void testLTTransposed() {
106         RealMatrix matrix = MatrixUtils.createRealMatrix(testData);
107         CholeskyDecomposition llt = new CholeskyDecomposition(matrix);
108         RealMatrix l  = llt.getL();
109         RealMatrix lt = llt.getLT();
110         double norm = l.subtract(lt.transpose()).getNorm();
111         Assert.assertEquals(0, norm, 1.0e-15);
112     }
113 
114     /** test matrices values */
115     @Test
116     public void testMatricesValues() {
117         RealMatrix lRef = MatrixUtils.createRealMatrix(new double[][] {
118                 {  1,  0,  0,  0,  0 },
119                 {  2,  3,  0,  0,  0 },
120                 {  4,  5,  6,  0,  0 },
121                 {  7,  8,  9, 10,  0 },
122                 { 11, 12, 13, 14, 15 }
123         });
124        CholeskyDecomposition llt =
125             new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData));
126 
127         // check values against known references
128         RealMatrix l = llt.getL();
129         Assert.assertEquals(0, l.subtract(lRef).getNorm(), 1.0e-13);
130         RealMatrix lt = llt.getLT();
131         Assert.assertEquals(0, lt.subtract(lRef.transpose()).getNorm(), 1.0e-13);
132 
133         // check the same cached instance is returned the second time
134         Assert.assertSame(l, llt.getL());
135         Assert.assertSame(lt, llt.getLT());
136     }
137 }