From d6a30c6516e8e2f923c8c2f6dce7592cb3b7d974 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 23 Feb 2017 15:39:01 +0000 Subject: [PATCH] JAL-2403 additional methods to support distance/similarity score conversion --- src/jalview/math/Matrix.java | 47 +++++++++++++++++++++++++++++++ src/jalview/math/MatrixI.java | 17 +++++++++++ test/jalview/math/MatrixTest.java | 56 +++++++++++++++++++++++++++++++++++-- 3 files changed, 118 insertions(+), 2 deletions(-) diff --git a/src/jalview/math/Matrix.java b/src/jalview/math/Matrix.java index de0bf77..3f0bae4 100755 --- a/src/jalview/math/Matrix.java +++ b/src/jalview/math/Matrix.java @@ -532,6 +532,7 @@ public class Matrix implements MatrixI return value[i][j]; } + @Override public void setValue(int i, int j, double val) { value[i][j] = val; @@ -889,4 +890,50 @@ public class Matrix implements MatrixI System.arraycopy(value[i], 0, row, 0, cols); return row; } + + @Override + public double getMaxValue() + { + if (value == null) + { + return 0; + } + double max = -Double.MAX_VALUE; + for (double[] row : value) + { + if (row != null) + { + for (double x : row) + { + if (x > max) + { + max = x; + } + } + } + } + return max; + } + + @Override + public void subtractAllFrom(double val) + { + if (value == null) + { + return; + } + + for (double[] row : value) + { + if (row != null) + { + int j = 0; + for (double x : row) + { + row[j] = val - x; + j++; + } + } + } + } } diff --git a/src/jalview/math/MatrixI.java b/src/jalview/math/MatrixI.java index d74a98b..05fe22f 100644 --- a/src/jalview/math/MatrixI.java +++ b/src/jalview/math/MatrixI.java @@ -28,6 +28,15 @@ public interface MatrixI double getValue(int i, int j); /** + * Sets the value at row i, colum j + * + * @param i + * @param j + * @param d + */ + void setValue(int i, int j, double d); + + /** * Answers a copy of the values in the i'th row * * @return @@ -56,4 +65,12 @@ public interface MatrixI void tred(); + double getMaxValue(); + + /** + * Update each value in the matrix by subtracting it from the given value + * + * @param val + */ + void subtractAllFrom(double val); } diff --git a/test/jalview/math/MatrixTest.java b/test/jalview/math/MatrixTest.java index 961602d..0d066dd 100644 --- a/test/jalview/math/MatrixTest.java +++ b/test/jalview/math/MatrixTest.java @@ -17,8 +17,8 @@ public class MatrixTest @Test(groups = "Timing") public void testPreMultiply_timing() { - int rows = 500; - int cols = 1000; + int rows = 50; // increase to stress test timing + int cols = 100; double[][] d1 = new double[rows][cols]; double[][] d2 = new double[cols][rows]; Matrix m1 = new Matrix(d1); @@ -378,4 +378,56 @@ public class MatrixTest ArrayAsserts.assertArrayEquals(m1.getD(), m2.getD(), 0.00001d); ArrayAsserts.assertArrayEquals(m1.getE(), m2.getE(), 0.00001d); } + + @Test(groups = "Functional") + public void testGetMaxValue() { + double[][] vals = new double[2][]; + vals[0] = new double[] {7d, 1d, -2.3d}; + vals[1] = new double[] {-12d, 94.3d, -102.34d}; + MatrixI m = new Matrix(vals); + assertEquals(m.getMaxValue(), 94.3d); + } + + @Test(groups = { "Functional", "Timing" }) + public void testGetMaxValue_timing() + { + Random r = new Random(); + int size = 1000; // increase to stress test timing + double[][] vals = new double[size][size]; + double max = -Double.MAX_VALUE; + for (int i = 0; i < size; i++) + { + vals[i] = new double[size]; + for (int j = 0; j < size; j++) + { + double d = r.nextDouble(); + if (d > max) + { + max = d; + } + vals[i][j] = d; + } + i++; + } + MatrixI m = new Matrix(vals); + long now = System.currentTimeMillis(); + double theMax = m.getMaxValue(); + System.out.println(String.format("getMaxValue for %d x %d took %dms", + size, size, (System.currentTimeMillis() - now))); + assertEquals(theMax, max); + } + + @Test(groups = "Functional") + public void testSubtractAllFrom() + { + Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { -3, 4, 15 } }); + m1.subtractAllFrom(12.5); + assertEquals(m1.getValue(0, 0), 10.5d); + assertEquals(m1.getValue(0, 1), 9.5d); + assertEquals(m1.getValue(0, 2), 8.5d); + assertEquals(m1.getValue(1, 0), 15.5d); + assertEquals(m1.getValue(1, 1), 8.5d); + assertEquals(m1.getValue(1, 2), -2.5d); + } + } -- 1.7.10.2