JAL-3438 spotless for 2.11.2.0
[jalview.git] / test / jalview / math / SparseMatrixTest.java
index 607d415..7295fad 100644 (file)
@@ -20,7 +20,8 @@ public class SparseMatrixTest
   public void testConstructor()
   {
     MatrixI m1 = new SparseMatrix(
-            new double[][] { { 2, 0, 4 }, { 0, 6, 0 } });
+            new double[][]
+            { { 2, 0, 4 }, { 0, 6, 0 } });
     assertEquals(m1.getValue(0, 0), 2d);
     assertEquals(m1.getValue(0, 1), 0d);
     assertEquals(m1.getValue(0, 2), 4d);
@@ -33,7 +34,8 @@ public class SparseMatrixTest
   public void testTranspose()
   {
     MatrixI m1 = new SparseMatrix(
-            new double[][] { { 2, 0, 4 }, { 5, 6, 0 } });
+            new double[][]
+            { { 2, 0, 4 }, { 5, 6, 0 } });
     MatrixI m2 = m1.transpose();
     assertTrue(m2 instanceof SparseMatrix);
     assertEquals(m2.height(), 3);
@@ -45,6 +47,7 @@ public class SparseMatrixTest
     assertEquals(m2.getValue(2, 0), 4d);
     assertEquals(m2.getValue(2, 1), 0d);
   }
+
   @Test(groups = "Functional")
   public void testPreMultiply()
   {
@@ -80,11 +83,13 @@ public class SparseMatrixTest
 
   @Test(
     groups = "Functional",
-    expectedExceptions = { IllegalArgumentException.class })
+    expectedExceptions =
+    { IllegalArgumentException.class })
   public void testPreMultiply_tooManyColumns()
   {
     Matrix m1 = new SparseMatrix(
-            new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }); // 2x3
+            new double[][]
+            { { 2, 3, 4 }, { 3, 4, 5 } }); // 2x3
 
     /*
      * 2x3 times 2x3 invalid operation - 
@@ -96,11 +101,13 @@ public class SparseMatrixTest
 
   @Test(
     groups = "Functional",
-    expectedExceptions = { IllegalArgumentException.class })
+    expectedExceptions =
+    { IllegalArgumentException.class })
   public void testPreMultiply_tooFewColumns()
   {
     Matrix m1 = new SparseMatrix(
-            new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }); // 2x3
+            new double[][]
+            { { 2, 3, 4 }, { 3, 4, 5 } }); // 2x3
 
     /*
      * 3x2 times 3x2 invalid operation - 
@@ -109,7 +116,7 @@ public class SparseMatrixTest
     m1.preMultiply(m1);
     fail("Expected exception");
   }
-  
+
   @Test(groups = "Functional")
   public void testPostMultiply()
   {
@@ -122,8 +129,9 @@ public class SparseMatrixTest
      * (5040 50400)
      */
     MatrixI m1 = new SparseMatrix(new double[][] { { 2, 3 }, { 4, 5 } });
-    MatrixI m2 = new SparseMatrix(new double[][] { { 10, 100 },
-        { 1000, 10000 } });
+    MatrixI m2 = new SparseMatrix(
+            new double[][]
+            { { 10, 100 }, { 1000, 10000 } });
     MatrixI m3 = m1.postMultiply(m2);
     assertEquals(m3.getValue(0, 0), 3020d);
     assertEquals(m3.getValue(0, 1), 30200d);
@@ -134,7 +142,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 +162,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 +184,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 +208,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 +227,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 +255,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);
   }
 
@@ -285,7 +291,7 @@ public class SparseMatrixTest
     int rows = 6;
     int cols = rows;
     double[][] d = getSparseValues(rows, cols, 3);
-  
+
     /*
      * make a copy of the values so m1, m2 are not
      * sharing arrays!
@@ -304,11 +310,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 +322,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];
         }
       }
@@ -350,10 +360,11 @@ public class SparseMatrixTest
   @Test(groups = "Functional")
   public void testPreMultiply_sparseProduct()
   {
-    MatrixI m1 = new SparseMatrix(new double[][] { { 1 }, { 0 }, { 0 },
-        { 0 }, { 0 } }); // 5x1
+    MatrixI m1 = new SparseMatrix(
+            new double[][]
+            { { 1 }, { 0 }, { 0 }, { 0 }, { 0 } }); // 5x1
     MatrixI m2 = new SparseMatrix(new double[][] { { 1, 1, 1, 1 } }); // 1x4
-  
+
     /*
      * m1.m2 makes a row of 4 1's, and 4 rows of zeros
      * 20% non-zero so not 'sparse'
@@ -373,8 +384,43 @@ public class SparseMatrixTest
   @Test(groups = "Functional")
   public void testFillRatio()
   {
-    SparseMatrix m1 = new SparseMatrix(new double[][] { { 2, 0, 4, 1, 0 },
-    { 0, 6, 0, 0, 0 } });
+    SparseMatrix m1 = new SparseMatrix(
+            new double[][]
+            { { 2, 0, 4, 1, 0 }, { 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