JAL-3054 table tooltip follow mouse in a sensible manner
[jalview.git] / src / jalview / math / Matrix.java
index aaeb8da..8910c67 100755 (executable)
@@ -63,9 +63,10 @@ public class Matrix implements MatrixI
   {
 
   }
-  
+
   /**
-   * Creates a new Matrix object. For example
+   * Creates a new Matrix object containing a copy of the supplied array values.
+   * For example
    * 
    * <pre>
    *   new Matrix(new double[][] {{2, 3, 4}, {5, 6, 7})
@@ -85,13 +86,27 @@ public class Matrix implements MatrixI
   {
     this.rows = values.length;
     this.cols = this.rows == 0 ? 0 : values[0].length;
-    this.value = values;
+
+    /*
+     * make a copy of the values array, for immutability
+     */
+    this.value = new double[rows][];
+    int i = 0;
+    for (double[] row : values)
+    {
+      if (row != null)
+      {
+        value[i] = new double[row.length];
+        System.arraycopy(row, 0, value[i], 0, row.length);
+      }
+      i++;
+    }
   }
 
   /**
    * Returns a new matrix which is the transpose of this one
    * 
-   * @return DOCUMENT ME!
+   * @return
    */
   @Override
   public MatrixI transpose()
@@ -467,8 +482,8 @@ public class Matrix implements MatrixI
           if (iter == maxIter)
           {
             throw new Exception(MessageManager.formatMessage(
-                    "exception.matrix_too_many_iteration", new String[] {
-                        "tqli", Integer.valueOf(maxIter).toString() }));
+                    "exception.matrix_too_many_iteration", new String[]
+                    { "tqli", Integer.valueOf(maxIter).toString() }));
           }
           else
           {
@@ -514,7 +529,8 @@ public class Matrix implements MatrixI
             {
               f = getValue(k - 1, i);
               setValue(k - 1, i, (s * getValue(k - 1, i - 1)) + (c * f));
-              setValue(k - 1, i - 1, (c * getValue(k - 1, i - 1)) - (s * f));
+              setValue(k - 1, i - 1,
+                      (c * getValue(k - 1, i - 1)) - (s * f));
             }
           }
 
@@ -730,8 +746,8 @@ public class Matrix implements MatrixI
           if (iter == maxIter)
           {
             throw new Exception(MessageManager.formatMessage(
-                    "exception.matrix_too_many_iteration", new String[] {
-                        "tqli2", Integer.valueOf(maxIter).toString() }));
+                    "exception.matrix_too_many_iteration", new String[]
+                    { "tqli2", Integer.valueOf(maxIter).toString() }));
           }
           else
           {
@@ -849,7 +865,8 @@ public class Matrix implements MatrixI
    * 
    * @param ps
    *          DOCUMENT ME!
-   * @param format TODO
+   * @param format
+   *          TODO
    */
   @Override
   public void printE(PrintStream ps, String format)
@@ -871,9 +888,10 @@ public class Matrix implements MatrixI
   {
     return e;
   }
-  
+
   @Override
-  public int height() {
+  public int height()
+  {
     return rows;
   }
 
@@ -957,4 +975,24 @@ public class Matrix implements MatrixI
       }
     }
   }
+
+  /**
+   * Multiplies every entry in the matrix by the given value.
+   * 
+   * @param
+   */
+  @Override
+  public void multiply(double by)
+  {
+    for (double[] row : value)
+    {
+      if (row != null)
+      {
+        for (int i = 0; i < row.length; i++)
+        {
+          row[i] *= by;
+        }
+      }
+    }
+  }
 }