Units tests of RealMatrix implementations: replaced reference to DecompositionSolver.solve(double[]) by DecompositionSolver.solve(RealVector) (see MATH-653)

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1164618 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastien Brisard 2011-09-02 16:40:18 +00:00
parent c76d918c77
commit 118387e272
3 changed files with 49 additions and 36 deletions

View File

@ -230,38 +230,38 @@ public final class Array2DRowRealMatrixTest {
Array2DRowRealMatrix mInv = new Array2DRowRealMatrix(testDataInv); Array2DRowRealMatrix mInv = new Array2DRowRealMatrix(testDataInv);
Array2DRowRealMatrix mPlusInv = new Array2DRowRealMatrix(testDataPlusInv); Array2DRowRealMatrix mPlusInv = new Array2DRowRealMatrix(testDataPlusInv);
Array2DRowRealMatrix identity = new Array2DRowRealMatrix(id); Array2DRowRealMatrix identity = new Array2DRowRealMatrix(id);
TestUtils.assertEquals("m^0", m.power(0), TestUtils.assertEquals("m^0", m.power(0),
identity, entryTolerance); identity, entryTolerance);
TestUtils.assertEquals("mInv^0", mInv.power(0), TestUtils.assertEquals("mInv^0", mInv.power(0),
identity, entryTolerance); identity, entryTolerance);
TestUtils.assertEquals("mPlusInv^0", mPlusInv.power(0), TestUtils.assertEquals("mPlusInv^0", mPlusInv.power(0),
identity, entryTolerance); identity, entryTolerance);
TestUtils.assertEquals("m^1", m.power(1), TestUtils.assertEquals("m^1", m.power(1),
m, entryTolerance); m, entryTolerance);
TestUtils.assertEquals("mInv^1", mInv.power(1), TestUtils.assertEquals("mInv^1", mInv.power(1),
mInv, entryTolerance); mInv, entryTolerance);
TestUtils.assertEquals("mPlusInv^1", mPlusInv.power(1), TestUtils.assertEquals("mPlusInv^1", mPlusInv.power(1),
mPlusInv, entryTolerance); mPlusInv, entryTolerance);
RealMatrix C1 = m.copy(); RealMatrix C1 = m.copy();
RealMatrix C2 = mInv.copy(); RealMatrix C2 = mInv.copy();
RealMatrix C3 = mPlusInv.copy(); RealMatrix C3 = mPlusInv.copy();
for (int i = 2; i <= 10; ++i) { for (int i = 2; i <= 10; ++i) {
C1 = C1.multiply(m); C1 = C1.multiply(m);
C2 = C2.multiply(mInv); C2 = C2.multiply(mInv);
C3 = C3.multiply(mPlusInv); C3 = C3.multiply(mPlusInv);
TestUtils.assertEquals("m^" + i, m.power(i), TestUtils.assertEquals("m^" + i, m.power(i),
C1, entryTolerance); C1, entryTolerance);
TestUtils.assertEquals("mInv^" + i, mInv.power(i), TestUtils.assertEquals("mInv^" + i, mInv.power(i),
C2, entryTolerance); C2, entryTolerance);
TestUtils.assertEquals("mPlusInv^" + i, mPlusInv.power(i), TestUtils.assertEquals("mPlusInv^" + i, mPlusInv.power(i),
C3, entryTolerance); C3, entryTolerance);
} }
try { try {
Array2DRowRealMatrix mNotSquare = new Array2DRowRealMatrix(testData2T); Array2DRowRealMatrix mNotSquare = new Array2DRowRealMatrix(testData2T);
mNotSquare.power(2); mNotSquare.power(2);
@ -269,7 +269,7 @@ public final class Array2DRowRealMatrixTest {
} catch (NonSquareMatrixException ex) { } catch (NonSquareMatrixException ex) {
// ignored // ignored
} }
try { try {
m.power(-1); m.power(-1);
Assert.fail("Expecting IllegalArgumentException"); Assert.fail("Expecting IllegalArgumentException");
@ -277,7 +277,7 @@ public final class Array2DRowRealMatrixTest {
// ignored // ignored
} }
} }
/** test trace */ /** test trace */
@Test @Test
public void testTrace() { public void testTrace() {
@ -437,12 +437,17 @@ public final class Array2DRowRealMatrixTest {
// Solve example // Solve example
double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}}; double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}};
RealMatrix coefficients = new Array2DRowRealMatrix(coefficientsData); RealMatrix coefficients = new Array2DRowRealMatrix(coefficientsData);
double[] constants = {1, -2, 1}; RealVector constants = new ArrayRealVector(new double[]{1, -2, 1}, false);
double[] solution = new LUDecompositionImpl(coefficients).getSolver().solve(constants); RealVector solution = new LUDecompositionImpl(coefficients).getSolver().solve(constants);
Assert.assertEquals(2 * solution[0] + 3 * solution[1] -2 * solution[2], constants[0], 1E-12); final double cst0 = constants.getEntry(0);
Assert.assertEquals(-1 * solution[0] + 7 * solution[1] + 6 * solution[2], constants[1], 1E-12); final double cst1 = constants.getEntry(1);
Assert.assertEquals(4 * solution[0] - 3 * solution[1] -5 * solution[2], constants[2], 1E-12); final double cst2 = constants.getEntry(2);
final double sol0 = solution.getEntry(0);
final double sol1 = solution.getEntry(1);
final double sol2 = solution.getEntry(2);
Assert.assertEquals(2 * sol0 + 3 * sol1 -2 * sol2, cst0, 1E-12);
Assert.assertEquals(-1 * sol0 + 7 * sol1 + 6 * sol2, cst1, 1E-12);
Assert.assertEquals(4 * sol0 - 3 * sol1 -5 * sol2, cst2, 1E-12);
} }
// test submatrix accessors // test submatrix accessors

View File

@ -482,12 +482,17 @@ public final class BlockRealMatrixTest {
// Solve example // Solve example
double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}}; double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}};
RealMatrix coefficients = new BlockRealMatrix(coefficientsData); RealMatrix coefficients = new BlockRealMatrix(coefficientsData);
double[] constants = {1, -2, 1}; RealVector constants = new ArrayRealVector(new double[]{1, -2, 1}, false);
double[] solution = new LUDecompositionImpl(coefficients).getSolver().solve(constants); RealVector solution = new LUDecompositionImpl(coefficients).getSolver().solve(constants);
Assert.assertEquals(2 * solution[0] + 3 * solution[1] -2 * solution[2], constants[0], 1E-12); final double cst0 = constants.getEntry(0);
Assert.assertEquals(-1 * solution[0] + 7 * solution[1] + 6 * solution[2], constants[1], 1E-12); final double cst1 = constants.getEntry(1);
Assert.assertEquals(4 * solution[0] - 3 * solution[1] -5 * solution[2], constants[2], 1E-12); final double cst2 = constants.getEntry(2);
final double sol0 = solution.getEntry(0);
final double sol1 = solution.getEntry(1);
final double sol2 = solution.getEntry(2);
Assert.assertEquals(2 * sol0 + 3 * sol1 -2 * sol2, cst0, 1E-12);
Assert.assertEquals(-1 * sol0 + 7 * sol1 + 6 * sol2, cst1, 1E-12);
Assert.assertEquals(4 * sol0 - 3 * sol1 -5 * sol2, cst2, 1E-12);
} }
// test submatrix accessors // test submatrix accessors

View File

@ -387,14 +387,17 @@ public final class SparseRealMatrixTest {
double[][] coefficientsData = { { 2, 3, -2 }, { -1, 7, 6 }, double[][] coefficientsData = { { 2, 3, -2 }, { -1, 7, 6 },
{ 4, -3, -5 } }; { 4, -3, -5 } };
RealMatrix coefficients = createSparseMatrix(coefficientsData); RealMatrix coefficients = createSparseMatrix(coefficientsData);
double[] constants = { 1, -2, 1 }; RealVector constants = new ArrayRealVector(new double[]{ 1, -2, 1 }, false);
double[] solution = new LUDecompositionImpl(coefficients).getSolver().solve(constants); RealVector solution = new LUDecompositionImpl(coefficients).getSolver().solve(constants);
Assert.assertEquals(2 * solution[0] + 3 * solution[1] - 2 * solution[2], final double cst0 = constants.getEntry(0);
constants[0], 1E-12); final double cst1 = constants.getEntry(1);
Assert.assertEquals(-1 * solution[0] + 7 * solution[1] + 6 * solution[2], final double cst2 = constants.getEntry(2);
constants[1], 1E-12); final double sol0 = solution.getEntry(0);
Assert.assertEquals(4 * solution[0] - 3 * solution[1] - 5 * solution[2], final double sol1 = solution.getEntry(1);
constants[2], 1E-12); final double sol2 = solution.getEntry(2);
Assert.assertEquals(2 * sol0 + 3 * sol1 - 2 * sol2, cst0, 1E-12);
Assert.assertEquals(-1 * sol0 + 7 * sol1 + 6 * sol2, cst1, 1E-12);
Assert.assertEquals(4 * sol0 - 3 * sol1 - 5 * sol2, cst2, 1E-12);
} }