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 * maximum number of iterations for tqli
36 private static final int MAX_ITER = 45;
37 // fudge - add 15 iterations, just in case
42 final protected int rows;
45 * the number of columns
47 final protected int cols;
50 * the cell values in row-major order
52 private double[][] value;
54 protected double[] d; // Diagonal
56 protected double[] e; // off diagonal
59 * Constructor given number of rows and columns
64 protected Matrix(int rowCount, int colCount)
71 * Creates a new Matrix object containing a copy of the supplied array values.
75 * new Matrix(new double[][] {{2, 3, 4}, {5, 6, 7})
81 * Note that ragged arrays (with not all rows, or columns, of the same
82 * length), are not supported by this class. They can be constructed, but
83 * results of operations on them are undefined and may throw exceptions.
86 * the matrix values in row-major order
88 public Matrix(double[][] values)
90 this.rows = values.length;
91 this.cols = this.rows == 0 ? 0 : values[0].length;
94 * make a copy of the values array, for immutability
96 this.value = new double[rows][];
98 for (double[] row : values)
102 value[i] = new double[row.length];
103 System.arraycopy(row, 0, value[i], 0, row.length);
110 public MatrixI transpose()
112 double[][] out = new double[cols][rows];
114 for (int i = 0; i < cols; i++)
116 for (int j = 0; j < rows; j++)
118 out[i][j] = value[j][i];
122 return new Matrix(out);
133 public void print(PrintStream ps, String format)
135 for (int i = 0; i < rows; i++)
137 for (int j = 0; j < cols; j++)
139 Format.print(ps, format, getValue(i, j));
147 public MatrixI preMultiply(MatrixI in)
149 if (in.width() != rows)
151 throw new IllegalArgumentException("Can't pre-multiply " + this.rows
152 + " rows by " + in.width() + " columns");
154 double[][] tmp = new double[in.height()][this.cols];
156 for (int i = 0; i < in.height(); i++)
158 for (int j = 0; j < this.cols; j++)
161 * result[i][j] is the vector product of
162 * in.row[i] and this.column[j]
164 for (int k = 0; k < in.width(); k++)
166 tmp[i][j] += (in.getValue(i, k) * this.value[k][j]);
171 return new Matrix(tmp);
180 public double[] vectorPostMultiply(double[] in)
182 double[] out = new double[in.length];
184 for (int i = 0; i < in.length; i++)
188 for (int k = 0; k < in.length; k++)
190 out[i] += (value[i][k] * in[k]);
198 public MatrixI postMultiply(MatrixI in)
200 if (in.height() != this.cols)
202 throw new IllegalArgumentException("Can't post-multiply " + this.cols
203 + " columns by " + in.height() + " rows");
205 return in.preMultiply(this);
209 public MatrixI copy()
211 double[][] newmat = new double[rows][cols];
213 for (int i = 0; i < rows; i++)
215 System.arraycopy(value[i], 0, newmat[i], 0, value[i].length);
218 return new Matrix(newmat);
238 this.d = new double[rows];
239 this.e = new double[rows];
241 for (i = n; i >= 2; i--)
249 for (k = 1; k <= l; k++)
251 double v = Math.abs(getValue(i - 1, k - 1));
257 e[i - 1] = getValue(i - 1, l - 1);
261 for (k = 1; k <= l; k++)
263 double v = divideValue(i - 1, k - 1, scale);
267 f = getValue(i - 1, l - 1);
271 g = -1.0 * Math.sqrt(h);
278 e[i - 1] = scale * g;
280 setValue(i - 1, l - 1, f - g);
283 for (j = 1; j <= l; j++)
285 double val = getValue(i - 1, j - 1) / h;
286 setValue(j - 1, i - 1, val);
289 for (k = 1; k <= j; k++)
291 g += (getValue(j - 1, k - 1) * getValue(i - 1, k - 1));
294 for (k = j + 1; k <= l; k++)
296 g += (getValue(k - 1, j - 1) * getValue(i - 1, k - 1));
300 f += (e[j - 1] * getValue(i - 1, j - 1));
305 for (j = 1; j <= l; j++)
307 f = getValue(i - 1, j - 1);
308 g = e[j - 1] - (hh * f);
311 for (k = 1; k <= j; k++)
313 double val = (f * e[k - 1]) + (g * getValue(i - 1, k - 1));
314 addValue(j - 1, k - 1, -val);
321 e[i - 1] = getValue(i - 1, l - 1);
330 for (i = 1; i <= n; i++)
336 for (j = 1; j <= l; j++)
340 for (k = 1; k <= l; k++)
342 g += (getValue(i - 1, k - 1) * getValue(k - 1, j - 1));
345 for (k = 1; k <= l; k++)
347 addValue(k - 1, j - 1, -(g * getValue(k - 1, i - 1)));
352 d[i - 1] = getValue(i - 1, i - 1);
353 setValue(i - 1, i - 1, 1.0);
355 for (j = 1; j <= l; j++)
357 setValue(j - 1, i - 1, 0.0);
358 setValue(i - 1, j - 1, 0.0);
364 * Adds f to the value at [i, j] and returns the new value
370 protected double addValue(int i, int j, double f)
372 double v = value[i][j] + f;
378 * Divides the value at [i, j] by divisor and returns the new value. If d is
379 * zero, returns the unchanged value.
386 protected double divideValue(int i, int j, double divisor)
390 return getValue(i, j);
392 double v = value[i][j];
402 public void tqli() throws Exception
421 for (i = 2; i <= n; i++)
428 for (l = 1; l <= n; l++)
434 for (m = l; m <= (n - 1); m++)
436 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
438 if ((Math.abs(e[m - 1]) + dd) == dd)
448 if (iter == MAX_ITER)
450 throw new Exception(MessageManager.formatMessage(
451 "exception.matrix_too_many_iteration", new String[]
452 { "tqli", Integer.valueOf(MAX_ITER).toString() }));
456 // System.out.println("Iteration " + iter);
459 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
460 r = Math.sqrt((g * g) + 1.0);
461 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
466 for (i = m - 1; i >= l; i--)
471 if (Math.abs(f) >= Math.abs(g))
474 r = Math.sqrt((c * c) + 1.0);
482 r = Math.sqrt((s * s) + 1.0);
489 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
494 for (k = 1; k <= n; k++)
496 f = getValue(k - 1, i);
497 setValue(k - 1, i, (s * getValue(k - 1, i - 1)) + (c * f));
498 setValue(k - 1, i - 1,
499 (c * getValue(k - 1, i - 1)) - (s * f));
503 d[l - 1] = d[l - 1] - p;
512 public double getValue(int i, int j)
518 public void setValue(int i, int j, double val)
540 this.d = new double[rows];
541 this.e = new double[rows];
543 for (i = n - 1; i >= 1; i--)
551 for (k = 0; k < l; k++)
553 scale += Math.abs(value[i][k]);
562 for (k = 0; k < l; k++)
564 value[i][k] /= scale;
565 h += (value[i][k] * value[i][k]);
572 g = -1.0 * Math.sqrt(h);
584 for (j = 0; j < l; j++)
586 value[j][i] = value[i][j] / h;
589 for (k = 0; k < j; k++)
591 g += (value[j][k] * value[i][k]);
594 for (k = j; k < l; k++)
596 g += (value[k][j] * value[i][k]);
600 f += (e[j] * value[i][j]);
605 for (j = 0; j < l; j++)
611 for (k = 0; k < j; k++)
613 value[j][k] -= ((f * e[k]) + (g * value[i][k]));
629 for (i = 0; i < n; i++)
635 for (j = 0; j < l; j++)
639 for (k = 0; k < l; k++)
641 g += (value[i][k] * value[k][j]);
644 for (k = 0; k < l; k++)
646 value[k][j] -= (g * value[k][i]);
654 for (j = 0; j < l; j++)
665 public void tqli2() throws Exception
685 for (i = 2; i <= n; i++)
692 for (l = 1; l <= n; l++)
698 for (m = l; m <= (n - 1); m++)
700 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
702 if ((Math.abs(e[m - 1]) + dd) == dd)
712 if (iter == MAX_ITER)
714 throw new Exception(MessageManager.formatMessage(
715 "exception.matrix_too_many_iteration", new String[]
716 { "tqli2", Integer.valueOf(MAX_ITER).toString() }));
720 // System.out.println("Iteration " + iter);
723 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
724 r = Math.sqrt((g * g) + 1.0);
725 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
730 for (i = m - 1; i >= l; i--)
735 if (Math.abs(f) >= Math.abs(g))
738 r = Math.sqrt((c * c) + 1.0);
746 r = Math.sqrt((s * s) + 1.0);
753 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
758 for (k = 1; k <= n; k++)
761 value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
762 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
766 d[l - 1] = d[l - 1] - p;
775 * Answers the first argument with the sign of the second argument
782 static double sign(double a, double b)
795 * Returns an array containing the values in the specified column
801 public double[] getColumn(int col)
803 double[] out = new double[rows];
805 for (int i = 0; i < rows; i++)
807 out[i] = value[i][col];
821 public void printD(PrintStream ps, String format)
823 for (int j = 0; j < rows; j++)
825 Format.print(ps, format, d[j]);
838 public void printE(PrintStream ps, String format)
840 for (int j = 0; j < rows; j++)
842 Format.print(ps, format, e[j]);
847 public double[] getD()
853 public double[] getE()
871 public double[] getRow(int i)
873 double[] row = new double[cols];
874 System.arraycopy(value[i], 0, row, 0, cols);
879 * Returns a length 2 array of {minValue, maxValue} of all values in the
880 * matrix. Returns null if the matrix is null or empty.
884 double[] findMinMax()
890 double min = Double.MAX_VALUE;
891 double max = -Double.MAX_VALUE;
892 boolean empty = true;
893 for (double[] row : value)
911 return empty ? null : new double[] { min, max };
918 public void reverseRange(boolean maxToZero)
924 double[] minMax = findMinMax();
927 return; // empty matrix
929 double subtractFrom = maxToZero ? minMax[1] : minMax[0] + minMax[1];
931 for (double[] row : value)
938 row[j] = subtractFrom - x;
946 * Multiplies every entry in the matrix by the given value.
951 public void multiply(double by)
953 for (double[] row : value)
957 for (int i = 0; i < row.length; i++)