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] = getValue(i - 1, l - 1);
289 for (k = 1; k <= l; k++)
291 double v = divideValue(i - 1, k - 1, scale);
295 f = getValue(i - 1, l - 1);
299 g = -1.0 * Math.sqrt(h);
306 e[i - 1] = scale * g;
308 setValue(i - 1, l - 1, f - g);
311 for (j = 1; j <= l; j++)
313 double val = getValue(i - 1, j - 1) / h;
314 setValue(j - 1, i - 1, val);
317 for (k = 1; k <= j; k++)
319 g += (getValue(j - 1, k - 1) * getValue(i - 1, k - 1));
322 for (k = j + 1; k <= l; k++)
324 g += (getValue(k - 1, j - 1) * getValue(i - 1, k - 1));
328 f += (e[j - 1] * getValue(i - 1, j - 1));
333 for (j = 1; j <= l; j++)
335 f = getValue(i - 1, j - 1);
336 g = e[j - 1] - (hh * f);
339 for (k = 1; k <= j; k++)
341 double val = (f * e[k - 1]) + (g * getValue(i - 1, k - 1));
342 addValue(j - 1, k - 1, -val);
349 e[i - 1] = getValue(i - 1, l - 1);
358 for (i = 1; i <= n; i++)
364 for (j = 1; j <= l; j++)
368 for (k = 1; k <= l; k++)
370 g += (getValue(i - 1, k - 1) * getValue(k - 1, j - 1));
373 for (k = 1; k <= l; k++)
375 double x = addValue(k - 1, j - 1, -(g * getValue(k - 1, i - 1)));
380 d[i - 1] = getValue(i - 1, i - 1);
381 setValue(i - 1, i - 1, 1.0);
383 for (j = 1; j <= l; j++)
385 setValue(j - 1, i - 1, 0.0);
386 setValue(i - 1, j - 1, 0.0);
392 * Adds f to the value at [i, j] and returns the new value
398 protected double addValue(int i, int j, double f)
400 double v = value[i][j] + f;
406 * Divides the value at [i, j] by divisor and returns the new value. If d is
407 * zero, returns the unchanged value.
414 protected double divideValue(int i, int j, double divisor)
418 return getValue(i, j);
420 double v = value[i][j];
430 public void tqli() throws Exception
449 for (i = 2; i <= n; i++)
456 for (l = 1; l <= n; l++)
462 for (m = l; m <= (n - 1); m++)
464 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
466 if ((Math.abs(e[m - 1]) + dd) == dd)
478 throw new Exception(MessageManager.formatMessage(
479 "exception.matrix_too_many_iteration", new String[] {
480 "tqli", Integer.valueOf(maxIter).toString() }));
484 // System.out.println("Iteration " + iter);
487 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
488 r = Math.sqrt((g * g) + 1.0);
489 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
494 for (i = m - 1; i >= l; i--)
499 if (Math.abs(f) >= Math.abs(g))
502 r = Math.sqrt((c * c) + 1.0);
510 r = Math.sqrt((s * s) + 1.0);
517 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
522 for (k = 1; k <= n; k++)
524 // f = value[k - 1][i];
525 // value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
526 // value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
527 f = getValue(k - 1, i);
528 setValue(k - 1, i, (s * getValue(k - 1, i - 1)) + (c * f));
529 setValue(k - 1, i - 1, (c * getValue(k - 1, i - 1)) - (s * f));
533 d[l - 1] = d[l - 1] - p;
542 public double getValue(int i, int j)
547 public void setValue(int i, int j, double val)
569 this.d = new double[rows];
570 this.e = new double[rows];
572 for (i = n - 1; i >= 1; i--)
580 for (k = 0; k < l; k++)
582 scale += Math.abs(value[i][k]);
591 for (k = 0; k < l; k++)
593 value[i][k] /= scale;
594 h += (value[i][k] * value[i][k]);
601 g = -1.0 * Math.sqrt(h);
613 for (j = 0; j < l; j++)
615 value[j][i] = value[i][j] / h;
618 for (k = 0; k < j; k++)
620 g += (value[j][k] * value[i][k]);
623 for (k = j; k < l; k++)
625 g += (value[k][j] * value[i][k]);
629 f += (e[j] * value[i][j]);
634 for (j = 0; j < l; j++)
640 for (k = 0; k < j; k++)
642 value[j][k] -= ((f * e[k]) + (g * value[i][k]));
658 for (i = 0; i < n; i++)
664 for (j = 0; j < l; j++)
668 for (k = 0; k < l; k++)
670 g += (value[i][k] * value[k][j]);
673 for (k = 0; k < l; k++)
675 value[k][j] -= (g * value[k][i]);
683 for (j = 0; j < l; j++)
694 public void tqli2() throws Exception
714 for (i = 2; i <= n; i++)
721 for (l = 1; l <= n; l++)
727 for (m = l; m <= (n - 1); m++)
729 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
731 if ((Math.abs(e[m - 1]) + dd) == dd)
743 throw new Exception(MessageManager.formatMessage(
744 "exception.matrix_too_many_iteration", new String[] {
745 "tqli2", Integer.valueOf(maxIter).toString() }));
749 // System.out.println("Iteration " + iter);
752 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
753 r = Math.sqrt((g * g) + 1.0);
754 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
759 for (i = m - 1; i >= l; i--)
764 if (Math.abs(f) >= Math.abs(g))
767 r = Math.sqrt((c * c) + 1.0);
775 r = Math.sqrt((s * s) + 1.0);
782 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
787 for (k = 1; k <= n; k++)
790 value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
791 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
795 d[l - 1] = d[l - 1] - p;
804 * Answers the first argument with the sign of the second argument
811 static double sign(double a, double b)
824 * Returns an array containing the values in the specified column
830 public double[] getColumn(int col)
832 double[] out = new double[rows];
834 for (int i = 0; i < rows; i++)
836 out[i] = value[i][col];
850 public void printD(PrintStream ps, String format)
852 for (int j = 0; j < rows; j++)
854 Format.print(ps, format, d[j]);
866 public void printE(PrintStream ps, String format)
868 for (int j = 0; j < rows; j++)
870 Format.print(ps, format, e[j]);
875 public double[] getD()
881 public double[] getE()
887 public int height() {
898 public double[] getRow(int i)
900 double[] row = new double[cols];
901 System.arraycopy(value[i], 0, row, 0, cols);