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;
39 public double[][] value;
48 public double[] d; // Diagonal
51 public double[] e; // off diagonal
54 * maximum number of iterations for tqli
57 int maxIter = 45; // fudge - add 15 iterations, just in case
60 * Creates a new Matrix object. For example
63 * new Matrix(new double[][] {{2, 3}, {4, 5}, 2, 2)
69 * Note that ragged arrays (with not all rows, or columns, of the same
70 * length), are not supported by this class. They can be constructed, but
71 * results of operations on them are undefined and may throw exceptions.
74 * the matrix values in row-major order
78 public Matrix(double[][] values, int rows, int cols)
86 * Returns a new matrix which is the transposes of this one
88 * @return DOCUMENT ME!
90 public Matrix transpose()
92 double[][] out = new double[cols][rows];
94 for (int i = 0; i < cols; i++)
96 for (int j = 0; j < rows; j++)
98 out[i][j] = value[j][i];
102 return new Matrix(out, cols, rows);
111 public void print(PrintStream ps)
113 for (int i = 0; i < rows; i++)
115 for (int j = 0; j < cols; j++)
117 Format.print(ps, "%8.2f", value[i][j]);
125 * Returns a new matrix which is the result of premultiplying this matrix by
126 * the supplied argument. If this of size AxB (A rows and B columns), and the
127 * argument is CxA (C rows and A columns), the result is of size CxB.
132 * @throws IllegalArgumentException
133 * if the number of columns in the pre-multiplier is not equal to
134 * the number of rows in the multiplicand (this)
136 public Matrix preMultiply(Matrix in)
138 if (in.cols != this.rows)
140 throw new IllegalArgumentException("Can't pre-multiply " + this.rows
141 + " rows by " + in.cols + " columns");
143 double[][] tmp = new double[in.rows][this.cols];
145 for (int i = 0; i < in.rows; i++)
147 for (int j = 0; j < this.cols; j++)
151 for (int k = 0; k < in.cols; k++)
153 tmp[i][j] += (in.value[i][k] * this.value[k][j]);
158 return new Matrix(tmp, in.rows, this.cols);
167 public double[] vectorPostMultiply(double[] in)
169 double[] out = new double[in.length];
171 for (int i = 0; i < in.length; i++)
175 for (int k = 0; k < in.length; k++)
177 out[i] += (value[i][k] * in[k]);
185 * Returns a new matrix which is the result of postmultiplying this matrix by
186 * the supplied argument. If this of size AxB (A rows and B columns), and the
187 * argument is BxC (B rows and C columns), the result is of size AxC.
189 * This method simply returns the result of in.preMultiply(this)
194 * @throws IllegalArgumentException
195 * if the number of rows in the post-multiplier is not equal to the
196 * number of columns in the multiplicand (this)
197 * @see #preMultiply(Matrix)
199 public Matrix postMultiply(Matrix in)
201 if (in.rows != this.cols)
203 throw new IllegalArgumentException("Can't post-multiply " + this.cols
204 + " columns by " + in.rows + " rows");
206 return in.preMultiply(this);
210 * Answers a new matrix with a copy of the values in this one
216 double[][] newmat = new double[rows][cols];
218 for (int i = 0; i < rows; i++)
220 System.arraycopy(value[i], 0, newmat[i], 0, value[i].length);
221 // for (int j = 0; j < cols; j++)
223 // newmat[i][j] = value[i][j];
227 return new Matrix(newmat, rows, cols);
247 this.d = new double[rows];
248 this.e = new double[rows];
250 for (i = n; i >= 2; i--)
258 for (k = 1; k <= l; k++)
260 scale += Math.abs(value[i - 1][k - 1]);
265 e[i - 1] = value[i - 1][l - 1];
269 for (k = 1; k <= l; k++)
271 value[i - 1][k - 1] /= scale;
272 h += (value[i - 1][k - 1] * value[i - 1][k - 1]);
275 f = value[i - 1][l - 1];
279 g = -1.0 * Math.sqrt(h);
286 e[i - 1] = scale * g;
288 value[i - 1][l - 1] = f - g;
291 for (j = 1; j <= l; j++)
293 value[j - 1][i - 1] = value[i - 1][j - 1] / h;
296 for (k = 1; k <= j; k++)
298 g += (value[j - 1][k - 1] * value[i - 1][k - 1]);
301 for (k = j + 1; k <= l; k++)
303 g += (value[k - 1][j - 1] * value[i - 1][k - 1]);
307 f += (e[j - 1] * value[i - 1][j - 1]);
312 for (j = 1; j <= l; j++)
314 f = value[i - 1][j - 1];
315 g = e[j - 1] - (hh * f);
318 for (k = 1; k <= j; k++)
320 value[j - 1][k - 1] -= ((f * e[k - 1]) + (g * value[i - 1][k - 1]));
327 e[i - 1] = value[i - 1][l - 1];
336 for (i = 1; i <= n; i++)
342 for (j = 1; j <= l; j++)
346 for (k = 1; k <= l; k++)
348 g += (value[i - 1][k - 1] * value[k - 1][j - 1]);
351 for (k = 1; k <= l; k++)
353 value[k - 1][j - 1] -= (g * value[k - 1][i - 1]);
358 d[i - 1] = value[i - 1][i - 1];
359 value[i - 1][i - 1] = 1.0;
361 for (j = 1; j <= l; j++)
363 value[j - 1][i - 1] = 0.0;
364 value[i - 1][j - 1] = 0.0;
372 public void tqli() throws Exception
392 for (i = 2; i <= n; i++)
399 for (l = 1; l <= n; l++)
405 for (m = l; m <= (n - 1); m++)
407 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
409 if ((Math.abs(e[m - 1]) + dd) == dd)
421 throw new Exception(MessageManager.formatMessage(
422 "exception.matrix_too_many_iteration", new String[] {
423 "tqli", Integer.valueOf(maxIter).toString() }));
427 // System.out.println("Iteration " + iter);
430 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
431 r = Math.sqrt((g * g) + 1.0);
432 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
437 for (i = m - 1; i >= l; i--)
442 if (Math.abs(f) >= Math.abs(g))
445 r = Math.sqrt((c * c) + 1.0);
453 r = Math.sqrt((s * s) + 1.0);
460 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
465 for (k = 1; k <= n; k++)
468 value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
469 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
473 d[l - 1] = d[l - 1] - p;
498 this.d = new double[rows];
499 this.e = new double[rows];
501 for (i = n - 1; i >= 1; i--)
509 for (k = 0; k < l; k++)
511 scale += Math.abs(value[i][k]);
520 for (k = 0; k < l; k++)
522 value[i][k] /= scale;
523 h += (value[i][k] * value[i][k]);
530 g = -1.0 * Math.sqrt(h);
542 for (j = 0; j < l; j++)
544 value[j][i] = value[i][j] / h;
547 for (k = 0; k < j; k++)
549 g += (value[j][k] * value[i][k]);
552 for (k = j; k < l; k++)
554 g += (value[k][j] * value[i][k]);
558 f += (e[j] * value[i][j]);
563 for (j = 0; j < l; j++)
569 for (k = 0; k < j; k++)
571 value[j][k] -= ((f * e[k]) + (g * value[i][k]));
587 for (i = 0; i < n; i++)
593 for (j = 0; j < l; j++)
597 for (k = 0; k < l; k++)
599 g += (value[i][k] * value[k][j]);
602 for (k = 0; k < l; k++)
604 value[k][j] -= (g * value[k][i]);
612 for (j = 0; j < l; j++)
623 public void tqli2() throws Exception
643 for (i = 2; i <= n; i++)
650 for (l = 1; l <= n; l++)
656 for (m = l; m <= (n - 1); m++)
658 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
660 if ((Math.abs(e[m - 1]) + dd) == dd)
672 throw new Exception(MessageManager.formatMessage(
673 "exception.matrix_too_many_iteration", new String[] {
674 "tqli2", Integer.valueOf(maxIter).toString() }));
678 // System.out.println("Iteration " + iter);
681 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
682 r = Math.sqrt((g * g) + 1.0);
683 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
688 for (i = m - 1; i >= l; i--)
693 if (Math.abs(f) >= Math.abs(g))
696 r = Math.sqrt((c * c) + 1.0);
704 r = Math.sqrt((s * s) + 1.0);
711 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
716 for (k = 1; k <= n; k++)
719 value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
720 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
724 d[l - 1] = d[l - 1] - p;
740 * @return DOCUMENT ME!
742 public double sign(double a, double b)
755 * Returns an array containing the values in the specified column
761 public double[] getColumn(int col)
763 double[] out = new double[rows];
765 for (int i = 0; i < rows; i++)
767 out[i] = value[i][col];
779 public void printD(PrintStream ps)
781 for (int j = 0; j < rows; j++)
783 Format.print(ps, "%15.4e", d[j]);
793 public void printE(PrintStream ps)
795 for (int j = 0; j < rows; j++)
797 Format.print(ps, "%15.4e", e[j]);