X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fmath%2FMatrixTest.java;h=9baacd513ac2abc65490da9e337ea4e109c322a6;hb=e9a1c2c372f4bbf6cf658de3dba73ef326b20c20;hp=61b98f362a0ae39845954e2e00865acd231fed2d;hpb=df1447b81289138e8bd0caaf7c7117ad71f74f29;p=jalview.git diff --git a/test/jalview/math/MatrixTest.java b/test/jalview/math/MatrixTest.java index 61b98f3..9baacd5 100644 --- a/test/jalview/math/MatrixTest.java +++ b/test/jalview/math/MatrixTest.java @@ -1,6 +1,28 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.math; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotSame; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; @@ -59,7 +81,8 @@ public class MatrixTest @Test( groups = "Functional", - expectedExceptions = { IllegalArgumentException.class }) + expectedExceptions = + { IllegalArgumentException.class }) public void testPreMultiply_tooManyColumns() { Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }); // 2x3 @@ -74,7 +97,8 @@ public class MatrixTest @Test( groups = "Functional", - expectedExceptions = { IllegalArgumentException.class }) + expectedExceptions = + { IllegalArgumentException.class }) public void testPreMultiply_tooFewColumns() { Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }); // 2x3 @@ -86,9 +110,9 @@ public class MatrixTest m1.preMultiply(m1); fail("Expected exception"); } - - - private boolean matrixEquals(Matrix m1, Matrix m2) { + + private boolean matrixEquals(Matrix m1, Matrix m2) + { if (m1.width() != m2.width() || m1.height() != m2.height()) { return false; @@ -115,7 +139,9 @@ public class MatrixTest * (5040 50400) */ MatrixI m1 = new Matrix(new double[][] { { 2, 3 }, { 4, 5 } }); - MatrixI m2 = new Matrix(new double[][] { { 10, 100 }, { 1000, 10000 } }); + MatrixI m2 = new Matrix( + new double[][] + { { 10, 100 }, { 1000, 10000 } }); MatrixI m3 = m1.postMultiply(m2); assertEquals(Arrays.toString(m3.getRow(0)), "[3020.0, 30200.0]"); assertEquals(Arrays.toString(m3.getRow(1)), "[5040.0, 50400.0]"); @@ -187,8 +213,23 @@ public class MatrixTest } } Matrix m1 = new Matrix(in); + Matrix m2 = (Matrix) m1.copy(); + assertNotSame(m1, m2); assertTrue(matrixEquals(m1, m2)); + assertNull(m2.d); + assertNull(m2.e); + + /* + * now add d and e vectors and recopy + */ + m1.d = Arrays.copyOf(in[2], in[2].length); + m1.e = Arrays.copyOf(in[4], in[4].length); + m2 = (Matrix) m1.copy(); + assertNotSame(m2.d, m1.d); + assertNotSame(m2.e, m1.e); + assertEquals(m2.d, m1.d); + assertEquals(m2.e, m1.e); } /** @@ -200,7 +241,7 @@ public class MatrixTest { int n = Integer.parseInt(args[0]); double[][] in = new double[n][n]; - + for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) @@ -208,31 +249,31 @@ public class MatrixTest in[i][j] = Math.random(); } } - + Matrix origmat = new Matrix(in); - + // System.out.println(" --- Original matrix ---- "); // / origmat.print(System.out); // System.out.println(); // System.out.println(" --- transpose matrix ---- "); MatrixI trans = origmat.transpose(); - + // trans.print(System.out); // System.out.println(); // System.out.println(" --- OrigT * Orig ---- "); MatrixI symm = trans.postMultiply(origmat); - + // symm.print(System.out); // System.out.println(); // Copy the symmetric matrix for later // Matrix origsymm = symm.copy(); - + // This produces the tridiagonal transformation matrix // long tstart = System.currentTimeMillis(); symm.tred(); - + // long tend = System.currentTimeMillis(); - + // System.out.println("Time take for tred = " + (tend-tstart) + "ms"); // System.out.println(" ---Tridiag transform matrix ---"); // symm.print(System.out); @@ -247,7 +288,7 @@ public class MatrixTest // tstart = System.currentTimeMillis(); symm.tqli(); // tend = System.currentTimeMillis(); - + // System.out.println("Time take for tqli = " + (tend-tstart) + " ms"); // System.out.println(" --- New diagonalization matrix ---"); // symm.print(System.out); @@ -317,7 +358,7 @@ public class MatrixTest } } return d; - + } /** @@ -333,7 +374,7 @@ public class MatrixTest int rows = 10; int cols = rows; double[][] d = getSparseValues(rows, cols, 3); - + /* * make a copy of the values so m1, m2 are not * sharing arrays! @@ -354,7 +395,7 @@ public class MatrixTest assertMatricesMatch(m1, m2); } - private void assertMatricesMatch(MatrixI m1, MatrixI m2) + public static void assertMatricesMatch(MatrixI m1, MatrixI m2) { if (m1.height() != m2.height()) { @@ -376,8 +417,10 @@ public class MatrixTest } } } - ArrayAsserts.assertArrayEquals(m1.getD(), m2.getD(), 0.00001d); - ArrayAsserts.assertArrayEquals(m1.getE(), m2.getE(), 0.00001d); + ArrayAsserts.assertArrayEquals("D vector", m1.getD(), m2.getD(), + 0.00001d); + ArrayAsserts.assertArrayEquals("E vector", m1.getE(), m2.getE(), + 0.00001d); } @Test(groups = "Functional") @@ -393,8 +436,8 @@ public class MatrixTest * normal case */ double[][] vals = new double[2][]; - vals[0] = new double[] {7d, 1d, -2.3d}; - vals[1] = new double[] {-12d, 94.3d, -102.34d}; + vals[0] = new double[] { 7d, 1d, -2.3d }; + vals[1] = new double[] { -12d, 94.3d, -102.34d }; m = new Matrix(vals); double[] minMax = m.findMinMax(); assertEquals(minMax[0], -102.34d); @@ -443,7 +486,8 @@ public class MatrixTest public void testReverseRange_maxToZero() { Matrix m1 = new Matrix( - new double[][] { { 2, 3.5, 4 }, { -3.4, 4, 15 } }); + new double[][] + { { 2, 3.5, 4 }, { -3.4, 4, 15 } }); /* * subtract all from max: range -3.4 to 15 becomes 18.4 to 0 @@ -475,8 +519,9 @@ public class MatrixTest public void testReverseRange_swapMinMax() { Matrix m1 = new Matrix( - new double[][] { { 2, 3.5, 4 }, { -3.4, 4, 15 } }); - + new double[][] + { { 2, 3.5, 4 }, { -3.4, 4, 15 } }); + /* * swap all values in min-max range * = subtract from (min + max = 11.6) @@ -489,7 +534,7 @@ public class MatrixTest assertEquals(m1.getValue(1, 0), 15d, DELTA); assertEquals(m1.getValue(1, 1), 7.6d, DELTA); assertEquals(m1.getValue(1, 2), -3.4d, DELTA); - + /* * repeat operation - original values restored */ @@ -502,4 +547,59 @@ public class MatrixTest assertEquals(m1.getValue(1, 2), 15d, DELTA); } + @Test(groups = "Functional") + public void testMultiply() + { + Matrix m = new Matrix( + new double[][] + { { 2, 3.5, 4 }, { -3.4, 4, 15 } }); + m.multiply(2d); + assertEquals(m.getValue(0, 0), 4d, DELTA); + assertEquals(m.getValue(0, 1), 7d, DELTA); + assertEquals(m.getValue(0, 2), 8d, DELTA); + assertEquals(m.getValue(1, 0), -6.8d, DELTA); + assertEquals(m.getValue(1, 1), 8d, DELTA); + assertEquals(m.getValue(1, 2), 30d, DELTA); + } + + @Test(groups = "Functional") + public void testConstructor() + { + double[][] values = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } }; + Matrix m = new Matrix(values); + assertEquals(m.getValue(0, 0), 1d, DELTA); + + /* + * verify the matrix has a copy of the original array + */ + assertNotSame(values[0], m.getRow(0)); + values[0][0] = -1d; + assertEquals(m.getValue(0, 0), 1d, DELTA); // unchanged + } + + @Test(groups = "Functional") + public void testEquals() + { + double[][] values = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } }; + Matrix m1 = new Matrix(values); + double[][] values2 = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } }; + Matrix m2 = new Matrix(values2); + + double delta = 0.0001d; + assertTrue(m1.equals(m1, delta)); + assertTrue(m1.equals(m2, delta)); + assertTrue(m2.equals(m1, delta)); + + double[][] values3 = new double[][] { { 1, 2, 3 }, { 4, 5, 7 } }; + m2 = new Matrix(values3); + assertFalse(m1.equals(m2, delta)); + assertFalse(m2.equals(m1, delta)); + + // must be same shape + values2 = new double[][] { { 1, 2, 3 } }; + m2 = new Matrix(values2); + assertFalse(m2.equals(m1, delta)); + + assertFalse(m1.equals(null, delta)); + } }