You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2011/03/08 14:42:18 UTC
svn commit: r1079350 [4/6] - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math/exception/
main/java/org/apache/commons/math/linear/
main/java/org/apache/commons/math/optimization/general/
main/java/org/apache/commons/math/random/ tes...
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BlockRealMatrixTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BlockRealMatrixTest.java?rev=1079350&r1=1079349&r2=1079350&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BlockRealMatrixTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/BlockRealMatrixTest.java Tue Mar 8 13:42:16 2011
@@ -19,16 +19,15 @@ package org.apache.commons.math.linear;
import java.util.Arrays;
import java.util.Random;
-import junit.framework.TestCase;
+import org.junit.Test;
+import org.junit.Assert;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.util.FastMath;
-import org.apache.commons.math.exception.MatrixDimensionMismatchException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.NoDataException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
-import org.apache.commons.math.exception.NonSquareMatrixException;
import org.apache.commons.math.exception.MathIllegalArgumentException;
/**
@@ -37,7 +36,7 @@ import org.apache.commons.math.exception
* @version $Revision$ $Date$
*/
-public final class BlockRealMatrixTest extends TestCase {
+public final class BlockRealMatrixTest {
// 3 x 3 identity matrix
protected double[][] id = { {1d,0d,0d}, {0d,1d,0d}, {0d,0d,1d} };
@@ -99,34 +98,33 @@ public final class BlockRealMatrixTest e
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
- public BlockRealMatrixTest(String name) {
- super(name);
- }
-
/** test dimensions */
+ @Test
public void testDimensions() {
BlockRealMatrix m = new BlockRealMatrix(testData);
BlockRealMatrix m2 = new BlockRealMatrix(testData2);
- assertEquals("testData row dimension",3,m.getRowDimension());
- assertEquals("testData column dimension",3,m.getColumnDimension());
- assertTrue("testData is square",m.isSquare());
- assertEquals("testData2 row dimension",m2.getRowDimension(),2);
- assertEquals("testData2 column dimension",m2.getColumnDimension(),3);
- assertTrue("testData2 is not square",!m2.isSquare());
+ Assert.assertEquals("testData row dimension",3,m.getRowDimension());
+ Assert.assertEquals("testData column dimension",3,m.getColumnDimension());
+ Assert.assertTrue("testData is square",m.isSquare());
+ Assert.assertEquals("testData2 row dimension",m2.getRowDimension(),2);
+ Assert.assertEquals("testData2 column dimension",m2.getColumnDimension(),3);
+ Assert.assertTrue("testData2 is not square",!m2.isSquare());
}
/** test copy functions */
+ @Test
public void testCopyFunctions() {
Random r = new Random(66636328996002l);
BlockRealMatrix m1 = createRandomMatrix(r, 47, 83);
BlockRealMatrix m2 = new BlockRealMatrix(m1.getData());
- assertEquals(m1, m2);
+ Assert.assertEquals(m1, m2);
BlockRealMatrix m3 = new BlockRealMatrix(testData);
BlockRealMatrix m4 = new BlockRealMatrix(m3.getData());
- assertEquals(m3, m4);
+ Assert.assertEquals(m3, m4);
}
/** test add */
+ @Test
public void testAdd() {
BlockRealMatrix m = new BlockRealMatrix(testData);
BlockRealMatrix mInv = new BlockRealMatrix(testDataInv);
@@ -134,7 +132,7 @@ public final class BlockRealMatrixTest e
double[][] sumEntries = mPlusMInv.getData();
for (int row = 0; row < m.getRowDimension(); row++) {
for (int col = 0; col < m.getColumnDimension(); col++) {
- assertEquals("sum entry entry",
+ Assert.assertEquals("sum entry entry",
testDataPlusInv[row][col],sumEntries[row][col],
entryTolerance);
}
@@ -142,48 +140,53 @@ public final class BlockRealMatrixTest e
}
/** test add failure */
+ @Test
public void testAddFail() {
BlockRealMatrix m = new BlockRealMatrix(testData);
BlockRealMatrix m2 = new BlockRealMatrix(testData2);
try {
m.add(m2);
- fail("MathIllegalArgumentException expected");
+ Assert.fail("MathIllegalArgumentException expected");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
/** test norm */
+ @Test
public void testNorm() {
BlockRealMatrix m = new BlockRealMatrix(testData);
BlockRealMatrix m2 = new BlockRealMatrix(testData2);
- assertEquals("testData norm",14d,m.getNorm(),entryTolerance);
- assertEquals("testData2 norm",7d,m2.getNorm(),entryTolerance);
+ Assert.assertEquals("testData norm",14d,m.getNorm(),entryTolerance);
+ Assert.assertEquals("testData2 norm",7d,m2.getNorm(),entryTolerance);
}
/** test Frobenius norm */
+ @Test
public void testFrobeniusNorm() {
BlockRealMatrix m = new BlockRealMatrix(testData);
BlockRealMatrix m2 = new BlockRealMatrix(testData2);
- assertEquals("testData Frobenius norm", FastMath.sqrt(117.0), m.getFrobeniusNorm(), entryTolerance);
- assertEquals("testData2 Frobenius norm", FastMath.sqrt(52.0), m2.getFrobeniusNorm(), entryTolerance);
+ Assert.assertEquals("testData Frobenius norm", FastMath.sqrt(117.0), m.getFrobeniusNorm(), entryTolerance);
+ Assert.assertEquals("testData2 Frobenius norm", FastMath.sqrt(52.0), m2.getFrobeniusNorm(), entryTolerance);
}
- /** test m-n = m + -n */
+ /** test m-n = m + -n */
+ @Test
public void testPlusMinus() {
BlockRealMatrix m = new BlockRealMatrix(testData);
BlockRealMatrix m2 = new BlockRealMatrix(testDataInv);
assertClose(m.subtract(m2), m2.scalarMultiply(-1d).add(m), entryTolerance);
try {
m.subtract(new BlockRealMatrix(testData2));
- fail("Expecting illegalArgumentException");
+ Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
/** test multiply */
- public void testMultiply() {
+ @Test
+ public void testMultiply() {
BlockRealMatrix m = new BlockRealMatrix(testData);
BlockRealMatrix mInv = new BlockRealMatrix(testDataInv);
BlockRealMatrix identity = new BlockRealMatrix(id);
@@ -195,14 +198,14 @@ public final class BlockRealMatrixTest e
assertClose(m2.multiply(identity), m2, entryTolerance);
try {
m.multiply(new BlockRealMatrix(bigSingular));
- fail("Expecting illegalArgumentException");
+ Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// expected
}
}
+ @Test
public void testSeveralBlocks() {
-
RealMatrix m = new BlockRealMatrix(35, 71);
for (int i = 0; i < m.getRowDimension(); ++i) {
for (int j = 0; j < m.getColumnDimension(); ++j) {
@@ -211,25 +214,25 @@ public final class BlockRealMatrixTest e
}
RealMatrix mT = m.transpose();
- assertEquals(m.getRowDimension(), mT.getColumnDimension());
- assertEquals(m.getColumnDimension(), mT.getRowDimension());
+ Assert.assertEquals(m.getRowDimension(), mT.getColumnDimension());
+ Assert.assertEquals(m.getColumnDimension(), mT.getRowDimension());
for (int i = 0; i < mT.getRowDimension(); ++i) {
for (int j = 0; j < mT.getColumnDimension(); ++j) {
- assertEquals(m.getEntry(j, i), mT.getEntry(i, j), 0);
+ Assert.assertEquals(m.getEntry(j, i), mT.getEntry(i, j), 0);
}
}
RealMatrix mPm = m.add(m);
for (int i = 0; i < mPm.getRowDimension(); ++i) {
for (int j = 0; j < mPm.getColumnDimension(); ++j) {
- assertEquals(2 * m.getEntry(i, j), mPm.getEntry(i, j), 0);
+ Assert.assertEquals(2 * m.getEntry(i, j), mPm.getEntry(i, j), 0);
}
}
RealMatrix mPmMm = mPm.subtract(m);
for (int i = 0; i < mPmMm.getRowDimension(); ++i) {
for (int j = 0; j < mPmMm.getColumnDimension(); ++j) {
- assertEquals(m.getEntry(i, j), mPmMm.getEntry(i, j), 0);
+ Assert.assertEquals(m.getEntry(i, j), mPmMm.getEntry(i, j), 0);
}
}
@@ -240,7 +243,7 @@ public final class BlockRealMatrixTest e
for (int k = 0; k < mT.getColumnDimension(); ++k) {
sum += (k + i / 1024.0) * (k + j / 1024.0);
}
- assertEquals(sum, mTm.getEntry(i, j), 0);
+ Assert.assertEquals(sum, mTm.getEntry(i, j), 0);
}
}
@@ -251,35 +254,35 @@ public final class BlockRealMatrixTest e
for (int k = 0; k < m.getColumnDimension(); ++k) {
sum += (i + k / 1024.0) * (j + k / 1024.0);
}
- assertEquals(sum, mmT.getEntry(i, j), 0);
+ Assert.assertEquals(sum, mmT.getEntry(i, j), 0);
}
}
RealMatrix sub1 = m.getSubMatrix(2, 9, 5, 20);
for (int i = 0; i < sub1.getRowDimension(); ++i) {
for (int j = 0; j < sub1.getColumnDimension(); ++j) {
- assertEquals((i + 2) + (j + 5) / 1024.0, sub1.getEntry(i, j), 0);
+ Assert.assertEquals((i + 2) + (j + 5) / 1024.0, sub1.getEntry(i, j), 0);
}
}
RealMatrix sub2 = m.getSubMatrix(10, 12, 3, 70);
for (int i = 0; i < sub2.getRowDimension(); ++i) {
for (int j = 0; j < sub2.getColumnDimension(); ++j) {
- assertEquals((i + 10) + (j + 3) / 1024.0, sub2.getEntry(i, j), 0);
+ Assert.assertEquals((i + 10) + (j + 3) / 1024.0, sub2.getEntry(i, j), 0);
}
}
RealMatrix sub3 = m.getSubMatrix(30, 34, 0, 5);
for (int i = 0; i < sub3.getRowDimension(); ++i) {
for (int j = 0; j < sub3.getColumnDimension(); ++j) {
- assertEquals((i + 30) + (j + 0) / 1024.0, sub3.getEntry(i, j), 0);
+ Assert.assertEquals((i + 30) + (j + 0) / 1024.0, sub3.getEntry(i, j), 0);
}
}
RealMatrix sub4 = m.getSubMatrix(30, 32, 62, 65);
for (int i = 0; i < sub4.getRowDimension(); ++i) {
for (int j = 0; j < sub4.getColumnDimension(); ++j) {
- assertEquals((i + 30) + (j + 62) / 1024.0, sub4.getEntry(i, j), 0);
+ Assert.assertEquals((i + 30) + (j + 62) / 1024.0, sub4.getEntry(i, j), 0);
}
}
@@ -291,33 +294,37 @@ public final class BlockRealMatrixTest e
private double[][] d4 = new double[][] {{1},{2},{3},{4}};
private double[][] d5 = new double[][] {{30},{70}};
+ @Test
public void testMultiply2() {
- RealMatrix m3 = new BlockRealMatrix(d3);
- RealMatrix m4 = new BlockRealMatrix(d4);
- RealMatrix m5 = new BlockRealMatrix(d5);
- assertClose(m3.multiply(m4), m5, entryTolerance);
- }
+ RealMatrix m3 = new BlockRealMatrix(d3);
+ RealMatrix m4 = new BlockRealMatrix(d4);
+ RealMatrix m5 = new BlockRealMatrix(d5);
+ assertClose(m3.multiply(m4), m5, entryTolerance);
+ }
/** test trace */
+ @Test
public void testTrace() {
RealMatrix m = new BlockRealMatrix(id);
- assertEquals("identity trace",3d,m.getTrace(),entryTolerance);
+ Assert.assertEquals("identity trace",3d,m.getTrace(),entryTolerance);
m = new BlockRealMatrix(testData2);
try {
m.getTrace();
- fail("Expecting NonSquareMatrixException");
+ Assert.fail("Expecting NonSquareMatrixException");
} catch (NonSquareMatrixException ex) {
// ignored
}
}
/** test scalarAdd */
+ @Test
public void testScalarAdd() {
RealMatrix m = new BlockRealMatrix(testData);
assertClose(new BlockRealMatrix(testDataPlus2), m.scalarAdd(2d), entryTolerance);
}
/** test operate */
+ @Test
public void testOperate() {
RealMatrix m = new BlockRealMatrix(id);
assertClose(testVector, m.operate(testVector), entryTolerance);
@@ -325,12 +332,13 @@ public final class BlockRealMatrixTest e
m = new BlockRealMatrix(bigSingular);
try {
m.operate(testVector);
- fail("Expecting illegalArgumentException");
+ Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
+ @Test
public void testOperateLarge() {
int p = (7 * BlockRealMatrix.BLOCK_SIZE) / 2;
int q = (5 * BlockRealMatrix.BLOCK_SIZE) / 2;
@@ -344,6 +352,7 @@ public final class BlockRealMatrixTest e
}
}
+ @Test
public void testOperatePremultiplyLarge() {
int p = (7 * BlockRealMatrix.BLOCK_SIZE) / 2;
int q = (5 * BlockRealMatrix.BLOCK_SIZE) / 2;
@@ -358,18 +367,20 @@ public final class BlockRealMatrixTest e
}
/** test issue MATH-209 */
+ @Test
public void testMath209() {
RealMatrix a = new BlockRealMatrix(new double[][] {
{ 1, 2 }, { 3, 4 }, { 5, 6 }
});
double[] b = a.operate(new double[] { 1, 1 });
- assertEquals(a.getRowDimension(), b.length);
- assertEquals( 3.0, b[0], 1.0e-12);
- assertEquals( 7.0, b[1], 1.0e-12);
- assertEquals(11.0, b[2], 1.0e-12);
+ Assert.assertEquals(a.getRowDimension(), b.length);
+ Assert.assertEquals( 3.0, b[0], 1.0e-12);
+ Assert.assertEquals( 7.0, b[1], 1.0e-12);
+ Assert.assertEquals(11.0, b[2], 1.0e-12);
}
/** test transpose */
+ @Test
public void testTranspose() {
RealMatrix m = new BlockRealMatrix(testData);
RealMatrix mIT = new LUDecompositionImpl(m).getSolver().getInverse().transpose();
@@ -381,6 +392,7 @@ public final class BlockRealMatrixTest e
}
/** test preMultiply by vector */
+ @Test
public void testPremultiplyVector() {
RealMatrix m = new BlockRealMatrix(testData);
assertClose(m.preMultiply(testVector), preMultTest, normTolerance);
@@ -389,12 +401,13 @@ public final class BlockRealMatrixTest e
m = new BlockRealMatrix(bigSingular);
try {
m.preMultiply(testVector);
- fail("expecting MathIllegalArgumentException");
+ Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
+ @Test
public void testPremultiply() {
RealMatrix m3 = new BlockRealMatrix(d3);
RealMatrix m4 = new BlockRealMatrix(d4);
@@ -410,42 +423,45 @@ public final class BlockRealMatrixTest e
assertClose(identity.preMultiply(mInv), mInv, entryTolerance);
try {
m.preMultiply(new BlockRealMatrix(bigSingular));
- fail("Expecting illegalArgumentException");
+ Assert.fail("Expecting illegalArgumentException");
} catch (MathIllegalArgumentException ex) {
// ignored
}
}
+ @Test
public void testGetVectors() {
RealMatrix m = new BlockRealMatrix(testData);
assertClose(m.getRow(0), testDataRow1, entryTolerance);
assertClose(m.getColumn(2), testDataCol3, entryTolerance);
try {
m.getRow(10);
- fail("expecting OutOfRangeException");
+ Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// ignored
}
try {
m.getColumn(-1);
- fail("expecting OutOfRangeException");
+ Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// ignored
}
}
+ @Test
public void testGetEntry() {
RealMatrix m = new BlockRealMatrix(testData);
- assertEquals("get entry",m.getEntry(0,1),2d,entryTolerance);
+ Assert.assertEquals("get entry",m.getEntry(0,1),2d,entryTolerance);
try {
m.getEntry(10, 4);
- fail ("Expecting OutOfRangeException");
+ Assert.fail ("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
/** test examples in user guide */
+ @Test
public void testExamples() {
// Create a real matrix with two rows and three columns
double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}};
@@ -455,25 +471,26 @@ public final class BlockRealMatrixTest e
RealMatrix n = new BlockRealMatrix(matrixData2);
// Now multiply m by n
RealMatrix p = m.multiply(n);
- assertEquals(2, p.getRowDimension());
- assertEquals(2, p.getColumnDimension());
+ Assert.assertEquals(2, p.getRowDimension());
+ Assert.assertEquals(2, p.getColumnDimension());
// Invert p
RealMatrix pInverse = new LUDecompositionImpl(p).getSolver().getInverse();
- assertEquals(2, pInverse.getRowDimension());
- assertEquals(2, pInverse.getColumnDimension());
+ Assert.assertEquals(2, pInverse.getRowDimension());
+ Assert.assertEquals(2, pInverse.getColumnDimension());
// Solve example
double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}};
RealMatrix coefficients = new BlockRealMatrix(coefficientsData);
double[] constants = {1, -2, 1};
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);
+ Assert.assertEquals(2 * solution[0] + 3 * solution[1] -2 * solution[2], constants[0], 1E-12);
+ Assert.assertEquals(-1 * solution[0] + 7 * solution[1] + 6 * solution[2], constants[1], 1E-12);
+ Assert.assertEquals(4 * solution[0] - 3 * solution[1] -5 * solution[2], constants[2], 1E-12);
}
// test submatrix accessors
+ @Test
public void testGetSubMatrix() {
RealMatrix m = new BlockRealMatrix(subTestData);
checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0);
@@ -498,9 +515,9 @@ public final class BlockRealMatrixTest e
try {
RealMatrix sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn);
if (reference != null) {
- assertEquals(new BlockRealMatrix(reference), sub);
+ Assert.assertEquals(new BlockRealMatrix(reference), sub);
} else {
- fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
+ Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
}
} catch (OutOfRangeException e) {
if (reference != null) {
@@ -522,9 +539,9 @@ public final class BlockRealMatrixTest e
try {
RealMatrix sub = m.getSubMatrix(selectedRows, selectedColumns);
if (reference != null) {
- assertEquals(new BlockRealMatrix(reference), sub);
+ Assert.assertEquals(new BlockRealMatrix(reference), sub);
} else {
- fail("Expecting OutOfRangeException or NumberIsTooSmallExceptiono r NoDataException");
+ Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallExceptiono r NoDataException");
}
} catch (OutOfRangeException e) {
if (reference != null) {
@@ -541,6 +558,7 @@ public final class BlockRealMatrixTest e
}
}
+ @Test
public void testGetSetMatrixLarge() {
int n = 3 * BlockRealMatrix.BLOCK_SIZE;
RealMatrix m = new BlockRealMatrix(n, n);
@@ -550,16 +568,17 @@ public final class BlockRealMatrixTest e
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if ((i < 2) || (i > n - 3) || (j < 2) || (j > n - 3)) {
- assertEquals(0.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
} else {
- assertEquals(1.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
}
}
}
- assertEquals(sub, m.getSubMatrix(2, n - 3, 2, n - 3));
+ Assert.assertEquals(sub, m.getSubMatrix(2, n - 3, 2, n - 3));
}
+ @Test
public void testCopySubMatrix() {
RealMatrix m = new BlockRealMatrix(subTestData);
checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0);
@@ -588,9 +607,9 @@ public final class BlockRealMatrixTest e
new double[reference.length][reference[0].length];
m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub);
if (reference != null) {
- assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub));
+ Assert.assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub));
} else {
- fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
+ Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
}
} catch (OutOfRangeException e) {
if (reference != null) {
@@ -615,9 +634,9 @@ public final class BlockRealMatrixTest e
new double[reference.length][reference[0].length];
m.copySubMatrix(selectedRows, selectedColumns, sub);
if (reference != null) {
- assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub));
+ Assert.assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub));
} else {
- fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
+ Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException");
}
} catch (OutOfRangeException e) {
if (reference != null) {
@@ -634,46 +653,49 @@ public final class BlockRealMatrixTest e
}
}
+ @Test
public void testGetRowMatrix() {
RealMatrix m = new BlockRealMatrix(subTestData);
RealMatrix mRow0 = new BlockRealMatrix(subRow0);
RealMatrix mRow3 = new BlockRealMatrix(subRow3);
- assertEquals("Row0", mRow0, m.getRowMatrix(0));
- assertEquals("Row3", mRow3, m.getRowMatrix(3));
+ Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0));
+ Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3));
try {
m.getRowMatrix(-1);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getRowMatrix(4);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
+ @Test
public void testSetRowMatrix() {
RealMatrix m = new BlockRealMatrix(subTestData);
RealMatrix mRow3 = new BlockRealMatrix(subRow3);
- assertNotSame(mRow3, m.getRowMatrix(0));
+ Assert.assertNotSame(mRow3, m.getRowMatrix(0));
m.setRowMatrix(0, mRow3);
- assertEquals(mRow3, m.getRowMatrix(0));
+ Assert.assertEquals(mRow3, m.getRowMatrix(0));
try {
m.setRowMatrix(-1, mRow3);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setRowMatrix(0, m);
- fail("Expecting MatrixDimensionMismatchException");
+ Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
+ @Test
public void testGetSetRowMatrixLarge() {
int n = 3 * BlockRealMatrix.BLOCK_SIZE;
RealMatrix m = new BlockRealMatrix(n, n);
@@ -683,56 +705,58 @@ public final class BlockRealMatrixTest e
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != 2) {
- assertEquals(0.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
} else {
- assertEquals(1.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
}
}
}
- assertEquals(sub, m.getRowMatrix(2));
-
+ Assert.assertEquals(sub, m.getRowMatrix(2));
}
+ @Test
public void testGetColumnMatrix() {
RealMatrix m = new BlockRealMatrix(subTestData);
RealMatrix mColumn1 = new BlockRealMatrix(subColumn1);
RealMatrix mColumn3 = new BlockRealMatrix(subColumn3);
- assertEquals(mColumn1, m.getColumnMatrix(1));
- assertEquals(mColumn3, m.getColumnMatrix(3));
+ Assert.assertEquals(mColumn1, m.getColumnMatrix(1));
+ Assert.assertEquals(mColumn3, m.getColumnMatrix(3));
try {
m.getColumnMatrix(-1);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getColumnMatrix(4);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
+ @Test
public void testSetColumnMatrix() {
RealMatrix m = new BlockRealMatrix(subTestData);
RealMatrix mColumn3 = new BlockRealMatrix(subColumn3);
- assertNotSame(mColumn3, m.getColumnMatrix(1));
+ Assert.assertNotSame(mColumn3, m.getColumnMatrix(1));
m.setColumnMatrix(1, mColumn3);
- assertEquals(mColumn3, m.getColumnMatrix(1));
+ Assert.assertEquals(mColumn3, m.getColumnMatrix(1));
try {
m.setColumnMatrix(-1, mColumn3);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setColumnMatrix(0, m);
- fail("Expecting MatrixDimensionMismatchException");
+ Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
+ @Test
public void testGetSetColumnMatrixLarge() {
int n = 3 * BlockRealMatrix.BLOCK_SIZE;
RealMatrix m = new BlockRealMatrix(n, n);
@@ -742,56 +766,59 @@ public final class BlockRealMatrixTest e
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j != 2) {
- assertEquals(0.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
} else {
- assertEquals(1.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
}
}
}
- assertEquals(sub, m.getColumnMatrix(2));
+ Assert.assertEquals(sub, m.getColumnMatrix(2));
}
+ @Test
public void testGetRowVector() {
RealMatrix m = new BlockRealMatrix(subTestData);
RealVector mRow0 = new ArrayRealVector(subRow0[0]);
RealVector mRow3 = new ArrayRealVector(subRow3[0]);
- assertEquals(mRow0, m.getRowVector(0));
- assertEquals(mRow3, m.getRowVector(3));
+ Assert.assertEquals(mRow0, m.getRowVector(0));
+ Assert.assertEquals(mRow3, m.getRowVector(3));
try {
m.getRowVector(-1);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getRowVector(4);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
+ @Test
public void testSetRowVector() {
RealMatrix m = new BlockRealMatrix(subTestData);
RealVector mRow3 = new ArrayRealVector(subRow3[0]);
- assertNotSame(mRow3, m.getRowMatrix(0));
+ Assert.assertNotSame(mRow3, m.getRowMatrix(0));
m.setRowVector(0, mRow3);
- assertEquals(mRow3, m.getRowVector(0));
+ Assert.assertEquals(mRow3, m.getRowVector(0));
try {
m.setRowVector(-1, mRow3);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setRowVector(0, new ArrayRealVector(5));
- fail("Expecting MatrixDimensionMismatchException");
+ Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
+ @Test
public void testGetSetRowVectorLarge() {
int n = 3 * BlockRealMatrix.BLOCK_SIZE;
RealMatrix m = new BlockRealMatrix(n, n);
@@ -801,56 +828,58 @@ public final class BlockRealMatrixTest e
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != 2) {
- assertEquals(0.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
} else {
- assertEquals(1.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
}
}
}
- assertEquals(sub, m.getRowVector(2));
-
+ Assert.assertEquals(sub, m.getRowVector(2));
}
+ @Test
public void testGetColumnVector() {
RealMatrix m = new BlockRealMatrix(subTestData);
RealVector mColumn1 = columnToVector(subColumn1);
RealVector mColumn3 = columnToVector(subColumn3);
- assertEquals(mColumn1, m.getColumnVector(1));
- assertEquals(mColumn3, m.getColumnVector(3));
+ Assert.assertEquals(mColumn1, m.getColumnVector(1));
+ Assert.assertEquals(mColumn3, m.getColumnVector(3));
try {
m.getColumnVector(-1);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getColumnVector(4);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
+ @Test
public void testSetColumnVector() {
RealMatrix m = new BlockRealMatrix(subTestData);
RealVector mColumn3 = columnToVector(subColumn3);
- assertNotSame(mColumn3, m.getColumnVector(1));
+ Assert.assertNotSame(mColumn3, m.getColumnVector(1));
m.setColumnVector(1, mColumn3);
- assertEquals(mColumn3, m.getColumnVector(1));
+ Assert.assertEquals(mColumn3, m.getColumnVector(1));
try {
m.setColumnVector(-1, mColumn3);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setColumnVector(0, new ArrayRealVector(5));
- fail("Expecting MatrixDimensionMismatchException");
+ Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
+ @Test
public void testGetSetColumnVectorLarge() {
int n = 3 * BlockRealMatrix.BLOCK_SIZE;
RealMatrix m = new BlockRealMatrix(n, n);
@@ -860,14 +889,13 @@ public final class BlockRealMatrixTest e
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j != 2) {
- assertEquals(0.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
} else {
- assertEquals(1.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
}
}
}
- assertEquals(sub, m.getColumnVector(2));
-
+ Assert.assertEquals(sub, m.getColumnVector(2));
}
private RealVector columnToVector(double[][] column) {
@@ -878,43 +906,46 @@ public final class BlockRealMatrixTest e
return new ArrayRealVector(data, false);
}
+ @Test
public void testGetRow() {
RealMatrix m = new BlockRealMatrix(subTestData);
checkArrays(subRow0[0], m.getRow(0));
checkArrays(subRow3[0], m.getRow(3));
try {
m.getRow(-1);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getRow(4);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
+ @Test
public void testSetRow() {
RealMatrix m = new BlockRealMatrix(subTestData);
- assertTrue(subRow3[0][0] != m.getRow(0)[0]);
+ Assert.assertTrue(subRow3[0][0] != m.getRow(0)[0]);
m.setRow(0, subRow3[0]);
checkArrays(subRow3[0], m.getRow(0));
try {
m.setRow(-1, subRow3[0]);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setRow(0, new double[5]);
- fail("Expecting MatrixDimensionMismatchException");
+ Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
+ @Test
public void testGetSetRowLarge() {
int n = 3 * BlockRealMatrix.BLOCK_SIZE;
RealMatrix m = new BlockRealMatrix(n, n);
@@ -925,16 +956,16 @@ public final class BlockRealMatrixTest e
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != 2) {
- assertEquals(0.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
} else {
- assertEquals(1.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
}
}
}
checkArrays(sub, m.getRow(2));
-
}
+ @Test
public void testGetColumn() {
RealMatrix m = new BlockRealMatrix(subTestData);
double[] mColumn1 = columnToArray(subColumn1);
@@ -943,38 +974,40 @@ public final class BlockRealMatrixTest e
checkArrays(mColumn3, m.getColumn(3));
try {
m.getColumn(-1);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.getColumn(4);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
}
+ @Test
public void testSetColumn() {
RealMatrix m = new BlockRealMatrix(subTestData);
double[] mColumn3 = columnToArray(subColumn3);
- assertTrue(mColumn3[0] != m.getColumn(1)[0]);
+ Assert.assertTrue(mColumn3[0] != m.getColumn(1)[0]);
m.setColumn(1, mColumn3);
checkArrays(mColumn3, m.getColumn(1));
try {
m.setColumn(-1, mColumn3);
- fail("Expecting OutOfRangeException");
+ Assert.fail("Expecting OutOfRangeException");
} catch (OutOfRangeException ex) {
// expected
}
try {
m.setColumn(0, new double[5]);
- fail("Expecting MatrixDimensionMismatchException");
+ Assert.fail("Expecting MatrixDimensionMismatchException");
} catch (MatrixDimensionMismatchException ex) {
// expected
}
}
+ @Test
public void testGetSetColumnLarge() {
int n = 3 * BlockRealMatrix.BLOCK_SIZE;
RealMatrix m = new BlockRealMatrix(n, n);
@@ -985,14 +1018,13 @@ public final class BlockRealMatrixTest e
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j != 2) {
- assertEquals(0.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(0.0, m.getEntry(i, j), 0.0);
} else {
- assertEquals(1.0, m.getEntry(i, j), 0.0);
+ Assert.assertEquals(1.0, m.getEntry(i, j), 0.0);
}
}
}
checkArrays(sub, m.getColumn(2));
-
}
private double[] columnToArray(double[][] column) {
@@ -1004,47 +1036,50 @@ public final class BlockRealMatrixTest e
}
private void checkArrays(double[] expected, double[] actual) {
- assertEquals(expected.length, actual.length);
+ Assert.assertEquals(expected.length, actual.length);
for (int i = 0; i < expected.length; ++i) {
- assertEquals(expected[i], actual[i]);
+ Assert.assertEquals(expected[i], actual[i], 0);
}
}
+ @Test
public void testEqualsAndHashCode() {
BlockRealMatrix m = new BlockRealMatrix(testData);
BlockRealMatrix m1 = m.copy();
BlockRealMatrix mt = m.transpose();
- assertTrue(m.hashCode() != mt.hashCode());
- assertEquals(m.hashCode(), m1.hashCode());
- assertEquals(m, m);
- assertEquals(m, m1);
- assertFalse(m.equals(null));
- assertFalse(m.equals(mt));
- assertFalse(m.equals(new BlockRealMatrix(bigSingular)));
+ Assert.assertTrue(m.hashCode() != mt.hashCode());
+ Assert.assertEquals(m.hashCode(), m1.hashCode());
+ Assert.assertEquals(m, m);
+ Assert.assertEquals(m, m1);
+ Assert.assertFalse(m.equals(null));
+ Assert.assertFalse(m.equals(mt));
+ Assert.assertFalse(m.equals(new BlockRealMatrix(bigSingular)));
}
+ @Test
public void testToString() {
BlockRealMatrix m = new BlockRealMatrix(testData);
- assertEquals("BlockRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
+ Assert.assertEquals("BlockRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}",
m.toString());
}
+ @Test
public void testSetSubMatrix() throws Exception {
BlockRealMatrix m = new BlockRealMatrix(testData);
m.setSubMatrix(detData2,1,1);
RealMatrix expected = new BlockRealMatrix
(new double[][] {{1.0,2.0,3.0},{2.0,1.0,3.0},{1.0,2.0,4.0}});
- assertEquals(expected, m);
+ Assert.assertEquals(expected, m);
m.setSubMatrix(detData2,0,0);
expected = new BlockRealMatrix
(new double[][] {{1.0,3.0,3.0},{2.0,4.0,3.0},{1.0,2.0,4.0}});
- assertEquals(expected, m);
+ Assert.assertEquals(expected, m);
m.setSubMatrix(testDataPlus2,0,0);
expected = new BlockRealMatrix
(new double[][] {{3.0,4.0,5.0},{4.0,7.0,5.0},{3.0,2.0,10.0}});
- assertEquals(expected, m);
+ Assert.assertEquals(expected, m);
// javadoc example
BlockRealMatrix matrix = new BlockRealMatrix
@@ -1052,25 +1087,25 @@ public final class BlockRealMatrixTest e
matrix.setSubMatrix(new double[][] {{3, 4}, {5, 6}}, 1, 1);
expected = new BlockRealMatrix
(new double[][] {{1, 2, 3, 4}, {5, 3, 4, 8}, {9, 5 ,6, 2}});
- assertEquals(expected, matrix);
+ Assert.assertEquals(expected, matrix);
// dimension overflow
try {
m.setSubMatrix(testData,1,1);
- fail("expecting OutOfRangeException");
+ Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
// dimension underflow
try {
m.setSubMatrix(testData,-1,1);
- fail("expecting OutOfRangeException");
+ Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
try {
m.setSubMatrix(testData,1,-1);
- fail("expecting OutOfRangeException");
+ Assert.fail("expecting OutOfRangeException");
} catch (OutOfRangeException e) {
// expected
}
@@ -1078,7 +1113,7 @@ public final class BlockRealMatrixTest e
// null
try {
m.setSubMatrix(null,1,1);
- fail("expecting NullPointerException");
+ Assert.fail("expecting NullPointerException");
} catch (NullPointerException e) {
// expected
}
@@ -1086,7 +1121,7 @@ public final class BlockRealMatrixTest e
// ragged
try {
m.setSubMatrix(new double[][] {{1}, {2, 3}}, 0, 0);
- fail("expecting MathIllegalArgumentException");
+ Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
}
@@ -1094,13 +1129,13 @@ public final class BlockRealMatrixTest e
// empty
try {
m.setSubMatrix(new double[][] {{}}, 0, 0);
- fail("expecting MathIllegalArgumentException");
+ Assert.fail("expecting MathIllegalArgumentException");
} catch (MathIllegalArgumentException e) {
// expected
}
-
}
+ @Test
public void testWalk() throws MathUserException {
int rows = 150;
int columns = 75;
@@ -1109,87 +1144,88 @@ public final class BlockRealMatrixTest e
m.walkInRowOrder(new SetVisitor());
GetVisitor getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor);
- assertEquals(rows * columns, getVisitor.getCount());
+ Assert.assertEquals(rows * columns, getVisitor.getCount());
m = new BlockRealMatrix(rows, columns);
m.walkInRowOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
- assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
+ Assert.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, columns - 1), 0);
+ Assert.assertEquals(0.0, m.getEntry(i, 0), 0);
+ Assert.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(rows - 1, j), 0);
+ Assert.assertEquals(0.0, m.getEntry(0, j), 0);
+ Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0);
}
m = new BlockRealMatrix(rows, columns);
m.walkInColumnOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor);
- assertEquals(rows * columns, getVisitor.getCount());
+ Assert.assertEquals(rows * columns, getVisitor.getCount());
m = new BlockRealMatrix(rows, columns);
m.walkInColumnOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2);
- assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
+ Assert.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, columns - 1), 0);
+ Assert.assertEquals(0.0, m.getEntry(i, 0), 0);
+ Assert.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(rows - 1, j), 0);
+ Assert.assertEquals(0.0, m.getEntry(0, j), 0);
+ Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0);
}
m = new BlockRealMatrix(rows, columns);
m.walkInOptimizedOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInRowOrder(getVisitor);
- assertEquals(rows * columns, getVisitor.getCount());
+ Assert.assertEquals(rows * columns, getVisitor.getCount());
m = new BlockRealMatrix(rows, columns);
m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2);
- assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
+ Assert.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, columns - 1), 0);
+ Assert.assertEquals(0.0, m.getEntry(i, 0), 0);
+ Assert.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(rows - 1, j), 0);
+ Assert.assertEquals(0.0, m.getEntry(0, j), 0);
+ Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0);
}
m = new BlockRealMatrix(rows, columns);
m.walkInOptimizedOrder(new SetVisitor());
getVisitor = new GetVisitor();
m.walkInColumnOrder(getVisitor);
- assertEquals(rows * columns, getVisitor.getCount());
+ Assert.assertEquals(rows * columns, getVisitor.getCount());
m = new BlockRealMatrix(rows, columns);
m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2);
getVisitor = new GetVisitor();
m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2);
- assertEquals((rows - 2) * (columns - 2), getVisitor.getCount());
+ Assert.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, columns - 1), 0);
+ Assert.assertEquals(0.0, m.getEntry(i, 0), 0);
+ Assert.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(rows - 1, j), 0);
+ Assert.assertEquals(0.0, m.getEntry(0, j), 0);
+ Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0);
}
}
+ @Test
public void testSerial() {
BlockRealMatrix m = new BlockRealMatrix(testData);
- assertEquals(m,TestUtils.serializeAndRecover(m));
+ Assert.assertEquals(m,TestUtils.serializeAndRecover(m));
}
private static class SetVisitor extends DefaultRealMatrixChangingVisitor {
@@ -1204,7 +1240,7 @@ public final class BlockRealMatrixTest e
@Override
public void visit(int i, int j, double value) {
++count;
- assertEquals(i + j / 1024.0, value, 0.0);
+ Assert.assertEquals(i + j / 1024.0, value, 0.0);
}
public int getCount() {
return count;
@@ -1215,16 +1251,16 @@ public final class BlockRealMatrixTest e
/** verifies that two matrices are close (1-norm) */
protected void assertClose(RealMatrix m, RealMatrix n, double tolerance) {
- assertTrue(m.subtract(n).getNorm() < tolerance);
+ Assert.assertTrue(m.subtract(n).getNorm() < tolerance);
}
/** verifies that two vectors are close (sup norm) */
protected void assertClose(double[] m, double[] n, double tolerance) {
if (m.length != n.length) {
- fail("vectors not same length");
+ Assert.fail("vectors not same length");
}
for (int i = 0; i < m.length; i++) {
- assertEquals(m[i], n[i], tolerance);
+ Assert.assertEquals(m[i], n[i], tolerance);
}
}
@@ -1237,6 +1273,5 @@ public final class BlockRealMatrixTest e
}
return m;
}
-
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/CholeskyDecompositionImplTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/CholeskyDecompositionImplTest.java?rev=1079350&r1=1079349&r2=1079350&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/CholeskyDecompositionImplTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/CholeskyDecompositionImplTest.java Tue Mar 8 13:42:16 2011
@@ -17,13 +17,8 @@
package org.apache.commons.math.linear;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import org.apache.commons.math.exception.NonSquareMatrixException;
-import org.apache.commons.math.exception.NonPositiveDefiniteMatrixException;
-import org.apache.commons.math.exception.NonSymmetricMatrixException;
import org.junit.Test;
+import org.junit.Assert;
public class CholeskyDecompositionImplTest {
@@ -40,10 +35,10 @@ public class CholeskyDecompositionImplTe
public void testDimensions() {
CholeskyDecomposition llt =
new CholeskyDecompositionImpl(MatrixUtils.createRealMatrix(testData));
- assertEquals(testData.length, llt.getL().getRowDimension());
- assertEquals(testData.length, llt.getL().getColumnDimension());
- assertEquals(testData.length, llt.getLT().getRowDimension());
- assertEquals(testData.length, llt.getLT().getColumnDimension());
+ Assert.assertEquals(testData.length, llt.getL().getRowDimension());
+ Assert.assertEquals(testData.length, llt.getL().getColumnDimension());
+ Assert.assertEquals(testData.length, llt.getLT().getRowDimension());
+ Assert.assertEquals(testData.length, llt.getLT().getColumnDimension());
}
/** test non-square matrix */
@@ -91,7 +86,7 @@ public class CholeskyDecompositionImplTe
RealMatrix l = llt.getL();
RealMatrix lt = llt.getLT();
double norm = l.multiply(lt).subtract(matrix).getNorm();
- assertEquals(0, norm, 1.0e-15);
+ Assert.assertEquals(0, norm, 1.0e-15);
}
/** test that L is lower triangular */
@@ -101,7 +96,7 @@ public class CholeskyDecompositionImplTe
RealMatrix l = new CholeskyDecompositionImpl(matrix).getL();
for (int i = 0; i < l.getRowDimension(); i++) {
for (int j = i + 1; j < l.getColumnDimension(); j++) {
- assertEquals(0.0, l.getEntry(i, j), 0.0);
+ Assert.assertEquals(0.0, l.getEntry(i, j), 0.0);
}
}
}
@@ -114,7 +109,7 @@ public class CholeskyDecompositionImplTe
RealMatrix l = llt.getL();
RealMatrix lt = llt.getLT();
double norm = l.subtract(lt.transpose()).getNorm();
- assertEquals(0, norm, 1.0e-15);
+ Assert.assertEquals(0, norm, 1.0e-15);
}
/** test matrices values */
@@ -132,14 +127,12 @@ public class CholeskyDecompositionImplTe
// check values against known references
RealMatrix l = llt.getL();
- assertEquals(0, l.subtract(lRef).getNorm(), 1.0e-13);
+ Assert.assertEquals(0, l.subtract(lRef).getNorm(), 1.0e-13);
RealMatrix lt = llt.getLT();
- assertEquals(0, lt.subtract(lRef.transpose()).getNorm(), 1.0e-13);
+ Assert.assertEquals(0, lt.subtract(lRef.transpose()).getNorm(), 1.0e-13);
// check the same cached instance is returned the second time
- assertTrue(l == llt.getL());
- assertTrue(lt == llt.getLT());
-
+ Assert.assertTrue(l == llt.getL());
+ Assert.assertTrue(lt == llt.getLT());
}
-
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/EigenSolverTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/EigenSolverTest.java?rev=1079350&r1=1079349&r2=1079350&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/EigenSolverTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/EigenSolverTest.java Tue Mar 8 13:42:16 2011
@@ -20,73 +20,75 @@ package org.apache.commons.math.linear;
import java.util.Random;
import org.apache.commons.math.util.MathUtils;
-import org.apache.commons.math.exception.SingularMatrixException;
import org.apache.commons.math.exception.MathIllegalArgumentException;
-import junit.framework.TestCase;
+import org.junit.Test;
+import org.junit.Assert;
-public class EigenSolverTest extends TestCase {
-
- private double[] refValues;
- private RealMatrix matrix;
-
- public EigenSolverTest(String name) {
- super(name);
- }
+public class EigenSolverTest {
/** test non invertible matrix */
+ @Test
public void testNonInvertible() {
Random r = new Random(9994100315209l);
RealMatrix m =
EigenDecompositionImplTest.createTestMatrix(r, new double[] { 1.0, 0.0, -1.0, -2.0, -3.0 });
DecompositionSolver es = new EigenDecompositionImpl(m, MathUtils.SAFE_MIN).getSolver();
- assertFalse(es.isNonSingular());
+ Assert.assertFalse(es.isNonSingular());
try {
es.getInverse();
- fail("an exception should have been thrown");
+ Assert.fail("an exception should have been thrown");
} catch (SingularMatrixException ime) {
// expected behavior
}
}
/** test invertible matrix */
+ @Test
public void testInvertible() {
Random r = new Random(9994100315209l);
RealMatrix m =
EigenDecompositionImplTest.createTestMatrix(r, new double[] { 1.0, 0.5, -1.0, -2.0, -3.0 });
DecompositionSolver es = new EigenDecompositionImpl(m, MathUtils.SAFE_MIN).getSolver();
- assertTrue(es.isNonSingular());
+ Assert.assertTrue(es.isNonSingular());
RealMatrix inverse = es.getInverse();
RealMatrix error =
m.multiply(inverse).subtract(MatrixUtils.createRealIdentityMatrix(m.getRowDimension()));
- assertEquals(0, error.getNorm(), 4.0e-15);
+ Assert.assertEquals(0, error.getNorm(), 4.0e-15);
}
/** test solve dimension errors */
+ @Test
public void testSolveDimensionErrors() {
+ final double[] refValues = new double[] {
+ 2.003, 2.002, 2.001, 1.001, 1.000, 0.001
+ };
+ final RealMatrix matrix = EigenDecompositionImplTest.createTestMatrix(new Random(35992629946426l), refValues);
+
DecompositionSolver es = new EigenDecompositionImpl(matrix, MathUtils.SAFE_MIN).getSolver();
RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]);
try {
es.solve(b);
- fail("an exception should have been thrown");
+ Assert.fail("an exception should have been thrown");
} catch (MathIllegalArgumentException iae) {
// expected behavior
}
try {
es.solve(b.getColumn(0));
- fail("an exception should have been thrown");
+ Assert.fail("an exception should have been thrown");
} catch (MathIllegalArgumentException iae) {
// expected behavior
}
try {
es.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0)));
- fail("an exception should have been thrown");
+ Assert.fail("an exception should have been thrown");
} catch (MathIllegalArgumentException iae) {
// expected behavior
}
}
/** test solve */
+ @Test
public void testSolve() {
RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {
{ 91, 5, 29, 32, 40, 14 },
@@ -116,18 +118,18 @@ public class EigenSolverTest extends Tes
// using RealMatrix
RealMatrix solution=es.solve(b);
- assertEquals(0, solution.subtract(xRef).getNorm(), 2.5e-12);
+ Assert.assertEquals(0, solution.subtract(xRef).getNorm(), 2.5e-12);
// using double[]
for (int i = 0; i < b.getColumnDimension(); ++i) {
- assertEquals(0,
+ Assert.assertEquals(0,
new ArrayRealVector(es.solve(b.getColumn(i))).subtract(xRef.getColumnVector(i)).getNorm(),
2.0e-11);
}
// using Array2DRowRealMatrix
for (int i = 0; i < b.getColumnDimension(); ++i) {
- assertEquals(0,
+ Assert.assertEquals(0,
es.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(),
2.0e-11);
}
@@ -136,25 +138,9 @@ public class EigenSolverTest extends Tes
for (int i = 0; i < b.getColumnDimension(); ++i) {
ArrayRealVectorTest.RealVectorTestImpl v =
new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i));
- assertEquals(0,
+ Assert.assertEquals(0,
es.solve(v).subtract(xRef.getColumnVector(i)).getNorm(),
2.0e-11);
}
-
}
-
- @Override
- public void setUp() {
- refValues = new double[] {
- 2.003, 2.002, 2.001, 1.001, 1.000, 0.001
- };
- matrix = EigenDecompositionImplTest.createTestMatrix(new Random(35992629946426l), refValues);
- }
-
- @Override
- public void tearDown() {
- refValues = null;
- matrix = null;
- }
-
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/FieldLUDecompositionImplTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/FieldLUDecompositionImplTest.java?rev=1079350&r1=1079349&r2=1079350&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/FieldLUDecompositionImplTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/linear/FieldLUDecompositionImplTest.java Tue Mar 8 13:42:16 2011
@@ -17,14 +17,14 @@
package org.apache.commons.math.linear;
-import junit.framework.TestCase;
+import org.junit.Test;
+import org.junit.Assert;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.fraction.Fraction;
import org.apache.commons.math.fraction.FractionField;
-import org.apache.commons.math.exception.NonSquareMatrixException;
-public class FieldLUDecompositionImplTest extends TestCase {
+public class FieldLUDecompositionImplTest {
private Fraction[][] testData = {
{ new Fraction(1), new Fraction(2), new Fraction(3)},
{ new Fraction(2), new Fraction(5), new Fraction(3)},
@@ -53,24 +53,22 @@ public class FieldLUDecompositionImplTes
{ new Fraction(3), new Fraction(7), new Fraction(6), new Fraction(8) }
}; // 4th row = 1st + 2nd
- public FieldLUDecompositionImplTest(String name) {
- super(name);
- }
-
/** test dimensions */
+ @Test
public void testDimensions() {
FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<Fraction>(testData);
FieldLUDecomposition<Fraction> LU = new FieldLUDecompositionImpl<Fraction>(matrix);
- assertEquals(testData.length, LU.getL().getRowDimension());
- assertEquals(testData.length, LU.getL().getColumnDimension());
- assertEquals(testData.length, LU.getU().getRowDimension());
- assertEquals(testData.length, LU.getU().getColumnDimension());
- assertEquals(testData.length, LU.getP().getRowDimension());
- assertEquals(testData.length, LU.getP().getColumnDimension());
+ Assert.assertEquals(testData.length, LU.getL().getRowDimension());
+ Assert.assertEquals(testData.length, LU.getL().getColumnDimension());
+ Assert.assertEquals(testData.length, LU.getU().getRowDimension());
+ Assert.assertEquals(testData.length, LU.getU().getColumnDimension());
+ Assert.assertEquals(testData.length, LU.getP().getRowDimension());
+ Assert.assertEquals(testData.length, LU.getP().getColumnDimension());
}
/** test non-square matrix */
+ @Test
public void testNonSquare() {
try {
new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(new Fraction[][] {
@@ -78,13 +76,14 @@ public class FieldLUDecompositionImplTes
{ Fraction.ZERO, Fraction.ZERO },
{ Fraction.ZERO, Fraction.ZERO }
}));
- fail("Expected NonSquareMatrixException");
+ Assert.fail("Expected NonSquareMatrixException");
} catch (NonSquareMatrixException ime) {
// expected behavior
}
}
/** test PA = LU */
+ @Test
public void testPAEqualLU() {
FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<Fraction>(testData);
FieldLUDecomposition<Fraction> lu = new FieldLUDecompositionImpl<Fraction>(matrix);
@@ -112,44 +111,47 @@ public class FieldLUDecompositionImplTes
matrix = new Array2DRowFieldMatrix<Fraction>(singular);
lu = new FieldLUDecompositionImpl<Fraction>(matrix);
- assertFalse(lu.getSolver().isNonSingular());
- assertNull(lu.getL());
- assertNull(lu.getU());
- assertNull(lu.getP());
+ Assert.assertFalse(lu.getSolver().isNonSingular());
+ Assert.assertNull(lu.getL());
+ Assert.assertNull(lu.getU());
+ Assert.assertNull(lu.getP());
matrix = new Array2DRowFieldMatrix<Fraction>(bigSingular);
lu = new FieldLUDecompositionImpl<Fraction>(matrix);
- assertFalse(lu.getSolver().isNonSingular());
- assertNull(lu.getL());
- assertNull(lu.getU());
- assertNull(lu.getP());
+ Assert.assertFalse(lu.getSolver().isNonSingular());
+ Assert.assertNull(lu.getL());
+ Assert.assertNull(lu.getU());
+ Assert.assertNull(lu.getP());
}
/** test that L is lower triangular with unit diagonal */
+ @Test
public void testLLowerTriangular() {
FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<Fraction>(testData);
FieldMatrix<Fraction> l = new FieldLUDecompositionImpl<Fraction>(matrix).getL();
for (int i = 0; i < l.getRowDimension(); i++) {
- assertEquals(Fraction.ONE, l.getEntry(i, i));
+ Assert.assertEquals(Fraction.ONE, l.getEntry(i, i));
for (int j = i + 1; j < l.getColumnDimension(); j++) {
- assertEquals(Fraction.ZERO, l.getEntry(i, j));
+ Assert.assertEquals(Fraction.ZERO, l.getEntry(i, j));
}
}
}
/** test that U is upper triangular */
+ @Test
public void testUUpperTriangular() {
FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<Fraction>(testData);
FieldMatrix<Fraction> u = new FieldLUDecompositionImpl<Fraction>(matrix).getU();
for (int i = 0; i < u.getRowDimension(); i++) {
for (int j = 0; j < i; j++) {
- assertEquals(Fraction.ZERO, u.getEntry(i, j));
+ Assert.assertEquals(Fraction.ZERO, u.getEntry(i, j));
}
}
}
/** test that P is a permutation matrix */
+ @Test
public void testPPermutation() {
FieldMatrix<Fraction> matrix = new Array2DRowFieldMatrix<Fraction>(testData);
FieldMatrix<Fraction> p = new FieldLUDecompositionImpl<Fraction>(matrix).getP();
@@ -177,9 +179,9 @@ public class FieldLUDecompositionImplTes
++otherCount;
}
}
- assertEquals(p.getColumnDimension() - 1, zeroCount);
- assertEquals(1, oneCount);
- assertEquals(0, otherCount);
+ Assert.assertEquals(p.getColumnDimension() - 1, zeroCount);
+ Assert.assertEquals(1, oneCount);
+ Assert.assertEquals(0, otherCount);
}
for (int j = 0; j < p.getColumnDimension(); j++) {
@@ -196,26 +198,28 @@ public class FieldLUDecompositionImplTes
++otherCount;
}
}
- assertEquals(p.getRowDimension() - 1, zeroCount);
- assertEquals(1, oneCount);
- assertEquals(0, otherCount);
+ Assert.assertEquals(p.getRowDimension() - 1, zeroCount);
+ Assert.assertEquals(1, oneCount);
+ Assert.assertEquals(0, otherCount);
}
}
/** test singular */
+ @Test
public void testSingular() {
FieldLUDecomposition<Fraction> lu =
new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(testData));
- assertTrue(lu.getSolver().isNonSingular());
+ Assert.assertTrue(lu.getSolver().isNonSingular());
lu = new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(singular));
- assertFalse(lu.getSolver().isNonSingular());
+ Assert.assertFalse(lu.getSolver().isNonSingular());
lu = new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(bigSingular));
- assertFalse(lu.getSolver().isNonSingular());
+ Assert.assertFalse(lu.getSolver().isNonSingular());
}
/** test matrices values */
+ @Test
public void testMatricesValues1() {
FieldLUDecomposition<Fraction> lu =
new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(testData));
@@ -245,17 +249,18 @@ public class FieldLUDecompositionImplTes
TestUtils.assertEquals(pRef, p);
int[] pivot = lu.getPivot();
for (int i = 0; i < pivotRef.length; ++i) {
- assertEquals(pivotRef[i], pivot[i]);
+ Assert.assertEquals(pivotRef[i], pivot[i]);
}
// check the same cached instance is returned the second time
- assertTrue(l == lu.getL());
- assertTrue(u == lu.getU());
- assertTrue(p == lu.getP());
+ Assert.assertTrue(l == lu.getL());
+ Assert.assertTrue(u == lu.getU());
+ Assert.assertTrue(p == lu.getP());
}
/** test matrices values */
+ @Test
public void testMatricesValues2() {
FieldLUDecomposition<Fraction> lu =
new FieldLUDecompositionImpl<Fraction>(new Array2DRowFieldMatrix<Fraction>(luData));
@@ -285,14 +290,12 @@ public class FieldLUDecompositionImplTes
TestUtils.assertEquals(pRef, p);
int[] pivot = lu.getPivot();
for (int i = 0; i < pivotRef.length; ++i) {
- assertEquals(pivotRef[i], pivot[i]);
+ Assert.assertEquals(pivotRef[i], pivot[i]);
}
// check the same cached instance is returned the second time
- assertTrue(l == lu.getL());
- assertTrue(u == lu.getU());
- assertTrue(p == lu.getP());
-
+ Assert.assertTrue(l == lu.getL());
+ Assert.assertTrue(u == lu.getU());
+ Assert.assertTrue(p == lu.getP());
}
-
}