You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ps...@apache.org on 2009/09/05 19:37:05 UTC
svn commit: r811685 [18/24] - in /commons/proper/math/trunk: ./
src/main/java/org/apache/commons/math/
src/main/java/org/apache/commons/math/analysis/
src/main/java/org/apache/commons/math/analysis/integration/
src/main/java/org/apache/commons/math/ana...
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/Array2DRowRealMatrixTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/Array2DRowRealMatrixTest.java?rev=811685&r1=811684&r2=811685&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/Array2DRowRealMatrixTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/Array2DRowRealMatrixTest.java Sat Sep 5 17:36:48 2009
@@ -5,9 +5,9 @@
* 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.
@@ -29,45 +29,45 @@
*/
public final class Array2DRowRealMatrixTest extends TestCase {
-
+
// 3 x 3 identity matrix
protected double[][] id = { {1d,0d,0d}, {0d,1d,0d}, {0d,0d,1d} };
-
+
// Test data for group operations
protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} };
protected double[][] testDataLU = {{2d, 5d, 3d}, {.5d, -2.5d, 6.5d}, {0.5d, 0.2d, .2d}};
protected double[][] testDataPlus2 = { {3d,4d,5d}, {4d,7d,5d}, {3d,2d,10d} };
- protected double[][] testDataMinus = { {-1d,-2d,-3d}, {-2d,-5d,-3d},
+ protected double[][] testDataMinus = { {-1d,-2d,-3d}, {-2d,-5d,-3d},
{-1d,0d,-8d} };
protected double[] testDataRow1 = {1d,2d,3d};
protected double[] testDataCol3 = {3d,3d,8d};
- protected double[][] testDataInv =
+ protected double[][] testDataInv =
{ {-40d,16d,9d}, {13d,-5d,-3d}, {5d,-2d,-1d} };
protected double[] preMultTest = {8,12,33};
protected double[][] testData2 ={ {1d,2d,3d}, {2d,5d,3d}};
protected double[][] testData2T = { {1d,2d}, {2d,5d}, {3d,3d}};
- protected double[][] testDataPlusInv =
+ protected double[][] testDataPlusInv =
{ {-39d,18d,12d}, {15d,0d,0d}, {6d,-2d,7d} };
-
+
// lu decomposition tests
protected double[][] luData = { {2d,3d,3d}, {0d,5d,7d}, {6d,9d,8d} };
protected double[][] luDataLUDecomposition = { {6d,9d,8d}, {0d,5d,7d},
{0.33333333333333,0d,0.33333333333333} };
-
+
// singular matrices
protected double[][] singular = { {2d,3d}, {2d,3d} };
protected double[][] bigSingular = {{1d,2d,3d,4d}, {2d,5d,3d,4d},
{7d,3d,256d,1930d}, {3d,7d,6d,8d}}; // 4th row = 1st + 2nd
protected double[][] detData = { {1d,2d,3d}, {4d,5d,6d}, {7d,8d,10d} };
protected double[][] detData2 = { {1d, 3d}, {2d, 4d}};
-
+
// vectors
protected double[] testVector = {1,2,3};
protected double[] testVector2 = {1,2,3,4};
-
+
// submatrix accessor tests
protected double[][] subTestData = {{1, 2, 3, 4}, {1.5, 2.5, 3.5, 4.5},
- {2, 4, 6, 8}, {4, 5, 6, 7}};
+ {2, 4, 6, 8}, {4, 5, 6, 7}};
// array selections
protected double[][] subRows02Cols13 = { {2, 4}, {4, 8}};
protected double[][] subRows03Cols12 = { {2, 3}, {5, 6}};
@@ -85,21 +85,21 @@
// column matrices
protected double[][] subColumn1 = {{2}, {2.5}, {4}, {5}};
protected double[][] subColumn3 = {{4}, {4.5}, {8}, {7}};
-
+
// tolerances
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
-
+
public Array2DRowRealMatrixTest(String name) {
super(name);
}
-
+
public static Test suite() {
TestSuite suite = new TestSuite(Array2DRowRealMatrixTest.class);
suite.setName("Array2DRowRealMatrix Tests");
return suite;
}
-
+
/** test dimensions */
public void testDimensions() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
@@ -110,8 +110,8 @@
assertEquals("testData2 row dimension",m2.getRowDimension(),2);
assertEquals("testData2 column dimension",m2.getColumnDimension(),3);
assertTrue("testData2 is not square",!m2.isSquare());
- }
-
+ }
+
/** test copy functions */
public void testCopyFunctions() {
Array2DRowRealMatrix m1 = new Array2DRowRealMatrix(testData);
@@ -120,8 +120,8 @@
Array2DRowRealMatrix m3 = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix m4 = new Array2DRowRealMatrix(m3.getData(), false);
assertEquals(m4,m3);
- }
-
+ }
+
/** test add */
public void testAdd() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
@@ -134,9 +134,9 @@
testDataPlusInv[row][col],sumEntries[row][col],
entryTolerance);
}
- }
+ }
}
-
+
/** test add failure */
public void testAddFail() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
@@ -148,7 +148,7 @@
// ignored
}
}
-
+
/** test norm */
public void testNorm() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
@@ -156,7 +156,7 @@
assertEquals("testData norm",14d,m.getNorm(),entryTolerance);
assertEquals("testData2 norm",7d,m2.getNorm(),entryTolerance);
}
-
+
/** test Frobenius norm */
public void testFrobeniusNorm() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
@@ -164,21 +164,21 @@
assertEquals("testData Frobenius norm", Math.sqrt(117.0), m.getFrobeniusNorm(), entryTolerance);
assertEquals("testData2 Frobenius norm", Math.sqrt(52.0), m2.getFrobeniusNorm(), entryTolerance);
}
-
+
/** test m-n = m + -n */
public void testPlusMinus() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(testDataInv);
TestUtils.assertEquals("m-n = m + -n",m.subtract(m2),
- m2.scalarMultiply(-1d).add(m),entryTolerance);
+ m2.scalarMultiply(-1d).add(m),entryTolerance);
try {
m.subtract(new Array2DRowRealMatrix(testData2));
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
// ignored
- }
+ }
}
-
+
/** test multiply */
public void testMultiply() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
@@ -194,28 +194,28 @@
TestUtils.assertEquals("identity multiply",identity.multiply(mInv),
mInv,entryTolerance);
TestUtils.assertEquals("identity multiply",m2.multiply(identity),
- m2,entryTolerance);
+ m2,entryTolerance);
try {
m.multiply(new Array2DRowRealMatrix(bigSingular));
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
// ignored
- }
- }
-
+ }
+ }
+
//Additional Test for Array2DRowRealMatrixTest.testMultiply
private double[][] d3 = new double[][] {{1,2,3,4},{5,6,7,8}};
private double[][] d4 = new double[][] {{1},{2},{3},{4}};
private double[][] d5 = new double[][] {{30},{70}};
-
- public void testMultiply2() {
- RealMatrix m3 = new Array2DRowRealMatrix(d3);
+
+ public void testMultiply2() {
+ RealMatrix m3 = new Array2DRowRealMatrix(d3);
RealMatrix m4 = new Array2DRowRealMatrix(d4);
RealMatrix m5 = new Array2DRowRealMatrix(d5);
TestUtils.assertEquals("m3*m4=m5", m3.multiply(m4), m5, entryTolerance);
- }
-
+ }
+
/** test trace */
public void testTrace() {
RealMatrix m = new Array2DRowRealMatrix(id);
@@ -226,16 +226,16 @@
fail("Expecting NonSquareMatrixException");
} catch (NonSquareMatrixException ex) {
// ignored
- }
+ }
}
-
+
/** test sclarAdd */
public void testScalarAdd() {
RealMatrix m = new Array2DRowRealMatrix(testData);
TestUtils.assertEquals("scalar add",new Array2DRowRealMatrix(testDataPlus2),
m.scalarAdd(2d),entryTolerance);
}
-
+
/** test operate */
public void testOperate() {
RealMatrix m = new Array2DRowRealMatrix(id);
@@ -249,7 +249,7 @@
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
// ignored
- }
+ }
}
/** test issue MATH-209 */
@@ -263,10 +263,10 @@
assertEquals( 7.0, b[1], 1.0e-12);
assertEquals(11.0, b[2], 1.0e-12);
}
-
+
/** test transpose */
public void testTranspose() {
- RealMatrix m = new Array2DRowRealMatrix(testData);
+ RealMatrix m = new Array2DRowRealMatrix(testData);
RealMatrix mIT = new LUDecompositionImpl(m).getSolver().getInverse().transpose();
RealMatrix mTI = new LUDecompositionImpl(m.transpose()).getSolver().getInverse();
TestUtils.assertEquals("inverse-transpose", mIT, mTI, normTolerance);
@@ -274,7 +274,7 @@
RealMatrix mt = new Array2DRowRealMatrix(testData2T);
TestUtils.assertEquals("transpose",mt,m.transpose(),normTolerance);
}
-
+
/** test preMultiply by vector */
public void testPremultiplyVector() {
RealMatrix m = new Array2DRowRealMatrix(testData);
@@ -290,13 +290,13 @@
// ignored
}
}
-
+
public void testPremultiply() {
- RealMatrix m3 = new Array2DRowRealMatrix(d3);
+ RealMatrix m3 = new Array2DRowRealMatrix(d3);
RealMatrix m4 = new Array2DRowRealMatrix(d4);
RealMatrix m5 = new Array2DRowRealMatrix(d5);
TestUtils.assertEquals("m3*m4=m5", m4.preMultiply(m3), m5, entryTolerance);
-
+
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix mInv = new Array2DRowRealMatrix(testDataInv);
Array2DRowRealMatrix identity = new Array2DRowRealMatrix(id);
@@ -313,9 +313,9 @@
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
// ignored
- }
+ }
}
-
+
public void testGetVectors() {
RealMatrix m = new Array2DRowRealMatrix(testData);
TestUtils.assertEquals("get row",m.getRow(0),testDataRow1,entryTolerance);
@@ -333,7 +333,7 @@
// ignored
}
}
-
+
public void testGetEntry() {
RealMatrix m = new Array2DRowRealMatrix(testData);
assertEquals("get entry",m.getEntry(0,1),2d,entryTolerance);
@@ -344,7 +344,7 @@
// expected
}
}
-
+
/** test examples in user guide */
public void testExamples() {
// Create a real matrix with two rows and three columns
@@ -358,10 +358,10 @@
assertEquals(2, p.getRowDimension());
assertEquals(2, p.getColumnDimension());
// Invert p
- RealMatrix pInverse = new LUDecompositionImpl(p).getSolver().getInverse();
+ RealMatrix pInverse = new LUDecompositionImpl(p).getSolver().getInverse();
assertEquals(2, pInverse.getRowDimension());
assertEquals(2, pInverse.getColumnDimension());
-
+
// Solve example
double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}};
RealMatrix coefficients = new Array2DRowRealMatrix(coefficientsData);
@@ -369,22 +369,22 @@
double[] solution = new LUDecompositionImpl(coefficients).getSolver().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);
-
+ assertEquals(4 * solution[0] - 3 * solution[1] -5 * solution[2], constants[2], 1E-12);
+
}
-
+
// test submatrix accessors
public void testGetSubMatrix() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0, false);
checkGetSubMatrix(m, subRows00Cols33, 0 , 0 , 3, 3, false);
- checkGetSubMatrix(m, subRows01Cols23, 0 , 1 , 2, 3, false);
- checkGetSubMatrix(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }, false);
- checkGetSubMatrix(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }, false);
- checkGetSubMatrix(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }, false);
- checkGetSubMatrix(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }, false);
- checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false);
- checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false);
+ checkGetSubMatrix(m, subRows01Cols23, 0 , 1 , 2, 3, false);
+ checkGetSubMatrix(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }, false);
+ checkGetSubMatrix(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }, false);
+ checkGetSubMatrix(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }, false);
+ checkGetSubMatrix(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }, false);
+ checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false);
+ checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false);
checkGetSubMatrix(m, null, 1, 0, 2, 4, true);
checkGetSubMatrix(m, null, -1, 1, 2, 2, true);
checkGetSubMatrix(m, null, 1, 0, 2, 2, true);
@@ -408,7 +408,7 @@
}
}
}
-
+
private void checkGetSubMatrix(RealMatrix m, double[][] reference,
int[] selectedRows, int[] selectedColumns,
boolean mustFail) {
@@ -429,14 +429,14 @@
RealMatrix m = new Array2DRowRealMatrix(subTestData);
checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0, false);
checkCopy(m, subRows00Cols33, 0 , 0 , 3, 3, false);
- checkCopy(m, subRows01Cols23, 0 , 1 , 2, 3, false);
- checkCopy(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }, false);
- checkCopy(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }, false);
- checkCopy(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }, false);
- checkCopy(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }, false);
- checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false);
- checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false);
-
+ checkCopy(m, subRows01Cols23, 0 , 1 , 2, 3, false);
+ checkCopy(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }, false);
+ checkCopy(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }, false);
+ checkCopy(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }, false);
+ checkCopy(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }, false);
+ checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false);
+ checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false);
+
checkCopy(m, null, 1, 0, 2, 4, true);
checkCopy(m, null, -1, 1, 2, 2, true);
checkCopy(m, null, 1, 0, 2, 2, true);
@@ -463,7 +463,7 @@
}
}
}
-
+
private void checkCopy(RealMatrix m, double[][] reference,
int[] selectedRows, int[] selectedColumns,
boolean mustFail) {
@@ -487,9 +487,9 @@
RealMatrix m = new Array2DRowRealMatrix(subTestData);
RealMatrix mRow0 = new Array2DRowRealMatrix(subRow0);
RealMatrix mRow3 = new Array2DRowRealMatrix(subRow3);
- assertEquals("Row0", mRow0,
+ assertEquals("Row0", mRow0,
m.getRowMatrix(0));
- assertEquals("Row3", mRow3,
+ assertEquals("Row3", mRow3,
m.getRowMatrix(3));
try {
m.getRowMatrix(-1);
@@ -504,7 +504,7 @@
// expected
}
}
-
+
public void testSetRowMatrix() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
RealMatrix mRow3 = new Array2DRowRealMatrix(subRow3);
@@ -524,14 +524,14 @@
// expected
}
}
-
+
public void testGetColumnMatrix() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
RealMatrix mColumn1 = new Array2DRowRealMatrix(subColumn1);
RealMatrix mColumn3 = new Array2DRowRealMatrix(subColumn3);
- assertEquals("Column1", mColumn1,
+ assertEquals("Column1", mColumn1,
m.getColumnMatrix(1));
- assertEquals("Column3", mColumn3,
+ assertEquals("Column3", mColumn3,
m.getColumnMatrix(3));
try {
m.getColumnMatrix(-1);
@@ -606,7 +606,7 @@
// expected
}
}
-
+
public void testGetColumnVector() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
RealVector mColumn1 = columnToVector(subColumn1);
@@ -691,7 +691,7 @@
// expected
}
}
-
+
public void testGetColumn() {
RealMatrix m = new Array2DRowRealMatrix(subTestData);
double[] mColumn1 = columnToArray(subColumn1);
@@ -743,10 +743,10 @@
private void checkArrays(double[] expected, double[] actual) {
assertEquals(expected.length, actual.length);
for (int i = 0; i < expected.length; ++i) {
- assertEquals(expected[i], actual[i]);
+ assertEquals(expected[i], actual[i]);
}
}
-
+
public void testEqualsAndHashCode() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
Array2DRowRealMatrix m1 = (Array2DRowRealMatrix) m.copy();
@@ -757,9 +757,9 @@
assertEquals(m, m1);
assertFalse(m.equals(null));
assertFalse(m.equals(mt));
- assertFalse(m.equals(new Array2DRowRealMatrix(bigSingular)));
+ assertFalse(m.equals(new Array2DRowRealMatrix(bigSingular)));
}
-
+
public void testToString() {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
assertEquals("Array2DRowRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
@@ -768,45 +768,45 @@
assertEquals("Array2DRowRealMatrix{}",
m.toString());
}
-
+
public void testSetSubMatrix() throws Exception {
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
m.setSubMatrix(detData2,1,1);
RealMatrix expected = MatrixUtils.createRealMatrix
(new double[][] {{1.0,2.0,3.0},{2.0,1.0,3.0},{1.0,2.0,4.0}});
- assertEquals(expected, m);
-
+ assertEquals(expected, m);
+
m.setSubMatrix(detData2,0,0);
expected = MatrixUtils.createRealMatrix
(new double[][] {{1.0,3.0,3.0},{2.0,4.0,3.0},{1.0,2.0,4.0}});
- assertEquals(expected, m);
-
- m.setSubMatrix(testDataPlus2,0,0);
+ assertEquals(expected, m);
+
+ m.setSubMatrix(testDataPlus2,0,0);
expected = MatrixUtils.createRealMatrix
(new double[][] {{3.0,4.0,5.0},{4.0,7.0,5.0},{3.0,2.0,10.0}});
- assertEquals(expected, m);
-
+ assertEquals(expected, m);
+
// dimension overflow
- try {
+ try {
m.setSubMatrix(testData,1,1);
fail("expecting MatrixIndexException");
} catch (MatrixIndexException e) {
// expected
}
// dimension underflow
- try {
+ try {
m.setSubMatrix(testData,-1,1);
fail("expecting MatrixIndexException");
} catch (MatrixIndexException e) {
// expected
}
- try {
+ try {
m.setSubMatrix(testData,1,-1);
fail("expecting MatrixIndexException");
} catch (MatrixIndexException e) {
// expected
}
-
+
// null
try {
m.setSubMatrix(null,1,1);
@@ -827,7 +827,7 @@
} catch (IllegalStateException e) {
// expected
}
-
+
// ragged
try {
m.setSubMatrix(new double[][] {{1}, {2, 3}}, 0, 0);
@@ -835,7 +835,7 @@
} catch (IllegalArgumentException e) {
// expected
}
-
+
// empty
try {
m.setSubMatrix(new double[][] {{}}, 0, 0);
@@ -843,7 +843,7 @@
} catch (IllegalArgumentException e) {
// expected
}
-
+
}
public void testWalk() {
@@ -862,11 +862,11 @@
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- assertEquals(0.0, m.getEntry(i, 0), 0);
+ assertEquals(0.0, m.getEntry(i, 0), 0);
assertEquals(0.0, m.getEntry(i, columns - 1), 0);
}
for (int j = 0; j < columns; ++j) {
- assertEquals(0.0, m.getEntry(0, j), 0);
+ assertEquals(0.0, m.getEntry(0, j), 0);
assertEquals(0.0, m.getEntry(rows - 1, j), 0);
}
@@ -882,11 +882,11 @@
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- assertEquals(0.0, m.getEntry(i, 0), 0);
+ assertEquals(0.0, m.getEntry(i, 0), 0);
assertEquals(0.0, m.getEntry(i, columns - 1), 0);
}
for (int j = 0; j < columns; ++j) {
- assertEquals(0.0, m.getEntry(0, j), 0);
+ assertEquals(0.0, m.getEntry(0, j), 0);
assertEquals(0.0, m.getEntry(rows - 1, j), 0);
}
@@ -902,11 +902,11 @@
m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- assertEquals(0.0, m.getEntry(i, 0), 0);
+ assertEquals(0.0, m.getEntry(i, 0), 0);
assertEquals(0.0, m.getEntry(i, columns - 1), 0);
}
for (int j = 0; j < columns; ++j) {
- assertEquals(0.0, m.getEntry(0, j), 0);
+ assertEquals(0.0, m.getEntry(0, j), 0);
assertEquals(0.0, m.getEntry(rows - 1, j), 0);
}
@@ -922,11 +922,11 @@
m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- assertEquals(0.0, m.getEntry(i, 0), 0);
+ assertEquals(0.0, m.getEntry(i, 0), 0);
assertEquals(0.0, m.getEntry(i, columns - 1), 0);
}
for (int j = 0; j < columns; ++j) {
- assertEquals(0.0, m.getEntry(0, j), 0);
+ assertEquals(0.0, m.getEntry(0, j), 0);
assertEquals(0.0, m.getEntry(rows - 1, j), 0);
}
@@ -936,8 +936,8 @@
Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData);
assertEquals(m,TestUtils.serializeAndRecover(m));
}
-
-
+
+
private static class SetVisitor extends DefaultRealMatrixChangingVisitor {
@Override
public double visit(int i, int j, double value) {
@@ -958,14 +958,14 @@
}
//--------------- -----------------Protected methods
-
+
/** extracts the l and u matrices from compact lu representation */
- protected void splitLU(RealMatrix lu, double[][] lowerData, double[][] upperData) throws InvalidMatrixException {
+ protected void splitLU(RealMatrix lu, double[][] lowerData, double[][] upperData) throws InvalidMatrixException {
if (!lu.isSquare() || lowerData.length != lowerData[0].length || upperData.length != upperData[0].length ||
lowerData.length != upperData.length
|| lowerData.length != lu.getRowDimension()) {
throw new InvalidMatrixException("incorrect dimensions");
- }
+ }
int n = lu.getRowDimension();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
@@ -978,11 +978,11 @@
} else {
lowerData[i][j] = 0d;
upperData[i][j] = lu.getEntry(i, j);
- }
+ }
}
}
}
-
+
/** Returns the result of applying the given row permutation to the matrix */
protected RealMatrix permuteRows(RealMatrix matrix, int[] permutation) {
if (!matrix.isSquare() || matrix.getRowDimension() != permutation.length) {
@@ -998,7 +998,7 @@
}
return new Array2DRowRealMatrix(out);
}
-
+
// /** Useful for debugging */
// private void dumpMatrix(RealMatrix m) {
// for (int i = 0; i < m.getRowDimension(); i++) {
@@ -1009,6 +1009,6 @@
// System.out.println(os);
// }
// }
-
+
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java?rev=811685&r1=811684&r2=811685&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayFieldVectorTest.java Sat Sep 5 17:36:48 2009
@@ -5,9 +5,9 @@
* 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.
@@ -36,7 +36,7 @@
*/
public class ArrayFieldVectorTest extends TestCase {
- //
+ //
protected Fraction[][] ma1 = {
{new Fraction(1), new Fraction(2), new Fraction(3)},
{new Fraction(4), new Fraction(5), new Fraction(6)},
@@ -58,7 +58,7 @@
{new Fraction(7), new Fraction(8), new Fraction(9)}
};
- // Testclass to test the FieldVector<Fraction> interface
+ // Testclass to test the FieldVector<Fraction> interface
// only with enough content to support the test
public static class FieldVectorTestImpl<T extends FieldElement<T>>
implements FieldVector<T>, Serializable {
@@ -340,7 +340,7 @@
ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(vec1);
ArrayFieldVector<Fraction> v2 = new ArrayFieldVector<Fraction>(vec2);
ArrayFieldVector<Fraction> v4 = new ArrayFieldVector<Fraction>(vec4);
- FieldVectorTestImpl<Fraction> v2_t = new FieldVectorTestImpl<Fraction>(vec2);
+ FieldVectorTestImpl<Fraction> v2_t = new FieldVectorTestImpl<Fraction>(vec2);
FieldVector<Fraction> v_append_1 = v1.append(v2);
assertEquals(6, v_append_1.getDimension());
@@ -436,7 +436,7 @@
}
- ArrayFieldVector<Fraction> vout10 = (ArrayFieldVector<Fraction>) v1.copy();
+ ArrayFieldVector<Fraction> vout10 = (ArrayFieldVector<Fraction>) v1.copy();
ArrayFieldVector<Fraction> vout10_2 = (ArrayFieldVector<Fraction>) v1.copy();
assertEquals(vout10, vout10_2);
vout10_2.setEntry(0, new Fraction(11, 10));
@@ -444,7 +444,7 @@
}
- public void testMapFunctions() {
+ public void testMapFunctions() {
ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(vec1);
//octave = v1 .+ 2.0
@@ -504,12 +504,12 @@
}
- public void testBasicFunctions() {
+ public void testBasicFunctions() {
ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(vec1);
ArrayFieldVector<Fraction> v2 = new ArrayFieldVector<Fraction>(vec2);
new ArrayFieldVector<Fraction>(vec_null);
- FieldVectorTestImpl<Fraction> v2_t = new FieldVectorTestImpl<Fraction>(vec2);
+ FieldVectorTestImpl<Fraction> v2_t = new FieldVectorTestImpl<Fraction>(vec2);
//octave = v1 + v2
ArrayFieldVector<Fraction> v_add = v1.add(v2);
@@ -570,46 +570,46 @@
Fraction[] result_projection_2 = {new Fraction(128, 77), new Fraction(160, 77), new Fraction(192, 77)};
checkArray("compare vect", v_projection_2.getData(), result_projection_2);
- }
+ }
- public void testMisc() {
+ public void testMisc() {
ArrayFieldVector<Fraction> v1 = new ArrayFieldVector<Fraction>(vec1);
ArrayFieldVector<Fraction> v4 = new ArrayFieldVector<Fraction>(vec4);
FieldVector<Fraction> v4_2 = new ArrayFieldVector<Fraction>(vec4);
String out1 = v1.toString();
assertTrue("some output ", out1.length()!=0);
- /*
+ /*
Fraction[] dout1 = v1.copyOut();
assertEquals(3, dout1.length);
- assertNotSame("testData not same object ", v1.data, dout1);
- */
+ assertNotSame("testData not same object ", v1.data, dout1);
+ */
try {
- v1.checkVectorDimensions(2);
+ v1.checkVectorDimensions(2);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
// expected behavior
} catch (Exception e) {
fail("wrong exception caught");
- }
+ }
try {
- v1.checkVectorDimensions(v4);
+ v1.checkVectorDimensions(v4);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
// expected behavior
} catch (Exception e) {
fail("wrong exception caught");
- }
+ }
try {
- v1.checkVectorDimensions(v4_2);
+ v1.checkVectorDimensions(v4_2);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
// expected behavior
} catch (Exception e) {
fail("wrong exception caught");
- }
+ }
}
@@ -617,7 +617,7 @@
ArrayFieldVector<Fraction> v = new ArrayFieldVector<Fraction>(vec1);
assertEquals(v,TestUtils.serializeAndRecover(v));
}
-
+
/** verifies that two vectors are equals */
protected void checkArray(String msg, Fraction[] m, Fraction[] n) {
if (m.length != n.length) {
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java?rev=811685&r1=811684&r2=811685&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/ArrayRealVectorTest.java Sat Sep 5 17:36:48 2009
@@ -5,9 +5,9 @@
* 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.
@@ -31,7 +31,7 @@
*/
public class ArrayRealVectorTest extends TestCase {
- //
+ //
protected double[][] ma1 = {{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}};
protected double[] vec1 = {1d, 2d, 3d};
protected double[] vec2 = {4d, 5d, 6d};
@@ -45,7 +45,7 @@
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
- // Testclass to test the RealVector interface
+ // Testclass to test the RealVector interface
// only with enough content to support the test
public static class RealVectorTestImpl implements RealVector, Serializable {
@@ -547,7 +547,7 @@
ArrayRealVector v1 = new ArrayRealVector(vec1);
ArrayRealVector v2 = new ArrayRealVector(vec2);
ArrayRealVector v4 = new ArrayRealVector(vec4);
- RealVectorTestImpl v2_t = new RealVectorTestImpl(vec2);
+ RealVectorTestImpl v2_t = new RealVectorTestImpl(vec2);
RealVector v_append_1 = v1.append(v2);
assertEquals("testData len", 6, v_append_1.getDimension());
@@ -643,7 +643,7 @@
}
- ArrayRealVector vout10 = (ArrayRealVector) v1.copy();
+ ArrayRealVector vout10 = (ArrayRealVector) v1.copy();
ArrayRealVector vout10_2 = (ArrayRealVector) v1.copy();
assertEquals(vout10, vout10_2);
vout10_2.setEntry(0, 1.1);
@@ -651,7 +651,7 @@
}
- public void testMapFunctions() {
+ public void testMapFunctions() {
ArrayRealVector v1 = new ArrayRealVector(vec1);
//octave = v1 .+ 2.0
@@ -852,7 +852,7 @@
//octave = asin(vat)
RealVector v_mapAsinToSelf = vat.copy();
- v_mapAsinToSelf.mapAsinToSelf();
+ v_mapAsinToSelf.mapAsinToSelf();
double[] result_mapAsinToSelf = {0.0d,5.235987755982989e-01d,1.570796326794897e+00d};
assertClose("compare vectors" ,result_mapAsinToSelf,v_mapAsinToSelf.getData(),normTolerance);
@@ -979,12 +979,12 @@
}
- public void testBasicFunctions() {
+ public void testBasicFunctions() {
ArrayRealVector v1 = new ArrayRealVector(vec1);
ArrayRealVector v2 = new ArrayRealVector(vec2);
ArrayRealVector v_null = new ArrayRealVector(vec_null);
- RealVectorTestImpl v2_t = new RealVectorTestImpl(vec2);
+ RealVectorTestImpl v2_t = new RealVectorTestImpl(vec2);
//octave = sqrt(sumsq(v1))
double d_getNorm = v1.getNorm();
@@ -1070,7 +1070,7 @@
assertEquals("compare val ",4d, m_outerProduct_2.getEntry(0,0));
RealVector v_unitVector = v1.unitVector();
- RealVector v_unitVector_2 = v1.mapDivide(v1.getNorm());
+ RealVector v_unitVector_2 = v1.mapDivide(v1.getNorm());
assertClose("compare vect" ,v_unitVector.getData(),v_unitVector_2.getData(),normTolerance);
try {
@@ -1102,46 +1102,46 @@
double[] result_projection_2 = {1.662337662337662, 2.0779220779220777, 2.493506493506493};
assertClose("compare vect", v_projection_2.getData(), result_projection_2, normTolerance);
- }
+ }
- public void testMisc() {
+ public void testMisc() {
ArrayRealVector v1 = new ArrayRealVector(vec1);
ArrayRealVector v4 = new ArrayRealVector(vec4);
RealVector v4_2 = new ArrayRealVector(vec4);
String out1 = v1.toString();
assertTrue("some output ", out1.length()!=0);
- /*
+ /*
double[] dout1 = v1.copyOut();
assertEquals("testData len", 3, dout1.length);
- assertNotSame("testData not same object ", v1.data, dout1);
- */
+ assertNotSame("testData not same object ", v1.data, dout1);
+ */
try {
- v1.checkVectorDimensions(2);
+ v1.checkVectorDimensions(2);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
// expected behavior
} catch (Exception e) {
fail("wrong exception caught");
- }
+ }
try {
- v1.checkVectorDimensions(v4);
+ v1.checkVectorDimensions(v4);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
// expected behavior
} catch (Exception e) {
fail("wrong exception caught");
- }
+ }
try {
- v1.checkVectorDimensions(v4_2);
+ v1.checkVectorDimensions(v4_2);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
// expected behavior
} catch (Exception e) {
fail("wrong exception caught");
- }
+ }
}
@@ -1176,8 +1176,8 @@
ArrayRealVector v = new ArrayRealVector(new double[] { 0, 1, 2 });
assertEquals(v,TestUtils.serializeAndRecover(v));
}
-
-
+
+
/** verifies that two vectors are close (sup norm) */
protected void assertClose(String msg, double[] m, double[] n,
double tolerance) {
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BiDiagonalTransformerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BiDiagonalTransformerTest.java?rev=811685&r1=811684&r2=811685&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BiDiagonalTransformerTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BiDiagonalTransformerTest.java Sat Sep 5 17:36:48 2009
@@ -92,7 +92,7 @@
private void checkOrthogonal(RealMatrix m) {
RealMatrix mTm = m.transpose().multiply(m);
RealMatrix id = MatrixUtils.createRealIdentityMatrix(mTm.getRowDimension());
- assertEquals(0, mTm.subtract(id).getNorm(), 1.0e-14);
+ assertEquals(0, mTm.subtract(id).getNorm(), 1.0e-14);
}
public void testBBiDiagonal() {
@@ -109,7 +109,7 @@
if (rows < cols) {
if ((i < j) || (i > j + 1)) {
assertEquals(0, m.getEntry(i, j), 1.0e-16);
- }
+ }
} else {
if ((i < j - 1) || (i > j)) {
assertEquals(0, m.getEntry(i, j), 1.0e-16);
@@ -148,7 +148,7 @@
assertTrue(u == transformer.getU());
assertTrue(b == transformer.getB());
assertTrue(v == transformer.getV());
-
+
}
public void testUpperOrLower() {
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BigMatrixImplTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BigMatrixImplTest.java?rev=811685&r1=811684&r2=811685&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BigMatrixImplTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BigMatrixImplTest.java Sat Sep 5 17:36:48 2009
@@ -5,9 +5,9 @@
* 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.
@@ -30,48 +30,48 @@
*/
@Deprecated
public final class BigMatrixImplTest extends TestCase {
-
+
// Test data for String constructors
protected String[][] testDataString = { {"1","2","3"}, {"2","5","3"}, {"1","0","8"} };
-
+
// 3 x 3 identity matrix
protected double[][] id = { {1d,0d,0d}, {0d,1d,0d}, {0d,0d,1d} };
-
+
// Test data for group operations
protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} };
protected double[][] testDataLU = {{2d, 5d, 3d}, {.5d, -2.5d, 6.5d}, {0.5d, 0.2d, .2d}};
protected double[][] testDataPlus2 = { {3d,4d,5d}, {4d,7d,5d}, {3d,2d,10d} };
- protected double[][] testDataMinus = { {-1d,-2d,-3d}, {-2d,-5d,-3d},
+ protected double[][] testDataMinus = { {-1d,-2d,-3d}, {-2d,-5d,-3d},
{-1d,0d,-8d} };
protected double[] testDataRow1 = {1d,2d,3d};
protected double[] testDataCol3 = {3d,3d,8d};
- protected double[][] testDataInv =
+ protected double[][] testDataInv =
{ {-40d,16d,9d}, {13d,-5d,-3d}, {5d,-2d,-1d} };
protected double[] preMultTest = {8,12,33};
protected double[][] testData2 ={ {1d,2d,3d}, {2d,5d,3d}};
protected double[][] testData2T = { {1d,2d}, {2d,5d}, {3d,3d}};
- protected double[][] testDataPlusInv =
+ protected double[][] testDataPlusInv =
{ {-39d,18d,12d}, {15d,0d,0d}, {6d,-2d,7d} };
-
+
// lu decomposition tests
protected double[][] luData = { {2d,3d,3d}, {0d,5d,7d}, {6d,9d,8d} };
protected double[][] luDataLUDecomposition = { {6d,9d,8d}, {0d,5d,7d},
{0.33333333333333,0d,0.33333333333333} };
-
+
// singular matrices
protected double[][] singular = { {2d,3d}, {2d,3d} };
protected double[][] bigSingular = {{1d,2d,3d,4d}, {2d,5d,3d,4d},
{7d,3d,256d,1930d}, {3d,7d,6d,8d}}; // 4th row = 1st + 2nd
protected double[][] detData = { {1d,2d,3d}, {4d,5d,6d}, {7d,8d,10d} };
protected double[][] detData2 = { {1d, 3d}, {2d, 4d}};
-
+
// vectors
protected double[] testVector = {1,2,3};
protected double[] testVector2 = {1,2,3,4};
-
+
// submatrix accessor tests
protected double[][] subTestData = {{1, 2, 3, 4}, {1.5, 2.5, 3.5, 4.5},
- {2, 4, 6, 8}, {4, 5, 6, 7}};
+ {2, 4, 6, 8}, {4, 5, 6, 7}};
// array selections
protected double[][] subRows02Cols13 = { {2, 4}, {4, 8}};
protected double[][] subRows03Cols12 = { {2, 3}, {5, 6}};
@@ -89,15 +89,15 @@
// column matrices
protected double[][] subColumn1 = {{2}, {2.5}, {4}, {5}};
protected double[][] subColumn3 = {{4}, {4.5}, {8}, {7}};
-
+
// tolerances
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
-
+
public BigMatrixImplTest(String name) {
super(name);
}
-
+
public static Test suite() {
TestSuite suite = new TestSuite(BigMatrixImplTest.class);
suite.setName("BigMatrixImpl Tests");
@@ -149,8 +149,8 @@
assertEquals("testData2 row dimension",m2.getRowDimension(),2);
assertEquals("testData2 column dimension",m2.getColumnDimension(),3);
assertTrue("testData2 is not square",!m2.isSquare());
- }
-
+ }
+
/** test copy functions */
public void testCopyFunctions() {
BigMatrixImpl m1 = new BigMatrixImpl(testData);
@@ -160,7 +160,7 @@
BigMatrixImpl m4 = new BigMatrixImpl(m3.getData(), false);
assertEquals(m4,m3);
}
-
+
/** test constructors */
public void testConstructors() {
BigMatrix m1 = new BigMatrixImpl(testData);
@@ -211,7 +211,7 @@
// expected
}
}
-
+
/** test add */
public void testAdd() {
BigMatrixImpl m = new BigMatrixImpl(testData);
@@ -224,9 +224,9 @@
testDataPlusInv[row][col],sumEntries[row][col],
entryTolerance);
}
- }
+ }
}
-
+
/** test add failure */
public void testAddFail() {
BigMatrixImpl m = new BigMatrixImpl(testData);
@@ -238,7 +238,7 @@
// ignored
}
}
-
+
/** test norm */
public void testNorm() {
BigMatrixImpl m = new BigMatrixImpl(testData);
@@ -246,7 +246,7 @@
assertEquals("testData norm",14d,m.getNorm().doubleValue(),entryTolerance);
assertEquals("testData2 norm",7d,m2.getNorm().doubleValue(),entryTolerance);
}
-
+
/** test m-n = m + -n */
public void testPlusMinus() {
BigMatrixImpl m = new BigMatrixImpl(testData);
@@ -258,9 +258,9 @@
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
// ignored
- }
+ }
}
-
+
/** test multiply */
public void testMultiply() {
BigMatrixImpl m = new BigMatrixImpl(testData);
@@ -276,28 +276,28 @@
assertClose("identity multiply",identity.multiply(mInv),
mInv,entryTolerance);
assertClose("identity multiply",m2.multiply(identity),
- m2,entryTolerance);
+ m2,entryTolerance);
try {
m.multiply(new BigMatrixImpl(bigSingular));
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
// ignored
- }
- }
-
+ }
+ }
+
//Additional Test for BigMatrixImplTest.testMultiply
private double[][] d3 = new double[][] {{1,2,3,4},{5,6,7,8}};
private double[][] d4 = new double[][] {{1},{2},{3},{4}};
private double[][] d5 = new double[][] {{30},{70}};
-
- public void testMultiply2() {
+
+ public void testMultiply2() {
BigMatrix m3 = new BigMatrixImpl(d3);
BigMatrix m4 = new BigMatrixImpl(d4);
BigMatrix m5 = new BigMatrixImpl(d5);
assertClose("m3*m4=m5", m3.multiply(m4), m5, entryTolerance);
- }
-
+ }
+
/** test isSingular */
public void testIsSingular() {
BigMatrixImpl m = new BigMatrixImpl(singular);
@@ -309,14 +309,14 @@
m = new BigMatrixImpl(testData);
assertTrue("testData nonsingular",!m.isSingular());
}
-
+
/** test inverse */
public void testInverse() {
BigMatrixImpl m = new BigMatrixImpl(testData);
BigMatrix mInv = new BigMatrixImpl(testDataInv);
assertClose("inverse",mInv,m.inverse(),normTolerance);
assertClose("inverse^2",m,m.inverse().inverse(),10E-12);
-
+
// Not square
m = new BigMatrixImpl(testData2);
try {
@@ -325,7 +325,7 @@
} catch (InvalidMatrixException ex) {
// expected
}
-
+
// Singular
m = new BigMatrixImpl(singular);
try {
@@ -335,7 +335,7 @@
// expected
}
}
-
+
/** test solve */
public void testSolve() {
BigMatrixImpl m = new BigMatrixImpl(testData);
@@ -350,7 +350,7 @@
fail("expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// ignored
- }
+ }
BigMatrix bs = new BigMatrixImpl(bigSingular);
try {
bs.solve(bs);
@@ -369,22 +369,22 @@
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
// ignored
- }
+ }
try {
(new BigMatrixImpl(testData2)).luDecompose();
fail("Expecting InvalidMatrixException");
} catch (InvalidMatrixException ex) {
// ignored
- }
+ }
}
-
+
/** test determinant */
- public void testDeterminant() {
+ public void testDeterminant() {
BigMatrix m = new BigMatrixImpl(bigSingular);
assertEquals("singular determinant",0,m.getDeterminant().doubleValue(),0);
m = new BigMatrixImpl(detData);
assertEquals("nonsingular test",-3d,m.getDeterminant().doubleValue(),normTolerance);
-
+
// Examples verified against R (version 1.8.1, Red Hat Linux 9)
m = new BigMatrixImpl(detData2);
assertEquals("nonsingular R test 1",-2d,m.getDeterminant().doubleValue(),normTolerance);
@@ -396,9 +396,9 @@
fail("Expecting InvalidMatrixException, got " + d);
} catch (InvalidMatrixException ex) {
// ignored
- }
+ }
}
-
+
/** test trace */
public void testTrace() {
BigMatrix m = new BigMatrixImpl(id);
@@ -409,16 +409,16 @@
fail("Expecting NonSquareMatrixException, got " + t);
} catch (NonSquareMatrixException ex) {
// ignored
- }
+ }
}
-
+
/** test sclarAdd */
public void testScalarAdd() {
BigMatrix m = new BigMatrixImpl(testData);
assertClose("scalar add",new BigMatrixImpl(testDataPlus2),
m.scalarAdd(new BigDecimal(2d)),entryTolerance);
}
-
+
/** test operate */
public void testOperate() {
BigMatrix m = new BigMatrixImpl(id);
@@ -430,7 +430,7 @@
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
// ignored
- }
+ }
}
/** test issue MATH-209 */
@@ -446,7 +446,7 @@
assertEquals( 7.0, b[1].doubleValue(), 1.0e-12);
assertEquals(11.0, b[2].doubleValue(), 1.0e-12);
}
-
+
/** test transpose */
public void testTranspose() {
BigMatrix m = new BigMatrixImpl(testData);
@@ -456,7 +456,7 @@
BigMatrix mt = new BigMatrixImpl(testData2T);
assertClose("transpose",mt,m.transpose(),normTolerance);
}
-
+
/** test preMultiply by vector */
public void testPremultiplyVector() {
BigMatrix m = new BigMatrixImpl(testData);
@@ -469,13 +469,13 @@
// ignored
}
}
-
+
public void testPremultiply() {
BigMatrix m3 = new BigMatrixImpl(d3);
BigMatrix m4 = new BigMatrixImpl(d4);
BigMatrix m5 = new BigMatrixImpl(d5);
assertClose("m3*m4=m5", m4.preMultiply(m3), m5, entryTolerance);
-
+
BigMatrixImpl m = new BigMatrixImpl(testData);
BigMatrixImpl mInv = new BigMatrixImpl(testDataInv);
BigMatrixImpl identity = new BigMatrixImpl(id);
@@ -493,9 +493,9 @@
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
// ignored
- }
+ }
}
-
+
public void testGetVectors() {
BigMatrix m = new BigMatrixImpl(testData);
assertClose("get row",m.getRowAsDoubleArray(0),testDataRow1,entryTolerance);
@@ -513,7 +513,7 @@
// ignored
}
}
-
+
public void testLUDecomposition() throws Exception {
BigMatrixImpl m = new BigMatrixImpl(testData);
BigMatrix lu = m.getLUMatrix();
@@ -544,7 +544,7 @@
// expected
}
}
-
+
/**
* test submatrix accessors
*/
@@ -558,25 +558,25 @@
BigMatrix mRows03Cols123 = new BigMatrixImpl(subRows03Cols123);
BigMatrix mRows20Cols123 = new BigMatrixImpl(subRows20Cols123);
BigMatrix mRows31Cols31 = new BigMatrixImpl(subRows31Cols31);
- assertEquals("Rows23Cols00", mRows23Cols00,
+ assertEquals("Rows23Cols00", mRows23Cols00,
m.getSubMatrix(2 , 3 , 0, 0));
- assertEquals("Rows00Cols33", mRows00Cols33,
+ assertEquals("Rows00Cols33", mRows00Cols33,
m.getSubMatrix(0 , 0 , 3, 3));
assertEquals("Rows01Cols23", mRows01Cols23,
- m.getSubMatrix(0 , 1 , 2, 3));
+ m.getSubMatrix(0 , 1 , 2, 3));
assertEquals("Rows02Cols13", mRows02Cols13,
- m.getSubMatrix(new int[] {0,2}, new int[] {1,3}));
+ m.getSubMatrix(new int[] {0,2}, new int[] {1,3}));
assertEquals("Rows03Cols12", mRows03Cols12,
- m.getSubMatrix(new int[] {0,3}, new int[] {1,2}));
+ m.getSubMatrix(new int[] {0,3}, new int[] {1,2}));
assertEquals("Rows03Cols123", mRows03Cols123,
- m.getSubMatrix(new int[] {0,3}, new int[] {1,2,3}));
+ m.getSubMatrix(new int[] {0,3}, new int[] {1,2,3}));
assertEquals("Rows20Cols123", mRows20Cols123,
- m.getSubMatrix(new int[] {2,0}, new int[] {1,2,3}));
+ m.getSubMatrix(new int[] {2,0}, new int[] {1,2,3}));
assertEquals("Rows31Cols31", mRows31Cols31,
- m.getSubMatrix(new int[] {3,1}, new int[] {3,1}));
+ m.getSubMatrix(new int[] {3,1}, new int[] {3,1}));
assertEquals("Rows31Cols31", mRows31Cols31,
- m.getSubMatrix(new int[] {3,1}, new int[] {3,1}));
-
+ m.getSubMatrix(new int[] {3,1}, new int[] {3,1}));
+
try {
m.getSubMatrix(1,0,2,4);
fail("Expecting MatrixIndexException");
@@ -614,14 +614,14 @@
// expected
}
}
-
+
public void testGetColumnMatrix() {
BigMatrix m = new BigMatrixImpl(subTestData);
BigMatrix mColumn1 = new BigMatrixImpl(subColumn1);
BigMatrix mColumn3 = new BigMatrixImpl(subColumn3);
- assertEquals("Column1", mColumn1,
+ assertEquals("Column1", mColumn1,
m.getColumnMatrix(1));
- assertEquals("Column3", mColumn3,
+ assertEquals("Column3", mColumn3,
m.getColumnMatrix(3));
try {
m.getColumnMatrix(-1);
@@ -636,14 +636,14 @@
// expected
}
}
-
+
public void testGetRowMatrix() {
BigMatrix m = new BigMatrixImpl(subTestData);
BigMatrix mRow0 = new BigMatrixImpl(subRow0);
BigMatrix mRow3 = new BigMatrixImpl(subRow3);
- assertEquals("Row0", mRow0,
+ assertEquals("Row0", mRow0,
m.getRowMatrix(0));
- assertEquals("Row3", mRow3,
+ assertEquals("Row3", mRow3,
m.getRowMatrix(3));
try {
m.getRowMatrix(-1);
@@ -658,7 +658,7 @@
// expected
}
}
-
+
public void testEqualsAndHashCode() {
BigMatrixImpl m = new BigMatrixImpl(testData);
BigMatrixImpl m1 = (BigMatrixImpl) m.copy();
@@ -676,7 +676,7 @@
assertTrue(m.hashCode() != m1.hashCode());
assertFalse(m.equals(m1));
}
-
+
public void testToString() {
BigMatrixImpl m = new BigMatrixImpl(testData);
assertEquals("BigMatrixImpl{{1,2,3},{2,5,3},{1,0,8}}",
@@ -685,28 +685,28 @@
assertEquals("BigMatrixImpl{}",
m.toString());
}
-
+
public void testSetSubMatrix() throws Exception {
- BigDecimal[][] detData3 =
+ BigDecimal[][] detData3 =
MatrixUtils.createBigMatrix(detData2).getData();
BigMatrixImpl m = new BigMatrixImpl(testData);
m.setSubMatrix(detData3,1,1);
BigMatrix expected = MatrixUtils.createBigMatrix
(new double[][] {{1.0,2.0,3.0},{2.0,1.0,3.0},{1.0,2.0,4.0}});
- assertEquals(expected, m);
-
+ assertEquals(expected, m);
+
m.setSubMatrix(detData3,0,0);
expected = MatrixUtils.createBigMatrix
(new double[][] {{1.0,3.0,3.0},{2.0,4.0,3.0},{1.0,2.0,4.0}});
- assertEquals(expected, m);
-
- BigDecimal[][] testDataPlus3 =
+ assertEquals(expected, m);
+
+ BigDecimal[][] testDataPlus3 =
MatrixUtils.createBigMatrix(testDataPlus2).getData();
- m.setSubMatrix(testDataPlus3,0,0);
+ m.setSubMatrix(testDataPlus3,0,0);
expected = MatrixUtils.createBigMatrix
(new double[][] {{3.0,4.0,5.0},{4.0,7.0,5.0},{3.0,2.0,10.0}});
- assertEquals(expected, m);
-
+ assertEquals(expected, m);
+
// javadoc example
BigMatrixImpl matrix = (BigMatrixImpl) MatrixUtils.createBigMatrix
(new double[][] {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 0, 1 , 2}});
@@ -718,16 +718,16 @@
new BigDecimal(3), new BigDecimal(4), new BigDecimal(8)},
{new BigDecimal(9), new BigDecimal(5) , new BigDecimal(6),
new BigDecimal(2)}});
- assertEquals(expected, matrix);
-
+ assertEquals(expected, matrix);
+
// dimension overflow
- try {
+ try {
m.setSubMatrix(matrix.getData(),1,1);
fail("expecting MatrixIndexException");
} catch (MatrixIndexException e) {
// expected
}
-
+
// null
try {
m.setSubMatrix(null,1,1);
@@ -735,7 +735,7 @@
} catch (NullPointerException e) {
// expected
}
-
+
// ragged
try {
m.setSubMatrix(new BigDecimal[][] {{new BigDecimal(1)},
@@ -744,7 +744,7 @@
} catch (IllegalArgumentException e) {
// expected
}
-
+
// empty
try {
m.setSubMatrix(new BigDecimal[][] {{}}, 0, 0);
@@ -752,17 +752,17 @@
} catch (IllegalArgumentException e) {
// expected
}
-
+
}
-
+
//--------------- -----------------Protected methods
-
- /** verifies that two matrices are close (1-norm) */
+
+ /** verifies that two matrices are close (1-norm) */
protected void assertClose(String msg, BigMatrix m, BigMatrix n,
double tolerance) {
assertTrue(msg,m.subtract(n).getNorm().doubleValue() < tolerance);
}
-
+
/** verifies that two vectors are close (sup norm) */
protected void assertClose(String msg, double[] m, double[] n,
double tolerance) {
@@ -770,18 +770,18 @@
fail("vectors not same length");
}
for (int i = 0; i < m.length; i++) {
- assertEquals(msg + " " + i + " elements differ",
+ assertEquals(msg + " " + i + " elements differ",
m[i],n[i],tolerance);
}
}
-
+
/** extracts the l and u matrices from compact lu representation */
protected void splitLU(BigMatrix lu, BigDecimal[][] lowerData, BigDecimal[][] upperData) throws InvalidMatrixException {
if (!lu.isSquare() || lowerData.length != lowerData[0].length || upperData.length != upperData[0].length ||
lowerData.length != upperData.length
|| lowerData.length != lu.getRowDimension()) {
throw new InvalidMatrixException("incorrect dimensions");
- }
+ }
int n = lu.getRowDimension();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
@@ -794,11 +794,11 @@
} else {
lowerData[i][j] = new BigDecimal(0);
upperData[i][j] = lu.getEntry(i, j);
- }
+ }
}
}
}
-
+
/** Returns the result of applying the given row permutation to the matrix */
protected BigMatrix permuteRows(BigMatrix matrix, int[] permutation) {
if (!matrix.isSquare() || matrix.getRowDimension() != permutation.length) {
@@ -814,7 +814,7 @@
}
return new BigMatrixImpl(out);
}
-
+
/** Extracts l and u matrices from lu and verifies that matrix = l times u modulo permutation */
protected void verifyDecomposition(BigMatrix matrix, BigMatrix lu) throws Exception{
int n = matrix.getRowDimension();
@@ -828,7 +828,7 @@
assertClose("lu decomposition does not work", permuted,
lower.multiply(upper), normTolerance);
}
-
+
// /** Useful for debugging */
// private void dumpMatrix(BigMatrix m) {
// for (int i = 0; i < m.getRowDimension(); i++) {
@@ -839,6 +839,6 @@
// System.out.println(os);
// }
// }
-
+
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BlockFieldMatrixTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BlockFieldMatrixTest.java?rev=811685&r1=811684&r2=811685&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BlockFieldMatrixTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BlockFieldMatrixTest.java Sat Sep 5 17:36:48 2009
@@ -5,9 +5,9 @@
* 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.
@@ -34,14 +34,14 @@
*/
public final class BlockFieldMatrixTest extends TestCase {
-
+
// 3 x 3 identity matrix
protected Fraction[][] id = {
{new Fraction(1),new Fraction(0),new Fraction(0)},
{new Fraction(0),new Fraction(1),new Fraction(0)},
{new Fraction(0),new Fraction(0),new Fraction(1)}
};
-
+
// Test data for group operations
protected Fraction[][] testData = {
{new Fraction(1),new Fraction(2),new Fraction(3)},
@@ -60,7 +60,7 @@
};
protected Fraction[][] testDataMinus = {
{new Fraction(-1),new Fraction(-2),new Fraction(-3)},
- {new Fraction(-2),new Fraction(-5),new Fraction(-3)},
+ {new Fraction(-2),new Fraction(-5),new Fraction(-3)},
{new Fraction(-1),new Fraction(0),new Fraction(-8)}
};
protected Fraction[] testDataRow1 = {new Fraction(1),new Fraction(2),new Fraction(3)};
@@ -85,7 +85,7 @@
{new Fraction(15),new Fraction(0),new Fraction(0)},
{new Fraction(6),new Fraction(-2),new Fraction(7)}
};
-
+
// lu decomposition tests
protected Fraction[][] luData = {
{new Fraction(2),new Fraction(3),new Fraction(3)},
@@ -97,7 +97,7 @@
{new Fraction(0),new Fraction(5),new Fraction(7)},
{new Fraction(1, 3),new Fraction(0),new Fraction(1, 3)}
};
-
+
// singular matrices
protected Fraction[][] singular = { {new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(3)} };
protected Fraction[][] bigSingular = {
@@ -112,18 +112,18 @@
{new Fraction(7),new Fraction(8),new Fraction(10)}
};
protected Fraction[][] detData2 = { {new Fraction(1), new Fraction(3)}, {new Fraction(2), new Fraction(4)}};
-
+
// vectors
protected Fraction[] testVector = {new Fraction(1),new Fraction(2),new Fraction(3)};
protected Fraction[] testVector2 = {new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)};
-
+
// submatrix accessor tests
protected Fraction[][] subTestData = {
{new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4)},
{new Fraction(3, 2), new Fraction(5, 2), new Fraction(7, 2), new Fraction(9, 2)},
{new Fraction(2), new Fraction(4), new Fraction(6), new Fraction(8)},
{new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7)}
- };
+ };
// array selections
protected Fraction[][] subRows02Cols13 = { {new Fraction(2), new Fraction(4)}, {new Fraction(4), new Fraction(8)}};
protected Fraction[][] subRows03Cols12 = { {new Fraction(2), new Fraction(3)}, {new Fraction(5), new Fraction(6)}};
@@ -147,21 +147,21 @@
// column matrices
protected Fraction[][] subColumn1 = {{new Fraction(2)}, {new Fraction(5, 2)}, {new Fraction(4)}, {new Fraction(5)}};
protected Fraction[][] subColumn3 = {{new Fraction(4)}, {new Fraction(9, 2)}, {new Fraction(8)}, {new Fraction(7)}};
-
+
// tolerances
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
-
+
public BlockFieldMatrixTest(String name) {
super(name);
}
-
+
public static Test suite() {
TestSuite suite = new TestSuite(BlockFieldMatrixTest.class);
suite.setName("BlockFieldMatrix<Fraction> Tests");
return suite;
}
-
+
/** test dimensions */
public void testDimensions() {
BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
@@ -172,8 +172,8 @@
assertEquals("testData2 row dimension",m2.getRowDimension(),2);
assertEquals("testData2 column dimension",m2.getColumnDimension(),3);
assertTrue("testData2 is not square",!m2.isSquare());
- }
-
+ }
+
/** test copy functions */
public void testCopyFunctions() {
Random r = new Random(66636328996002l);
@@ -183,8 +183,8 @@
BlockFieldMatrix<Fraction> m3 = new BlockFieldMatrix<Fraction>(testData);
BlockFieldMatrix<Fraction> m4 = new BlockFieldMatrix<Fraction>(m3.getData());
assertEquals(m3, m4);
- }
-
+ }
+
/** test add */
public void testAdd() {
BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
@@ -195,9 +195,9 @@
for (int col = 0; col < m.getColumnDimension(); col++) {
assertEquals(testDataPlusInv[row][col],sumEntries[row][col]);
}
- }
+ }
}
-
+
/** test add failure */
public void testAddFail() {
BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
@@ -209,20 +209,20 @@
// ignored
}
}
-
+
/** test m-n = m + -n */
public void testPlusMinus() {
BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
BlockFieldMatrix<Fraction> m2 = new BlockFieldMatrix<Fraction>(testDataInv);
- TestUtils.assertEquals(m.subtract(m2), m2.scalarMultiply(new Fraction(-1)).add(m));
+ TestUtils.assertEquals(m.subtract(m2), m2.scalarMultiply(new Fraction(-1)).add(m));
try {
m.subtract(new BlockFieldMatrix<Fraction>(testData2));
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
// ignored
- }
+ }
}
-
+
/** test multiply */
public void testMultiply() {
BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
@@ -233,13 +233,13 @@
TestUtils.assertEquals(mInv.multiply(m), identity);
TestUtils.assertEquals(m.multiply(identity), m);
TestUtils.assertEquals(identity.multiply(mInv), mInv);
- TestUtils.assertEquals(m2.multiply(identity), m2);
+ TestUtils.assertEquals(m2.multiply(identity), m2);
try {
m.multiply(new BlockFieldMatrix<Fraction>(bigSingular));
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
- }
+ }
}
public void testSeveralBlocks() {
@@ -340,14 +340,14 @@
{new Fraction(4)}
};
private Fraction[][] d5 = new Fraction[][] {{new Fraction(30)},{new Fraction(70)}};
-
- public void testMultiply2() {
- FieldMatrix<Fraction> m3 = new BlockFieldMatrix<Fraction>(d3);
+
+ public void testMultiply2() {
+ FieldMatrix<Fraction> m3 = new BlockFieldMatrix<Fraction>(d3);
FieldMatrix<Fraction> m4 = new BlockFieldMatrix<Fraction>(d4);
FieldMatrix<Fraction> m5 = new BlockFieldMatrix<Fraction>(d5);
TestUtils.assertEquals(m3.multiply(m4), m5);
- }
-
+ }
+
/** test trace */
public void testTrace() {
FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(id);
@@ -358,16 +358,16 @@
fail("Expecting NonSquareMatrixException");
} catch (NonSquareMatrixException ex) {
// ignored
- }
+ }
}
-
+
/** test scalarAdd */
public void testScalarAdd() {
FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
TestUtils.assertEquals(new BlockFieldMatrix<Fraction>(testDataPlus2),
m.scalarAdd(new Fraction(2)));
}
-
+
/** test operate */
public void testOperate() {
FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(id);
@@ -379,7 +379,7 @@
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
// ignored
- }
+ }
}
public void testOperateLarge() {
@@ -421,10 +421,10 @@
assertEquals( new Fraction(7), b[1]);
assertEquals(new Fraction(11), b[2]);
}
-
+
/** test transpose */
public void testTranspose() {
- FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
+ FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
FieldMatrix<Fraction> mIT = new FieldLUDecompositionImpl<Fraction>(m).getSolver().getInverse().transpose();
FieldMatrix<Fraction> mTI = new FieldLUDecompositionImpl<Fraction>(m.transpose()).getSolver().getInverse();
TestUtils.assertEquals(mIT, mTI);
@@ -432,7 +432,7 @@
FieldMatrix<Fraction> mt = new BlockFieldMatrix<Fraction>(testData2T);
TestUtils.assertEquals(mt, m.transpose());
}
-
+
/** test preMultiply by vector */
public void testPremultiplyVector() {
FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
@@ -447,13 +447,13 @@
// ignored
}
}
-
+
public void testPremultiply() {
- FieldMatrix<Fraction> m3 = new BlockFieldMatrix<Fraction>(d3);
+ FieldMatrix<Fraction> m3 = new BlockFieldMatrix<Fraction>(d3);
FieldMatrix<Fraction> m4 = new BlockFieldMatrix<Fraction>(d4);
FieldMatrix<Fraction> m5 = new BlockFieldMatrix<Fraction>(d5);
TestUtils.assertEquals(m4.preMultiply(m3), m5);
-
+
BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
BlockFieldMatrix<Fraction> mInv = new BlockFieldMatrix<Fraction>(testDataInv);
BlockFieldMatrix<Fraction> identity = new BlockFieldMatrix<Fraction>(id);
@@ -466,9 +466,9 @@
fail("Expecting illegalArgumentException");
} catch (IllegalArgumentException ex) {
// ignored
- }
+ }
}
-
+
public void testGetVectors() {
FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
TestUtils.assertEquals(m.getRow(0), testDataRow1);
@@ -486,7 +486,7 @@
// ignored
}
}
-
+
public void testGetEntry() {
FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
assertEquals(m.getEntry(0,1),new Fraction(2));
@@ -497,7 +497,7 @@
// expected
}
}
-
+
/** test examples in user guide */
public void testExamples() {
// Create a real matrix with two rows and three columns
@@ -518,10 +518,10 @@
assertEquals(2, p.getRowDimension());
assertEquals(2, p.getColumnDimension());
// Invert p
- FieldMatrix<Fraction> pInverse = new FieldLUDecompositionImpl<Fraction>(p).getSolver().getInverse();
+ FieldMatrix<Fraction> pInverse = new FieldLUDecompositionImpl<Fraction>(p).getSolver().getInverse();
assertEquals(2, pInverse.getRowDimension());
assertEquals(2, pInverse.getColumnDimension());
-
+
// Solve example
Fraction[][] coefficientsData = {
{new Fraction(2), new Fraction(3), new Fraction(-2)},
@@ -542,22 +542,22 @@
assertEquals(new Fraction(4).multiply(solution[0]).
subtract(new Fraction(3).multiply(solution[1])).
subtract(new Fraction(5).multiply(solution[2])),
- constants[2]);
-
+ constants[2]);
+
}
-
+
// test submatrix accessors
public void testGetSubMatrix() {
FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(subTestData);
checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0);
checkGetSubMatrix(m, subRows00Cols33, 0 , 0 , 3, 3);
- checkGetSubMatrix(m, subRows01Cols23, 0 , 1 , 2, 3);
- checkGetSubMatrix(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 });
- checkGetSubMatrix(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 });
- checkGetSubMatrix(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 });
- checkGetSubMatrix(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 });
- checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 });
- checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 });
+ checkGetSubMatrix(m, subRows01Cols23, 0 , 1 , 2, 3);
+ checkGetSubMatrix(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 });
+ checkGetSubMatrix(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 });
+ checkGetSubMatrix(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 });
+ checkGetSubMatrix(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 });
+ checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 });
+ checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 });
checkGetSubMatrix(m, null, 1, 0, 2, 4);
checkGetSubMatrix(m, null, -1, 1, 2, 2);
checkGetSubMatrix(m, null, 1, 0, 2, 2);
@@ -581,7 +581,7 @@
}
}
}
-
+
private void checkGetSubMatrix(FieldMatrix<Fraction> m, Fraction[][] reference,
int[] selectedRows, int[] selectedColumns) {
try {
@@ -623,14 +623,14 @@
FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(subTestData);
checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0);
checkCopy(m, subRows00Cols33, 0 , 0 , 3, 3);
- checkCopy(m, subRows01Cols23, 0 , 1 , 2, 3);
- checkCopy(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 });
- checkCopy(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 });
- checkCopy(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 });
- checkCopy(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 });
- checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 });
- checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 });
-
+ checkCopy(m, subRows01Cols23, 0 , 1 , 2, 3);
+ checkCopy(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 });
+ checkCopy(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 });
+ checkCopy(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 });
+ checkCopy(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 });
+ checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 });
+ checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 });
+
checkCopy(m, null, 1, 0, 2, 4);
checkCopy(m, null, -1, 1, 2, 2);
checkCopy(m, null, 1, 0, 2, 2);
@@ -657,7 +657,7 @@
}
}
}
-
+
private void checkCopy(FieldMatrix<Fraction> m, Fraction[][] reference,
int[] selectedRows, int[] selectedColumns) {
try {
@@ -716,7 +716,7 @@
// expected
}
}
-
+
public void testGetSetRowMatrixLarge() {
int n = 3 * BlockFieldMatrix.BLOCK_SIZE;
FieldMatrix<Fraction> m =
@@ -737,7 +737,7 @@
assertEquals(sub, m.getRowMatrix(2));
}
-
+
public void testGetColumnMatrix() {
FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(subTestData);
FieldMatrix<Fraction> mColumn1 = new BlockFieldMatrix<Fraction>(subColumn1);
@@ -798,7 +798,7 @@
assertEquals(sub, m.getColumnMatrix(2));
}
-
+
public void testGetRowVector() {
FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(subTestData);
FieldVector<Fraction> mRow0 = new ArrayFieldVector<Fraction>(subRow0[0]);
@@ -857,7 +857,7 @@
assertEquals(sub, m.getRowVector(2));
}
-
+
public void testGetColumnVector() {
FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(subTestData);
FieldVector<Fraction> mColumn1 = columnToVector(subColumn1);
@@ -916,7 +916,7 @@
assertEquals(sub, m.getColumnVector(2));
}
-
+
private FieldVector<Fraction> columnToVector(Fraction[][] column) {
Fraction[] data = new Fraction[column.length];
for (int i = 0; i < data.length; ++i) {
@@ -981,7 +981,7 @@
checkArrays(sub, m.getRow(2));
}
-
+
public void testGetColumn() {
FieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(subTestData);
Fraction[] mColumn1 = columnToArray(subColumn1);
@@ -1041,7 +1041,7 @@
checkArrays(sub, m.getColumn(2));
}
-
+
private Fraction[] columnToArray(Fraction[][] column) {
Fraction[] data = new Fraction[column.length];
for (int i = 0; i < data.length; ++i) {
@@ -1053,10 +1053,10 @@
private void checkArrays(Fraction[] expected, Fraction[] actual) {
assertEquals(expected.length, actual.length);
for (int i = 0; i < expected.length; ++i) {
- assertEquals(expected[i], actual[i]);
+ assertEquals(expected[i], actual[i]);
}
}
-
+
public void testEqualsAndHashCode() {
BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
BlockFieldMatrix<Fraction> m1 = (BlockFieldMatrix<Fraction>) m.copy();
@@ -1067,31 +1067,31 @@
assertEquals(m, m1);
assertFalse(m.equals(null));
assertFalse(m.equals(mt));
- assertFalse(m.equals(new BlockFieldMatrix<Fraction>(bigSingular)));
+ assertFalse(m.equals(new BlockFieldMatrix<Fraction>(bigSingular)));
}
-
+
public void testToString() {
BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
assertEquals("BlockFieldMatrix{{1,2,3},{2,5,3},{1,0,8}}", m.toString());
}
-
+
public void testSetSubMatrix() throws Exception {
BlockFieldMatrix<Fraction> m = new BlockFieldMatrix<Fraction>(testData);
m.setSubMatrix(detData2,1,1);
FieldMatrix<Fraction> expected = new BlockFieldMatrix<Fraction>
(new Fraction[][] {{new Fraction(1),new Fraction(2),new Fraction(3)},{new Fraction(2),new Fraction(1),new Fraction(3)},{new Fraction(1),new Fraction(2),new Fraction(4)}});
- assertEquals(expected, m);
-
+ assertEquals(expected, m);
+
m.setSubMatrix(detData2,0,0);
expected = new BlockFieldMatrix<Fraction>
(new Fraction[][] {{new Fraction(1),new Fraction(3),new Fraction(3)},{new Fraction(2),new Fraction(4),new Fraction(3)},{new Fraction(1),new Fraction(2),new Fraction(4)}});
- assertEquals(expected, m);
-
- m.setSubMatrix(testDataPlus2,0,0);
+ assertEquals(expected, m);
+
+ m.setSubMatrix(testDataPlus2,0,0);
expected = new BlockFieldMatrix<Fraction>
(new Fraction[][] {{new Fraction(3),new Fraction(4),new Fraction(5)},{new Fraction(4),new Fraction(7),new Fraction(5)},{new Fraction(3),new Fraction(2),new Fraction(10)}});
- assertEquals(expected, m);
-
+ assertEquals(expected, m);
+
// javadoc example
BlockFieldMatrix<Fraction> matrix =
new BlockFieldMatrix<Fraction>(new Fraction[][] {
@@ -1109,29 +1109,29 @@
{new Fraction(5), new Fraction(3), new Fraction(4), new Fraction(8)},
{new Fraction(9), new Fraction(5) ,new Fraction(6), new Fraction(2)}
});
- assertEquals(expected, matrix);
+ assertEquals(expected, matrix);
// dimension overflow
- try {
+ try {
m.setSubMatrix(testData,1,1);
fail("expecting MatrixIndexException");
} catch (MatrixIndexException e) {
// expected
}
// dimension underflow
- try {
+ try {
m.setSubMatrix(testData,-1,1);
fail("expecting MatrixIndexException");
} catch (MatrixIndexException e) {
// expected
}
- try {
+ try {
m.setSubMatrix(testData,1,-1);
fail("expecting MatrixIndexException");
} catch (MatrixIndexException e) {
// expected
}
-
+
// null
try {
m.setSubMatrix(null,1,1);
@@ -1139,7 +1139,7 @@
} catch (NullPointerException e) {
// expected
}
-
+
// ragged
try {
m.setSubMatrix(new Fraction[][] {{new Fraction(1)}, {new Fraction(2), new Fraction(3)}}, 0, 0);
@@ -1147,7 +1147,7 @@
} catch (IllegalArgumentException e) {
// expected
}
-
+
// empty
try {
m.setSubMatrix(new Fraction[][] {{}}, 0, 0);
@@ -1155,7 +1155,7 @@
} catch (IllegalArgumentException e) {
// expected
}
-
+
}
public void testWalk() {
@@ -1174,11 +1174,11 @@
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- assertEquals(new Fraction(0), m.getEntry(i, 0));
+ assertEquals(new Fraction(0), m.getEntry(i, 0));
assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
- assertEquals(new Fraction(0), m.getEntry(0, j));
+ assertEquals(new Fraction(0), m.getEntry(0, j));
assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
}
@@ -1194,11 +1194,11 @@
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- assertEquals(new Fraction(0), m.getEntry(i, 0));
+ assertEquals(new Fraction(0), m.getEntry(i, 0));
assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
- assertEquals(new Fraction(0), m.getEntry(0, j));
+ assertEquals(new Fraction(0), m.getEntry(0, j));
assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
}
@@ -1214,11 +1214,11 @@
m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- assertEquals(new Fraction(0), m.getEntry(i, 0));
+ assertEquals(new Fraction(0), m.getEntry(i, 0));
assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
- assertEquals(new Fraction(0), m.getEntry(0, j));
+ assertEquals(new Fraction(0), m.getEntry(0, j));
assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
}
@@ -1234,11 +1234,11 @@
m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2);
assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
for (int i = 0; i < rows; ++i) {
- assertEquals(new Fraction(0), m.getEntry(i, 0));
+ assertEquals(new Fraction(0), m.getEntry(i, 0));
assertEquals(new Fraction(0), m.getEntry(i, columns - 1));
}
for (int j = 0; j < columns; ++j) {
- assertEquals(new Fraction(0), m.getEntry(0, j));
+ assertEquals(new Fraction(0), m.getEntry(0, j));
assertEquals(new Fraction(0), m.getEntry(rows - 1, j));
}
@@ -1290,6 +1290,6 @@
}
return m;
}
-
+
}