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