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