Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java?rev=723496&r1=723495&r2=723496&view=diff ============================================================================== --- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java (original) +++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java Thu Dec 4 15:48:18 2008 @@ -18,7 +18,7 @@ package org.apache.commons.math.linear; import org.apache.commons.math.ConvergenceException; -import org.apache.commons.math.MathRuntimeException; +import org.apache.commons.math.util.MathUtils; /** * Calculates the Singular Value Decomposition of a matrix. @@ -34,7 +34,7 @@ public class SingularValueDecompositionImpl implements SingularValueDecomposition { /** Serializable version identifier. */ - private static final long serialVersionUID = -2357152028714378552L; + private static final long serialVersionUID = -238768772547767847L; /** Number of rows of the initial matrix. */ private int m; @@ -79,19 +79,6 @@ private RealMatrix cachedVt; /** - * Build a new instance. - * <p>Note that [EMAIL PROTECTED] #decompose(RealMatrix)} <strong>must</strong> be called - * before any of the [EMAIL PROTECTED] #getU()}, [EMAIL PROTECTED] #getS()}, [EMAIL PROTECTED] #getV()}, - * [EMAIL PROTECTED] #getSingularValues()}, [EMAIL PROTECTED] #getNorm()}, [EMAIL PROTECTED] #getConditionNumber()}, - * [EMAIL PROTECTED] #getRank()}, [EMAIL PROTECTED] #solve(double[])}, [EMAIL PROTECTED] #solve(RealMatrix)}, - * [EMAIL PROTECTED] #solve(RealVector)} or [EMAIL PROTECTED] #solve(RealVectorImpl)} methods can be - * called.</p> - * @see #decompose(RealMatrix) - */ - public SingularValueDecompositionImpl() { - } - - /** * Calculates the Singular Value Decomposition of the given matrix. * <p>Calling this constructor is equivalent to first call the no-arguments * constructor and then call [EMAIL PROTECTED] #decompose(RealMatrix)}.</p> @@ -101,11 +88,6 @@ */ public SingularValueDecompositionImpl(RealMatrix matrix) throws InvalidMatrixException { - decompose(matrix); - } - - /** [EMAIL PROTECTED] */ - public void decompose(final RealMatrix matrix) { m = matrix.getRowDimension(); n = matrix.getColumnDimension(); @@ -133,7 +115,9 @@ } // compute singular values - eigenDecomposition = new EigenDecompositionImpl(mainTridiagonal, secondaryTridiagonal); + eigenDecomposition = + new EigenDecompositionImpl(mainTridiagonal, secondaryTridiagonal, + MathUtils.SAFE_MIN); singularValues = eigenDecomposition.getEigenvalues(); for (int i = 0; i < singularValues.length; ++i) { singularValues[i] = Math.sqrt(singularValues[i]); @@ -147,8 +131,6 @@ if (cachedU == null) { - checkDecomposed(); - if (m >= n) { // the tridiagonal matrix is Bt.B, where B is upper bidiagonal final double[][] eData = eigenDecomposition.getV().getData(); @@ -207,8 +189,6 @@ if (cachedS == null) { - checkDecomposed(); - final int p = singularValues.length; final double[][] sData = new double[p][p]; for (int i = 0; i < p; ++i) { @@ -225,7 +205,6 @@ /** [EMAIL PROTECTED] */ public double[] getSingularValues() throws InvalidMatrixException { - checkDecomposed(); return singularValues.clone(); } @@ -235,8 +214,6 @@ if (cachedV == null) { - checkDecomposed(); - if (m >= n) { // the tridiagonal matrix is Bt.B, where B is upper bidiagonal cachedV = transformer.getV().multiply(eigenDecomposition.getV()); @@ -292,14 +269,12 @@ /** [EMAIL PROTECTED] */ public double getNorm() throws InvalidMatrixException { - checkDecomposed(); return singularValues[0]; } /** [EMAIL PROTECTED] */ public double getConditionNumber() throws InvalidMatrixException { - checkDecomposed(); return singularValues[0] / singularValues[singularValues.length - 1]; } @@ -307,8 +282,6 @@ public int getRank() throws IllegalStateException { - checkDecomposed(); - final double threshold = Math.max(m, n) * Math.ulp(singularValues[0]); for (int i = singularValues.length - 1; i >= 0; --i) { @@ -320,116 +293,4 @@ } - /** [EMAIL PROTECTED] */ - public boolean isNonSingular() - throws IllegalStateException { - return getRank() == singularValues.length; - } - - /** [EMAIL PROTECTED] */ - public double[] solve(final double[] b) - throws IllegalArgumentException, InvalidMatrixException { - - checkDecomposed(); - - if (b.length != m) { - throw new IllegalArgumentException("constant vector has wrong length"); - } - - final double[] w = getUT().operate(b); - for (int i = 0; i < singularValues.length; ++i) { - final double si = singularValues[i]; - if (si == 0) { - throw new SingularMatrixException(); - } - w[i] /= si; - } - return getV().operate(w); - - } - - /** [EMAIL PROTECTED] */ - public RealVector solve(final RealVector b) - throws IllegalArgumentException, InvalidMatrixException { - try { - return solve((RealVectorImpl) b); - } catch (ClassCastException cce) { - - checkDecomposed(); - - if (b.getDimension() != m) { - throw new IllegalArgumentException("constant vector has wrong length"); - } - - final RealVector w = getUT().operate(b); - for (int i = 0; i < singularValues.length; ++i) { - final double si = singularValues[i]; - if (si == 0) { - throw new SingularMatrixException(); - } - w.set(i, w.getEntry(i) / si); - } - return getV().operate(w); - - } - } - - /** Solve the linear equation A × X = B. - * <p>The A matrix is implicit here. It is </p> - * @param b right-hand side of the equation A × X = B - * @return a vector X such that A × X = B - * @throws IllegalArgumentException if matrices dimensions don't match - * @throws InvalidMatrixException if decomposed matrix is singular - */ - public RealVectorImpl solve(final RealVectorImpl b) - throws IllegalArgumentException, InvalidMatrixException { - return new RealVectorImpl(solve(b.getDataRef()), false); - } - - /** [EMAIL PROTECTED] */ - public RealMatrix solve(final RealMatrix b) - throws IllegalArgumentException, InvalidMatrixException { - - checkDecomposed(); - - if (b.getRowDimension() != m) { - throw new IllegalArgumentException("Incorrect row dimension"); - } - - final RealMatrixImpl w = (RealMatrixImpl) getUT().multiply(b); - final double[][] wData = w.getDataRef(); - for (int i = 0; i < singularValues.length; ++i) { - final double si = singularValues[i]; - if (si == 0) { - throw new SingularMatrixException(); - } - final double inv = 1.0 / si; - final double[] wi = wData[i]; - for (int j = 0; j < b.getColumnDimension(); ++j) { - wi[j] *= inv; - } - } - return getV().multiply(w); - - } - - /** [EMAIL PROTECTED] */ - public RealMatrix getInverse() - throws IllegalStateException, InvalidMatrixException { - checkDecomposed(); - return solve(MatrixUtils.createRealIdentityMatrix(singularValues.length)); - } - - /** - * Check if [EMAIL PROTECTED] #decompose(RealMatrix)} has been called. - * @exception IllegalStateException if [EMAIL PROTECTED] #decompose(RealMatrix) decompose} - * has not been called - */ - private void checkDecomposed() - throws IllegalStateException { - if (singularValues == null) { - throw MathRuntimeException.createIllegalStateException("no matrix have been decomposed yet", null); - } - } - }
Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/GLSMultipleLinearRegression.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/GLSMultipleLinearRegression.java?rev=723496&r1=723495&r2=723496&view=diff ============================================================================== --- commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/GLSMultipleLinearRegression.java (original) +++ commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/GLSMultipleLinearRegression.java Thu Dec 4 15:48:18 2008 @@ -16,7 +16,7 @@ */ package org.apache.commons.math.stat.regression; -import org.apache.commons.math.linear.LUDecompositionImpl; +import org.apache.commons.math.linear.DecompositionSolver; import org.apache.commons.math.linear.RealMatrix; import org.apache.commons.math.linear.RealMatrixImpl; @@ -78,7 +78,8 @@ */ protected RealMatrix getOmegaInverse() { if (OmegaInverse == null) { - OmegaInverse = new LUDecompositionImpl(Omega).getInverse(); + DecompositionSolver solver = new DecompositionSolver(Omega); + OmegaInverse = solver.getInverse(solver.luDecompose()); } return OmegaInverse; } @@ -94,7 +95,8 @@ RealMatrix OI = getOmegaInverse(); RealMatrix XT = X.transpose(); RealMatrix XTOIX = XT.multiply(OI).multiply(X); - return new LUDecompositionImpl(XTOIX).getInverse().multiply(XT).multiply(OI).multiply(Y); + DecompositionSolver solver = new DecompositionSolver(XTOIX); + return solver.getInverse(solver.luDecompose()).multiply(XT).multiply(OI).multiply(Y); } /** @@ -107,7 +109,8 @@ protected RealMatrix calculateBetaVariance() { RealMatrix OI = getOmegaInverse(); RealMatrix XTOIX = X.transpose().multiply(OI).multiply(X); - return new LUDecompositionImpl(XTOIX).getInverse(); + DecompositionSolver solver = new DecompositionSolver(XTOIX); + return solver.getInverse(solver.luDecompose()); } /** Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java?rev=723496&r1=723495&r2=723496&view=diff ============================================================================== --- commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java (original) +++ commons/proper/math/trunk/src/java/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.java Thu Dec 4 15:48:18 2008 @@ -16,6 +16,7 @@ */ package org.apache.commons.math.stat.regression; +import org.apache.commons.math.linear.DecompositionSolver; import org.apache.commons.math.linear.LUDecompositionImpl; import org.apache.commons.math.linear.QRDecomposition; import org.apache.commons.math.linear.QRDecompositionImpl; @@ -108,7 +109,8 @@ */ protected RealMatrix calculateBetaVariance() { RealMatrix XTX = X.transpose().multiply(X); - return new LUDecompositionImpl(XTX).getInverse(); + DecompositionSolver solver = new DecompositionSolver(XTX); + return solver.getInverse(solver.luDecompose()); } 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=723496&r1=723495&r2=723496&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 Thu Dec 4 15:48:18 2008 @@ -20,6 +20,8 @@ import java.util.Arrays; import java.util.Random; +import org.apache.commons.math.util.MathUtils; + import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -44,7 +46,7 @@ new RealMatrixImpl(new double[][] { { 1.5 } }, false); - EigenDecomposition ed = new EigenDecompositionImpl(matrix); + EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose(); assertEquals(1.5, ed.getEigenvalue(0), 1.0e-15); } @@ -54,7 +56,7 @@ { 59.0, 12.0 }, { Double.NaN, 66.0 } }, false); - EigenDecomposition ed = new EigenDecompositionImpl(matrix); + EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose(); assertEquals(75.0, ed.getEigenvalue(0), 1.0e-15); assertEquals(50.0, ed.getEigenvalue(1), 1.0e-15); } @@ -66,7 +68,7 @@ { Double.NaN, 8693.0, 7920.0 }, { Double.NaN, Double.NaN, 17300.0 } }, false); - EigenDecomposition ed = new EigenDecompositionImpl(matrix); + EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose(); 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); @@ -80,24 +82,7 @@ { Double.NaN, Double.NaN, 0.164, -0.048 }, { Double.NaN, Double.NaN, Double.NaN, 0.136 } }, false); - EigenDecomposition ed = new EigenDecompositionImpl(matrix); - 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); - assertEquals(0.1, ed.getEigenvalue(3), 1.0e-15); - } - - public void testAbsoluteSplit() { - RealMatrix matrix = - new RealMatrixImpl(new double[][] { - { 0.784, -0.288, 0.000, 0.000 }, - { Double.NaN, 0.616, 0.000, 0.000 }, - { Double.NaN, Double.NaN, 0.164, -0.048 }, - { Double.NaN, Double.NaN, Double.NaN, 0.136 } - }, false); - EigenDecompositionImpl ed = new EigenDecompositionImpl(); - ed.setAbsoluteSplitTolerance(1.0e-13); - ed.decompose(matrix); + EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose(); 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); @@ -112,7 +97,7 @@ { 0.1152, -0.2304, 0.3088, -0.1344 }, { -0.2976, 0.1152, -0.1344, 0.3872 } }, false); - EigenDecomposition ed = new EigenDecompositionImpl(matrix); + EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose(); 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); @@ -135,7 +120,8 @@ new TriDiagonalTransformer(createTestMatrix(r, ref)); EigenDecomposition ed = new EigenDecompositionImpl(t.getMainDiagonalRef(), - t.getSecondaryDiagonalRef()); + t.getSecondaryDiagonalRef(), + MathUtils.SAFE_MIN); double[] eigenValues = ed.getEigenvalues(); assertEquals(ref.length, eigenValues.length); for (int i = 0; i < ref.length; ++i) { @@ -147,7 +133,7 @@ /** test dimensions */ public void testDimensions() { final int m = matrix.getRowDimension(); - EigenDecomposition ed = new EigenDecompositionImpl(matrix); + EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose(); assertEquals(m, ed.getV().getRowDimension()); assertEquals(m, ed.getV().getColumnDimension()); assertEquals(m, ed.getD().getColumnDimension()); @@ -158,7 +144,7 @@ /** test eigenvalues */ public void testEigenvalues() { - EigenDecomposition ed = new EigenDecompositionImpl(matrix); + EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose(); double[] eigenValues = ed.getEigenvalues(); assertEquals(refValues.length, eigenValues.length); for (int i = 0; i < refValues.length; ++i) { @@ -174,7 +160,8 @@ bigValues[i] = 2 * r.nextDouble() - 1; } Arrays.sort(bigValues); - EigenDecomposition ed = new EigenDecompositionImpl(createTestMatrix(r, bigValues)); + EigenDecomposition ed = + new DecompositionSolver(createTestMatrix(r, bigValues)).eigenDecompose(); double[] eigenValues = ed.getEigenvalues(); assertEquals(bigValues.length, eigenValues.length); for (int i = 0; i < bigValues.length; ++i) { @@ -184,7 +171,7 @@ /** test eigenvectors */ public void testEigenvectors() { - EigenDecomposition ed = new EigenDecompositionImpl(matrix); + EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose(); for (int i = 0; i < matrix.getRowDimension(); ++i) { double lambda = ed.getEigenvalue(i); RealVector v = ed.getEigenvector(i); @@ -195,7 +182,7 @@ /** test A = VDVt */ public void testAEqualVDVt() { - EigenDecomposition ed = new EigenDecompositionImpl(matrix); + EigenDecomposition ed = new DecompositionSolver(matrix).eigenDecompose(); RealMatrix v = ed.getV(); RealMatrix d = ed.getD(); RealMatrix vT = ed.getVT(); @@ -205,7 +192,7 @@ /** test that V is orthogonal */ public void testVOrthogonal() { - RealMatrix v = new EigenDecompositionImpl(matrix).getV(); + RealMatrix v = new DecompositionSolver(matrix).eigenDecompose().getV(); RealMatrix vTv = v.transpose().multiply(v); RealMatrix id = MatrixUtils.createRealIdentityMatrix(vTv.getRowDimension()); assertEquals(0, vTv.subtract(id).getNorm(), 2.0e-13); @@ -214,11 +201,12 @@ /** test non invertible matrix */ public void testNonInvertible() { Random r = new Random(9994100315209l); - EigenDecomposition ed = - new EigenDecompositionImpl(createTestMatrix(r, new double[] { 1.0, 0.0, -1.0, -2.0, -3.0 })); - assertFalse(ed.isNonSingular()); + 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 { - ed.getInverse(); + ds.getInverse(ed); fail("an exception should have been thrown"); } catch (InvalidMatrixException ime) { // expected behavior @@ -232,9 +220,10 @@ Random r = new Random(9994100315209l); RealMatrix m = createTestMatrix(r, new double[] { 1.0, 0.5, -1.0, -2.0, -3.0 }); - EigenDecomposition ed = new EigenDecompositionImpl(m); - assertTrue(ed.isNonSingular()); - RealMatrix inverse = ed.getInverse(); + 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); @@ -243,8 +232,8 @@ /** test diagonal matrix */ public void testDiagonal() { double[] diagonal = new double[] { -3.0, -2.0, 2.0, 5.0 }; - EigenDecomposition ed = - new EigenDecompositionImpl(createDiagonalMatrix(diagonal, diagonal.length, diagonal.length)); + RealMatrix m = createDiagonalMatrix(diagonal, diagonal.length, diagonal.length); + EigenDecomposition ed = new DecompositionSolver(m).eigenDecompose(); 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); @@ -253,10 +242,11 @@ /** test solve dimension errors */ public void testSolveDimensionErrors() { - EigenDecomposition ed = new EigenDecompositionImpl(matrix); + DecompositionSolver ds = new DecompositionSolver(matrix); + EigenDecomposition ed = ds.eigenDecompose(); RealMatrix b = new RealMatrixImpl(new double[2][2]); try { - ed.solve(b); + ds.solve(b, ed); fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected behavior @@ -264,7 +254,7 @@ fail("wrong exception caught"); } try { - ed.solve(b.getColumn(0)); + ds.solve(b.getColumn(0), ed); fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected behavior @@ -272,7 +262,7 @@ fail("wrong exception caught"); } try { - ed.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0))); + ds.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)), ed); fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected behavior @@ -291,8 +281,9 @@ { 40, 2, 21, 9, 51, 19 }, { 14, -1, 8, 0, 19, 14 } }); - EigenDecomposition ed = new EigenDecompositionImpl(m); - assertEquals(184041, ed.getDeterminant(), 2.0e-8); + 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 }, @@ -311,19 +302,19 @@ }); // using RealMatrix - assertEquals(0, ed.solve(b).subtract(xRef).getNorm(), 2.0e-12); + 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(ed.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(), + 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, - ed.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(), + ds.solve(b.getColumnVector(i), ed).subtract(xRef.getColumnVector(i)).getNorm(), 2.0e-11); } @@ -332,7 +323,7 @@ RealVectorImplTest.RealVectorTestImpl v = new RealVectorImplTest.RealVectorTestImpl(b.getColumn(i)); assertEquals(0, - ed.solve(v).subtract(xRef.getColumnVector(i)).getNorm(), + ds.solve(v, ed).subtract(xRef.getColumnVector(i)).getNorm(), 2.0e-11); } @@ -347,7 +338,7 @@ {2, 0, 2}, {4, 2, 3} }); - EigenDecomposition ed = new EigenDecompositionImpl(repeated); + EigenDecomposition ed = new DecompositionSolver(repeated).eigenDecompose(); checkEigenValues((new double[] {8, -1, -1}), ed, 1E-12); checkEigenVector((new double[] {2, 1, 2}), ed, 1E-12); } @@ -361,7 +352,7 @@ {1, 3, -4}, {-4, -4, 8} }); - EigenDecomposition ed = new EigenDecompositionImpl(distinct); + EigenDecomposition ed = new DecompositionSolver(distinct).eigenDecompose(); 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); 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=723496&r1=723495&r2=723496&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 Thu Dec 4 15:48:18 2008 @@ -88,18 +88,6 @@ } } - /** test no call to decompose */ - public void testNoDecompose() { - try { - new LUDecompositionImpl().getPivot(); - fail("an exception should have been caught"); - } catch (IllegalStateException ise) { - // expected behavior - } catch (Exception e) { - fail("wrong exception caught"); - } - } - /** test threshold impact */ public void testThreshold() { final RealMatrix matrix = new RealMatrixImpl(new double[][] { @@ -107,8 +95,9 @@ { 2.0, 5.0, 3.0}, { 4.000001, 9.0, 9.0} }, false); - assertFalse(new LUDecompositionImpl(matrix, 1.0e-5).isNonSingular()); - assertTrue(new LUDecompositionImpl(matrix, 1.0e-10).isNonSingular()); + DecompositionSolver solver = new DecompositionSolver(matrix); + assertFalse(solver.isNonSingular(solver.luDecompose(1.0e-5))); + assertTrue(solver.isNonSingular(solver.luDecompose(1.0e-10))); } /** test PA = LU */ @@ -139,14 +128,14 @@ matrix = new RealMatrixImpl(singular, false); lu = new LUDecompositionImpl(matrix); - assertFalse(lu.isNonSingular()); + assertTrue(lu.isSingular()); assertNull(lu.getL()); assertNull(lu.getU()); assertNull(lu.getP()); matrix = new RealMatrixImpl(bigSingular, false); lu = new LUDecompositionImpl(matrix); - assertFalse(lu.isNonSingular()); + assertTrue(lu.isSingular()); assertNull(lu.getL()); assertNull(lu.getU()); assertNull(lu.getP()); @@ -230,20 +219,20 @@ public void testSingular() { LUDecomposition lu = new LUDecompositionImpl(new RealMatrixImpl(testData, false)); - assertTrue(lu.isNonSingular()); + assertFalse(lu.isSingular()); lu = new LUDecompositionImpl(new RealMatrixImpl(singular, false)); - assertFalse(lu.isNonSingular()); + assertTrue(lu.isSingular()); lu = new LUDecompositionImpl(new RealMatrixImpl(bigSingular, false)); - assertFalse(lu.isNonSingular()); + assertTrue(lu.isSingular()); } /** test solve dimension errors */ public void testSolveDimensionErrors() { - LUDecomposition lu = - new LUDecompositionImpl(new RealMatrixImpl(testData, false)); + DecompositionSolver solver = + new DecompositionSolver(new RealMatrixImpl(testData, false)); RealMatrix b = new RealMatrixImpl(new double[2][2]); try { - lu.solve(b); + solver.solve(b, solver.luDecompose()); fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected behavior @@ -251,7 +240,7 @@ fail("wrong exception caught"); } try { - lu.solve(b.getColumn(0)); + solver.solve(b.getColumn(0), solver.luDecompose()); fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected behavior @@ -259,7 +248,8 @@ fail("wrong exception caught"); } try { - lu.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0))); + solver.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)), + solver.luDecompose()); fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected behavior @@ -270,11 +260,11 @@ /** test solve singularity errors */ public void testSolveSingularityErrors() { - LUDecomposition lu = - new LUDecompositionImpl(new RealMatrixImpl(singular, false)); + DecompositionSolver solver = + new DecompositionSolver(new RealMatrixImpl(singular, false)); RealMatrix b = new RealMatrixImpl(new double[2][2]); try { - lu.solve(b); + solver.solve(b, solver.luDecompose()); fail("an exception should have been thrown"); } catch (InvalidMatrixException ime) { // expected behavior @@ -282,7 +272,7 @@ fail("wrong exception caught"); } try { - lu.solve(b.getColumn(0)); + solver.solve(b.getColumn(0), solver.luDecompose()); fail("an exception should have been thrown"); } catch (InvalidMatrixException ime) { // expected behavior @@ -290,7 +280,7 @@ fail("wrong exception caught"); } try { - lu.solve(b.getColumnVector(0)); + solver.solve(b.getColumnVector(0), solver.luDecompose()); fail("an exception should have been thrown"); } catch (InvalidMatrixException ime) { // expected behavior @@ -298,7 +288,8 @@ fail("wrong exception caught"); } try { - lu.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0))); + solver.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)), + solver.luDecompose()); fail("an exception should have been thrown"); } catch (InvalidMatrixException ime) { // expected behavior @@ -309,8 +300,9 @@ /** test solve */ public void testSolve() { - LUDecomposition lu = - new LUDecompositionImpl(new RealMatrixImpl(testData, false)); + DecompositionSolver solver = + new DecompositionSolver(new RealMatrixImpl(testData, false)); + LUDecomposition lu = solver.luDecompose(); RealMatrix b = new RealMatrixImpl(new double[][] { { 1, 0 }, { 2, -5 }, { 3, 1 } }); @@ -319,19 +311,19 @@ }); // using RealMatrix - assertEquals(0, lu.solve(b).subtract(xRef).getNorm(), 1.0e-13); + 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(lu.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(), + 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, - lu.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(), + solver.solve(b.getColumnVector(i), lu).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13); } @@ -340,7 +332,7 @@ RealVectorImplTest.RealVectorTestImpl v = new RealVectorImplTest.RealVectorTestImpl(b.getColumn(i)); assertEquals(0, - lu.solve(v).subtract(xRef.getColumnVector(i)).getNorm(), + solver.solve(v, lu).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13); } @@ -428,18 +420,15 @@ /** test determinant */ public void testDeterminant() { - assertEquals(-1, - new LUDecompositionImpl(new RealMatrixImpl(testData, false)).getDeterminant(), - 1.0e-15); - assertEquals(-10, - new LUDecompositionImpl(new RealMatrixImpl(luData, false)).getDeterminant(), - 1.0e-14); - assertEquals(0, - new LUDecompositionImpl(new RealMatrixImpl(singular, false)).getDeterminant(), - 1.0e-17); - assertEquals(0, - new LUDecompositionImpl(new RealMatrixImpl(bigSingular, false)).getDeterminant(), - 1.0e-17); + 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()); } } 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=723496&r1=723495&r2=723496&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 Thu Dec 4 15:48:18 2008 @@ -198,28 +198,28 @@ /** test rank */ public void testRank() { - QRDecomposition qr = - new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false)); - assertTrue(qr.isNonSingular()); + DecompositionSolver ds = + new DecompositionSolver(new RealMatrixImpl(testData3x3NonSingular, false)); + assertTrue(ds.isNonSingular(ds.qrDecompose())); - qr = new QRDecompositionImpl(new RealMatrixImpl(testData3x3Singular, false)); - assertFalse(qr.isNonSingular()); + ds = new DecompositionSolver(new RealMatrixImpl(testData3x3Singular, false)); + assertFalse(ds.isNonSingular(ds.qrDecompose())); - qr = new QRDecompositionImpl(new RealMatrixImpl(testData3x4, false)); - assertFalse(qr.isNonSingular()); + ds = new DecompositionSolver(new RealMatrixImpl(testData3x4, false)); + assertTrue(ds.isNonSingular(ds.qrDecompose())); - qr = new QRDecompositionImpl(new RealMatrixImpl(testData4x3, false)); - assertTrue(qr.isNonSingular()); + ds = new DecompositionSolver(new RealMatrixImpl(testData4x3, false)); + assertTrue(ds.isNonSingular(ds.qrDecompose())); } /** test solve dimension errors */ public void testSolveDimensionErrors() { - QRDecomposition qr = - new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false)); + DecompositionSolver solver = + new DecompositionSolver(new RealMatrixImpl(testData3x3NonSingular, false)); RealMatrix b = new RealMatrixImpl(new double[2][2]); try { - qr.solve(b); + solver.solve(b, solver.qrDecompose()); fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected behavior @@ -227,7 +227,7 @@ fail("wrong exception caught"); } try { - qr.solve(b.getColumn(0)); + solver.solve(b.getColumn(0), solver.qrDecompose()); fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected behavior @@ -235,7 +235,7 @@ fail("wrong exception caught"); } try { - qr.solve(b.getColumnVector(0)); + solver.solve(b.getColumnVector(0), solver.qrDecompose()); fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected behavior @@ -246,11 +246,11 @@ /** test solve rank errors */ public void testSolveRankErrors() { - QRDecomposition qr = - new QRDecompositionImpl(new RealMatrixImpl(testData3x3Singular, false)); + DecompositionSolver solver = + new DecompositionSolver(new RealMatrixImpl(testData3x3Singular, false)); RealMatrix b = new RealMatrixImpl(new double[3][2]); try { - qr.solve(b); + solver.solve(b, solver.qrDecompose()); fail("an exception should have been thrown"); } catch (InvalidMatrixException iae) { // expected behavior @@ -258,7 +258,7 @@ fail("wrong exception caught"); } try { - qr.solve(b.getColumn(0)); + solver.solve(b.getColumn(0), solver.qrDecompose()); fail("an exception should have been thrown"); } catch (InvalidMatrixException iae) { // expected behavior @@ -266,7 +266,7 @@ fail("wrong exception caught"); } try { - qr.solve(b.getColumnVector(0)); + solver.solve(b.getColumnVector(0), solver.qrDecompose()); fail("an exception should have been thrown"); } catch (InvalidMatrixException iae) { // expected behavior @@ -277,8 +277,9 @@ /** test solve */ public void testSolve() { - QRDecomposition qr = - new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false)); + DecompositionSolver ds = + new DecompositionSolver(new RealMatrixImpl(testData3x3NonSingular, false)); + QRDecomposition qr = ds.qrDecompose(); RealMatrix b = new RealMatrixImpl(new double[][] { { -102, 12250 }, { 544, 24500 }, { 167, -36750 } }); @@ -287,19 +288,19 @@ }); // using RealMatrix - assertEquals(0, qr.solve(b).subtract(xRef).getNorm(), 1.0e-13); + 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(qr.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(), + 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, - qr.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(), + ds.solve(b.getColumnVector(i), qr).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13); } @@ -308,7 +309,7 @@ RealVectorImplTest.RealVectorTestImpl v = new RealVectorImplTest.RealVectorTestImpl(b.getColumn(i)); assertEquals(0, - qr.solve(v).subtract(xRef.getColumnVector(i)).getNorm(), + ds.solve(v, qr).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13); } @@ -316,8 +317,9 @@ /** test matrices values */ public void testMatricesValues() { - QRDecomposition qr = - new QRDecompositionImpl(new RealMatrixImpl(testData3x3NonSingular, false)); + DecompositionSolver ds = + new DecompositionSolver(new RealMatrixImpl(testData3x3NonSingular, false)); + QRDecomposition qr = ds.qrDecompose(); 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 }, @@ -351,16 +353,4 @@ } - /** test no call to decompose */ - public void testNoDecompose() { - try { - new QRDecompositionImpl().isNonSingular(); - fail("an exception should have been caught"); - } catch (IllegalStateException ise) { - // expected behavior - } catch (Exception e) { - fail("wrong exception caught"); - } - } - } 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=723496&r1=723495&r2=723496&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 Thu Dec 4 15:48:18 2008 @@ -261,8 +261,10 @@ /** test transpose */ public void testTranspose() { RealMatrix m = new RealMatrixImpl(testData); - RealMatrix mIT = new LUDecompositionImpl(m).getInverse().transpose(); - RealMatrix mTI = new LUDecompositionImpl(m.transpose()).getInverse(); + DecompositionSolver ds1 = new DecompositionSolver(m); + RealMatrix mIT = ds1.getInverse(ds1.luDecompose()).transpose(); + DecompositionSolver ds2 = new DecompositionSolver(m.transpose()); + RealMatrix mTI = ds2.getInverse(ds2.luDecompose()); assertClose("inverse-transpose", mIT, mTI, normTolerance); m = new RealMatrixImpl(testData2); RealMatrix mt = new RealMatrixImpl(testData2T); @@ -352,7 +354,8 @@ assertEquals(2, p.getRowDimension()); assertEquals(2, p.getColumnDimension()); // Invert p - RealMatrix pInverse = new LUDecompositionImpl(p).getInverse(); + DecompositionSolver ds1 = new DecompositionSolver(p); + RealMatrix pInverse = ds1.getInverse(ds1.luDecompose()); assertEquals(2, pInverse.getRowDimension()); assertEquals(2, pInverse.getColumnDimension()); @@ -360,7 +363,8 @@ double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}}; RealMatrix coefficients = new RealMatrixImpl(coefficientsData); double[] constants = {1, -2, 1}; - double[] solution = new LUDecompositionImpl(coefficients).solve(constants); + DecompositionSolver ds2 = new DecompositionSolver(coefficients); + double[] solution = ds2.solve(constants, ds2.luDecompose()); 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=723496&r1=723495&r2=723496&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 Thu Dec 4 15:48:18 2008 @@ -131,11 +131,12 @@ /** test solve dimension errors */ public void testSolveDimensionErrors() { - SingularValueDecomposition svd = - new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false)); + DecompositionSolver ds = + new DecompositionSolver(new RealMatrixImpl(testSquare, false)); + SingularValueDecomposition svd = ds.singularDecompose(); RealMatrix b = new RealMatrixImpl(new double[3][2]); try { - svd.solve(b); + ds.solve(b, svd); fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected behavior @@ -143,7 +144,7 @@ fail("wrong exception caught"); } try { - svd.solve(b.getColumn(0)); + ds.solve(b.getColumn(0), svd); fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected behavior @@ -151,7 +152,7 @@ fail("wrong exception caught"); } try { - svd.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0))); + ds.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)), svd); fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected behavior @@ -162,14 +163,15 @@ /** test solve singularity errors */ public void testSolveSingularityErrors() { - SingularValueDecomposition svd = - new SingularValueDecompositionImpl(new RealMatrixImpl(new double[][] { - { 1.0, 0.0 }, - { 0.0, 0.0 } - }, false)); + 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 { - svd.solve(b); + ds.solve(b, svd); fail("an exception should have been thrown"); } catch (InvalidMatrixException ime) { // expected behavior @@ -177,7 +179,7 @@ fail("wrong exception caught"); } try { - svd.solve(b.getColumn(0)); + ds.solve(b.getColumn(0), svd); fail("an exception should have been thrown"); } catch (InvalidMatrixException ime) { // expected behavior @@ -185,7 +187,7 @@ fail("wrong exception caught"); } try { - svd.solve(b.getColumnVector(0)); + ds.solve(b.getColumnVector(0), svd); fail("an exception should have been thrown"); } catch (InvalidMatrixException ime) { // expected behavior @@ -193,7 +195,7 @@ fail("wrong exception caught"); } try { - svd.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0))); + ds.solve(new RealVectorImplTest.RealVectorTestImpl(b.getColumn(0)), svd); fail("an exception should have been thrown"); } catch (InvalidMatrixException ime) { // expected behavior @@ -204,8 +206,9 @@ /** test solve */ public void testSolve() { - SingularValueDecomposition svd = - new SingularValueDecompositionImpl(new RealMatrixImpl(testSquare, false)); + DecompositionSolver ds = + new DecompositionSolver(new RealMatrixImpl(testSquare, false)); + SingularValueDecomposition svd = ds.singularDecompose(); RealMatrix b = new RealMatrixImpl(new double[][] { { 1, 2, 3 }, { 0, -5, 1 } }); @@ -215,19 +218,19 @@ }); // using RealMatrix - assertEquals(0, svd.solve(b).subtract(xRef).getNorm(), normTolerance); + assertEquals(0, ds.solve(b, svd).subtract(xRef).getNorm(), normTolerance); // using double[] for (int i = 0; i < b.getColumnDimension(); ++i) { assertEquals(0, - new RealVectorImpl(svd.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(), + 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, - svd.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(), + ds.solve(b.getColumnVector(i), svd).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13); } @@ -236,7 +239,7 @@ RealVectorImplTest.RealVectorTestImpl v = new RealVectorImplTest.RealVectorTestImpl(b.getColumn(i)); assertEquals(0, - svd.solve(v).subtract(xRef.getColumnVector(i)).getNorm(), + ds.solve(v, svd).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13); } @@ -295,8 +298,8 @@ }); // check values against known references - SingularValueDecomposition svd = new SingularValueDecompositionImpl(); - svd.decompose(new RealMatrixImpl(testNonSquare, false)); + SingularValueDecomposition svd = + new DecompositionSolver(new RealMatrixImpl(testNonSquare, false)).singularDecompose(); RealMatrix u = svd.getU(); assertEquals(0, u.subtract(uRef).getNorm(), normTolerance); RealMatrix s = svd.getS();
