X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fmath%2FMatrix.java;h=8910c67a7ec07f7ce4d2c5c13880640972db3e95;hb=4b2acd8fcf40e386397f46128be3d2ab74000325;hp=2b2314b3c917f758a89009be3588f6d4dfa19b31;hpb=80ff57c095b792b8e8e123ddb0eff4d1780d6cbd;p=jalview.git diff --git a/src/jalview/math/Matrix.java b/src/jalview/math/Matrix.java index 2b2314b..8910c67 100755 --- a/src/jalview/math/Matrix.java +++ b/src/jalview/math/Matrix.java @@ -26,15 +26,12 @@ import jalview.util.MessageManager; import java.io.PrintStream; /** - * DOCUMENT ME! - * - * @author $author$ - * @version $Revision$ + * A class to model rectangular matrices of double values and operations on them */ public class Matrix implements MatrixI { /* - * the [row][column] values in the matrix + * the cell values in row-major order */ private double[][] value; @@ -48,10 +45,8 @@ public class Matrix implements MatrixI */ protected int cols; - /** DOCUMENT ME!! */ protected double[] d; // Diagonal - /** DOCUMENT ME!! */ protected double[] e; // off diagonal /** @@ -68,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 * *
* new Matrix(new double[][] {{2, 3, 4}, {5, 6, 7}) @@ -89,17 +85,28 @@ public class Matrix implements MatrixI public Matrix(double[][] values) { this.rows = values.length; - if (rows > 0) + this.cols = this.rows == 0 ? 0 : values[0].length; + + /* + * make a copy of the values array, for immutability + */ + this.value = new double[rows][]; + int i = 0; + for (double[] row : values) { - this.cols = values[0].length; + if (row != null) + { + value[i] = new double[row.length]; + System.arraycopy(row, 0, value[i], 0, row.length); + } + i++; } - this.value = values; } /** * Returns a new matrix which is the transpose of this one * - * @return DOCUMENT ME! + * @return */ @Override public MatrixI transpose() @@ -275,7 +282,6 @@ public class Matrix implements MatrixI { for (k = 1; k <= l; k++) { - // double v = Math.abs(value[i - 1][k - 1]); double v = Math.abs(getValue(i - 1, k - 1)); scale += v; } @@ -372,7 +378,7 @@ public class Matrix implements MatrixI for (k = 1; k <= l; k++) { - double x = addValue(k - 1, j - 1, -(g * getValue(k - 1, i - 1))); + addValue(k - 1, j - 1, -(g * getValue(k - 1, i - 1))); } } } @@ -476,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 { @@ -521,12 +527,10 @@ public class Matrix implements MatrixI for (k = 1; k <= n; k++) { - // f = value[k - 1][i]; - // value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f); - // value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f); 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)); } } @@ -544,6 +548,7 @@ public class Matrix implements MatrixI return value[i][j]; } + @Override public void setValue(int i, int j, double val) { value[i][j] = val; @@ -741,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 { @@ -860,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) @@ -882,9 +888,10 @@ public class Matrix implements MatrixI { return e; } - + @Override - public int height() { + public int height() + { return rows; } @@ -901,4 +908,91 @@ 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++; + } + } + } + } + + /** + * 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; + } + } + } + } }