X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fmath%2FMatrix.java;h=821fc664a0c3134003248d0aec828d019452bff6;hb=567c2595554096f10feab130153f97286f3f7d80;hp=de0bf77334a5e49847e2b058b812363033150a92;hpb=cf78dc7e8c37a03da8e7e396fec7e7ec54666847;p=jalview.git diff --git a/src/jalview/math/Matrix.java b/src/jalview/math/Matrix.java index de0bf77..821fc66 100755 --- a/src/jalview/math/Matrix.java +++ b/src/jalview/math/Matrix.java @@ -532,6 +532,7 @@ public class Matrix implements MatrixI return value[i][j]; } + @Override public void setValue(int i, int j, double val) { value[i][j] = val; @@ -889,4 +890,90 @@ public class Matrix implements MatrixI System.arraycopy(value[i], 0, row, 0, cols); return row; } + + /** + * 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 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 empty ? null : new double[] { min, max }; + } + + /** + * {@inheritDoc} + */ + @Override + 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) + { + if (row != null) + { + int j = 0; + for (double x : row) + { + row[j] = subtractFrom - x; + j++; + } + } + } + } + + /** + * Multiply every entry in the matrix by the given value. This method is not + * thread-safe. + */ + @Override + public void multiply(double d) + { + for (double[] row : value) + { + if (row != null) + { + for (int i = 0; i < row.length; i++) + { + row[i] *= d; + } + } + } + } }