2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.util.Format;
24 import jalview.util.MessageManager;
26 import java.io.PrintStream;
27 import java.lang.Math;
28 import java.util.Arrays;
31 * A class to model rectangular matrices of double values and operations on them
33 public class Matrix implements MatrixI
36 * maximum number of iterations for tqli
38 private static final int MAX_ITER = 45;
39 // fudge - add 15 iterations, just in case
44 final protected int rows;
47 * the number of columns
49 final protected int cols;
52 * the cell values in row-major order
54 private double[][] value;
56 protected double[] d; // Diagonal
58 protected double[] e; // off diagonal
61 * Constructor given number of rows and columns
66 protected Matrix(int rowCount, int colCount)
73 * Creates a new Matrix object containing a copy of the supplied array values.
77 * new Matrix(new double[][] {{2, 3, 4}, {5, 6, 7})
83 * Note that ragged arrays (with not all rows, or columns, of the same
84 * length), are not supported by this class. They can be constructed, but
85 * results of operations on them are undefined and may throw exceptions.
88 * the matrix values in row-major order
90 public Matrix(double[][] values)
92 this.rows = values.length;
93 this.cols = this.rows == 0 ? 0 : values[0].length;
96 * make a copy of the values array, for immutability
98 this.value = new double[rows][];
100 for (double[] row : values)
104 value[i] = new double[row.length];
105 System.arraycopy(row, 0, value[i], 0, row.length);
111 public Matrix(float[][] values)
113 this.rows = values.length;
114 this.cols = this.rows == 0 ? 0 : values[0].length;
117 * make a copy of the values array, for immutability
119 this.value = new double[rows][];
121 for (float[] row : values)
125 value[i] = new double[row.length];
127 for (float oldValue : row)
129 value[i][j] = oldValue;
138 public MatrixI transpose()
140 double[][] out = new double[cols][rows];
142 for (int i = 0; i < cols; i++)
144 for (int j = 0; j < rows; j++)
146 out[i][j] = value[j][i];
150 return new Matrix(out);
161 public void print(PrintStream ps, String format)
163 for (int i = 0; i < rows; i++)
165 for (int j = 0; j < cols; j++)
167 Format.print(ps, format, getValue(i, j));
175 public MatrixI preMultiply(MatrixI in)
177 if (in.width() != rows)
179 throw new IllegalArgumentException("Can't pre-multiply " + this.rows
180 + " rows by " + in.width() + " columns");
182 double[][] tmp = new double[in.height()][this.cols];
184 for (int i = 0; i < in.height(); i++)
186 for (int j = 0; j < this.cols; j++)
189 * result[i][j] is the vector product of
190 * in.row[i] and this.column[j]
192 for (int k = 0; k < in.width(); k++)
194 tmp[i][j] += (in.getValue(i, k) * this.value[k][j]);
199 return new Matrix(tmp);
208 public double[] vectorPostMultiply(double[] in)
210 double[] out = new double[in.length];
212 for (int i = 0; i < in.length; i++)
216 for (int k = 0; k < in.length; k++)
218 out[i] += (value[i][k] * in[k]);
226 public MatrixI postMultiply(MatrixI in)
228 if (in.height() != this.cols)
230 throw new IllegalArgumentException("Can't post-multiply " + this.cols
231 + " columns by " + in.height() + " rows");
233 return in.preMultiply(this);
237 public MatrixI copy()
239 double[][] newmat = new double[rows][cols];
241 for (int i = 0; i < rows; i++)
243 System.arraycopy(value[i], 0, newmat[i], 0, value[i].length);
246 Matrix m = new Matrix(newmat);
249 m.d = Arrays.copyOf(this.d, this.d.length);
253 m.e = Arrays.copyOf(this.e, this.e.length);
276 this.d = new double[rows];
277 this.e = new double[rows];
279 for (i = n; i >= 2; i--)
287 for (k = 1; k <= l; k++)
289 double v = Math.abs(getValue(i - 1, k - 1));
295 e[i - 1] = getValue(i - 1, l - 1);
299 for (k = 1; k <= l; k++)
301 double v = divideValue(i - 1, k - 1, scale);
305 f = getValue(i - 1, l - 1);
309 g = -1.0 * Math.sqrt(h);
316 e[i - 1] = scale * g;
318 setValue(i - 1, l - 1, f - g);
321 for (j = 1; j <= l; j++)
323 double val = getValue(i - 1, j - 1) / h;
324 setValue(j - 1, i - 1, val);
327 for (k = 1; k <= j; k++)
329 g += (getValue(j - 1, k - 1) * getValue(i - 1, k - 1));
332 for (k = j + 1; k <= l; k++)
334 g += (getValue(k - 1, j - 1) * getValue(i - 1, k - 1));
338 f += (e[j - 1] * getValue(i - 1, j - 1));
343 for (j = 1; j <= l; j++)
345 f = getValue(i - 1, j - 1);
346 g = e[j - 1] - (hh * f);
349 for (k = 1; k <= j; k++)
351 double val = (f * e[k - 1]) + (g * getValue(i - 1, k - 1));
352 addValue(j - 1, k - 1, -val);
359 e[i - 1] = getValue(i - 1, l - 1);
368 for (i = 1; i <= n; i++)
374 for (j = 1; j <= l; j++)
378 for (k = 1; k <= l; k++)
380 g += (getValue(i - 1, k - 1) * getValue(k - 1, j - 1));
383 for (k = 1; k <= l; k++)
385 addValue(k - 1, j - 1, -(g * getValue(k - 1, i - 1)));
390 d[i - 1] = getValue(i - 1, i - 1);
391 setValue(i - 1, i - 1, 1.0);
393 for (j = 1; j <= l; j++)
395 setValue(j - 1, i - 1, 0.0);
396 setValue(i - 1, j - 1, 0.0);
402 * Adds f to the value at [i, j] and returns the new value
408 protected double addValue(int i, int j, double f)
410 double v = value[i][j] + f;
416 * Divides the value at [i, j] by divisor and returns the new value. If d is
417 * zero, returns the unchanged value.
424 protected double divideValue(int i, int j, double divisor)
428 return getValue(i, j);
430 double v = value[i][j];
440 public void tqli() throws Exception
459 for (i = 2; i <= n; i++)
466 for (l = 1; l <= n; l++)
472 for (m = l; m <= (n - 1); m++)
474 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
476 if ((Math.abs(e[m - 1]) + dd) == dd)
486 if (iter == MAX_ITER)
488 throw new Exception(MessageManager.formatMessage(
489 "exception.matrix_too_many_iteration", new String[]
490 { "tqli", Integer.valueOf(MAX_ITER).toString() }));
494 // System.out.println("Iteration " + iter);
497 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
498 r = Math.sqrt((g * g) + 1.0);
499 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
504 for (i = m - 1; i >= l; i--)
509 if (Math.abs(f) >= Math.abs(g))
512 r = Math.sqrt((c * c) + 1.0);
520 r = Math.sqrt((s * s) + 1.0);
527 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
532 for (k = 1; k <= n; k++)
534 f = getValue(k - 1, i);
535 setValue(k - 1, i, (s * getValue(k - 1, i - 1)) + (c * f));
536 setValue(k - 1, i - 1,
537 (c * getValue(k - 1, i - 1)) - (s * f));
541 d[l - 1] = d[l - 1] - p;
550 public double getValue(int i, int j)
556 public void setValue(int i, int j, double val)
578 this.d = new double[rows];
579 this.e = new double[rows];
581 for (i = n - 1; i >= 1; i--)
589 for (k = 0; k < l; k++)
591 scale += Math.abs(value[i][k]);
600 for (k = 0; k < l; k++)
602 value[i][k] /= scale;
603 h += (value[i][k] * value[i][k]);
610 g = -1.0 * Math.sqrt(h);
622 for (j = 0; j < l; j++)
624 value[j][i] = value[i][j] / h;
627 for (k = 0; k < j; k++)
629 g += (value[j][k] * value[i][k]);
632 for (k = j; k < l; k++)
634 g += (value[k][j] * value[i][k]);
638 f += (e[j] * value[i][j]);
643 for (j = 0; j < l; j++)
649 for (k = 0; k < j; k++)
651 value[j][k] -= ((f * e[k]) + (g * value[i][k]));
667 for (i = 0; i < n; i++)
673 for (j = 0; j < l; j++)
677 for (k = 0; k < l; k++)
679 g += (value[i][k] * value[k][j]);
682 for (k = 0; k < l; k++)
684 value[k][j] -= (g * value[k][i]);
692 for (j = 0; j < l; j++)
703 public void tqli2() throws Exception
723 for (i = 2; i <= n; i++)
730 for (l = 1; l <= n; l++)
736 for (m = l; m <= (n - 1); m++)
738 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
740 if ((Math.abs(e[m - 1]) + dd) == dd)
750 if (iter == MAX_ITER)
752 throw new Exception(MessageManager.formatMessage(
753 "exception.matrix_too_many_iteration", new String[]
754 { "tqli2", Integer.valueOf(MAX_ITER).toString() }));
758 // System.out.println("Iteration " + iter);
761 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
762 r = Math.sqrt((g * g) + 1.0);
763 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
768 for (i = m - 1; i >= l; i--)
773 if (Math.abs(f) >= Math.abs(g))
776 r = Math.sqrt((c * c) + 1.0);
784 r = Math.sqrt((s * s) + 1.0);
791 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
796 for (k = 1; k <= n; k++)
799 value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
800 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
804 d[l - 1] = d[l - 1] - p;
813 * Answers the first argument with the sign of the second argument
820 static double sign(double a, double b)
833 * Returns an array containing the values in the specified column
839 public double[] getColumn(int col)
841 double[] out = new double[rows];
843 for (int i = 0; i < rows; i++)
845 out[i] = value[i][col];
859 public void printD(PrintStream ps, String format)
861 for (int j = 0; j < rows; j++)
863 Format.print(ps, format, d[j]);
876 public void printE(PrintStream ps, String format)
878 for (int j = 0; j < rows; j++)
880 Format.print(ps, format, e[j]);
885 public double[] getD()
891 public double[] getE()
909 public double[] getRow(int i)
911 double[] row = new double[cols];
912 System.arraycopy(value[i], 0, row, 0, cols);
917 * Returns a length 2 array of {minValue, maxValue} of all values in the
918 * matrix. Returns null if the matrix is null or empty.
922 double[] findMinMax()
928 double min = Double.MAX_VALUE;
929 double max = -Double.MAX_VALUE;
930 boolean empty = true;
931 for (double[] row : value)
949 return empty ? null : new double[] { min, max };
956 public void reverseRange(boolean maxToZero)
962 double[] minMax = findMinMax();
965 return; // empty matrix
967 double subtractFrom = maxToZero ? minMax[1] : minMax[0] + minMax[1];
969 for (double[] row : value)
976 row[j] = subtractFrom - x;
984 * Multiplies every entry in the matrix by the given value.
989 public void multiply(double by)
991 for (double[] row : value)
995 for (int i = 0; i < row.length; i++)
1004 public void setD(double[] v)
1010 public void setE(double[] v)
1015 public double getTotal()
1018 for (int i = 0; i < this.height(); i++)
1020 for (int j = 0; j < this.width(); j++)
1032 public boolean equals(MatrixI m2, double delta)
1034 if (m2 == null || this.height() != m2.height()
1035 || this.width() != m2.width())
1039 for (int i = 0; i < this.height(); i++)
1041 for (int j = 0; j < this.width(); j++)
1043 double diff = this.getValue(i, j) - m2.getValue(i, j);
1044 if (Math.abs(diff) > delta)
1054 * Returns a copy in which every value in the matrix is its absolute
1058 public MatrixI absolute()
1060 MatrixI copy = this.copy();
1061 for (int i = 0; i < copy.width(); i++)
1063 double[] row = copy.getRow(i);
1066 for (int j = 0; j < row.length; j++)
1068 row[j] = Math.abs(row[j]);
1076 * Returns the mean of each row
1080 public double[] meanRow()
1082 double[] mean = new double[rows];
1084 for (double[] row : value)
1088 mean[i++] = MiscMath.mean(row);
1095 * Returns the mean of each column
1099 public double[] meanCol()
1101 double[] mean = new double[cols];
1102 for (int j = 0; j < cols; j++)
1104 double[] column = getColumn(j);
1107 mean[j] = MiscMath.mean(column);
1114 * fills up a diagonal matrix with its transposed copy
1115 * !other side should be filled with 0
1117 * TODO check on which side it was diagonal and only do calculations for the other side
1120 public void fillDiagonal()
1124 MatrixI copy = this.transpose();
1125 for (int i = 0; i < n; i++)
1127 for (int j = 0; j < m; j++)
1131 this.addValue(i, j, copy.getValue(i,j));
1138 * counts the number of Double.NaN in the matrix
1143 public int countNaN()
1146 for (int i = 0; i < this.rows; i++)
1148 for (int j = 0; j < this.cols; j++)
1150 if (Double.isNaN(this.getValue(i,j)))