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;
29 * A class to model rectangular matrices of double values and operations on them
31 public class Matrix implements MatrixI
34 * the cell values in row-major order
36 private double[][] value;
44 * the number of columns
48 protected double[] d; // Diagonal
50 protected double[] e; // off diagonal
53 * maximum number of iterations for tqli
56 private static final int maxIter = 45; // fudge - add 15 iterations, just in
68 * Creates a new Matrix object containing a copy of the supplied array values.
72 * new Matrix(new double[][] {{2, 3, 4}, {5, 6, 7})
78 * Note that ragged arrays (with not all rows, or columns, of the same
79 * length), are not supported by this class. They can be constructed, but
80 * results of operations on them are undefined and may throw exceptions.
83 * the matrix values in row-major order
85 public Matrix(double[][] values)
87 this.rows = values.length;
88 this.cols = this.rows == 0 ? 0 : values[0].length;
91 * make a copy of the values array, for immutability
93 this.value = new double[rows][];
95 for (double[] row : values)
99 value[i] = new double[row.length];
100 System.arraycopy(row, 0, value[i], 0, row.length);
107 * Returns a new matrix which is the transpose of this one
112 public MatrixI transpose()
114 double[][] out = new double[cols][rows];
116 for (int i = 0; i < cols; i++)
118 for (int j = 0; j < rows; j++)
120 out[i][j] = value[j][i];
124 return new Matrix(out);
135 public void print(PrintStream ps, String format)
137 for (int i = 0; i < rows; i++)
139 for (int j = 0; j < cols; j++)
141 Format.print(ps, format, getValue(i, j));
149 * Returns a new matrix which is the result of premultiplying this matrix by
150 * the supplied argument. If this of size AxB (A rows and B columns), and the
151 * argument is CxA (C rows and A columns), the result is of size CxB.
156 * @throws IllegalArgumentException
157 * if the number of columns in the pre-multiplier is not equal to
158 * the number of rows in the multiplicand (this)
161 public MatrixI preMultiply(MatrixI in)
163 if (in.width() != rows)
165 throw new IllegalArgumentException("Can't pre-multiply " + this.rows
166 + " rows by " + in.width() + " columns");
168 double[][] tmp = new double[in.height()][this.cols];
170 for (int i = 0; i < in.height(); i++)
172 for (int j = 0; j < this.cols; j++)
175 * result[i][j] is the vector product of
176 * in.row[i] and this.column[j]
178 for (int k = 0; k < in.width(); k++)
180 tmp[i][j] += (in.getValue(i, k) * this.value[k][j]);
185 return new Matrix(tmp);
194 public double[] vectorPostMultiply(double[] in)
196 double[] out = new double[in.length];
198 for (int i = 0; i < in.length; i++)
202 for (int k = 0; k < in.length; k++)
204 out[i] += (value[i][k] * in[k]);
212 * Returns a new matrix which is the result of postmultiplying this matrix by
213 * the supplied argument. If this of size AxB (A rows and B columns), and the
214 * argument is BxC (B rows and C columns), the result is of size AxC.
216 * This method simply returns the result of in.preMultiply(this)
221 * @throws IllegalArgumentException
222 * if the number of rows in the post-multiplier is not equal to the
223 * number of columns in the multiplicand (this)
224 * @see #preMultiply(Matrix)
227 public MatrixI postMultiply(MatrixI in)
229 if (in.height() != this.cols)
231 throw new IllegalArgumentException("Can't post-multiply " + this.cols
232 + " columns by " + in.height() + " rows");
234 return in.preMultiply(this);
238 * Answers a new matrix with a copy of the values in this one
243 public MatrixI copy()
245 double[][] newmat = new double[rows][cols];
247 for (int i = 0; i < rows; i++)
249 System.arraycopy(value[i], 0, newmat[i], 0, value[i].length);
252 return new Matrix(newmat);
272 this.d = new double[rows];
273 this.e = new double[rows];
275 for (i = n; i >= 2; i--)
283 for (k = 1; k <= l; k++)
285 double v = Math.abs(getValue(i - 1, k - 1));
291 e[i - 1] = getValue(i - 1, l - 1);
295 for (k = 1; k <= l; k++)
297 double v = divideValue(i - 1, k - 1, scale);
301 f = getValue(i - 1, l - 1);
305 g = -1.0 * Math.sqrt(h);
312 e[i - 1] = scale * g;
314 setValue(i - 1, l - 1, f - g);
317 for (j = 1; j <= l; j++)
319 double val = getValue(i - 1, j - 1) / h;
320 setValue(j - 1, i - 1, val);
323 for (k = 1; k <= j; k++)
325 g += (getValue(j - 1, k - 1) * getValue(i - 1, k - 1));
328 for (k = j + 1; k <= l; k++)
330 g += (getValue(k - 1, j - 1) * getValue(i - 1, k - 1));
334 f += (e[j - 1] * getValue(i - 1, j - 1));
339 for (j = 1; j <= l; j++)
341 f = getValue(i - 1, j - 1);
342 g = e[j - 1] - (hh * f);
345 for (k = 1; k <= j; k++)
347 double val = (f * e[k - 1]) + (g * getValue(i - 1, k - 1));
348 addValue(j - 1, k - 1, -val);
355 e[i - 1] = getValue(i - 1, l - 1);
364 for (i = 1; i <= n; i++)
370 for (j = 1; j <= l; j++)
374 for (k = 1; k <= l; k++)
376 g += (getValue(i - 1, k - 1) * getValue(k - 1, j - 1));
379 for (k = 1; k <= l; k++)
381 addValue(k - 1, j - 1, -(g * getValue(k - 1, i - 1)));
386 d[i - 1] = getValue(i - 1, i - 1);
387 setValue(i - 1, i - 1, 1.0);
389 for (j = 1; j <= l; j++)
391 setValue(j - 1, i - 1, 0.0);
392 setValue(i - 1, j - 1, 0.0);
398 * Adds f to the value at [i, j] and returns the new value
404 protected double addValue(int i, int j, double f)
406 double v = value[i][j] + f;
412 * Divides the value at [i, j] by divisor and returns the new value. If d is
413 * zero, returns the unchanged value.
420 protected double divideValue(int i, int j, double divisor)
424 return getValue(i, j);
426 double v = value[i][j];
436 public void tqli() throws Exception
455 for (i = 2; i <= n; i++)
462 for (l = 1; l <= n; l++)
468 for (m = l; m <= (n - 1); m++)
470 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
472 if ((Math.abs(e[m - 1]) + dd) == dd)
484 throw new Exception(MessageManager.formatMessage(
485 "exception.matrix_too_many_iteration", new String[]
486 { "tqli", Integer.valueOf(maxIter).toString() }));
490 // System.out.println("Iteration " + iter);
493 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
494 r = Math.sqrt((g * g) + 1.0);
495 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
500 for (i = m - 1; i >= l; i--)
505 if (Math.abs(f) >= Math.abs(g))
508 r = Math.sqrt((c * c) + 1.0);
516 r = Math.sqrt((s * s) + 1.0);
523 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
528 for (k = 1; k <= n; k++)
530 f = getValue(k - 1, i);
531 setValue(k - 1, i, (s * getValue(k - 1, i - 1)) + (c * f));
532 setValue(k - 1, i - 1,
533 (c * getValue(k - 1, i - 1)) - (s * f));
537 d[l - 1] = d[l - 1] - p;
546 public double getValue(int i, int j)
552 public void setValue(int i, int j, double val)
574 this.d = new double[rows];
575 this.e = new double[rows];
577 for (i = n - 1; i >= 1; i--)
585 for (k = 0; k < l; k++)
587 scale += Math.abs(value[i][k]);
596 for (k = 0; k < l; k++)
598 value[i][k] /= scale;
599 h += (value[i][k] * value[i][k]);
606 g = -1.0 * Math.sqrt(h);
618 for (j = 0; j < l; j++)
620 value[j][i] = value[i][j] / h;
623 for (k = 0; k < j; k++)
625 g += (value[j][k] * value[i][k]);
628 for (k = j; k < l; k++)
630 g += (value[k][j] * value[i][k]);
634 f += (e[j] * value[i][j]);
639 for (j = 0; j < l; j++)
645 for (k = 0; k < j; k++)
647 value[j][k] -= ((f * e[k]) + (g * value[i][k]));
663 for (i = 0; i < n; i++)
669 for (j = 0; j < l; j++)
673 for (k = 0; k < l; k++)
675 g += (value[i][k] * value[k][j]);
678 for (k = 0; k < l; k++)
680 value[k][j] -= (g * value[k][i]);
688 for (j = 0; j < l; j++)
699 public void tqli2() throws Exception
719 for (i = 2; i <= n; i++)
726 for (l = 1; l <= n; l++)
732 for (m = l; m <= (n - 1); m++)
734 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
736 if ((Math.abs(e[m - 1]) + dd) == dd)
748 throw new Exception(MessageManager.formatMessage(
749 "exception.matrix_too_many_iteration", new String[]
750 { "tqli2", Integer.valueOf(maxIter).toString() }));
754 // System.out.println("Iteration " + iter);
757 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
758 r = Math.sqrt((g * g) + 1.0);
759 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
764 for (i = m - 1; i >= l; i--)
769 if (Math.abs(f) >= Math.abs(g))
772 r = Math.sqrt((c * c) + 1.0);
780 r = Math.sqrt((s * s) + 1.0);
787 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
792 for (k = 1; k <= n; k++)
795 value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
796 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
800 d[l - 1] = d[l - 1] - p;
809 * Answers the first argument with the sign of the second argument
816 static double sign(double a, double b)
829 * Returns an array containing the values in the specified column
835 public double[] getColumn(int col)
837 double[] out = new double[rows];
839 for (int i = 0; i < rows; i++)
841 out[i] = value[i][col];
855 public void printD(PrintStream ps, String format)
857 for (int j = 0; j < rows; j++)
859 Format.print(ps, format, d[j]);
872 public void printE(PrintStream ps, String format)
874 for (int j = 0; j < rows; j++)
876 Format.print(ps, format, e[j]);
881 public double[] getD()
887 public double[] getE()
905 public double[] getRow(int i)
907 double[] row = new double[cols];
908 System.arraycopy(value[i], 0, row, 0, cols);
913 * Returns a length 2 array of {minValue, maxValue} of all values in the
914 * matrix. Returns null if the matrix is null or empty.
918 double[] findMinMax()
924 double min = Double.MAX_VALUE;
925 double max = -Double.MAX_VALUE;
926 boolean empty = true;
927 for (double[] row : value)
945 return empty ? null : new double[] { min, max };
952 public void reverseRange(boolean maxToZero)
958 double[] minMax = findMinMax();
961 return; // empty matrix
963 double subtractFrom = maxToZero ? minMax[1] : minMax[0] + minMax[1];
965 for (double[] row : value)
972 row[j] = subtractFrom - x;
980 * Multiplies every entry in the matrix by the given value.
985 public void multiply(double by)
987 for (double[] row : value)
991 for (int i = 0; i < row.length; i++)