Merge branch 'kjvdh/features/PhylogenyViewer_tabbedsupport' into merge/2_11_2/kjvdh...
[jalview.git] / src / jalview / math / Matrix.java
index 5c264a8..b5a2b33 100755 (executable)
@@ -24,6 +24,7 @@ import jalview.util.Format;
 import jalview.util.MessageManager;
 
 import java.io.PrintStream;
+import java.util.Arrays;
 
 /**
  * A class to model rectangular matrices of double values and operations on them
@@ -31,37 +32,40 @@ import java.io.PrintStream;
 public class Matrix implements MatrixI
 {
   /*
-   * the cell values in row-major order
+   * maximum number of iterations for tqli
    */
-  private double[][] value;
+  private static final int MAX_ITER = 45;
+  // fudge - add 15 iterations, just in case
 
   /*
    * the number of rows
    */
-  protected int rows;
+  final protected int rows;
 
   /*
    * the number of columns
    */
-  protected int cols;
+  final protected int cols;
+
+  /*
+   * the cell values in row-major order
+   */
+  private double[][] value;
 
   protected double[] d; // Diagonal
 
   protected double[] e; // off diagonal
 
   /**
-   * maximum number of iterations for tqli
+   * Constructor given number of rows and columns
    * 
+   * @param colCount
+   * @param rowCount
    */
-  private static final int maxIter = 45; // fudge - add 15 iterations, just in
-                                         // case
-
-  /**
-   * Default constructor
-   */
-  public Matrix()
+  protected Matrix(int rowCount, int colCount)
   {
-
+    rows = rowCount;
+    cols = colCount;
   }
 
   /**
@@ -103,11 +107,6 @@ public class Matrix implements MatrixI
     }
   }
 
-  /**
-   * Returns a new matrix which is the transpose of this one
-   * 
-   * @return
-   */
   @Override
   public MatrixI transpose()
   {
@@ -145,18 +144,6 @@ public class Matrix implements MatrixI
     }
   }
 
-  /**
-   * Returns a new matrix which is the result of premultiplying this matrix by
-   * the supplied argument. If this of size AxB (A rows and B columns), and the
-   * argument is CxA (C rows and A columns), the result is of size CxB.
-   * 
-   * @param in
-   * 
-   * @return
-   * @throws IllegalArgumentException
-   *           if the number of columns in the pre-multiplier is not equal to
-   *           the number of rows in the multiplicand (this)
-   */
   @Override
   public MatrixI preMultiply(MatrixI in)
   {
@@ -208,21 +195,6 @@ public class Matrix implements MatrixI
     return out;
   }
 
-  /**
-   * Returns a new matrix which is the result of postmultiplying this matrix by
-   * the supplied argument. If this of size AxB (A rows and B columns), and the
-   * argument is BxC (B rows and C columns), the result is of size AxC.
-   * <p>
-   * This method simply returns the result of in.preMultiply(this)
-   * 
-   * @param in
-   * 
-   * @return
-   * @throws IllegalArgumentException
-   *           if the number of rows in the post-multiplier is not equal to the
-   *           number of columns in the multiplicand (this)
-   * @see #preMultiply(Matrix)
-   */
   @Override
   public MatrixI postMultiply(MatrixI in)
   {
@@ -234,11 +206,6 @@ public class Matrix implements MatrixI
     return in.preMultiply(this);
   }
 
-  /**
-   * Answers a new matrix with a copy of the values in this one
-   * 
-   * @return
-   */
   @Override
   public MatrixI copy()
   {
@@ -249,7 +216,17 @@ public class Matrix implements MatrixI
       System.arraycopy(value[i], 0, newmat[i], 0, value[i].length);
     }
 
-    return new Matrix(newmat);
+    Matrix m = new Matrix(newmat);
+    if (this.d != null)
+    {
+      m.d = Arrays.copyOf(this.d, this.d.length);
+    }
+    if (this.e != null)
+    {
+      m.e = Arrays.copyOf(this.e, this.e.length);
+    }
+
+    return m;
   }
 
   /**
@@ -479,11 +456,11 @@ public class Matrix implements MatrixI
         {
           iter++;
 
-          if (iter == maxIter)
+          if (iter == MAX_ITER)
           {
             throw new Exception(MessageManager.formatMessage(
                     "exception.matrix_too_many_iteration", new String[]
-                    { "tqli", Integer.valueOf(maxIter).toString() }));
+                    { "tqli", Integer.valueOf(MAX_ITER).toString() }));
           }
           else
           {
@@ -743,11 +720,11 @@ public class Matrix implements MatrixI
         {
           iter++;
 
-          if (iter == maxIter)
+          if (iter == MAX_ITER)
           {
             throw new Exception(MessageManager.formatMessage(
                     "exception.matrix_too_many_iteration", new String[]
-                    { "tqli2", Integer.valueOf(maxIter).toString() }));
+                    { "tqli2", Integer.valueOf(MAX_ITER).toString() }));
           }
           else
           {
@@ -996,6 +973,55 @@ public class Matrix implements MatrixI
     }
   }
 
+  @Override
+  public void setD(double[] v)
+  {
+    d = v;
+  }
+
+  @Override
+  public void setE(double[] v)
+  {
+    e = v;
+  }
+
+  public double getTotal()
+  {
+    double d = 0d;
+    for (int i = 0; i < this.height(); i++)
+    {
+      for (int j = 0; j < this.width(); j++)
+      {
+        d += value[i][j];
+      }
+    }
+    return d;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  public boolean equals(MatrixI m2, double delta)
+  {
+    if (m2 == null || this.height() != m2.height()
+            || this.width() != m2.width())
+    {
+      return false;
+    }
+    for (int i = 0; i < this.height(); i++)
+    {
+      for (int j = 0; j < this.width(); j++)
+      {
+        double diff = this.getValue(i, j) - m2.getValue(i, j);
+        if (Math.abs(diff) > delta)
+        {
+          return false;
+        }
+      }
+    }
+    return true;
+  }
 
   @Override
   public double[][] getValues()