From 730dad4cdbe6e1dc00ab170791e8d223a8cfc48d Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Mon, 25 Oct 2004 02:23:29 +0000 Subject: [PATCH] Added tests for row, column, subMatrix accessors, equals and hashcode, toString. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141482 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/linear/BigMatrixImplTest.java | 228 ++++++++++++++++-- 1 file changed, 202 insertions(+), 26 deletions(-) diff --git a/src/test/org/apache/commons/math/linear/BigMatrixImplTest.java b/src/test/org/apache/commons/math/linear/BigMatrixImplTest.java index 87028ad38..032eebce1 100644 --- a/src/test/org/apache/commons/math/linear/BigMatrixImplTest.java +++ b/src/test/org/apache/commons/math/linear/BigMatrixImplTest.java @@ -24,38 +24,73 @@ import java.math.BigDecimal; /** * Test cases for the {@link BigMatrixImpl} class. * - * @version $Revision: 1.3 $ $Date: 2004/09/05 01:19:23 $ + * @version $Revision: 1.4 $ $Date: 2004/10/25 02:23:29 $ */ public final class BigMatrixImplTest extends TestCase { - private double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} }; - private String[][] testDataString = { {"1","2","3"}, {"2","5","3"}, {"1","0","8"} }; - private double[][] testDataLU = {{2d, 5d, 3d}, {.5d, -2.5d, 6.5d}, {0.5d, 0.2d, .2d}}; - private double[][] testDataPlus2 = { {3d,4d,5d}, {4d,7d,5d}, {3d,2d,10d} }; - private double[][] testDataMinus = { {-1d,-2d,-3d}, {-2d,-5d,-3d}, - {-1d,0d,-8d} }; - private double[] testDataRow1 = {1d,2d,3d}; - private double[] testDataCol3 = {3d,3d,8d}; - private double[][] testDataInv = + // 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}, + {-1d,0d,-8d} }; + protected double[] testDataRow1 = {1d,2d,3d}; + protected double[] testDataCol3 = {3d,3d,8d}; + protected double[][] testDataInv = { {-40d,16d,9d}, {13d,-5d,-3d}, {5d,-2d,-1d} }; - private double[] preMultTest = {8,12,33}; - private double[][] testData2 ={ {1d,2d,3d}, {2d,5d,3d}}; - private double[][] testData2T = { {1d,2d}, {2d,5d}, {3d,3d}}; - private double[][] testDataPlusInv = + 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 = { {-39d,18d,12d}, {15d,0d,0d}, {6d,-2d,7d} }; - private double[][] id = { {1d,0d,0d}, {0d,1d,0d}, {0d,0d,1d} }; - private double[][] luData = { {2d,3d,3d}, {0d,5d,7d}, {6d,9d,8d} }; - private double[][] luDataLUDecomposition = { {6d,9d,8d}, {0d,5d,7d}, {0.33333333333333,0d,0.33333333333333} }; - private double[][] singular = { {2d,3d}, {2d,3d} }; - private double[][] bigSingular = {{1d,2d,3d,4d}, {2d,5d,3d,4d}, - {7d,3d,256d,1930d}, {3d,7d,6d,8d}}; // 4th row = 1st + 2nd - private double[][] detData = { {1d,2d,3d}, {4d,5d,6d}, {7d,8d,10d} }; - private double[][] detData2 = { {1d, 3d}, {2d, 4d}}; - private double[] testVector = {1,2,3}; - private double[] testVector2 = {1,2,3,4}; - private double entryTolerance = 10E-16; - private double normTolerance = 10E-14; + + // 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}}; + // array selections + protected double[][] subRows02Cols13 = { {2, 4}, {4, 8}}; + protected double[][] subRows03Cols12 = { {2, 3}, {5, 6}}; + protected double[][] subRows03Cols123 = { {2, 3, 4} , {5, 6, 7}}; + // effective permutations + protected double[][] subRows20Cols123 = { {4, 6, 8} , {2, 3, 4}}; + protected double[][] subRows31Cols31 = {{7, 5}, {4.5, 2.5}}; + // contiguous ranges + protected double[][] subRows01Cols23 = {{3,4} , {3.5, 4.5}}; + protected double[][] subRows23Cols00 = {{2} , {4}}; + protected double[][] subRows00Cols33 = {{4}}; + // row matrices + protected double[][] subRow0 = {{1,2,3,4}}; + protected double[][] subRow3 = {{4,5,6,7}}; + // 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); @@ -501,6 +536,147 @@ public final class BigMatrixImplTest extends TestCase { } } + /** + * test submatrix accessors + */ + public void testSubMatrix() { + BigMatrix m = new BigMatrixImpl(subTestData); + BigMatrix mRows23Cols00 = new BigMatrixImpl(subRows23Cols00); + BigMatrix mRows00Cols33 = new BigMatrixImpl(subRows00Cols33); + BigMatrix mRows01Cols23 = new BigMatrixImpl(subRows01Cols23); + BigMatrix mRows02Cols13 = new BigMatrixImpl(subRows02Cols13); + BigMatrix mRows03Cols12 = new BigMatrixImpl(subRows03Cols12); + BigMatrix mRows03Cols123 = new BigMatrixImpl(subRows03Cols123); + BigMatrix mRows20Cols123 = new BigMatrixImpl(subRows20Cols123); + BigMatrix mRows31Cols31 = new BigMatrixImpl(subRows31Cols31); + assertEquals("Rows23Cols00", mRows23Cols00, + m.getSubMatrix(2 , 3 , 0, 0)); + assertEquals("Rows00Cols33", mRows00Cols33, + m.getSubMatrix(0 , 0 , 3, 3)); + assertEquals("Rows01Cols23", mRows01Cols23, + m.getSubMatrix(0 , 1 , 2, 3)); + assertEquals("Rows02Cols13", mRows02Cols13, + m.getSubMatrix(new int[] {0,2}, new int[] {1,3})); + assertEquals("Rows03Cols12", mRows03Cols12, + m.getSubMatrix(new int[] {0,3}, new int[] {1,2})); + assertEquals("Rows03Cols123", mRows03Cols123, + 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})); + assertEquals("Rows31Cols31", mRows31Cols31, + m.getSubMatrix(new int[] {3,1}, new int[] {3,1})); + assertEquals("Rows31Cols31", mRows31Cols31, + m.getSubMatrix(new int[] {3,1}, new int[] {3,1})); + + try { + m.getSubMatrix(1,0,2,4); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + try { + m.getSubMatrix(-1,1,2,2); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + try { + m.getSubMatrix(1,0,2,2); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + try { + m.getSubMatrix(1,0,2,4); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + try { + m.getSubMatrix(new int[] {}, new int[] {0}); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + try { + m.getSubMatrix(new int[] {0}, new int[] {4}); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + } + + public void testGetColumnMatrix() { + BigMatrix m = new BigMatrixImpl(subTestData); + BigMatrix mColumn1 = new BigMatrixImpl(subColumn1); + BigMatrix mColumn3 = new BigMatrixImpl(subColumn3); + assertEquals("Column1", mColumn1, + m.getColumnMatrix(1)); + assertEquals("Column3", mColumn3, + m.getColumnMatrix(3)); + try { + m.getColumnMatrix(-1); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + try { + m.getColumnMatrix(4); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + } + + public void testGetRowMatrix() { + BigMatrix m = new BigMatrixImpl(subTestData); + BigMatrix mRow0 = new BigMatrixImpl(subRow0); + BigMatrix mRow3 = new BigMatrixImpl(subRow3); + assertEquals("Row0", mRow0, + m.getRowMatrix(0)); + assertEquals("Row3", mRow3, + m.getRowMatrix(3)); + try { + m.getRowMatrix(-1); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + try { + m.getRowMatrix(4); + fail("Expecting MatrixIndexException"); + } catch (MatrixIndexException ex) { + // expected + } + } + + public void testEqualsAndHashCode() { + BigMatrixImpl m = new BigMatrixImpl(testData); + BigMatrixImpl m1 = (BigMatrixImpl) m.copy(); + BigMatrixImpl mt = (BigMatrixImpl) 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 BigMatrixImpl(bigSingular))); + // Different scales make BigDecimals, so matrices unequal + m = new BigMatrixImpl(new String[][] {{"2.0"}}); + m1 = new BigMatrixImpl(new String[][] {{"2.00"}}); + 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}}", + m.toString()); + m = new BigMatrixImpl(); + assertEquals("BigMatrixImpl{}", + m.toString()); + } + //--------------- -----------------Protected methods /** verifies that two matrices are close (1-norm) */