~ubuntu-branches/ubuntu/quantal/commons-math/quantal

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/math/linear/SingularValueSolverTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2010-04-05 23:33:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100405233302-gpqlceked76nw28a
Tags: 2.1-1
* New upstream release.
* Bump Standards-Version to 3.8.4: no changes needed
* Bump debhelper to >= 7
* Switch to 3.0 (quilt) source format:
  - Remove B-D on quilt
  - Add d/source/format
  - Remove d/README.source

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
package org.apache.commons.math.linear;
19
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 {
 
20
import org.junit.Assert;
 
21
import org.junit.Test;
 
22
 
 
23
public class SingularValueSolverTest {
32
24
 
33
25
    private double[][] testSquare = {
34
26
            { 24.0 / 25.0, 43.0 / 25.0 },
37
29
 
38
30
    private static final double normTolerance = 10e-14;
39
31
 
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
32
    /** test solve dimension errors */
 
33
    @Test
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]);
55
38
        try {
56
39
            solver.solve(b);
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");
62
45
        }
63
46
        try {
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");
70
53
        }
71
54
        try {
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");
78
61
        }
79
62
    }
80
63
 
81
 
    /** test solve singularity errors */
82
 
    public void testSolveSingularityErrors() {
 
64
    /** test least square solve */
 
65
    @Test
 
66
    public void testLeastSquareSolve() {
83
67
        RealMatrix m =
84
68
            MatrixUtils.createRealMatrix(new double[][] {
85
69
                                   { 1.0, 0.0 },
86
70
                                   { 0.0, 0.0 }
87
71
                               });
88
72
        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
 
        }
 
73
        RealMatrix b = MatrixUtils.createRealMatrix(new double[][] {
 
74
            { 11, 12 }, { 21, 22 }
 
75
        });
 
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);
122
90
    }
123
91
 
124
92
    /** test solve */
 
93
    @Test
125
94
    public void testSolve() {
126
95
        DecompositionSolver solver =
127
96
            new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)).getSolver();
134
103
        });
135
104
 
136
105
        // using RealMatrix
137
 
        assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), normTolerance);
 
106
        Assert.assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), normTolerance);
138
107
 
139
108
        // using double[]
140
109
        for (int i = 0; i < b.getColumnDimension(); ++i) {
141
 
            assertEquals(0,
 
110
            Assert.assertEquals(0,
142
111
                         new ArrayRealVector(solver.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(),
143
112
                         1.0e-13);
144
113
        }
145
114
 
146
115
        // using Array2DRowRealMatrix
147
116
        for (int i = 0; i < b.getColumnDimension(); ++i) {
148
 
            assertEquals(0,
 
117
            Assert.assertEquals(0,
149
118
                         solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
150
119
                         1.0e-13);
151
120
        }
154
123
        for (int i = 0; i < b.getColumnDimension(); ++i) {
155
124
            ArrayRealVectorTest.RealVectorTestImpl v =
156
125
                new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i));
157
 
            assertEquals(0,
 
126
            Assert.assertEquals(0,
158
127
                         solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
159
128
                         1.0e-13);
160
129
        }
162
131
    }
163
132
 
164
133
    /** test condition number */
 
134
    @Test
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);
 
140
    }
 
141
 
 
142
    @Test
 
143
    public void testMath320B() {
 
144
        RealMatrix rm = new Array2DRowRealMatrix(new double[][] {
 
145
            { 1.0, 2.0 }, { 1.0, 2.0 }
 
146
        });
 
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);
169
151
    }
170
152
 
171
153
}