JAL-2403 additional methods to support distance/similarity score
[jalview.git] / test / jalview / math / MatrixTest.java
index 961602d..0d066dd 100644 (file)
@@ -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);
+  }
+
 }