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