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(value[i - 1][k - 1]);
271 double v = Math.abs(getValue(i - 1, k - 1));
277 e[i - 1] = getValue(i - 1, l - 1);
281 for (k = 1; k <= l; k++)
283 double v = divideValue(i - 1, k - 1, scale);
287 f = getValue(i - 1, l - 1);
291 g = -1.0 * Math.sqrt(h);
298 e[i - 1] = scale * g;
300 setValue(i - 1, l - 1, f - g);
303 for (j = 1; j <= l; j++)
305 double val = getValue(i - 1, j - 1) / h;
306 setValue(j - 1, i - 1, val);
309 for (k = 1; k <= j; k++)
311 g += (getValue(j - 1, k - 1) * getValue(i - 1, k - 1));
314 for (k = j + 1; k <= l; k++)
316 g += (getValue(k - 1, j - 1) * getValue(i - 1, k - 1));
320 f += (e[j - 1] * getValue(i - 1, j - 1));
325 for (j = 1; j <= l; j++)
327 f = getValue(i - 1, j - 1);
328 g = e[j - 1] - (hh * f);
331 for (k = 1; k <= j; k++)
333 double val = (f * e[k - 1]) + (g * getValue(i - 1, k - 1));
334 addValue(j - 1, k - 1, -val);
341 e[i - 1] = getValue(i - 1, l - 1);
350 for (i = 1; i <= n; i++)
356 for (j = 1; j <= l; j++)
360 for (k = 1; k <= l; k++)
362 g += (getValue(i - 1, k - 1) * getValue(k - 1, j - 1));
365 for (k = 1; k <= l; k++)
367 addValue(k - 1, j - 1, -(g * getValue(k - 1, i - 1)));
372 d[i - 1] = getValue(i - 1, i - 1);
373 setValue(i - 1, i - 1, 1.0);
375 for (j = 1; j <= l; j++)
377 setValue(j - 1, i - 1, 0.0);
378 setValue(i - 1, j - 1, 0.0);
384 * Adds f to the value at [i, j] and returns the new value
390 protected double addValue(int i, int j, double f)
392 double v = value[i][j] + f;
398 * Divides the value at [i, j] by divisor and returns the new value. If d is
399 * zero, returns the unchanged value.
406 protected double divideValue(int i, int j, double divisor)
410 return getValue(i, j);
412 double v = value[i][j];
422 public void tqli() throws Exception
441 for (i = 2; i <= n; i++)
448 for (l = 1; l <= n; l++)
454 for (m = l; m <= (n - 1); m++)
456 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
458 if ((Math.abs(e[m - 1]) + dd) == dd)
470 throw new Exception(MessageManager.formatMessage(
471 "exception.matrix_too_many_iteration", new String[] {
472 "tqli", Integer.valueOf(maxIter).toString() }));
476 // System.out.println("Iteration " + iter);
479 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
480 r = Math.sqrt((g * g) + 1.0);
481 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
486 for (i = m - 1; i >= l; i--)
491 if (Math.abs(f) >= Math.abs(g))
494 r = Math.sqrt((c * c) + 1.0);
502 r = Math.sqrt((s * s) + 1.0);
509 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
514 for (k = 1; k <= n; k++)
516 // f = value[k - 1][i];
517 // value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
518 // value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
519 f = getValue(k - 1, i);
520 setValue(k - 1, i, (s * getValue(k - 1, i - 1)) + (c * f));
521 setValue(k - 1, i - 1, (c * getValue(k - 1, i - 1)) - (s * f));
525 d[l - 1] = d[l - 1] - p;
534 public double getValue(int i, int j)
539 public void setValue(int i, int j, double val)
561 this.d = new double[rows];
562 this.e = new double[rows];
564 for (i = n - 1; i >= 1; i--)
572 for (k = 0; k < l; k++)
574 scale += Math.abs(value[i][k]);
583 for (k = 0; k < l; k++)
585 value[i][k] /= scale;
586 h += (value[i][k] * value[i][k]);
593 g = -1.0 * Math.sqrt(h);
605 for (j = 0; j < l; j++)
607 value[j][i] = value[i][j] / h;
610 for (k = 0; k < j; k++)
612 g += (value[j][k] * value[i][k]);
615 for (k = j; k < l; k++)
617 g += (value[k][j] * value[i][k]);
621 f += (e[j] * value[i][j]);
626 for (j = 0; j < l; j++)
632 for (k = 0; k < j; k++)
634 value[j][k] -= ((f * e[k]) + (g * value[i][k]));
650 for (i = 0; i < n; i++)
656 for (j = 0; j < l; j++)
660 for (k = 0; k < l; k++)
662 g += (value[i][k] * value[k][j]);
665 for (k = 0; k < l; k++)
667 value[k][j] -= (g * value[k][i]);
675 for (j = 0; j < l; j++)
686 public void tqli2() throws Exception
706 for (i = 2; i <= n; i++)
713 for (l = 1; l <= n; l++)
719 for (m = l; m <= (n - 1); m++)
721 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
723 if ((Math.abs(e[m - 1]) + dd) == dd)
735 throw new Exception(MessageManager.formatMessage(
736 "exception.matrix_too_many_iteration", new String[] {
737 "tqli2", Integer.valueOf(maxIter).toString() }));
741 // System.out.println("Iteration " + iter);
744 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
745 r = Math.sqrt((g * g) + 1.0);
746 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
751 for (i = m - 1; i >= l; i--)
756 if (Math.abs(f) >= Math.abs(g))
759 r = Math.sqrt((c * c) + 1.0);
767 r = Math.sqrt((s * s) + 1.0);
774 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
779 for (k = 1; k <= n; k++)
782 value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
783 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
787 d[l - 1] = d[l - 1] - p;
796 * Answers the first argument with the sign of the second argument
803 static double sign(double a, double b)
816 * Returns an array containing the values in the specified column
822 public double[] getColumn(int col)
824 double[] out = new double[rows];
826 for (int i = 0; i < rows; i++)
828 out[i] = value[i][col];
842 public void printD(PrintStream ps, String format)
844 for (int j = 0; j < rows; j++)
846 Format.print(ps, format, d[j]);
858 public void printE(PrintStream ps, String format)
860 for (int j = 0; j < rows; j++)
862 Format.print(ps, format, e[j]);
867 public double[] getD()
873 public double[] getE()
879 public int height() {
890 public double[] getRow(int i)
892 double[] row = new double[cols];
893 System.arraycopy(value[i], 0, row, 0, cols);