18
18
package org.apache.commons.math.linear;
20
import junit.framework.Test;
21
import junit.framework.TestCase;
22
import junit.framework.TestSuite;
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;
31
public class SingularValueSolverTest extends TestCase {
20
import org.junit.Assert;
21
import org.junit.Test;
23
public class SingularValueSolverTest {
33
25
private double[][] testSquare = {
34
26
{ 24.0 / 25.0, 43.0 / 25.0 },
38
30
private static final double normTolerance = 10e-14;
40
public SingularValueSolverTest(String name) {
44
public static Test suite() {
45
TestSuite suite = new TestSuite(SingularValueSolverTest.class);
46
suite.setName("SingularValueSolver Tests");
50
32
/** test solve dimension errors */
51
34
public void testSolveDimensionErrors() {
52
35
DecompositionSolver solver =
53
36
new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)).getSolver();
54
37
RealMatrix b = MatrixUtils.createRealMatrix(new double[3][2]);
57
fail("an exception should have been thrown");
40
Assert.fail("an exception should have been thrown");
58
41
} catch (IllegalArgumentException iae) {
59
42
// expected behavior
60
43
} catch (Exception e) {
61
fail("wrong exception caught");
44
Assert.fail("wrong exception caught");
64
47
solver.solve(b.getColumn(0));
65
fail("an exception should have been thrown");
48
Assert.fail("an exception should have been thrown");
66
49
} catch (IllegalArgumentException iae) {
67
50
// expected behavior
68
51
} catch (Exception e) {
69
fail("wrong exception caught");
52
Assert.fail("wrong exception caught");
72
55
solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
73
fail("an exception should have been thrown");
56
Assert.fail("an exception should have been thrown");
74
57
} catch (IllegalArgumentException iae) {
75
58
// expected behavior
76
59
} catch (Exception e) {
77
fail("wrong exception caught");
60
Assert.fail("wrong exception caught");
81
/** test solve singularity errors */
82
public void testSolveSingularityErrors() {
64
/** test least square solve */
66
public void testLeastSquareSolve() {
84
68
MatrixUtils.createRealMatrix(new double[][] {
88
72
DecompositionSolver solver = new SingularValueDecompositionImpl(m).getSolver();
89
RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
92
fail("an exception should have been thrown");
93
} catch (InvalidMatrixException ime) {
95
} catch (Exception e) {
96
fail("wrong exception caught");
99
solver.solve(b.getColumn(0));
100
fail("an exception should have been thrown");
101
} catch (InvalidMatrixException ime) {
103
} catch (Exception e) {
104
fail("wrong exception caught");
107
solver.solve(b.getColumnVector(0));
108
fail("an exception should have been thrown");
109
} catch (InvalidMatrixException ime) {
111
} catch (Exception e) {
112
fail("wrong exception caught");
115
solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
116
fail("an exception should have been thrown");
117
} catch (InvalidMatrixException ime) {
119
} catch (Exception e) {
120
fail("wrong exception caught");
73
RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
74
{ 11, 12 }, { 21, 22 }
76
RealMatrix xMatrix = solver.solve(b);
77
Assert.assertEquals(11, xMatrix.getEntry(0, 0), 1.0e-15);
78
Assert.assertEquals(12, xMatrix.getEntry(0, 1), 1.0e-15);
79
Assert.assertEquals(0, xMatrix.getEntry(1, 0), 1.0e-15);
80
Assert.assertEquals(0, xMatrix.getEntry(1, 1), 1.0e-15);
81
double[] xCol = solver.solve(b.getColumn(0));
82
Assert.assertEquals(11, xCol[0], 1.0e-15);
83
Assert.assertEquals(0, xCol[1], 1.0e-15);
84
RealVector xColVec = solver.solve(b.getColumnVector(0));
85
Assert.assertEquals(11, xColVec.getEntry(0), 1.0e-15);
86
Assert.assertEquals(0, xColVec.getEntry(1), 1.0e-15);
87
RealVector xColOtherVec = solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
88
Assert.assertEquals(11, xColOtherVec.getEntry(0), 1.0e-15);
89
Assert.assertEquals(0, xColOtherVec.getEntry(1), 1.0e-15);
125
94
public void testSolve() {
126
95
DecompositionSolver solver =
127
96
new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)).getSolver();
164
133
/** test condition number */
165
135
public void testConditionNumber() {
166
136
SingularValueDecompositionImpl svd =
167
137
new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare));
168
assertEquals(3.0, svd.getConditionNumber(), 1.0e-15);
138
// replace 1.0e-15 with 1.5e-15
139
Assert.assertEquals(3.0, svd.getConditionNumber(), 1.5e-15);
143
public void testMath320B() {
144
RealMatrix rm = new Array2DRowRealMatrix(new double[][] {
145
{ 1.0, 2.0 }, { 1.0, 2.0 }
147
SingularValueDecomposition svd =
148
new SingularValueDecompositionImpl(rm);
149
RealMatrix recomposed = svd.getU().multiply(svd.getS()).multiply(svd.getVT());
150
Assert.assertEquals(0.0, recomposed.subtract(rm).getNorm(), 2.0e-15);