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.junit.Assert;
26  import org.junit.Test;
27  
28  public class RectangularCholeskyDecompositionTest {
29  
30      @Test
31      public void testDecomposition3x3() {
32  
33          RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
34              { 1,   9,   9 },
35              { 9, 225, 225 },
36              { 9, 225, 625 }
37          });
38  
39          RectangularCholeskyDecomposition d =
40                  new RectangularCholeskyDecomposition(m, 1.0e-6);
41  
42          // as this decomposition permutes lines and columns, the root is NOT triangular
43          // (in fact here it is the lower right part of the matrix which is zero and
44          //  the upper left non-zero)
45          Assert.assertEquals(0.8,  d.getRootMatrix().getEntry(0, 2), 1.0e-15);
46          Assert.assertEquals(25.0, d.getRootMatrix().getEntry(2, 0), 1.0e-15);
47          Assert.assertEquals(0.0,  d.getRootMatrix().getEntry(2, 2), 1.0e-15);
48  
49          RealMatrix root = d.getRootMatrix();
50          RealMatrix rebuiltM = root.multiplyTransposed(root);
51          Assert.assertEquals(0.0, m.subtract(rebuiltM).getNorm1(), 1.0e-15);
52  
53      }
54  
55      @Test
56      public void testFullRank() {
57  
58          RealMatrix base = MatrixUtils.createRealMatrix(new double[][] {
59              { 0.1159548705,      0.,           0.,           0.      },
60              { 0.0896442724, 0.1223540781,      0.,           0.      },
61              { 0.0852155322, 4.558668e-3,  0.1083577299,      0.      },
62              { 0.0905486674, 0.0213768077, 0.0128878333, 0.1014155693 }
63          });
64  
65          RealMatrix m = base.multiplyTransposed(base);
66  
67          RectangularCholeskyDecomposition d =
68                  new RectangularCholeskyDecomposition(m, 1.0e-10);
69  
70          RealMatrix root = d.getRootMatrix();
71          RealMatrix rebuiltM = root.multiply(root.transpose());
72          Assert.assertEquals(0.0, m.subtract(rebuiltM).getNorm1(), 1.0e-15);
73  
74          // the pivoted Cholesky decomposition is *not* unique. Here, the root is
75          // not equal to the original triangular base matrix
76          Assert.assertTrue(root.subtract(base).getNorm1() > 0.25);
77  
78      }
79  
80      @Test
81      public void testMath789() {
82  
83          final RealMatrix m1 = MatrixUtils.createRealMatrix(new double[][]{
84              {0.013445532, 0.010394690, 0.009881156, 0.010499559},
85              {0.010394690, 0.023006616, 0.008196856, 0.010732709},
86              {0.009881156, 0.008196856, 0.019023866, 0.009210099},
87              {0.010499559, 0.010732709, 0.009210099, 0.019107243}
88          });
89          composeAndTest(m1, 4);
90  
91          final RealMatrix m2 = MatrixUtils.createRealMatrix(new double[][]{
92              {0.0, 0.0, 0.0, 0.0, 0.0},
93              {0.0, 0.013445532, 0.010394690, 0.009881156, 0.010499559},
94              {0.0, 0.010394690, 0.023006616, 0.008196856, 0.010732709},
95              {0.0, 0.009881156, 0.008196856, 0.019023866, 0.009210099},
96              {0.0, 0.010499559, 0.010732709, 0.009210099, 0.019107243}
97          });
98          composeAndTest(m2, 4);
99  
100         final RealMatrix m3 = MatrixUtils.createRealMatrix(new double[][]{
101             {0.013445532, 0.010394690, 0.0, 0.009881156, 0.010499559},
102             {0.010394690, 0.023006616, 0.0, 0.008196856, 0.010732709},
103             {0.0, 0.0, 0.0, 0.0, 0.0},
104             {0.009881156, 0.008196856, 0.0, 0.019023866, 0.009210099},
105             {0.010499559, 0.010732709, 0.0, 0.009210099, 0.019107243}
106         });
107         composeAndTest(m3, 4);
108 
109     }
110 
111     private void composeAndTest(RealMatrix m, int expectedRank) {
112         RectangularCholeskyDecomposition r = new RectangularCholeskyDecomposition(m);
113         Assert.assertEquals(expectedRank, r.getRank());
114         RealMatrix root = r.getRootMatrix();
115         RealMatrix rebuiltMatrix = root.multiplyTransposed(root);
116         Assert.assertEquals(0.0, m.subtract(rebuiltMatrix).getNorm1(), 1.0e-16);
117     }
118 
119 }