X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=test%2Fjalview%2Fmath%2FSparseMatrixTest.java;h=3c2ccaa21f8aac9f889c34cf1246e11c2b8e44d8;hb=80ff57c095b792b8e8e123ddb0eff4d1780d6cbd;hp=607d415fd15dc63b43ef97ae9158eaa58e2351b4;hpb=ab7ccf4373a27d71bca31bd6356c1390d9a0054c;p=jalview.git diff --git a/test/jalview/math/SparseMatrixTest.java b/test/jalview/math/SparseMatrixTest.java index 607d415..3c2ccaa 100644 --- a/test/jalview/math/SparseMatrixTest.java +++ b/test/jalview/math/SparseMatrixTest.java @@ -134,7 +134,7 @@ public class SparseMatrixTest * also check m2.preMultiply(m1) - should be same as m1.postMultiply(m2) */ MatrixI m4 = m2.preMultiply(m1); - assertMatricesMatch(m3, m4); + assertMatricesMatch(m3, m4, 0.00001d); /* * m1 has more rows than columns @@ -154,7 +154,7 @@ public class SparseMatrixTest assertEquals(m3.getValue(1, 2), 3000d); m4 = m2.preMultiply(m1); - assertMatricesMatch(m3, m4); + assertMatricesMatch(m3, m4, 0.00001d); /* * m1 has more columns than rows @@ -176,7 +176,7 @@ public class SparseMatrixTest * and check premultiply equivalent */ m4 = m2.preMultiply(m1); - assertMatricesMatch(m3, m4); + assertMatricesMatch(m3, m4, 0.00001d); } @Test(groups = "Timing") @@ -200,10 +200,8 @@ public class SparseMatrixTest { /* * make a pseudo-random symmetric matrix as required for tred/tqli - * note: test fails for matrices larger than 6x6 due to double value - * rounding only (random values result in very small values) */ - int rows = 6; + int rows = 10; int cols = rows; double[][] d = getSparseValues(rows, cols, 3); @@ -221,13 +219,13 @@ public class SparseMatrixTest } Matrix m1 = new Matrix(d); Matrix m2 = new SparseMatrix(d1); - assertMatricesMatch(m1, m2); // sanity check + assertMatricesMatch(m1, m2, 0.00001d); // sanity check m1.tred(); m2.tred(); - assertMatricesMatch(m1, m2); + assertMatricesMatch(m1, m2, 0.00001d); } - private void assertMatricesMatch(MatrixI m1, MatrixI m2) + private void assertMatricesMatch(MatrixI m1, MatrixI m2, double delta) { if (m1.height() != m2.height()) { @@ -249,7 +247,7 @@ public class SparseMatrixTest } } } - ArrayAsserts.assertArrayEquals(m1.getD(), m2.getD(), 0.00001d); + ArrayAsserts.assertArrayEquals(m1.getD(), m2.getD(), delta); ArrayAsserts.assertArrayEquals(m1.getE(), m2.getE(), 0.00001d); } @@ -304,11 +302,11 @@ public class SparseMatrixTest // have to do tred() before doing tqli() m1.tred(); m2.tred(); - assertMatricesMatch(m1, m2); + assertMatricesMatch(m1, m2, 0.00001d); m1.tqli(); m2.tqli(); - assertMatricesMatch(m1, m2); + assertMatricesMatch(m1, m2, 0.00001d); } /** @@ -316,25 +314,29 @@ public class SparseMatrixTest * * @param rows * @param cols - * @param fraction - * one n fraction entries will be non-zero + * @param occupancy + * one in 'occupancy' entries will be non-zero * @return */ - public double[][] getSparseValues(int rows, int cols, int fraction) + public double[][] getSparseValues(int rows, int cols, int occupancy) { + /* + * generate whole number values between -12 and +12 + * (to mimic score matrices used in Jalview) + */ double[][] d = new double[rows][cols]; int m = 0; for (int i = 0; i < rows; i++) { - if (++m % fraction == 0) + if (++m % occupancy == 0) { - d[i][i] = r.nextDouble(); // diagonal + d[i][i] = r.nextInt() % 13; // diagonal } for (int j = 0; j < i; j++) { - if (++m % fraction == 0) + if (++m % occupancy == 0) { - d[i][j] = r.nextDouble(); + d[i][j] = r.nextInt() % 13; d[j][i] = d[i][j]; } } @@ -377,4 +379,38 @@ public class SparseMatrixTest { 0, 6, 0, 0, 0 } }); assertEquals(m1.getFillRatio(), 0.4f); } + + /** + * Verify that the results of method tred() are the same if the calculation is + * redone + */ + @Test(groups = "Functional") + public void testTred_reproducible() + { + /* + * make a pseudo-random symmetric matrix as required for tred/tqli + */ + 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! + */ + double[][] d1 = new double[rows][cols]; + for (int row = 0; row < rows; row++) + { + for (int col = 0; col < cols; col++) + { + d1[row][col] = d[row][col]; + } + } + Matrix m1 = new SparseMatrix(d); + Matrix m2 = new SparseMatrix(d1); + assertMatricesMatch(m1, m2, 1.0e16); // sanity check + m1.tred(); + m2.tred(); + assertMatricesMatch(m1, m2, 0.00001d); + } } \ No newline at end of file