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.math.linear;
19  
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  import org.apache.commons.math.linear.DecompositionSolver;
25  import org.apache.commons.math.linear.InvalidMatrixException;
26  import org.apache.commons.math.linear.MatrixUtils;
27  import org.apache.commons.math.linear.RealMatrix;
28  import org.apache.commons.math.linear.ArrayRealVector;
29  import org.apache.commons.math.linear.SingularValueDecompositionImpl;
30  
31  public class SingularValueSolverTest extends TestCase {
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  
38      private static final double normTolerance = 10e-14;
39  
40      public SingularValueSolverTest(String name) {
41          super(name);
42      }
43  
44      public static Test suite() {
45          TestSuite suite = new TestSuite(SingularValueSolverTest.class);
46          suite.setName("SingularValueSolver Tests");
47          return suite;
48      }
49  
50      /** test solve dimension errors */
51      public void testSolveDimensionErrors() {
52          DecompositionSolver solver =
53              new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)).getSolver();
54          RealMatrix b = MatrixUtils.createRealMatrix(new double[3][2]);
55          try {
56              solver.solve(b);
57              fail("an exception should have been thrown");
58          } catch (IllegalArgumentException iae) {
59              // expected behavior
60          } catch (Exception e) {
61              fail("wrong exception caught");
62          }
63          try {
64              solver.solve(b.getColumn(0));
65              fail("an exception should have been thrown");
66          } catch (IllegalArgumentException iae) {
67              // expected behavior
68          } catch (Exception e) {
69              fail("wrong exception caught");
70          }
71          try {
72              solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
73              fail("an exception should have been thrown");
74          } catch (IllegalArgumentException iae) {
75              // expected behavior
76          } catch (Exception e) {
77              fail("wrong exception caught");
78          }
79      }
80  
81      /** test solve singularity errors */
82      public void testSolveSingularityErrors() {
83          RealMatrix m =
84              MatrixUtils.createRealMatrix(new double[][] {
85                                     { 1.0, 0.0 },
86                                     { 0.0, 0.0 }
87                                 });
88          DecompositionSolver solver = new SingularValueDecompositionImpl(m).getSolver();
89          RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
90          try {
91              solver.solve(b);
92              fail("an exception should have been thrown");
93          } catch (InvalidMatrixException ime) {
94              // expected behavior
95          } catch (Exception e) {
96              fail("wrong exception caught");
97          }
98          try {
99              solver.solve(b.getColumn(0));
100             fail("an exception should have been thrown");
101         } catch (InvalidMatrixException ime) {
102             // expected behavior
103         } catch (Exception e) {
104             fail("wrong exception caught");
105         }
106         try {
107             solver.solve(b.getColumnVector(0));
108             fail("an exception should have been thrown");
109         } catch (InvalidMatrixException ime) {
110             // expected behavior
111         } catch (Exception e) {
112             fail("wrong exception caught");
113         }
114         try {
115             solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
116             fail("an exception should have been thrown");
117         } catch (InvalidMatrixException ime) {
118             // expected behavior
119         } catch (Exception e) {
120             fail("wrong exception caught");
121         }
122     }
123 
124     /** test solve */
125     public void testSolve() {
126         DecompositionSolver solver =
127             new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)).getSolver();
128         RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
129                 { 1, 2, 3 }, { 0, -5, 1 }
130         });
131         RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] {
132                 { -8.0 / 25.0, -263.0 / 75.0, -29.0 / 75.0 },
133                 { 19.0 / 25.0,   78.0 / 25.0,  49.0 / 25.0 }
134         });
135 
136         // using RealMatrix
137         assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), normTolerance);
138 
139         // using double[]
140         for (int i = 0; i < b.getColumnDimension(); ++i) {
141             assertEquals(0,
142                          new ArrayRealVector(solver.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(),
143                          1.0e-13);
144         }
145 
146         // using Array2DRowRealMatrix
147         for (int i = 0; i < b.getColumnDimension(); ++i) {
148             assertEquals(0,
149                          solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
150                          1.0e-13);
151         }
152 
153         // using RealMatrix with an alternate implementation
154         for (int i = 0; i < b.getColumnDimension(); ++i) {
155             ArrayRealVectorTest.RealVectorTestImpl v =
156                 new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i));
157             assertEquals(0,
158                          solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
159                          1.0e-13);
160         }
161 
162     }
163 
164     /** test condition number */
165     public void testConditionNumber() {
166         SingularValueDecompositionImpl svd =
167             new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare));
168         assertEquals(3.0, svd.getConditionNumber(), 1.0e-15);
169     }
170 
171 }