JAL-2403 JAL-1483 push 'reverseRange' inside Matrix
[jalview.git] / src / jalview / math / Matrix.java
index 3f0bae4..aaeb8da 100755 (executable)
@@ -891,37 +891,58 @@ public class Matrix implements MatrixI
     return row;
   }
 
-  @Override
-  public double getMaxValue()
+  /**
+   * Returns a length 2 array of {minValue, maxValue} of all values in the
+   * matrix. Returns null if the matrix is null or empty.
+   * 
+   * @return
+   */
+  double[] findMinMax()
   {
     if (value == null)
     {
-      return 0;
+      return null;
     }
+    double min = Double.MAX_VALUE;
     double max = -Double.MAX_VALUE;
+    boolean empty = true;
     for (double[] row : value)
     {
       if (row != null)
       {
         for (double x : row)
         {
+          empty = false;
           if (x > max)
           {
             max = x;
           }
+          if (x < min)
+          {
+            min = x;
+          }
         }
       }
     }
-    return max;
+    return empty ? null : new double[] { min, max };
   }
 
+  /**
+   * {@inheritDoc}
+   */
   @Override
-  public void subtractAllFrom(double val)
+  public void reverseRange(boolean maxToZero)
   {
     if (value == null)
     {
       return;
     }
+    double[] minMax = findMinMax();
+    if (minMax == null)
+    {
+      return; // empty matrix
+    }
+    double subtractFrom = maxToZero ? minMax[1] : minMax[0] + minMax[1];
 
     for (double[] row : value)
     {
@@ -930,7 +951,7 @@ public class Matrix implements MatrixI
         int j = 0;
         for (double x : row)
         {
-          row[j] = val - x;
+          row[j] = subtractFrom - x;
           j++;
         }
       }