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.hipparchus.exception.MathIllegalArgumentException;
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  public class SingularValueSolverTest {
30  
31      private double[][] testSquare = {
32              { 24.0 / 25.0, 43.0 / 25.0 },
33              { 57.0 / 25.0, 24.0 / 25.0 }
34      };
35      private double[][] bigSingular = {
36          { 1.0, 2.0,   3.0,    4.0 },
37          { 2.0, 5.0,   3.0,    4.0 },
38          { 7.0, 3.0, 256.0, 1930.0 },
39          { 3.0, 7.0,   6.0,    8.0 }
40      }; // 4th row = 1st + 2nd
41  
42      private static final double normTolerance = 10e-14;
43  
44      /** test solve dimension errors */
45      @Test
46      public void testSolveDimensionErrors() {
47          DecompositionSolver solver =
48              new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare)).getSolver();
49          RealMatrix b = MatrixUtils.createRealMatrix(new double[3][2]);
50          try {
51              solver.solve(b);
52              Assert.fail("an exception should have been thrown");
53          } catch (MathIllegalArgumentException iae) {
54              // expected behavior
55          }
56          try {
57              solver.solve(b.getColumnVector(0));
58              Assert.fail("an exception should have been thrown");
59          } catch (MathIllegalArgumentException iae) {
60              // expected behavior
61          }
62          try {
63              solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
64              Assert.fail("an exception should have been thrown");
65          } catch (MathIllegalArgumentException iae) {
66              // expected behavior
67          }
68      }
69  
70      /** test least square solve */
71      @Test
72      public void testLeastSquareSolve() {
73          RealMatrix m =
74              MatrixUtils.createRealMatrix(new double[][] {
75                                     { 1.0, 0.0 },
76                                     { 0.0, 0.0 }
77                                 });
78          DecompositionSolver solver = new SingularValueDecomposition(m).getSolver();
79          RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
80              { 11, 12 }, { 21, 22 }
81          });
82          RealMatrix xMatrix = solver.solve(b);
83          Assert.assertEquals(11, xMatrix.getEntry(0, 0), 1.0e-15);
84          Assert.assertEquals(12, xMatrix.getEntry(0, 1), 1.0e-15);
85          Assert.assertEquals(0, xMatrix.getEntry(1, 0), 1.0e-15);
86          Assert.assertEquals(0, xMatrix.getEntry(1, 1), 1.0e-15);
87          RealVector xColVec = solver.solve(b.getColumnVector(0));
88          Assert.assertEquals(11, xColVec.getEntry(0), 1.0e-15);
89          Assert.assertEquals(0, xColVec.getEntry(1), 1.0e-15);
90          RealVector xColOtherVec = solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
91          Assert.assertEquals(11, xColOtherVec.getEntry(0), 1.0e-15);
92          Assert.assertEquals(0, xColOtherVec.getEntry(1), 1.0e-15);
93      }
94  
95      /** test solve */
96      @Test
97      public void testSolve() {
98          DecompositionSolver solver =
99              new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare)).getSolver();
100         Assert.assertEquals(testSquare.length, solver.getRowDimension());
101         Assert.assertEquals(testSquare[0].length, solver.getColumnDimension());
102         RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
103                 { 1, 2, 3 }, { 0, -5, 1 }
104         });
105         RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] {
106                 { -8.0 / 25.0, -263.0 / 75.0, -29.0 / 75.0 },
107                 { 19.0 / 25.0,   78.0 / 25.0,  49.0 / 25.0 }
108         });
109 
110         // using RealMatrix
111         Assert.assertEquals(0, solver.solve(b).subtract(xRef).getNorm1(), normTolerance);
112 
113         // using ArrayRealVector
114         for (int i = 0; i < b.getColumnDimension(); ++i) {
115             Assert.assertEquals(0,
116                          solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
117                          1.0e-13);
118         }
119 
120         // using RealVector with an alternate implementation
121         for (int i = 0; i < b.getColumnDimension(); ++i) {
122             ArrayRealVectorTest.RealVectorTestImpl v =
123                 new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i));
124             Assert.assertEquals(0,
125                          solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
126                          1.0e-13);
127         }
128 
129     }
130 
131     /** test condition number */
132     @Test
133     public void testConditionNumber() {
134         SingularValueDecomposition svd =
135             new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare));
136         // replace 1.0e-15 with 1.5e-15
137         Assert.assertEquals(3.0, svd.getConditionNumber(), 1.5e-15);
138     }
139 
140     @Test
141     public void testMath320B() {
142         RealMatrix rm = new Array2DRowRealMatrix(new double[][] {
143             { 1.0, 2.0 }, { 1.0, 2.0 }
144         });
145         SingularValueDecomposition svd =
146             new SingularValueDecomposition(rm);
147         RealMatrix recomposed = svd.getU().multiply(svd.getS()).multiply(svd.getVT());
148         Assert.assertEquals(0.0, recomposed.subtract(rm).getNorm1(), 2.0e-15);
149     }
150 
151     @Test
152     public void testSingular() {
153       SingularValueDecomposition svd =
154           new SingularValueDecomposition(MatrixUtils.createRealMatrix(bigSingular));
155       RealMatrix pseudoInverse = svd.getSolver().getInverse();
156       RealMatrix expected = new Array2DRowRealMatrix(new double[][] {
157           {-0.0355022687,0.0512742236,-0.0001045523,0.0157719549},
158           {-0.3214992438,0.3162419255,0.0000348508,-0.0052573183},
159           {0.5437098346,-0.4107754586,-0.0008256918,0.132934376},
160           {-0.0714905202,0.053808742,0.0006279816,-0.0176817782}
161       });
162       Assert.assertEquals(0, expected.subtract(pseudoInverse).getNorm1(), 1.0e-9);
163     }
164 
165 }