You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2008/12/05 15:05:51 UTC
svn commit: r723736 [2/2] - in /commons/proper/math/trunk/src:
java/org/apache/commons/math/estimation/ java/org/apache/commons/math/linear/
java/org/apache/commons/math/stat/regression/ site/xdoc/userguide/
test/org/apache/commons/math/linear/
Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/EigenDecompositionImplTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/EigenDecompositionImplTest.java?rev=723736&r1=723735&r2=723736&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/EigenDecompositionImplTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/EigenDecompositionImplTest.java Fri Dec 5 06:05:50 2008
@@ -46,7 +46,7 @@
new RealMatrixImpl(new double[][] {
{ 1.5 }
}, false);
- EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose();
+ EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
assertEquals(1.5, ed.getEigenvalue(0), 1.0e-15);
}
@@ -56,7 +56,7 @@
{ 59.0, 12.0 },
{ Double.NaN, 66.0 }
}, false);
- EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose();
+ EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
assertEquals(75.0, ed.getEigenvalue(0), 1.0e-15);
assertEquals(50.0, ed.getEigenvalue(1), 1.0e-15);
}
@@ -68,7 +68,7 @@
{ Double.NaN, 8693.0, 7920.0 },
{ Double.NaN, Double.NaN, 17300.0 }
}, false);
- EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose();
+ EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
assertEquals(50000.0, ed.getEigenvalue(0), 3.0e-11);
assertEquals(12500.0, ed.getEigenvalue(1), 3.0e-11);
assertEquals( 3125.0, ed.getEigenvalue(2), 3.0e-11);
@@ -82,7 +82,7 @@
{ Double.NaN, Double.NaN, 0.164, -0.048 },
{ Double.NaN, Double.NaN, Double.NaN, 0.136 }
}, false);
- EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose();
+ EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
assertEquals(1.0, ed.getEigenvalue(0), 1.0e-15);
assertEquals(0.4, ed.getEigenvalue(1), 1.0e-15);
assertEquals(0.2, ed.getEigenvalue(2), 1.0e-15);
@@ -97,7 +97,7 @@
{ 0.1152, -0.2304, 0.3088, -0.1344 },
{ -0.2976, 0.1152, -0.1344, 0.3872 }
}, false);
- EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose();
+ EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
assertEquals(1.0, ed.getEigenvalue(0), 1.0e-15);
assertEquals(0.4, ed.getEigenvalue(1), 1.0e-15);
assertEquals(0.2, ed.getEigenvalue(2), 1.0e-15);
@@ -133,7 +133,7 @@
/** test dimensions */
public void testDimensions() {
final int m = matrix.getRowDimension();
- EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose();
+ EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
assertEquals(m, ed.getV().getRowDimension());
assertEquals(m, ed.getV().getColumnDimension());
assertEquals(m, ed.getD().getColumnDimension());
@@ -144,7 +144,7 @@
/** test eigenvalues */
public void testEigenvalues() {
- EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose();
+ EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
double[] eigenValues = ed.getEigenvalues();
assertEquals(refValues.length, eigenValues.length);
for (int i = 0; i < refValues.length; ++i) {
@@ -161,7 +161,7 @@
}
Arrays.sort(bigValues);
EigenDecomposition ed =
- new DecompositionSolver(createTestMatrix(r, bigValues)).eigenDecompose();
+ new EigenDecompositionImpl(createTestMatrix(r, bigValues), MathUtils.SAFE_MIN);
double[] eigenValues = ed.getEigenvalues();
assertEquals(bigValues.length, eigenValues.length);
for (int i = 0; i < bigValues.length; ++i) {
@@ -171,7 +171,7 @@
/** test eigenvectors */
public void testEigenvectors() {
- EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose();
+ EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
for (int i = 0; i < matrix.getRowDimension(); ++i) {
double lambda = ed.getEigenvalue(i);
RealVector v = ed.getEigenvector(i);
@@ -182,7 +182,7 @@
/** test A = VDVt */
public void testAEqualVDVt() {
- EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose();
+ EigenDecomposition ed = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN);
RealMatrix v = ed.getV();
RealMatrix d = ed.getD();
RealMatrix vT = ed.getVT();
@@ -192,143 +192,23 @@
/** test that V is orthogonal */
public void testVOrthogonal() {
- RealMatrix v = new DecompositionSolver(matrix).eigenDecompose().getV();
+ RealMatrix v = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN).getV();
RealMatrix vTv = v.transpose().multiply(v);
RealMatrix id = MatrixUtils.createRealIdentityMatrix(vTv.getRowDimension());
assertEquals(0, vTv.subtract(id).getNorm(), 2.0e-13);
}
- /** test non invertible matrix */
- public void testNonInvertible() {
- Random r = new Random(9994100315209l);
- DecompositionSolver ds =
- new DecompositionSolver(createTestMatrix(r, new double[] { 1.0, 0.0, -1.0, -2.0, -3.0 }));
- EigenDecomposition ed = ds.eigenDecompose();
- assertFalse(ds.isNonSingular(ed));
- try {
- ds.getInverse(ed);
- fail("an exception should have been thrown");
- } catch (InvalidMatrixException ime) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- }
-
- /** test invertible matrix */
- public void testInvertible() {
- Random r = new Random(9994100315209l);
- RealMatrix m =
- createTestMatrix(r, new double[] { 1.0, 0.5, -1.0, -2.0, -3.0 });
- DecompositionSolver ds = new DecompositionSolver(m);
- EigenDecomposition ed = ds.eigenDecompose();
- assertTrue(ds.isNonSingular(ed));
- RealMatrix inverse = ds.getInverse(ed);
- RealMatrix error =
- m.multiply(inverse).subtract(MatrixUtils.createRealIdentityMatrix(m.getRowDimension()));
- assertEquals(0, error.getNorm(), 4.0e-15);
- }
-
/** test diagonal matrix */
public void testDiagonal() {
double[] diagonal = new double[] { -3.0, -2.0, 2.0, 5.0 };
RealMatrix m = createDiagonalMatrix(diagonal, diagonal.length, diagonal.length);
- EigenDecomposition ed = new DecompositionSolver(m).eigenDecompose();
+ EigenDecomposition ed = new EigenDecompositionImpl(m, MathUtils.SAFE_MIN);
assertEquals(diagonal[0], ed.getEigenvalue(3), 2.0e-15);
assertEquals(diagonal[1], ed.getEigenvalue(2), 2.0e-15);
assertEquals(diagonal[2], ed.getEigenvalue(1), 2.0e-15);
assertEquals(diagonal[3], ed.getEigenvalue(0), 2.0e-15);
}
- /** test solve dimension errors */
- public void testSolveDimensionErrors() {
- DecompositionSolver ds = new DecompositionSolver(matrix);
- EigenDecomposition ed = ds.eigenDecompose();
- RealMatrix b = new RealMatrixImpl(new double[2][2]);
- try {
- ds.solve(b, ed);
- fail("an exception should have been thrown");
- } catch (IllegalArgumentException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- ds.solve(b.getColumn(0), ed);
- fail("an exception should have been thrown");
- } catch (IllegalArgumentException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- ds.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)), ed);
- fail("an exception should have been thrown");
- } catch (IllegalArgumentException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- }
-
- /** test solve */
- public void testSolve() {
- RealMatrix m = new RealMatrixImpl(new double[][] {
- { 91, 5, 29, 32, 40, 14 },
- { 5, 34, -1, 0, 2, -1 },
- { 29, -1, 12, 9, 21, 8 },
- { 32, 0, 9, 14, 9, 0 },
- { 40, 2, 21, 9, 51, 19 },
- { 14, -1, 8, 0, 19, 14 }
- });
- DecompositionSolver ds = new DecompositionSolver(m);
- EigenDecomposition ed = ds.eigenDecompose();
- assertEquals(184041, ds.getDeterminant(ed), 2.0e-8);
- RealMatrix b = new RealMatrixImpl(new double[][] {
- { 1561, 269, 188 },
- { 69, -21, 70 },
- { 739, 108, 63 },
- { 324, 86, 59 },
- { 1624, 194, 107 },
- { 796, 69, 36 }
- });
- RealMatrix xRef = new RealMatrixImpl(new double[][] {
- { 1, 2, 1 },
- { 2, -1, 2 },
- { 4, 2, 3 },
- { 8, -1, 0 },
- { 16, 2, 0 },
- { 32, -1, 0 }
- });
-
- // using RealMatrix
- assertEquals(0, ds.solve(b, ed).subtract(xRef).getNorm(), 2.0e-12);
-
- // using double[]
- for (int i = 0; i < b.getColumnDimension(); ++i) {
- assertEquals(0,
- new RealVectorImpl(ds.solve(b.getColumn(i), ed)).subtract(xRef.getColumnVector(i)).getNorm(),
- 2.0e-11);
- }
-
- // using RealMatrixImpl
- for (int i = 0; i < b.getColumnDimension(); ++i) {
- assertEquals(0,
- ds.solve(b.getColumnVector(i), ed).subtract(xRef.getColumnVector(i)).getNorm(),
- 2.0e-11);
- }
-
- // using RealMatrix with an alternate implementation
- for (int i = 0; i < b.getColumnDimension(); ++i) {
- RealVectorImplTest.RealVectorTestImpl v =
- new RealVectorImplTest.RealVectorTestImpl(b.getColumn(i));
- assertEquals(0,
- ds.solve(v, ed).subtract(xRef.getColumnVector(i)).getNorm(),
- 2.0e-11);
- }
-
- }
-
/**
* Matrix with eigenvalues {8, -1, -1}
*/
@@ -338,7 +218,7 @@
{2, 0, 2},
{4, 2, 3}
});
- EigenDecomposition ed = new DecompositionSolver(repeated).eigenDecompose();
+ EigenDecomposition ed = new EigenDecompositionImpl(repeated, MathUtils.SAFE_MIN);
checkEigenValues((new double[] {8, -1, -1}), ed, 1E-12);
checkEigenVector((new double[] {2, 1, 2}), ed, 1E-12);
}
@@ -352,7 +232,7 @@
{1, 3, -4},
{-4, -4, 8}
});
- EigenDecomposition ed = new DecompositionSolver(distinct).eigenDecompose();
+ EigenDecomposition ed = new EigenDecompositionImpl(distinct, MathUtils.SAFE_MIN);
checkEigenValues((new double[] {2, 0, 12}), ed, 1E-12);
checkEigenVector((new double[] {1, -1, 0}), ed, 1E-12);
checkEigenVector((new double[] {1, 1, 1}), ed, 1E-12);
@@ -442,7 +322,7 @@
matrix = null;
}
- private RealMatrix createTestMatrix(final Random r, final double[] eigenValues) {
+ static RealMatrix createTestMatrix(final Random r, final double[] eigenValues) {
final int n = eigenValues.length;
final RealMatrix v = createOrthogonalMatrix(r, n);
final RealMatrix d = createDiagonalMatrix(eigenValues, n, n);
Added: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/EigenSolverTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/EigenSolverTest.java?rev=723736&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/EigenSolverTest.java (added)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/EigenSolverTest.java Fri Dec 5 06:05:50 2008
@@ -0,0 +1,172 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.linear;
+
+import java.util.Random;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.math.util.MathUtils;
+
+public class EigenSolverTest extends TestCase {
+
+ private double[] refValues;
+ private RealMatrix matrix;
+
+ public EigenSolverTest(String name) {
+ super(name);
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(EigenSolverTest.class);
+ suite.setName("EigenSolver Tests");
+ return suite;
+ }
+
+ /** test non invertible matrix */
+ public void testNonInvertible() {
+ Random r = new Random(9994100315209l);
+ RealMatrix m =
+ EigenDecompositionImplTest.createTestMatrix(r, new double[] { 1.0, 0.0, -1.0, -2.0, -3.0 });
+ EigenSolver es = new EigenSolver(new EigenDecompositionImpl(m, MathUtils.SAFE_MIN));
+ assertFalse(es.isNonSingular());
+ try {
+ es.getInverse();
+ fail("an exception should have been thrown");
+ } catch (InvalidMatrixException ime) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ }
+
+ /** test invertible matrix */
+ public void testInvertible() {
+ Random r = new Random(9994100315209l);
+ RealMatrix m =
+ EigenDecompositionImplTest.createTestMatrix(r, new double[] { 1.0, 0.5, -1.0, -2.0, -3.0 });
+ EigenSolver es = new EigenSolver(new EigenDecompositionImpl(m, MathUtils.SAFE_MIN));
+ assertTrue(es.isNonSingular());
+ RealMatrix inverse = es.getInverse();
+ RealMatrix error =
+ m.multiply(inverse).subtract(MatrixUtils.createRealIdentityMatrix(m.getRowDimension()));
+ assertEquals(0, error.getNorm(), 4.0e-15);
+ }
+
+ /** test solve dimension errors */
+ public void testSolveDimensionErrors() {
+ EigenSolver es = new EigenSolver(new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN));
+ RealMatrix b = new RealMatrixImpl(new double[2][2]);
+ try {
+ es.solve(b);
+ fail("an exception should have been thrown");
+ } catch (IllegalArgumentException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ es.solve(b.getColumn(0));
+ fail("an exception should have been thrown");
+ } catch (IllegalArgumentException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ es.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)));
+ fail("an exception should have been thrown");
+ } catch (IllegalArgumentException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ }
+
+ /** test solve */
+ public void testSolve() {
+ RealMatrix m = new RealMatrixImpl(new double[][] {
+ { 91, 5, 29, 32, 40, 14 },
+ { 5, 34, -1, 0, 2, -1 },
+ { 29, -1, 12, 9, 21, 8 },
+ { 32, 0, 9, 14, 9, 0 },
+ { 40, 2, 21, 9, 51, 19 },
+ { 14, -1, 8, 0, 19, 14 }
+ });
+ EigenSolver es = new EigenSolver(new EigenDecompositionImpl(m, MathUtils.SAFE_MIN));
+ assertEquals(184041, es.getDeterminant(), 2.0e-8);
+ RealMatrix b = new RealMatrixImpl(new double[][] {
+ { 1561, 269, 188 },
+ { 69, -21, 70 },
+ { 739, 108, 63 },
+ { 324, 86, 59 },
+ { 1624, 194, 107 },
+ { 796, 69, 36 }
+ });
+ RealMatrix xRef = new RealMatrixImpl(new double[][] {
+ { 1, 2, 1 },
+ { 2, -1, 2 },
+ { 4, 2, 3 },
+ { 8, -1, 0 },
+ { 16, 2, 0 },
+ { 32, -1, 0 }
+ });
+
+ // using RealMatrix
+ assertEquals(0, es.solve(b).subtract(xRef).getNorm(), 2.0e-12);
+
+ // using double[]
+ for (int i = 0; i < b.getColumnDimension(); ++i) {
+ assertEquals(0,
+ new RealVectorImpl(es.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(),
+ 2.0e-11);
+ }
+
+ // using RealMatrixImpl
+ for (int i = 0; i < b.getColumnDimension(); ++i) {
+ assertEquals(0,
+ es.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
+ 2.0e-11);
+ }
+
+ // using RealMatrix with an alternate implementation
+ for (int i = 0; i < b.getColumnDimension(); ++i) {
+ RealVectorImplTest.RealVectorTestImpl v =
+ new RealVectorImplTest.RealVectorTestImpl(b.getColumn(i));
+ assertEquals(0,
+ es.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
+ 2.0e-11);
+ }
+
+ }
+
+ public void setUp() {
+ refValues = new double[] {
+ 2.003, 2.002, 2.001, 1.001, 1.000, 0.001
+ };
+ matrix = EigenDecompositionImplTest.createTestMatrix(new Random(35992629946426l), refValues);
+ }
+
+ public void tearDown() {
+ refValues = null;
+ matrix = null;
+ }
+
+}
Propchange: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/EigenSolverTest.java
------------------------------------------------------------------------------
svn:mergeinfo =
Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/LUDecompositionImplTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/LUDecompositionImplTest.java?rev=723736&r1=723735&r2=723736&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/LUDecompositionImplTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/LUDecompositionImplTest.java Fri Dec 5 06:05:50 2008
@@ -88,18 +88,6 @@
}
}
- /** test threshold impact */
- public void testThreshold() {
- final RealMatrix matrix = new RealMatrixImpl(new double[][] {
- { 1.0, 2.0, 3.0},
- { 2.0, 5.0, 3.0},
- { 4.000001, 9.0, 9.0}
- }, false);
- DecompositionSolver solver = new DecompositionSolver(matrix);
- assertFalse(solver.isNonSingular(solver.luDecompose(1.0e-5)));
- assertTrue(solver.isNonSingular(solver.luDecompose(1.0e-10)));
- }
-
/** test PA = LU */
public void testPAEqualLU() {
RealMatrix matrix = new RealMatrixImpl(testData, false);
@@ -226,118 +214,6 @@
assertTrue(lu.isSingular());
}
- /** test solve dimension errors */
- public void testSolveDimensionErrors() {
- DecompositionSolver solver =
- new DecompositionSolver(new RealMatrixImpl(testData, false));
- RealMatrix b = new RealMatrixImpl(new double[2][2]);
- try {
- solver.solve(b, solver.luDecompose());
- fail("an exception should have been thrown");
- } catch (IllegalArgumentException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- solver.solve(b.getColumn(0), solver.luDecompose());
- fail("an exception should have been thrown");
- } catch (IllegalArgumentException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- solver.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)),
- solver.luDecompose());
- fail("an exception should have been thrown");
- } catch (IllegalArgumentException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- }
-
- /** test solve singularity errors */
- public void testSolveSingularityErrors() {
- DecompositionSolver solver =
- new DecompositionSolver(new RealMatrixImpl(singular, false));
- RealMatrix b = new RealMatrixImpl(new double[2][2]);
- try {
- solver.solve(b, solver.luDecompose());
- fail("an exception should have been thrown");
- } catch (InvalidMatrixException ime) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- solver.solve(b.getColumn(0), solver.luDecompose());
- fail("an exception should have been thrown");
- } catch (InvalidMatrixException ime) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- solver.solve(b.getColumnVector(0), solver.luDecompose());
- fail("an exception should have been thrown");
- } catch (InvalidMatrixException ime) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- solver.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)),
- solver.luDecompose());
- fail("an exception should have been thrown");
- } catch (InvalidMatrixException ime) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- }
-
- /** test solve */
- public void testSolve() {
- DecompositionSolver solver =
- new DecompositionSolver(new RealMatrixImpl(testData, false));
- LUDecomposition lu = solver.luDecompose();
- RealMatrix b = new RealMatrixImpl(new double[][] {
- { 1, 0 }, { 2, -5 }, { 3, 1 }
- });
- RealMatrix xRef = new RealMatrixImpl(new double[][] {
- { 19, -71 }, { -6, 22 }, { -2, 9 }
- });
-
- // using RealMatrix
- assertEquals(0, solver.solve(b, lu).subtract(xRef).getNorm(), 1.0e-13);
-
- // using double[]
- for (int i = 0; i < b.getColumnDimension(); ++i) {
- assertEquals(0,
- new RealVectorImpl(solver.solve(b.getColumn(i), lu)).subtract(xRef.getColumnVector(i)).getNorm(),
- 1.0e-13);
- }
-
- // using RealVectorImpl
- for (int i = 0; i < b.getColumnDimension(); ++i) {
- assertEquals(0,
- solver.solve(b.getColumnVector(i), lu).subtract(xRef.getColumnVector(i)).getNorm(),
- 1.0e-13);
- }
-
- // using RealVector with an alternate implementation
- for (int i = 0; i < b.getColumnDimension(); ++i) {
- RealVectorImplTest.RealVectorTestImpl v =
- new RealVectorImplTest.RealVectorTestImpl(b.getColumn(i));
- assertEquals(0,
- solver.solve(v, lu).subtract(xRef.getColumnVector(i)).getNorm(),
- 1.0e-13);
- }
-
- }
-
/** test matrices values */
public void testMatricesValues1() {
LUDecomposition lu =
@@ -418,17 +294,4 @@
}
- /** test determinant */
- public void testDeterminant() {
- assertEquals( -1, getDeterminant(new RealMatrixImpl(testData, false)), 1.0e-15);
- assertEquals(-10, getDeterminant(new RealMatrixImpl(luData, false)), 1.0e-14);
- assertEquals( 0, getDeterminant(new RealMatrixImpl(singular, false)), 1.0e-17);
- assertEquals( 0, getDeterminant(new RealMatrixImpl(bigSingular, false)), 1.0e-10);
- }
-
- private double getDeterminant(RealMatrix m) {
- DecompositionSolver ds = new DecompositionSolver(m);
- return ds.getDeterminant(ds.luDecompose());
- }
-
}
Added: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/LUSolverTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/LUSolverTest.java?rev=723736&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/LUSolverTest.java (added)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/LUSolverTest.java Fri Dec 5 06:05:50 2008
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.linear;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class LUSolverTest extends TestCase {
+ private double[][] testData = {
+ { 1.0, 2.0, 3.0},
+ { 2.0, 5.0, 3.0},
+ { 1.0, 0.0, 8.0}
+ };
+ private double[][] luData = {
+ { 2.0, 3.0, 3.0 },
+ { 0.0, 5.0, 7.0 },
+ { 6.0, 9.0, 8.0 }
+ };
+
+ // singular matrices
+ private double[][] singular = {
+ { 2.0, 3.0 },
+ { 2.0, 3.0 }
+ };
+ private double[][] bigSingular = {
+ { 1.0, 2.0, 3.0, 4.0 },
+ { 2.0, 5.0, 3.0, 4.0 },
+ { 7.0, 3.0, 256.0, 1930.0 },
+ { 3.0, 7.0, 6.0, 8.0 }
+ }; // 4th row = 1st + 2nd
+
+ public LUSolverTest(String name) {
+ super(name);
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(LUSolverTest.class);
+ suite.setName("LUSolver Tests");
+ return suite;
+ }
+
+ /** test threshold impact */
+ public void testThreshold() {
+ final RealMatrix matrix = new RealMatrixImpl(new double[][] {
+ { 1.0, 2.0, 3.0},
+ { 2.0, 5.0, 3.0},
+ { 4.000001, 9.0, 9.0}
+ }, false);
+ assertFalse(new LUSolver(new LUDecompositionImpl(matrix, 1.0e-5)).isNonSingular());
+ assertTrue(new LUSolver(new LUDecompositionImpl(matrix, 1.0e-10)).isNonSingular());
+ }
+
+ /** test singular */
+ public void testSingular() {
+ LUSolver lu =
+ new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(testData, false)));
+ assertTrue(lu.isNonSingular());
+ lu = new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(singular, false)));
+ assertFalse(lu.isNonSingular());
+ lu = new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(bigSingular, false)));
+ assertFalse(lu.isNonSingular());
+ }
+
+ /** test solve dimension errors */
+ public void testSolveDimensionErrors() {
+ LUSolver solver =
+ new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(testData, false)));
+ RealMatrix b = new RealMatrixImpl(new double[2][2]);
+ try {
+ solver.solve(b);
+ fail("an exception should have been thrown");
+ } catch (IllegalArgumentException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(b.getColumn(0));
+ fail("an exception should have been thrown");
+ } catch (IllegalArgumentException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)));
+ fail("an exception should have been thrown");
+ } catch (IllegalArgumentException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ }
+
+ /** test solve singularity errors */
+ public void testSolveSingularityErrors() {
+ LUSolver solver =
+ new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(singular, false)));
+ RealMatrix b = new RealMatrixImpl(new double[2][2]);
+ try {
+ solver.solve(b);
+ fail("an exception should have been thrown");
+ } catch (InvalidMatrixException ime) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(b.getColumn(0));
+ fail("an exception should have been thrown");
+ } catch (InvalidMatrixException ime) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(b.getColumnVector(0));
+ fail("an exception should have been thrown");
+ } catch (InvalidMatrixException ime) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)));
+ fail("an exception should have been thrown");
+ } catch (InvalidMatrixException ime) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ }
+
+ /** test solve */
+ public void testSolve() {
+ LUSolver solver =
+ new LUSolver(new LUDecompositionImpl(new RealMatrixImpl(testData, false)));
+ RealMatrix b = new RealMatrixImpl(new double[][] {
+ { 1, 0 }, { 2, -5 }, { 3, 1 }
+ });
+ RealMatrix xRef = new RealMatrixImpl(new double[][] {
+ { 19, -71 }, { -6, 22 }, { -2, 9 }
+ });
+
+ // using RealMatrix
+ assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), 1.0e-13);
+
+ // using double[]
+ for (int i = 0; i < b.getColumnDimension(); ++i) {
+ assertEquals(0,
+ new RealVectorImpl(solver.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(),
+ 1.0e-13);
+ }
+
+ // using RealVectorImpl
+ for (int i = 0; i < b.getColumnDimension(); ++i) {
+ assertEquals(0,
+ solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
+ 1.0e-13);
+ }
+
+ // using RealVector with an alternate implementation
+ for (int i = 0; i < b.getColumnDimension(); ++i) {
+ RealVectorImplTest.RealVectorTestImpl v =
+ new RealVectorImplTest.RealVectorTestImpl(b.getColumn(i));
+ assertEquals(0,
+ solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
+ 1.0e-13);
+ }
+
+ }
+
+ /** test determinant */
+ public void testDeterminant() {
+ assertEquals( -1, getDeterminant(new RealMatrixImpl(testData, false)), 1.0e-15);
+ assertEquals(-10, getDeterminant(new RealMatrixImpl(luData, false)), 1.0e-14);
+ assertEquals( 0, getDeterminant(new RealMatrixImpl(singular, false)), 1.0e-17);
+ assertEquals( 0, getDeterminant(new RealMatrixImpl(bigSingular, false)), 1.0e-10);
+ }
+
+ private double getDeterminant(RealMatrix m) {
+ return new LUSolver(new LUDecompositionImpl(m)).getDeterminant();
+ }
+
+}
Propchange: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/LUSolverTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/LUSolverTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/QRDecompositionImplTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/QRDecompositionImplTest.java?rev=723736&r1=723735&r2=723736&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/QRDecompositionImplTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/QRDecompositionImplTest.java Fri Dec 5 06:05:50 2008
@@ -196,130 +196,10 @@
}
- /** test rank */
- public void testRank() {
- DecompositionSolver ds =
- new DecompositionSolver(new RealMatrixImpl(testData3x3NonSingular, false));
- assertTrue(ds.isNonSingular(ds.qrDecompose()));
-
- ds = new DecompositionSolver(new RealMatrixImpl(testData3x3Singular, false));
- assertFalse(ds.isNonSingular(ds.qrDecompose()));
-
- ds = new DecompositionSolver(new RealMatrixImpl(testData3x4, false));
- assertTrue(ds.isNonSingular(ds.qrDecompose()));
-
- ds = new DecompositionSolver(new RealMatrixImpl(testData4x3, false));
- assertTrue(ds.isNonSingular(ds.qrDecompose()));
-
- }
-
- /** test solve dimension errors */
- public void testSolveDimensionErrors() {
- DecompositionSolver solver =
- new DecompositionSolver(new RealMatrixImpl(testData3x3NonSingular, false));
- RealMatrix b = new RealMatrixImpl(new double[2][2]);
- try {
- solver.solve(b, solver.qrDecompose());
- fail("an exception should have been thrown");
- } catch (IllegalArgumentException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- solver.solve(b.getColumn(0), solver.qrDecompose());
- fail("an exception should have been thrown");
- } catch (IllegalArgumentException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- solver.solve(b.getColumnVector(0), solver.qrDecompose());
- fail("an exception should have been thrown");
- } catch (IllegalArgumentException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- }
-
- /** test solve rank errors */
- public void testSolveRankErrors() {
- DecompositionSolver solver =
- new DecompositionSolver(new RealMatrixImpl(testData3x3Singular, false));
- RealMatrix b = new RealMatrixImpl(new double[3][2]);
- try {
- solver.solve(b, solver.qrDecompose());
- fail("an exception should have been thrown");
- } catch (InvalidMatrixException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- solver.solve(b.getColumn(0), solver.qrDecompose());
- fail("an exception should have been thrown");
- } catch (InvalidMatrixException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- solver.solve(b.getColumnVector(0), solver.qrDecompose());
- fail("an exception should have been thrown");
- } catch (InvalidMatrixException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- }
-
- /** test solve */
- public void testSolve() {
- DecompositionSolver ds =
- new DecompositionSolver(new RealMatrixImpl(testData3x3NonSingular, false));
- QRDecomposition qr = ds.qrDecompose();
- RealMatrix b = new RealMatrixImpl(new double[][] {
- { -102, 12250 }, { 544, 24500 }, { 167, -36750 }
- });
- RealMatrix xRef = new RealMatrixImpl(new double[][] {
- { 1, 2515 }, { 2, 422 }, { -3, 898 }
- });
-
- // using RealMatrix
- assertEquals(0, ds.solve(b, qr).subtract(xRef).getNorm(), 1.0e-13);
-
- // using double[]
- for (int i = 0; i < b.getColumnDimension(); ++i) {
- assertEquals(0,
- new RealVectorImpl(ds.solve(b.getColumn(i), qr)).subtract(xRef.getColumnVector(i)).getNorm(),
- 1.0e-13);
- }
-
- // using RealVectorImpl
- for (int i = 0; i < b.getColumnDimension(); ++i) {
- assertEquals(0,
- ds.solve(b.getColumnVector(i), qr).subtract(xRef.getColumnVector(i)).getNorm(),
- 1.0e-13);
- }
-
- // using RealVector with an alternate implementation
- for (int i = 0; i < b.getColumnDimension(); ++i) {
- RealVectorImplTest.RealVectorTestImpl v =
- new RealVectorImplTest.RealVectorTestImpl(b.getColumn(i));
- assertEquals(0,
- ds.solve(v, qr).subtract(xRef.getColumnVector(i)).getNorm(),
- 1.0e-13);
- }
-
- }
-
/** test matrices values */
public void testMatricesValues() {
- DecompositionSolver ds =
- new DecompositionSolver(new RealMatrixImpl(testData3x3NonSingular, false));
- QRDecomposition qr = ds.qrDecompose();
+ QRDecomposition qr =
+ new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false));
RealMatrix qRef = new RealMatrixImpl(new double[][] {
{ -12.0 / 14.0, 69.0 / 175.0, -58.0 / 175.0 },
{ -6.0 / 14.0, -158.0 / 175.0, 6.0 / 175.0 },
Added: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/QRSolverTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/QRSolverTest.java?rev=723736&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/QRSolverTest.java (added)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/QRSolverTest.java Fri Dec 5 06:05:50 2008
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.linear;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class QRSolverTest extends TestCase {
+ double[][] testData3x3NonSingular = {
+ { 12, -51, 4 },
+ { 6, 167, -68 },
+ { -4, 24, -41 }, };
+
+ double[][] testData3x3Singular = {
+ { 1, 4, 7, },
+ { 2, 5, 8, },
+ { 3, 6, 9, }, };
+
+ double[][] testData3x4 = {
+ { 12, -51, 4, 1 },
+ { 6, 167, -68, 2 },
+ { -4, 24, -41, 3 }, };
+
+ double[][] testData4x3 = {
+ { 12, -51, 4, },
+ { 6, 167, -68, },
+ { -4, 24, -41, },
+ { -5, 34, 7, }, };
+
+ public QRSolverTest(String name) {
+ super(name);
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(QRSolverTest.class);
+ suite.setName("QRSolver Tests");
+ return suite;
+ }
+
+ /** test rank */
+ public void testRank() {
+ QRSolver solver =
+ new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false)));
+ assertTrue(solver.isNonSingular());
+
+ solver = new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3Singular, false)));
+ assertFalse(solver.isNonSingular());
+
+ solver = new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x4, false)));
+ assertTrue(solver.isNonSingular());
+
+ solver = new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData4x3, false)));
+ assertTrue(solver.isNonSingular());
+
+ }
+
+ /** test solve dimension errors */
+ public void testSolveDimensionErrors() {
+ QRSolver solver =
+ new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false)));
+ RealMatrix b = new RealMatrixImpl(new double[2][2]);
+ try {
+ solver.solve(b);
+ fail("an exception should have been thrown");
+ } catch (IllegalArgumentException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(b.getColumn(0));
+ fail("an exception should have been thrown");
+ } catch (IllegalArgumentException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(b.getColumnVector(0));
+ fail("an exception should have been thrown");
+ } catch (IllegalArgumentException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ }
+
+ /** test solve rank errors */
+ public void testSolveRankErrors() {
+ QRSolver solver =
+ new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3Singular, false)));
+ RealMatrix b = new RealMatrixImpl(new double[3][2]);
+ try {
+ solver.solve(b);
+ fail("an exception should have been thrown");
+ } catch (InvalidMatrixException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(b.getColumn(0));
+ fail("an exception should have been thrown");
+ } catch (InvalidMatrixException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(b.getColumnVector(0));
+ fail("an exception should have been thrown");
+ } catch (InvalidMatrixException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ }
+
+ /** test solve */
+ public void testSolve() {
+ QRSolver solver =
+ new QRSolver(new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false)));
+ RealMatrix b = new RealMatrixImpl(new double[][] {
+ { -102, 12250 }, { 544, 24500 }, { 167, -36750 }
+ });
+ RealMatrix xRef = new RealMatrixImpl(new double[][] {
+ { 1, 2515 }, { 2, 422 }, { -3, 898 }
+ });
+
+ // using RealMatrix
+ assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), 1.0e-13);
+
+ // using double[]
+ for (int i = 0; i < b.getColumnDimension(); ++i) {
+ assertEquals(0,
+ new RealVectorImpl(solver.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(),
+ 1.0e-13);
+ }
+
+ // using RealVectorImpl
+ for (int i = 0; i < b.getColumnDimension(); ++i) {
+ assertEquals(0,
+ solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
+ 1.0e-13);
+ }
+
+ // using RealVector with an alternate implementation
+ for (int i = 0; i < b.getColumnDimension(); ++i) {
+ RealVectorImplTest.RealVectorTestImpl v =
+ new RealVectorImplTest.RealVectorTestImpl(b.getColumn(i));
+ assertEquals(0,
+ solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
+ 1.0e-13);
+ }
+
+ }
+
+}
Propchange: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/QRSolverTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/QRSolverTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java?rev=723736&r1=723735&r2=723736&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/RealMatrixImplTest.java Fri Dec 5 06:05:50 2008
@@ -261,10 +261,8 @@
/** test transpose */
public void testTranspose() {
RealMatrix m = new RealMatrixImpl(testData);
- DecompositionSolver ds1 = new DecompositionSolver(m);
- RealMatrix mIT = ds1.getInverse(ds1.luDecompose()).transpose();
- DecompositionSolver ds2 = new DecompositionSolver(m.transpose());
- RealMatrix mTI = ds2.getInverse(ds2.luDecompose());
+ RealMatrix mIT = new LUSolver(new LUDecompositionImpl(m)).getInverse().transpose();
+ RealMatrix mTI = new LUSolver(new LUDecompositionImpl(m.transpose())).getInverse();
assertClose("inverse-transpose", mIT, mTI, normTolerance);
m = new RealMatrixImpl(testData2);
RealMatrix mt = new RealMatrixImpl(testData2T);
@@ -354,8 +352,7 @@
assertEquals(2, p.getRowDimension());
assertEquals(2, p.getColumnDimension());
// Invert p
- DecompositionSolver ds1 = new DecompositionSolver(p);
- RealMatrix pInverse = ds1.getInverse(ds1.luDecompose());
+ RealMatrix pInverse = new LUSolver(new LUDecompositionImpl(p)).getInverse();
assertEquals(2, pInverse.getRowDimension());
assertEquals(2, pInverse.getColumnDimension());
@@ -363,8 +360,7 @@
double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}};
RealMatrix coefficients = new RealMatrixImpl(coefficientsData);
double[] constants = {1, -2, 1};
- DecompositionSolver ds2 = new DecompositionSolver(coefficients);
- double[] solution = ds2.solve(constants, ds2.luDecompose());
+ double[] solution = new LUSolver(new LUDecompositionImpl(coefficients)).solve(constants);
assertEquals(2 * solution[0] + 3 * solution[1] -2 * solution[2], constants[0], 1E-12);
assertEquals(-1 * solution[0] + 7 * solution[1] + 6 * solution[2], constants[1], 1E-12);
assertEquals(4 * solution[0] - 3 * solution[1] -5 * solution[2], constants[2], 1E-12);
Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SingularValueDecompositionImplTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SingularValueDecompositionImplTest.java?rev=723736&r1=723735&r2=723736&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SingularValueDecompositionImplTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SingularValueDecompositionImplTest.java Fri Dec 5 06:05:50 2008
@@ -129,122 +129,6 @@
assertEquals(0, mTm.subtract(id).getNorm(), normTolerance);
}
- /** test solve dimension errors */
- public void testSolveDimensionErrors() {
- DecompositionSolver ds =
- new DecompositionSolver(new RealMatrixImpl(testSquare, false));
- SingularValueDecomposition svd = ds.singularDecompose();
- RealMatrix b = new RealMatrixImpl(new double[3][2]);
- try {
- ds.solve(b, svd);
- fail("an exception should have been thrown");
- } catch (IllegalArgumentException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- ds.solve(b.getColumn(0), svd);
- fail("an exception should have been thrown");
- } catch (IllegalArgumentException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- ds.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)), svd);
- fail("an exception should have been thrown");
- } catch (IllegalArgumentException iae) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- }
-
- /** test solve singularity errors */
- public void testSolveSingularityErrors() {
- DecompositionSolver ds =
- new DecompositionSolver(new RealMatrixImpl(new double[][] {
- { 1.0, 0.0 },
- { 0.0, 0.0 }
- }, false));
- SingularValueDecomposition svd = ds.singularDecompose();
- RealMatrix b = new RealMatrixImpl(new double[2][2]);
- try {
- ds.solve(b, svd);
- fail("an exception should have been thrown");
- } catch (InvalidMatrixException ime) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- ds.solve(b.getColumn(0), svd);
- fail("an exception should have been thrown");
- } catch (InvalidMatrixException ime) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- ds.solve(b.getColumnVector(0), svd);
- fail("an exception should have been thrown");
- } catch (InvalidMatrixException ime) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- try {
- ds.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)), svd);
- fail("an exception should have been thrown");
- } catch (InvalidMatrixException ime) {
- // expected behavior
- } catch (Exception e) {
- fail("wrong exception caught");
- }
- }
-
- /** test solve */
- public void testSolve() {
- DecompositionSolver ds =
- new DecompositionSolver(new RealMatrixImpl(testSquare, false));
- SingularValueDecomposition svd = ds.singularDecompose();
- RealMatrix b = new RealMatrixImpl(new double[][] {
- { 1, 2, 3 }, { 0, -5, 1 }
- });
- RealMatrix xRef = new RealMatrixImpl(new double[][] {
- { -8.0 / 25.0, -263.0 / 75.0, -29.0 / 75.0 },
- { 19.0 / 25.0, 78.0 / 25.0, 49.0 / 25.0 }
- });
-
- // using RealMatrix
- assertEquals(0, ds.solve(b, svd).subtract(xRef).getNorm(), normTolerance);
-
- // using double[]
- for (int i = 0; i < b.getColumnDimension(); ++i) {
- assertEquals(0,
- new RealVectorImpl(ds.solve(b.getColumn(i), svd)).subtract(xRef.getColumnVector(i)).getNorm(),
- 1.0e-13);
- }
-
- // using RealMatrixImpl
- for (int i = 0; i < b.getColumnDimension(); ++i) {
- assertEquals(0,
- ds.solve(b.getColumnVector(i), svd).subtract(xRef.getColumnVector(i)).getNorm(),
- 1.0e-13);
- }
-
- // using RealMatrix with an alternate implementation
- for (int i = 0; i < b.getColumnDimension(); ++i) {
- RealVectorImplTest.RealVectorTestImpl v =
- new RealVectorImplTest.RealVectorTestImpl(b.getColumn(i));
- assertEquals(0,
- ds.solve(v, svd).subtract(xRef.getColumnVector(i)).getNorm(),
- 1.0e-13);
- }
-
- }
-
/** test matrices values */
public void testMatricesValues1() {
SingularValueDecomposition svd =
@@ -299,7 +183,7 @@
// check values against known references
SingularValueDecomposition svd =
- new DecompositionSolver(new RealMatrixImpl(testNonSquare, false)).singularDecompose();
+ new SingularValueDecompositionImpl(new RealMatrixImpl(testNonSquare, false));
RealMatrix u = svd.getU();
assertEquals(0, u.subtract(uRef).getNorm(), normTolerance);
RealMatrix s = svd.getS();
Added: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SingularValueSolverTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SingularValueSolverTest.java?rev=723736&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SingularValueSolverTest.java (added)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SingularValueSolverTest.java Fri Dec 5 06:05:50 2008
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.linear;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class SingularValueSolverTest extends TestCase {
+
+ private double[][] testSquare = {
+ { 24.0 / 25.0, 43.0 / 25.0 },
+ { 57.0 / 25.0, 24.0 / 25.0 }
+ };
+
+ private static final double normTolerance = 10e-14;
+
+ public SingularValueSolverTest(String name) {
+ super(name);
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(SingularValueSolverTest.class);
+ suite.setName("SingularValueSolver Tests");
+ return suite;
+ }
+
+ /** test solve dimension errors */
+ public void testSolveDimensionErrors() {
+ SingularValueSolver solver =
+ new SingularValueSolver(new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false)));
+ RealMatrix b = new RealMatrixImpl(new double[3][2]);
+ try {
+ solver.solve(b);
+ fail("an exception should have been thrown");
+ } catch (IllegalArgumentException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(b.getColumn(0));
+ fail("an exception should have been thrown");
+ } catch (IllegalArgumentException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)));
+ fail("an exception should have been thrown");
+ } catch (IllegalArgumentException iae) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ }
+
+ /** test solve singularity errors */
+ public void testSolveSingularityErrors() {
+ RealMatrix m =
+ new RealMatrixImpl(new double[][] {
+ { 1.0, 0.0 },
+ { 0.0, 0.0 }
+ }, false);
+ SingularValueSolver solver = new SingularValueSolver(new SingularValueDecompositionImpl(m));
+ RealMatrix b = new RealMatrixImpl(new double[2][2]);
+ try {
+ solver.solve(b);
+ fail("an exception should have been thrown");
+ } catch (InvalidMatrixException ime) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(b.getColumn(0));
+ fail("an exception should have been thrown");
+ } catch (InvalidMatrixException ime) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(b.getColumnVector(0));
+ fail("an exception should have been thrown");
+ } catch (InvalidMatrixException ime) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ try {
+ solver.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)));
+ fail("an exception should have been thrown");
+ } catch (InvalidMatrixException ime) {
+ // expected behavior
+ } catch (Exception e) {
+ fail("wrong exception caught");
+ }
+ }
+
+ /** test solve */
+ public void testSolve() {
+ SingularValueSolver solver =
+ new SingularValueSolver(new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false)));
+ RealMatrix b = new RealMatrixImpl(new double[][] {
+ { 1, 2, 3 }, { 0, -5, 1 }
+ });
+ RealMatrix xRef = new RealMatrixImpl(new double[][] {
+ { -8.0 / 25.0, -263.0 / 75.0, -29.0 / 75.0 },
+ { 19.0 / 25.0, 78.0 / 25.0, 49.0 / 25.0 }
+ });
+
+ // using RealMatrix
+ assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), normTolerance);
+
+ // using double[]
+ for (int i = 0; i < b.getColumnDimension(); ++i) {
+ assertEquals(0,
+ new RealVectorImpl(solver.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(),
+ 1.0e-13);
+ }
+
+ // using RealMatrixImpl
+ for (int i = 0; i < b.getColumnDimension(); ++i) {
+ assertEquals(0,
+ solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
+ 1.0e-13);
+ }
+
+ // using RealMatrix with an alternate implementation
+ for (int i = 0; i < b.getColumnDimension(); ++i) {
+ RealVectorImplTest.RealVectorTestImpl v =
+ new RealVectorImplTest.RealVectorTestImpl(b.getColumn(i));
+ assertEquals(0,
+ solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
+ 1.0e-13);
+ }
+
+ }
+
+ /** test condition number */
+ public void testConditionNumber() {
+ SingularValueDecompositionImpl svd =
+ new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false));
+ assertEquals(3.0, svd.getConditionNumber(), 1.0e-15);
+ }
+
+}
Propchange: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SingularValueSolverTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/test/org/apache/commons/math/linear/SingularValueSolverTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision