1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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
76 } catch (Exception e) {
77 fail("wrong exception caught");
78 }
79 }
80
81
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
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
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
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
119 } catch (Exception e) {
120 fail("wrong exception caught");
121 }
122 }
123
124
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
137 assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), normTolerance);
138
139
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
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
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
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 }