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