2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
22 import jalview.util.*;
35 public double[][] value;
44 public double[] d; // Diagonal
47 public double[] e; // off diagonal
50 * Creates a new Matrix object.
59 public Matrix(double[][] value, int rows, int cols)
69 * @return DOCUMENT ME!
71 public Matrix transpose()
73 double[][] out = new double[cols][rows];
75 for (int i = 0; i < cols; i++)
77 for (int j = 0; j < rows; j++)
79 out[i][j] = value[j][i];
83 return new Matrix(out, cols, rows);
92 public void print(PrintStream ps)
94 for (int i = 0; i < rows; i++)
96 for (int j = 0; j < cols; j++)
98 Format.print(ps, "%8.2f", value[i][j]);
111 * @return DOCUMENT ME!
113 public Matrix preMultiply(Matrix in)
115 double[][] tmp = new double[in.rows][this.cols];
117 for (int i = 0; i < in.rows; i++)
119 for (int j = 0; j < this.cols; j++)
123 for (int k = 0; k < in.cols; k++)
125 tmp[i][j] += (in.value[i][k] * this.value[k][j]);
130 return new Matrix(tmp, in.rows, this.cols);
139 * @return DOCUMENT ME!
141 public double[] vectorPostMultiply(double[] in)
143 double[] out = new double[in.length];
145 for (int i = 0; i < in.length; i++)
149 for (int k = 0; k < in.length; k++)
151 out[i] += (value[i][k] * in[k]);
164 * @return DOCUMENT ME!
166 public Matrix postMultiply(Matrix in)
168 double[][] out = new double[this.rows][in.cols];
170 for (int i = 0; i < this.rows; i++)
172 for (int j = 0; j < in.cols; j++)
176 for (int k = 0; k < rows; k++)
178 out[i][j] = out[i][j] + (value[i][k] * in.value[k][j]);
183 return new Matrix(out, this.cols, in.rows);
189 * @return DOCUMENT ME!
193 double[][] newmat = new double[rows][cols];
195 for (int i = 0; i < rows; i++)
197 for (int j = 0; j < cols; j++)
199 newmat[i][j] = value[i][j];
203 return new Matrix(newmat, rows, cols);
223 this.d = new double[rows];
224 this.e = new double[rows];
226 for (i = n; i >= 2; i--)
234 for (k = 1; k <= l; k++)
236 scale += Math.abs(value[i - 1][k - 1]);
241 e[i - 1] = value[i - 1][l - 1];
245 for (k = 1; k <= l; k++)
247 value[i - 1][k - 1] /= scale;
248 h += (value[i - 1][k - 1] * value[i - 1][k - 1]);
251 f = value[i - 1][l - 1];
255 g = -1.0 * Math.sqrt(h);
262 e[i - 1] = scale * g;
264 value[i - 1][l - 1] = f - g;
267 for (j = 1; j <= l; j++)
269 value[j - 1][i - 1] = value[i - 1][j - 1] / h;
272 for (k = 1; k <= j; k++)
274 g += (value[j - 1][k - 1] * value[i - 1][k - 1]);
277 for (k = j + 1; k <= l; k++)
279 g += (value[k - 1][j - 1] * value[i - 1][k - 1]);
283 f += (e[j - 1] * value[i - 1][j - 1]);
288 for (j = 1; j <= l; j++)
290 f = value[i - 1][j - 1];
291 g = e[j - 1] - (hh * f);
294 for (k = 1; k <= j; k++)
296 value[j - 1][k - 1] -= ((f * e[k - 1]) + (g * value[i - 1][k - 1]));
303 e[i - 1] = value[i - 1][l - 1];
312 for (i = 1; i <= n; i++)
318 for (j = 1; j <= l; j++)
322 for (k = 1; k <= l; k++)
324 g += (value[i - 1][k - 1] * value[k - 1][j - 1]);
327 for (k = 1; k <= l; k++)
329 value[k - 1][j - 1] -= (g * value[k - 1][i - 1]);
334 d[i - 1] = value[i - 1][i - 1];
335 value[i - 1][i - 1] = 1.0;
337 for (j = 1; j <= l; j++)
339 value[j - 1][i - 1] = 0.0;
340 value[i - 1][j - 1] = 0.0;
368 for (i = 2; i <= n; i++)
375 for (l = 1; l <= n; l++)
381 for (m = l; m <= (n - 1); m++)
383 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
385 if ((Math.abs(e[m - 1]) + dd) == dd)
397 System.err.print("Too many iterations in tqli");
398 System.exit(0); // JBPNote - should this really be here ???
402 // System.out.println("Iteration " + iter);
405 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
406 r = Math.sqrt((g * g) + 1.0);
407 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
412 for (i = m - 1; i >= l; i--)
417 if (Math.abs(f) >= Math.abs(g))
420 r = Math.sqrt((c * c) + 1.0);
428 r = Math.sqrt((s * s) + 1.0);
435 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
440 for (k = 1; k <= n; k++)
443 value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
444 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
448 d[l - 1] = d[l - 1] - p;
473 this.d = new double[rows];
474 this.e = new double[rows];
476 for (i = n - 1; i >= 1; i--)
484 for (k = 0; k < l; k++)
486 scale += Math.abs(value[i][k]);
495 for (k = 0; k < l; k++)
497 value[i][k] /= scale;
498 h += (value[i][k] * value[i][k]);
505 g = -1.0 * Math.sqrt(h);
517 for (j = 0; j < l; j++)
519 value[j][i] = value[i][j] / h;
522 for (k = 0; k < j; k++)
524 g += (value[j][k] * value[i][k]);
527 for (k = j; k < l; k++)
529 g += (value[k][j] * value[i][k]);
533 f += (e[j] * value[i][j]);
538 for (j = 0; j < l; j++)
544 for (k = 0; k < j; k++)
546 value[j][k] -= ((f * e[k]) + (g * value[i][k]));
562 for (i = 0; i < n; i++)
568 for (j = 0; j < l; j++)
572 for (k = 0; k < l; k++)
574 g += (value[i][k] * value[k][j]);
577 for (k = 0; k < l; k++)
579 value[k][j] -= (g * value[k][i]);
587 for (j = 0; j < l; j++)
618 for (i = 2; i <= n; i++)
625 for (l = 1; l <= n; l++)
631 for (m = l; m <= (n - 1); m++)
633 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
635 if ((Math.abs(e[m - 1]) + dd) == dd)
647 System.err.print("Too many iterations in tqli");
648 System.exit(0); // JBPNote - same as above - not a graceful exit!
652 // System.out.println("Iteration " + iter);
655 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
656 r = Math.sqrt((g * g) + 1.0);
657 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
662 for (i = m - 1; i >= l; i--)
667 if (Math.abs(f) >= Math.abs(g))
670 r = Math.sqrt((c * c) + 1.0);
678 r = Math.sqrt((s * s) + 1.0);
685 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
690 for (k = 1; k <= n; k++)
693 value[k - 1][i] = (s * value[k - 1][i - 1]) + (c * f);
694 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) - (s * f);
698 d[l - 1] = d[l - 1] - p;
714 * @return DOCUMENT ME!
716 public double sign(double a, double b)
734 * @return DOCUMENT ME!
736 public double[] getColumn(int n)
738 double[] out = new double[rows];
740 for (int i = 0; i < rows; i++)
742 out[i] = value[i][n];
754 public void printD(PrintStream ps)
756 for (int j = 0; j < rows; j++)
758 Format.print(ps, "%15.4e", d[j]);
768 public void printE(PrintStream ps)
770 for (int j = 0; j < rows; j++)
772 Format.print(ps, "%15.4e", e[j]);
782 public static void main(String[] args)
784 int n = Integer.parseInt(args[0]);
785 double[][] in = new double[n][n];
787 for (int i = 0; i < n; i++)
789 for (int j = 0; j < n; j++)
791 in[i][j] = (double) Math.random();
795 Matrix origmat = new Matrix(in, n, n);
797 // System.out.println(" --- Original matrix ---- ");
798 // / origmat.print(System.out);
799 // System.out.println();
800 // System.out.println(" --- transpose matrix ---- ");
801 Matrix trans = origmat.transpose();
803 // trans.print(System.out);
804 // System.out.println();
805 // System.out.println(" --- OrigT * Orig ---- ");
806 Matrix symm = trans.postMultiply(origmat);
808 // symm.print(System.out);
809 // System.out.println();
810 // Copy the symmetric matrix for later
811 // Matrix origsymm = symm.copy();
813 // This produces the tridiagonal transformation matrix
814 // long tstart = System.currentTimeMillis();
817 // long tend = System.currentTimeMillis();
819 // System.out.println("Time take for tred = " + (tend-tstart) + "ms");
820 // System.out.println(" ---Tridiag transform matrix ---");
821 // symm.print(System.out);
822 // System.out.println();
823 // System.out.println(" --- D vector ---");
824 // symm.printD(System.out);
825 // System.out.println();
826 // System.out.println(" --- E vector ---");
827 // symm.printE(System.out);
828 // System.out.println();
829 // Now produce the diagonalization matrix
830 // tstart = System.currentTimeMillis();
832 // tend = System.currentTimeMillis();
834 // System.out.println("Time take for tqli = " + (tend-tstart) + " ms");
835 // System.out.println(" --- New diagonalization matrix ---");
836 // symm.print(System.out);
837 // System.out.println();
838 // System.out.println(" --- D vector ---");
839 // symm.printD(System.out);
840 // System.out.println();
841 // System.out.println(" --- E vector ---");
842 // symm.printE(System.out);
843 // System.out.println();
844 // System.out.println(" --- First eigenvector --- ");
845 // double[] eigenv = symm.getColumn(0);
846 // for (int i=0; i < eigenv.length;i++) {
847 // Format.print(System.out,"%15.4f",eigenv[i]);
849 // System.out.println();
850 // double[] neigenv = origsymm.vectorPostMultiply(eigenv);
851 // for (int i=0; i < neigenv.length;i++) {
852 // Format.print(System.out,"%15.4f",neigenv[i]/symm.d[0]);
854 // System.out.println();