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
34 * the cell values in row-major order
36 public double[][] value;
44 * the number of columns
49 public double[] d; // Diagonal
52 public double[] e; // off diagonal
55 * maximum number of iterations for tqli
58 int maxIter = 45; // fudge - add 15 iterations, just in case
61 * Creates a new Matrix object. For example
64 * new Matrix(new double[][] {{2, 3, 4}, {5, 6, 7})
70 * Note that ragged arrays (with not all rows, or columns, of the same
71 * length), are not supported by this class. They can be constructed, but
72 * results of operations on them are undefined and may throw exceptions.
75 * the matrix values in row-major order
77 public Matrix(double[][] values)
79 this.rows = values.length;
80 this.cols = this.rows == 0 ? 0 : values[0].length;
85 * Returns a new matrix which is the transposes of this one
87 * @return DOCUMENT ME!
89 public Matrix transpose()
91 double[][] out = new double[cols][rows];
93 for (int i = 0; i < cols; i++)
95 for (int j = 0; j < rows; j++)
97 out[i][j] = value[j][i];
101 return new Matrix(out);
110 public void print(PrintStream ps)
112 for (int i = 0; i < rows; i++)
114 for (int j = 0; j < cols; j++)
116 Format.print(ps, "%8.2f", value[i][j]);
124 * Returns a new matrix which is the result of premultiplying this matrix by
125 * the supplied argument. If this of size AxB (A rows and B columns), and the
126 * argument is CxA (C rows and A columns), the result is of size CxB.
131 * @throws IllegalArgumentException
132 * if the number of columns in the pre-multiplier is not equal to
133 * the number of rows in the multiplicand (this)
135 public Matrix preMultiply(Matrix in)
137 if (in.cols != this.rows)
139 throw new IllegalArgumentException("Can't pre-multiply " + this.rows
140 + " rows by " + in.cols + " columns");
142 double[][] tmp = new double[in.rows][this.cols];
144 for (int i = 0; i < in.rows; i++)
146 for (int j = 0; j < this.cols; j++)
150 for (int k = 0; k < in.cols; k++)
152 tmp[i][j] += (in.value[i][k] * this.value[k][j]);
157 return new Matrix(tmp);
166 public double[] vectorPostMultiply(double[] in)
168 double[] out = new double[in.length];
170 for (int i = 0; i < in.length; i++)
174 for (int k = 0; k < in.length; k++)
176 out[i] += (value[i][k] * in[k]);
184 * Returns a new matrix which is the result of postmultiplying this matrix by
185 * the supplied argument. If this of size AxB (A rows and B columns), and the
186 * argument is BxC (B rows and C columns), the result is of size AxC.
188 * This method simply returns the result of in.preMultiply(this)
193 * @throws IllegalArgumentException
194 * if the number of rows in the post-multiplier is not equal to the
195 * number of columns in the multiplicand (this)
196 * @see #preMultiply(Matrix)
198 public Matrix postMultiply(Matrix in)
200 if (in.rows != this.cols)
202 throw new IllegalArgumentException("Can't post-multiply " + this.cols
203 + " columns by " + in.rows + " rows");
205 return in.preMultiply(this);
209 * Answers a new matrix with a copy of the values in this one
215 double[][] newmat = new double[rows][cols];
217 for (int i = 0; i < rows; i++)
219 System.arraycopy(value[i], 0, newmat[i], 0, value[i].length);
222 return new Matrix(newmat);
242 this.d = new double[rows];
243 this.e = new double[rows];
245 for (i = n; i >= 2; i--)
253 for (k = 1; k <= l; k++)
255 scale += Math.abs(value[i - 1][k - 1]);
260 e[i - 1] = value[i - 1][l - 1];
264 for (k = 1; k <= l; k++)
266 value[i - 1][k - 1] /= scale;
267 h += (value[i - 1][k - 1] * value[i - 1][k - 1]);
270 f = value[i - 1][l - 1];
274 g = -1.0 * Math.sqrt(h);
281 e[i - 1] = scale * g;
283 value[i - 1][l - 1] = f - g;
286 for (j = 1; j <= l; j++)
288 value[j - 1][i - 1] = value[i - 1][j - 1] / h;
291 for (k = 1; k <= j; k++)
293 g += (value[j - 1][k - 1] * value[i - 1][k - 1]);
296 for (k = j + 1; k <= l; k++)
298 g += (value[k - 1][j - 1] * value[i - 1][k - 1]);
302 f += (e[j - 1] * value[i - 1][j - 1]);
307 for (j = 1; j <= l; j++)
309 f = value[i - 1][j - 1];
310 g = e[j - 1] - (hh * f);
313 for (k = 1; k <= j; k++)
315 value[j - 1][k - 1] -= ((f * e[k - 1]) + (g * value[i - 1][k - 1]));
322 e[i - 1] = value[i - 1][l - 1];
331 for (i = 1; i <= n; i++)
337 for (j = 1; j <= l; j++)
341 for (k = 1; k <= l; k++)
343 g += (value[i - 1][k - 1] * value[k - 1][j - 1]);
346 for (k = 1; k <= l; k++)
348 value[k - 1][j - 1] -= (g * value[k - 1][i - 1]);
353 d[i - 1] = value[i - 1][i - 1];
354 value[i - 1][i - 1] = 1.0;
356 for (j = 1; j <= l; j++)
358 value[j - 1][i - 1] = 0.0;
359 value[i - 1][j - 1] = 0.0;
367 public void tqli() throws Exception
387 for (i = 2; i <= n; i++)
394 for (l = 1; l <= n; l++)
400 for (m = l; m <= (n - 1); m++)
402 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
404 if ((Math.abs(e[m - 1]) + dd) == dd)
416 throw new Exception(MessageManager.formatMessage(
417 "exception.matrix_too_many_iteration", new String[] {
418 "tqli", Integer.valueOf(maxIter).toString() }));
422 // System.out.println("Iteration " + iter);
425 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
426 r = Math.sqrt((g * g) + 1.0);
427 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
432 for (i = m - 1; i >= l; i--)
437 if (Math.abs(f) >= Math.abs(g))
440 r = Math.sqrt((c * c) + 1.0);
448 r = Math.sqrt((s * s) + 1.0);
455 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
460 for (k = 1; k <= n; k++)
463 value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
464 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
468 d[l - 1] = d[l - 1] - p;
493 this.d = new double[rows];
494 this.e = new double[rows];
496 for (i = n - 1; i >= 1; i--)
504 for (k = 0; k < l; k++)
506 scale += Math.abs(value[i][k]);
515 for (k = 0; k < l; k++)
517 value[i][k] /= scale;
518 h += (value[i][k] * value[i][k]);
525 g = -1.0 * Math.sqrt(h);
537 for (j = 0; j < l; j++)
539 value[j][i] = value[i][j] / h;
542 for (k = 0; k < j; k++)
544 g += (value[j][k] * value[i][k]);
547 for (k = j; k < l; k++)
549 g += (value[k][j] * value[i][k]);
553 f += (e[j] * value[i][j]);
558 for (j = 0; j < l; j++)
564 for (k = 0; k < j; k++)
566 value[j][k] -= ((f * e[k]) + (g * value[i][k]));
582 for (i = 0; i < n; i++)
588 for (j = 0; j < l; j++)
592 for (k = 0; k < l; k++)
594 g += (value[i][k] * value[k][j]);
597 for (k = 0; k < l; k++)
599 value[k][j] -= (g * value[k][i]);
607 for (j = 0; j < l; j++)
618 public void tqli2() throws Exception
638 for (i = 2; i <= n; i++)
645 for (l = 1; l <= n; l++)
651 for (m = l; m <= (n - 1); m++)
653 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
655 if ((Math.abs(e[m - 1]) + dd) == dd)
667 throw new Exception(MessageManager.formatMessage(
668 "exception.matrix_too_many_iteration", new String[] {
669 "tqli2", Integer.valueOf(maxIter).toString() }));
673 // System.out.println("Iteration " + iter);
676 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
677 r = Math.sqrt((g * g) + 1.0);
678 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
683 for (i = m - 1; i >= l; i--)
688 if (Math.abs(f) >= Math.abs(g))
691 r = Math.sqrt((c * c) + 1.0);
699 r = Math.sqrt((s * s) + 1.0);
706 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
711 for (k = 1; k <= n; k++)
714 value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
715 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
719 d[l - 1] = d[l - 1] - p;
735 * @return DOCUMENT ME!
737 public double sign(double a, double b)
750 * Returns an array containing the values in the specified column
756 public double[] getColumn(int col)
758 double[] out = new double[rows];
760 for (int i = 0; i < rows; i++)
762 out[i] = value[i][col];
774 public void printD(PrintStream ps)
776 for (int j = 0; j < rows; j++)
778 Format.print(ps, "%15.4e", d[j]);
788 public void printE(PrintStream ps)
790 for (int j = 0; j < rows; j++)
792 Format.print(ps, "%15.4e", e[j]);