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. For example
71 * new Matrix(new double[][] {{2, 3, 4}, {5, 6, 7})
77 * Note that ragged arrays (with not all rows, or columns, of the same
78 * length), are not supported by this class. They can be constructed, but
79 * results of operations on them are undefined and may throw exceptions.
82 * the matrix values in row-major order
84 public Matrix(double[][] values)
86 this.rows = values.length;
87 this.cols = this.rows == 0 ? 0 : values[0].length;
92 * Returns a new matrix which is the transpose of this one
94 * @return DOCUMENT ME!
97 public MatrixI transpose()
99 double[][] out = new double[cols][rows];
101 for (int i = 0; i < cols; i++)
103 for (int j = 0; j < rows; j++)
105 out[i][j] = value[j][i];
109 return new Matrix(out);
120 public void print(PrintStream ps, String format)
122 for (int i = 0; i < rows; i++)
124 for (int j = 0; j < cols; j++)
126 Format.print(ps, format, getValue(i, j));
134 * Returns a new matrix which is the result of premultiplying this matrix by
135 * the supplied argument. If this of size AxB (A rows and B columns), and the
136 * argument is CxA (C rows and A columns), the result is of size CxB.
141 * @throws IllegalArgumentException
142 * if the number of columns in the pre-multiplier is not equal to
143 * the number of rows in the multiplicand (this)
146 public MatrixI preMultiply(MatrixI in)
148 if (in.width() != rows)
150 throw new IllegalArgumentException("Can't pre-multiply " + this.rows
151 + " rows by " + in.width() + " columns");
153 double[][] tmp = new double[in.height()][this.cols];
155 for (int i = 0; i < in.height(); i++)
157 for (int j = 0; j < this.cols; j++)
160 * result[i][j] is the vector product of
161 * in.row[i] and this.column[j]
163 for (int k = 0; k < in.width(); k++)
165 tmp[i][j] += (in.getValue(i, k) * this.value[k][j]);
170 return new Matrix(tmp);
179 public double[] vectorPostMultiply(double[] in)
181 double[] out = new double[in.length];
183 for (int i = 0; i < in.length; i++)
187 for (int k = 0; k < in.length; k++)
189 out[i] += (value[i][k] * in[k]);
197 * Returns a new matrix which is the result of postmultiplying this matrix by
198 * the supplied argument. If this of size AxB (A rows and B columns), and the
199 * argument is BxC (B rows and C columns), the result is of size AxC.
201 * This method simply returns the result of in.preMultiply(this)
206 * @throws IllegalArgumentException
207 * if the number of rows in the post-multiplier is not equal to the
208 * number of columns in the multiplicand (this)
209 * @see #preMultiply(Matrix)
212 public MatrixI postMultiply(MatrixI in)
214 if (in.height() != this.cols)
216 throw new IllegalArgumentException("Can't post-multiply " + this.cols
217 + " columns by " + in.height() + " rows");
219 return in.preMultiply(this);
223 * Answers a new matrix with a copy of the values in this one
228 public MatrixI copy()
230 double[][] newmat = new double[rows][cols];
232 for (int i = 0; i < rows; i++)
234 System.arraycopy(value[i], 0, newmat[i], 0, value[i].length);
237 return new Matrix(newmat);
257 this.d = new double[rows];
258 this.e = new double[rows];
260 for (i = n; i >= 2; i--)
268 for (k = 1; k <= l; k++)
270 double v = Math.abs(getValue(i - 1, k - 1));
276 e[i - 1] = getValue(i - 1, l - 1);
280 for (k = 1; k <= l; k++)
282 double v = divideValue(i - 1, k - 1, scale);
286 f = getValue(i - 1, l - 1);
290 g = -1.0 * Math.sqrt(h);
297 e[i - 1] = scale * g;
299 setValue(i - 1, l - 1, f - g);
302 for (j = 1; j <= l; j++)
304 double val = getValue(i - 1, j - 1) / h;
305 setValue(j - 1, i - 1, val);
308 for (k = 1; k <= j; k++)
310 g += (getValue(j - 1, k - 1) * getValue(i - 1, k - 1));
313 for (k = j + 1; k <= l; k++)
315 g += (getValue(k - 1, j - 1) * getValue(i - 1, k - 1));
319 f += (e[j - 1] * getValue(i - 1, j - 1));
324 for (j = 1; j <= l; j++)
326 f = getValue(i - 1, j - 1);
327 g = e[j - 1] - (hh * f);
330 for (k = 1; k <= j; k++)
332 double val = (f * e[k - 1]) + (g * getValue(i - 1, k - 1));
333 addValue(j - 1, k - 1, -val);
340 e[i - 1] = getValue(i - 1, l - 1);
349 for (i = 1; i <= n; i++)
355 for (j = 1; j <= l; j++)
359 for (k = 1; k <= l; k++)
361 g += (getValue(i - 1, k - 1) * getValue(k - 1, j - 1));
364 for (k = 1; k <= l; k++)
366 addValue(k - 1, j - 1, -(g * getValue(k - 1, i - 1)));
371 d[i - 1] = getValue(i - 1, i - 1);
372 setValue(i - 1, i - 1, 1.0);
374 for (j = 1; j <= l; j++)
376 setValue(j - 1, i - 1, 0.0);
377 setValue(i - 1, j - 1, 0.0);
383 * Adds f to the value at [i, j] and returns the new value
389 protected double addValue(int i, int j, double f)
391 double v = value[i][j] + f;
397 * Divides the value at [i, j] by divisor and returns the new value. If d is
398 * zero, returns the unchanged value.
405 protected double divideValue(int i, int j, double divisor)
409 return getValue(i, j);
411 double v = value[i][j];
421 public void tqli() throws Exception
440 for (i = 2; i <= n; i++)
447 for (l = 1; l <= n; l++)
453 for (m = l; m <= (n - 1); m++)
455 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
457 if ((Math.abs(e[m - 1]) + dd) == dd)
469 throw new Exception(MessageManager.formatMessage(
470 "exception.matrix_too_many_iteration", new String[] {
471 "tqli", Integer.valueOf(maxIter).toString() }));
475 // System.out.println("Iteration " + iter);
478 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
479 r = Math.sqrt((g * g) + 1.0);
480 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
485 for (i = m - 1; i >= l; i--)
490 if (Math.abs(f) >= Math.abs(g))
493 r = Math.sqrt((c * c) + 1.0);
501 r = Math.sqrt((s * s) + 1.0);
508 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
513 for (k = 1; k <= n; k++)
515 f = getValue(k - 1, i);
516 setValue(k - 1, i, (s * getValue(k - 1, i - 1)) + (c * f));
517 setValue(k - 1, i - 1, (c * getValue(k - 1, i - 1)) - (s * f));
521 d[l - 1] = d[l - 1] - p;
530 public double getValue(int i, int j)
535 public void setValue(int i, int j, double val)
557 this.d = new double[rows];
558 this.e = new double[rows];
560 for (i = n - 1; i >= 1; i--)
568 for (k = 0; k < l; k++)
570 scale += Math.abs(value[i][k]);
579 for (k = 0; k < l; k++)
581 value[i][k] /= scale;
582 h += (value[i][k] * value[i][k]);
589 g = -1.0 * Math.sqrt(h);
601 for (j = 0; j < l; j++)
603 value[j][i] = value[i][j] / h;
606 for (k = 0; k < j; k++)
608 g += (value[j][k] * value[i][k]);
611 for (k = j; k < l; k++)
613 g += (value[k][j] * value[i][k]);
617 f += (e[j] * value[i][j]);
622 for (j = 0; j < l; j++)
628 for (k = 0; k < j; k++)
630 value[j][k] -= ((f * e[k]) + (g * value[i][k]));
646 for (i = 0; i < n; i++)
652 for (j = 0; j < l; j++)
656 for (k = 0; k < l; k++)
658 g += (value[i][k] * value[k][j]);
661 for (k = 0; k < l; k++)
663 value[k][j] -= (g * value[k][i]);
671 for (j = 0; j < l; j++)
682 public void tqli2() throws Exception
702 for (i = 2; i <= n; i++)
709 for (l = 1; l <= n; l++)
715 for (m = l; m <= (n - 1); m++)
717 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
719 if ((Math.abs(e[m - 1]) + dd) == dd)
731 throw new Exception(MessageManager.formatMessage(
732 "exception.matrix_too_many_iteration", new String[] {
733 "tqli2", Integer.valueOf(maxIter).toString() }));
737 // System.out.println("Iteration " + iter);
740 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
741 r = Math.sqrt((g * g) + 1.0);
742 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
747 for (i = m - 1; i >= l; i--)
752 if (Math.abs(f) >= Math.abs(g))
755 r = Math.sqrt((c * c) + 1.0);
763 r = Math.sqrt((s * s) + 1.0);
770 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
775 for (k = 1; k <= n; k++)
778 value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
779 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
783 d[l - 1] = d[l - 1] - p;
792 * Answers the first argument with the sign of the second argument
799 static double sign(double a, double b)
812 * Returns an array containing the values in the specified column
818 public double[] getColumn(int col)
820 double[] out = new double[rows];
822 for (int i = 0; i < rows; i++)
824 out[i] = value[i][col];
838 public void printD(PrintStream ps, String format)
840 for (int j = 0; j < rows; j++)
842 Format.print(ps, format, d[j]);
854 public void printE(PrintStream ps, String format)
856 for (int j = 0; j < rows; j++)
858 Format.print(ps, format, e[j]);
863 public double[] getD()
869 public double[] getE()
875 public int height() {
886 public double[] getRow(int i)
888 double[] row = new double[cols];
889 System.arraycopy(value[i], 0, row, 0, cols);