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;
34 public class Matrix implements MatrixI
37 * the [row][column] values in the matrix
39 private double[][] value;
47 * the number of columns
52 protected double[] d; // Diagonal
55 protected double[] e; // off diagonal
58 * maximum number of iterations for tqli
61 private static final int maxIter = 45; // fudge - add 15 iterations, just in
73 * Creates a new Matrix object. For example
76 * new Matrix(new double[][] {{2, 3, 4}, {5, 6, 7})
82 * Note that ragged arrays (with not all rows, or columns, of the same
83 * length), are not supported by this class. They can be constructed, but
84 * results of operations on them are undefined and may throw exceptions.
87 * the matrix values in row-major order
89 public Matrix(double[][] values)
91 this.rows = values.length;
94 this.cols = values[0].length;
100 * Returns a new matrix which is the transpose of this one
102 * @return DOCUMENT ME!
105 public MatrixI transpose()
107 double[][] out = new double[cols][rows];
109 for (int i = 0; i < cols; i++)
111 for (int j = 0; j < rows; j++)
113 out[i][j] = value[j][i];
117 return new Matrix(out);
128 public void print(PrintStream ps, String format)
130 for (int i = 0; i < rows; i++)
132 for (int j = 0; j < cols; j++)
134 Format.print(ps, format, getValue(i, j));
142 * Returns a new matrix which is the result of premultiplying this matrix by
143 * the supplied argument. If this of size AxB (A rows and B columns), and the
144 * argument is CxA (C rows and A columns), the result is of size CxB.
149 * @throws IllegalArgumentException
150 * if the number of columns in the pre-multiplier is not equal to
151 * the number of rows in the multiplicand (this)
154 public MatrixI preMultiply(MatrixI in)
156 if (in.width() != rows)
158 throw new IllegalArgumentException("Can't pre-multiply " + this.rows
159 + " rows by " + in.width() + " columns");
161 double[][] tmp = new double[in.height()][this.cols];
163 for (int i = 0; i < in.height(); i++)
165 for (int j = 0; j < this.cols; j++)
168 * result[i][j] is the vector product of
169 * in.row[i] and this.column[j]
171 for (int k = 0; k < in.width(); k++)
173 tmp[i][j] += (in.getValue(i, k) * this.value[k][j]);
178 return new Matrix(tmp);
187 public double[] vectorPostMultiply(double[] in)
189 double[] out = new double[in.length];
191 for (int i = 0; i < in.length; i++)
195 for (int k = 0; k < in.length; k++)
197 out[i] += (value[i][k] * in[k]);
205 * Returns a new matrix which is the result of postmultiplying this matrix by
206 * the supplied argument. If this of size AxB (A rows and B columns), and the
207 * argument is BxC (B rows and C columns), the result is of size AxC.
209 * This method simply returns the result of in.preMultiply(this)
214 * @throws IllegalArgumentException
215 * if the number of rows in the post-multiplier is not equal to the
216 * number of columns in the multiplicand (this)
217 * @see #preMultiply(Matrix)
220 public MatrixI postMultiply(MatrixI in)
222 if (in.height() != this.cols)
224 throw new IllegalArgumentException("Can't post-multiply " + this.cols
225 + " columns by " + in.height() + " rows");
227 return in.preMultiply(this);
231 * Answers a new matrix with a copy of the values in this one
236 public MatrixI copy()
238 double[][] newmat = new double[rows][cols];
240 for (int i = 0; i < rows; i++)
242 System.arraycopy(value[i], 0, newmat[i], 0, value[i].length);
245 return new Matrix(newmat);
265 this.d = new double[rows];
266 this.e = new double[rows];
268 for (i = n; i >= 2; i--)
276 for (k = 1; k <= l; k++)
278 // double v = Math.abs(value[i - 1][k - 1]);
279 double v = Math.abs(getValue(i - 1, k - 1));
285 // e[i - 1] = value[i - 1][l - 1];
286 e[i - 1] = getValue(i - 1, l - 1);
290 for (k = 1; k <= l; k++)
292 // value[i - 1][k - 1] /= scale;
293 // h += (value[i - 1][k - 1] * value[i - 1][k - 1]);
294 double v = divideValue(i - 1, k - 1, scale);
298 // f = value[i - 1][l - 1];
299 f = getValue(i - 1, l - 1);
303 g = -1.0 * Math.sqrt(h);
310 e[i - 1] = scale * g;
312 // value[i - 1][l - 1] = f - g;
313 setValue(i - 1, l - 1, f - g);
314 // System.out.println(String.format("%d %d %f %f %f %f %f %.5e", i,
316 // scale, f, g, h, getValue(i - 1, l - 1), checksum()));
319 for (j = 1; j <= l; j++)
321 // value[j - 1][i - 1] = value[i - 1][j - 1] / h;
322 double val = getValue(i - 1, j - 1) / h;
323 setValue(j - 1, i - 1, val);
326 for (k = 1; k <= j; k++)
328 // g += (value[j - 1][k - 1] * value[i - 1][k - 1]);
329 g += (getValue(j - 1, k - 1) * getValue(i - 1, k - 1));
332 for (k = j + 1; k <= l; k++)
334 // g += (value[k - 1][j - 1] * value[i - 1][k - 1]);
335 g += (getValue(k - 1, j - 1) * getValue(i - 1, k - 1));
339 // f += (e[j - 1] * value[i - 1][j - 1]);
340 f += (e[j - 1] * getValue(i - 1, j - 1));
345 for (j = 1; j <= l; j++)
347 // f = value[i - 1][j - 1];
348 f = getValue(i - 1, j - 1);
349 g = e[j - 1] - (hh * f);
352 for (k = 1; k <= j; k++)
354 // value[j - 1][k - 1] -= ((f * e[k - 1]) + (g * value[i - 1][k -
356 double val = (f * e[k - 1]) + (g * getValue(i - 1, k - 1));
357 addValue(j - 1, k - 1, -val);
364 // e[i - 1] = value[i - 1][l - 1];
365 e[i - 1] = getValue(i - 1, l - 1);
374 for (i = 1; i <= n; i++)
380 for (j = 1; j <= l; j++)
384 for (k = 1; k <= l; k++)
386 // g += (value[i - 1][k - 1] * value[k - 1][j - 1]);
387 g += (getValue(i - 1, k - 1) * getValue(k - 1, j - 1));
390 for (k = 1; k <= l; k++)
392 // value[k - 1][j - 1] -= (g * value[k - 1][i - 1]);
393 addValue(k - 1, j - 1, -(g * getValue(k - 1, i - 1)));
398 // d[i - 1] = value[i - 1][i - 1];
399 // value[i - 1][i - 1] = 1.0;
400 d[i - 1] = getValue(i - 1, i - 1);
401 setValue(i - 1, i - 1, 1.0);
403 for (j = 1; j <= l; j++)
405 // value[j - 1][i - 1] = 0.0;
406 // value[i - 1][j - 1] = 0.0;
407 setValue(j - 1, i - 1, 0.0);
408 setValue(i - 1, j - 1, 0.0);
414 * Adds f to the value at [i, j] and returns the new value
420 protected double addValue(int i, int j, double f)
422 double v = value[i][j] + f;
428 * Divides the value at [i, j] by divisor and returns the new value. If d is
429 * zero, returns the unchanged value.
436 protected double divideValue(int i, int j, double divisor)
440 return getValue(i, j);
442 double v = value[i][j];
452 public void tqli() throws Exception
471 for (i = 2; i <= n; i++)
478 for (l = 1; l <= n; l++)
484 for (m = l; m <= (n - 1); m++)
486 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
488 if ((Math.abs(e[m - 1]) + dd) == dd)
500 throw new Exception(MessageManager.formatMessage(
501 "exception.matrix_too_many_iteration", new String[] {
502 "tqli", Integer.valueOf(maxIter).toString() }));
506 // System.out.println("Iteration " + iter);
509 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
510 r = Math.sqrt((g * g) + 1.0);
511 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
516 for (i = m - 1; i >= l; i--)
521 if (Math.abs(f) >= Math.abs(g))
524 r = Math.sqrt((c * c) + 1.0);
532 r = Math.sqrt((s * s) + 1.0);
539 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
544 for (k = 1; k <= n; k++)
546 // f = value[k - 1][i];
547 // value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
548 // value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
549 f = getValue(k - 1, i);
550 setValue(k - 1, i, (s * getValue(k - 1, i - 1)) + (c * f));
551 setValue(k - 1, i - 1, (c * getValue(k - 1, i - 1)) - (s * f));
555 d[l - 1] = d[l - 1] - p;
564 public double getValue(int i, int j)
569 public void setValue(int i, int j, double val)
591 this.d = new double[rows];
592 this.e = new double[rows];
594 for (i = n - 1; i >= 1; i--)
602 for (k = 0; k < l; k++)
604 scale += Math.abs(value[i][k]);
613 for (k = 0; k < l; k++)
615 value[i][k] /= scale;
616 h += (value[i][k] * value[i][k]);
623 g = -1.0 * Math.sqrt(h);
635 for (j = 0; j < l; j++)
637 value[j][i] = value[i][j] / h;
640 for (k = 0; k < j; k++)
642 g += (value[j][k] * value[i][k]);
645 for (k = j; k < l; k++)
647 g += (value[k][j] * value[i][k]);
651 f += (e[j] * value[i][j]);
656 for (j = 0; j < l; j++)
662 for (k = 0; k < j; k++)
664 value[j][k] -= ((f * e[k]) + (g * value[i][k]));
680 for (i = 0; i < n; i++)
686 for (j = 0; j < l; j++)
690 for (k = 0; k < l; k++)
692 g += (value[i][k] * value[k][j]);
695 for (k = 0; k < l; k++)
697 value[k][j] -= (g * value[k][i]);
705 for (j = 0; j < l; j++)
716 public void tqli2() throws Exception
736 for (i = 2; i <= n; i++)
743 for (l = 1; l <= n; l++)
749 for (m = l; m <= (n - 1); m++)
751 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
753 if ((Math.abs(e[m - 1]) + dd) == dd)
765 throw new Exception(MessageManager.formatMessage(
766 "exception.matrix_too_many_iteration", new String[] {
767 "tqli2", Integer.valueOf(maxIter).toString() }));
771 // System.out.println("Iteration " + iter);
774 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
775 r = Math.sqrt((g * g) + 1.0);
776 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
781 for (i = m - 1; i >= l; i--)
786 if (Math.abs(f) >= Math.abs(g))
789 r = Math.sqrt((c * c) + 1.0);
797 r = Math.sqrt((s * s) + 1.0);
804 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
809 for (k = 1; k <= n; k++)
812 value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
813 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
817 d[l - 1] = d[l - 1] - p;
826 * Answers the first argument with the sign of the second argument
833 static double sign(double a, double b)
846 * Returns an array containing the values in the specified column
852 public double[] getColumn(int col)
854 double[] out = new double[rows];
856 for (int i = 0; i < rows; i++)
858 out[i] = value[i][col];
872 public void printD(PrintStream ps, String format)
874 for (int j = 0; j < rows; j++)
876 Format.print(ps, format, d[j]);
888 public void printE(PrintStream ps, String format)
890 for (int j = 0; j < rows; j++)
892 Format.print(ps, format, e[j]);
897 public double[] getD()
903 public double[] getE()
909 public int height() {
920 public double[] getRow(int i)
922 double[] row = new double[cols];
923 System.arraycopy(value[i], 0, row, 0, cols);