2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.math;
\r
23 import jalview.util.*;
\r
29 * @version $Revision$
\r
36 public double[][] value;
\r
38 /** DOCUMENT ME!! */
\r
41 /** DOCUMENT ME!! */
\r
44 /** DOCUMENT ME!! */
\r
45 public double[] d; // Diagonal
\r
47 /** DOCUMENT ME!! */
\r
48 public double[] e; // off diagonal
\r
51 * Creates a new Matrix object.
\r
53 * @param value DOCUMENT ME!
\r
54 * @param rows DOCUMENT ME!
\r
55 * @param cols DOCUMENT ME!
\r
57 public Matrix(double[][] value, int rows, int cols)
\r
67 * @return DOCUMENT ME!
\r
69 public Matrix transpose()
\r
71 double[][] out = new double[cols][rows];
\r
73 for (int i = 0; i < cols; i++)
\r
75 for (int j = 0; j < rows; j++)
\r
77 out[i][j] = value[j][i];
\r
81 return new Matrix(out, cols, rows);
\r
87 * @param ps DOCUMENT ME!
\r
89 public void print(PrintStream ps)
\r
91 for (int i = 0; i < rows; i++)
\r
93 for (int j = 0; j < cols; j++)
\r
95 Format.print(ps, "%8.2f", value[i][j]);
\r
105 * @param in DOCUMENT ME!
\r
107 * @return DOCUMENT ME!
\r
109 public Matrix preMultiply(Matrix in)
\r
111 double[][] tmp = new double[in.rows][this.cols];
\r
113 for (int i = 0; i < in.rows; i++)
\r
115 for (int j = 0; j < this.cols; j++)
\r
119 for (int k = 0; k < in.cols; k++)
\r
121 tmp[i][j] += (in.value[i][k] * this.value[k][j]);
\r
126 return new Matrix(tmp, in.rows, this.cols);
\r
132 * @param in DOCUMENT ME!
\r
134 * @return DOCUMENT ME!
\r
136 public double[] vectorPostMultiply(double[] in)
\r
138 double[] out = new double[in.length];
\r
140 for (int i = 0; i < in.length; i++)
\r
144 for (int k = 0; k < in.length; k++)
\r
146 out[i] += (value[i][k] * in[k]);
\r
156 * @param in DOCUMENT ME!
\r
158 * @return DOCUMENT ME!
\r
160 public Matrix postMultiply(Matrix in)
\r
162 double[][] out = new double[this.rows][in.cols];
\r
164 for (int i = 0; i < this.rows; i++)
\r
166 for (int j = 0; j < in.cols; j++)
\r
170 for (int k = 0; k < rows; k++)
\r
172 out[i][j] = out[i][j] + (value[i][k] * in.value[k][j]);
\r
177 return new Matrix(out, this.cols, in.rows);
\r
183 * @return DOCUMENT ME!
\r
185 public Matrix copy()
\r
187 double[][] newmat = new double[rows][cols];
\r
189 for (int i = 0; i < rows; i++)
\r
191 for (int j = 0; j < cols; j++)
\r
193 newmat[i][j] = value[i][j];
\r
197 return new Matrix(newmat, rows, cols);
\r
217 this.d = new double[rows];
\r
218 this.e = new double[rows];
\r
220 for (i = n; i >= 2; i--)
\r
228 for (k = 1; k <= l; k++)
\r
230 scale += Math.abs(value[i - 1][k - 1]);
\r
235 e[i - 1] = value[i - 1][l - 1];
\r
239 for (k = 1; k <= l; k++)
\r
241 value[i - 1][k - 1] /= scale;
\r
242 h += (value[i - 1][k - 1] * value[i - 1][k - 1]);
\r
245 f = value[i - 1][l - 1];
\r
249 g = -1.0 * Math.sqrt(h);
\r
256 e[i - 1] = scale * g;
\r
258 value[i - 1][l - 1] = f - g;
\r
261 for (j = 1; j <= l; j++)
\r
263 value[j - 1][i - 1] = value[i - 1][j - 1] / h;
\r
266 for (k = 1; k <= j; k++)
\r
268 g += (value[j - 1][k - 1] * value[i - 1][k - 1]);
\r
271 for (k = j + 1; k <= l; k++)
\r
273 g += (value[k - 1][j - 1] * value[i - 1][k - 1]);
\r
277 f += (e[j - 1] * value[i - 1][j - 1]);
\r
282 for (j = 1; j <= l; j++)
\r
284 f = value[i - 1][j - 1];
\r
285 g = e[j - 1] - (hh * f);
\r
288 for (k = 1; k <= j; k++)
\r
290 value[j - 1][k - 1] -= ( (f * e[k - 1]) +
\r
291 (g * value[i - 1][k - 1]));
\r
298 e[i - 1] = value[i - 1][l - 1];
\r
307 for (i = 1; i <= n; i++)
\r
311 if (d[i - 1] != 0.0)
\r
313 for (j = 1; j <= l; j++)
\r
317 for (k = 1; k <= l; k++)
\r
319 g += (value[i - 1][k - 1] * value[k - 1][j - 1]);
\r
322 for (k = 1; k <= l; k++)
\r
324 value[k - 1][j - 1] -= (g * value[k - 1][i - 1]);
\r
329 d[i - 1] = value[i - 1][i - 1];
\r
330 value[i - 1][i - 1] = 1.0;
\r
332 for (j = 1; j <= l; j++)
\r
334 value[j - 1][i - 1] = 0.0;
\r
335 value[i - 1][j - 1] = 0.0;
\r
363 for (i = 2; i <= n; i++)
\r
365 e[i - 2] = e[i - 1];
\r
370 for (l = 1; l <= n; l++)
\r
376 for (m = l; m <= (n - 1); m++)
\r
378 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
\r
380 if ( (Math.abs(e[m - 1]) + dd) == dd)
\r
392 System.err.print("Too many iterations in tqli");
\r
393 System.exit(0); // JBPNote - should this really be here ???
\r
397 // System.out.println("Iteration " + iter);
\r
400 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
\r
401 r = Math.sqrt( (g * g) + 1.0);
\r
402 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
\r
407 for (i = m - 1; i >= l; i--)
\r
412 if (Math.abs(f) >= Math.abs(g))
\r
415 r = Math.sqrt( (c * c) + 1.0);
\r
423 r = Math.sqrt( (s * s) + 1.0);
\r
430 r = ( (d[i - 1] - g) * s) + (2.0 * c * b);
\r
435 for (k = 1; k <= n; k++)
\r
437 f = value[k - 1][i];
\r
438 value[k - 1][i] = (s * value[k - 1][i - 1]) +
\r
440 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) -
\r
445 d[l - 1] = d[l - 1] - p;
\r
457 public void tred2()
\r
471 this.d = new double[rows];
\r
472 this.e = new double[rows];
\r
474 for (i = n - 1; i >= 1; i--)
\r
482 for (k = 0; k < l; k++)
\r
484 scale += Math.abs(value[i][k]);
\r
489 e[i] = value[i][l];
\r
493 for (k = 0; k < l; k++)
\r
495 value[i][k] /= scale;
\r
496 h += (value[i][k] * value[i][k]);
\r
503 g = -1.0 * Math.sqrt(h);
\r
512 value[i][l] = f - g;
\r
515 for (j = 0; j < l; j++)
\r
517 value[j][i] = value[i][j] / h;
\r
520 for (k = 0; k < j; k++)
\r
522 g += (value[j][k] * value[i][k]);
\r
525 for (k = j; k < l; k++)
\r
527 g += (value[k][j] * value[i][k]);
\r
531 f += (e[j] * value[i][j]);
\r
536 for (j = 0; j < l; j++)
\r
539 g = e[j] - (hh * f);
\r
542 for (k = 0; k < j; k++)
\r
544 value[j][k] -= ( (f * e[k]) + (g * value[i][k]));
\r
551 e[i] = value[i][l];
\r
560 for (i = 0; i < n; i++)
\r
566 for (j = 0; j < l; j++)
\r
570 for (k = 0; k < l; k++)
\r
572 g += (value[i][k] * value[k][j]);
\r
575 for (k = 0; k < l; k++)
\r
577 value[k][j] -= (g * value[k][i]);
\r
582 d[i] = value[i][i];
\r
585 for (j = 0; j < l; j++)
\r
596 public void tqli2()
\r
616 for (i = 2; i <= n; i++)
\r
618 e[i - 2] = e[i - 1];
\r
623 for (l = 1; l <= n; l++)
\r
629 for (m = l; m <= (n - 1); m++)
\r
631 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
\r
633 if ( (Math.abs(e[m - 1]) + dd) == dd)
\r
645 System.err.print("Too many iterations in tqli");
\r
646 System.exit(0); // JBPNote - same as above - not a graceful exit!
\r
650 // System.out.println("Iteration " + iter);
\r
653 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
\r
654 r = Math.sqrt( (g * g) + 1.0);
\r
655 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
\r
660 for (i = m - 1; i >= l; i--)
\r
665 if (Math.abs(f) >= Math.abs(g))
\r
668 r = Math.sqrt( (c * c) + 1.0);
\r
676 r = Math.sqrt( (s * s) + 1.0);
\r
683 r = ( (d[i - 1] - g) * s) + (2.0 * c * b);
\r
688 for (k = 1; k <= n; k++)
\r
690 f = value[k - 1][i];
\r
691 value[k - 1][i] = (s * value[k - 1][i - 1]) +
\r
693 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) -
\r
698 d[l - 1] = d[l - 1] - p;
\r
710 * @param a DOCUMENT ME!
\r
711 * @param b DOCUMENT ME!
\r
713 * @return DOCUMENT ME!
\r
715 public double sign(double a, double b)
\r
719 return -Math.abs(a);
\r
723 return Math.abs(a);
\r
730 * @param n DOCUMENT ME!
\r
732 * @return DOCUMENT ME!
\r
734 public double[] getColumn(int n)
\r
736 double[] out = new double[rows];
\r
738 for (int i = 0; i < rows; i++)
\r
740 out[i] = value[i][n];
\r
749 * @param ps DOCUMENT ME!
\r
751 public void printD(PrintStream ps)
\r
753 for (int j = 0; j < rows; j++)
\r
755 Format.print(ps, "%15.4e", d[j]);
\r
762 * @param ps DOCUMENT ME!
\r
764 public void printE(PrintStream ps)
\r
766 for (int j = 0; j < rows; j++)
\r
768 Format.print(ps, "%15.4e", e[j]);
\r
775 * @param args DOCUMENT ME!
\r
777 public static void main(String[] args)
\r
779 int n = Integer.parseInt(args[0]);
\r
780 double[][] in = new double[n][n];
\r
782 for (int i = 0; i < n; i++)
\r
784 for (int j = 0; j < n; j++)
\r
786 in[i][j] = (double) Math.random();
\r
790 Matrix origmat = new Matrix(in, n, n);
\r
792 // System.out.println(" --- Original matrix ---- ");
\r
793 /// origmat.print(System.out);
\r
794 //System.out.println();
\r
795 //System.out.println(" --- transpose matrix ---- ");
\r
796 Matrix trans = origmat.transpose();
\r
798 //trans.print(System.out);
\r
799 //System.out.println();
\r
800 //System.out.println(" --- OrigT * Orig ---- ");
\r
801 Matrix symm = trans.postMultiply(origmat);
\r
803 //symm.print(System.out);
\r
804 //System.out.println();
\r
805 // Copy the symmetric matrix for later
\r
806 //Matrix origsymm = symm.copy();
\r
808 // This produces the tridiagonal transformation matrix
\r
809 //long tstart = System.currentTimeMillis();
\r
812 //long tend = System.currentTimeMillis();
\r
814 //System.out.println("Time take for tred = " + (tend-tstart) + "ms");
\r
815 //System.out.println(" ---Tridiag transform matrix ---");
\r
816 //symm.print(System.out);
\r
817 //System.out.println();
\r
818 //System.out.println(" --- D vector ---");
\r
819 //symm.printD(System.out);
\r
820 //System.out.println();
\r
821 //System.out.println(" --- E vector ---");
\r
822 //symm.printE(System.out);
\r
823 //System.out.println();
\r
824 // Now produce the diagonalization matrix
\r
825 //tstart = System.currentTimeMillis();
\r
827 //tend = System.currentTimeMillis();
\r
829 //System.out.println("Time take for tqli = " + (tend-tstart) + " ms");
\r
830 //System.out.println(" --- New diagonalization matrix ---");
\r
831 //symm.print(System.out);
\r
832 //System.out.println();
\r
833 //System.out.println(" --- D vector ---");
\r
834 //symm.printD(System.out);
\r
835 //System.out.println();
\r
836 //System.out.println(" --- E vector ---");
\r
837 //symm.printE(System.out);
\r
838 //System.out.println();
\r
839 //System.out.println(" --- First eigenvector --- ");
\r
840 //double[] eigenv = symm.getColumn(0);
\r
841 //for (int i=0; i < eigenv.length;i++) {
\r
842 // Format.print(System.out,"%15.4f",eigenv[i]);
\r
844 //System.out.println();
\r
845 //double[] neigenv = origsymm.vectorPostMultiply(eigenv);
\r
846 //for (int i=0; i < neigenv.length;i++) {
\r
847 // Format.print(System.out,"%15.4f",neigenv[i]/symm.d[0]);
\r
849 //System.out.println();
\r