2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 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
21 import jalview.util.*;
\r
30 * @version $Revision$
\r
37 public double[][] value;
\r
39 /** DOCUMENT ME!! */
\r
42 /** DOCUMENT ME!! */
\r
45 /** DOCUMENT ME!! */
\r
46 public double[] d; // Diagonal
\r
48 /** DOCUMENT ME!! */
\r
49 public double[] e; // off diagonal
\r
52 * Creates a new Matrix object.
\r
54 * @param value DOCUMENT ME!
\r
55 * @param rows DOCUMENT ME!
\r
56 * @param cols DOCUMENT ME!
\r
58 public Matrix(double[][] value, int rows, int cols)
\r
68 * @return DOCUMENT ME!
\r
70 public Matrix transpose()
\r
72 double[][] out = new double[cols][rows];
\r
74 for (int i = 0; i < cols; i++)
\r
76 for (int j = 0; j < rows; j++)
\r
78 out[i][j] = value[j][i];
\r
82 return new Matrix(out, cols, rows);
\r
88 * @param ps DOCUMENT ME!
\r
90 public void print(PrintStream ps)
\r
92 for (int i = 0; i < rows; i++)
\r
94 for (int j = 0; j < cols; j++)
\r
96 Format.print(ps, "%8.2f", value[i][j]);
\r
106 * @param in DOCUMENT ME!
\r
108 * @return DOCUMENT ME!
\r
110 public Matrix preMultiply(Matrix in)
\r
112 double[][] tmp = new double[in.rows][this.cols];
\r
114 for (int i = 0; i < in.rows; i++)
\r
116 for (int j = 0; j < this.cols; j++)
\r
120 for (int k = 0; k < in.cols; k++)
\r
122 tmp[i][j] += (in.value[i][k] * this.value[k][j]);
\r
127 return new Matrix(tmp, in.rows, this.cols);
\r
133 * @param in DOCUMENT ME!
\r
135 * @return DOCUMENT ME!
\r
137 public double[] vectorPostMultiply(double[] in)
\r
139 double[] out = new double[in.length];
\r
141 for (int i = 0; i < in.length; i++)
\r
145 for (int k = 0; k < in.length; k++)
\r
147 out[i] += (value[i][k] * in[k]);
\r
157 * @param in DOCUMENT ME!
\r
159 * @return DOCUMENT ME!
\r
161 public Matrix postMultiply(Matrix in)
\r
163 double[][] out = new double[this.rows][in.cols];
\r
165 for (int i = 0; i < this.rows; i++)
\r
167 for (int j = 0; j < in.cols; j++)
\r
171 for (int k = 0; k < rows; k++)
\r
173 out[i][j] = out[i][j] + (value[i][k] * in.value[k][j]);
\r
178 return new Matrix(out, this.cols, in.rows);
\r
184 * @return DOCUMENT ME!
\r
186 public Matrix copy()
\r
188 double[][] newmat = new double[rows][cols];
\r
190 for (int i = 0; i < rows; i++)
\r
192 for (int j = 0; j < cols; j++)
\r
194 newmat[i][j] = value[i][j];
\r
198 return new Matrix(newmat, rows, cols);
\r
218 this.d = new double[rows];
\r
219 this.e = new double[rows];
\r
221 for (i = n; i >= 2; i--)
\r
229 for (k = 1; k <= l; k++)
\r
231 scale += Math.abs(value[i - 1][k - 1]);
\r
236 e[i - 1] = value[i - 1][l - 1];
\r
240 for (k = 1; k <= l; k++)
\r
242 value[i - 1][k - 1] /= scale;
\r
243 h += (value[i - 1][k - 1] * value[i - 1][k - 1]);
\r
246 f = value[i - 1][l - 1];
\r
250 g = -1.0 * Math.sqrt(h);
\r
257 e[i - 1] = scale * g;
\r
259 value[i - 1][l - 1] = f - g;
\r
262 for (j = 1; j <= l; j++)
\r
264 value[j - 1][i - 1] = value[i - 1][j - 1] / h;
\r
267 for (k = 1; k <= j; k++)
\r
269 g += (value[j - 1][k - 1] * value[i - 1][k - 1]);
\r
272 for (k = j + 1; k <= l; k++)
\r
274 g += (value[k - 1][j - 1] * value[i - 1][k - 1]);
\r
278 f += (e[j - 1] * value[i - 1][j - 1]);
\r
283 for (j = 1; j <= l; j++)
\r
285 f = value[i - 1][j - 1];
\r
286 g = e[j - 1] - (hh * f);
\r
289 for (k = 1; k <= j; k++)
\r
291 value[j - 1][k - 1] -= ((f * e[k - 1]) +
\r
292 (g * value[i - 1][k - 1]));
\r
299 e[i - 1] = value[i - 1][l - 1];
\r
308 for (i = 1; i <= n; i++)
\r
312 if (d[i - 1] != 0.0)
\r
314 for (j = 1; j <= l; j++)
\r
318 for (k = 1; k <= l; k++)
\r
320 g += (value[i - 1][k - 1] * value[k - 1][j - 1]);
\r
323 for (k = 1; k <= l; k++)
\r
325 value[k - 1][j - 1] -= (g * value[k - 1][i - 1]);
\r
330 d[i - 1] = value[i - 1][i - 1];
\r
331 value[i - 1][i - 1] = 1.0;
\r
333 for (j = 1; j <= l; j++)
\r
335 value[j - 1][i - 1] = 0.0;
\r
336 value[i - 1][j - 1] = 0.0;
\r
364 for (i = 2; i <= n; i++)
\r
366 e[i - 2] = e[i - 1];
\r
371 for (l = 1; l <= n; l++)
\r
377 for (m = l; m <= (n - 1); m++)
\r
379 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
\r
381 if ((Math.abs(e[m - 1]) + dd) == dd)
\r
393 System.err.print("Too many iterations in tqli");
\r
394 System.exit(0); // JBPNote - should this really be here ???
\r
398 // System.out.println("Iteration " + iter);
\r
401 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
\r
402 r = Math.sqrt((g * g) + 1.0);
\r
403 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
\r
408 for (i = m - 1; i >= l; i--)
\r
413 if (Math.abs(f) >= Math.abs(g))
\r
416 r = Math.sqrt((c * c) + 1.0);
\r
424 r = Math.sqrt((s * s) + 1.0);
\r
431 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
\r
436 for (k = 1; k <= n; k++)
\r
438 f = value[k - 1][i];
\r
439 value[k - 1][i] = (s * value[k - 1][i - 1]) +
\r
441 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) -
\r
446 d[l - 1] = d[l - 1] - p;
\r
458 public void tred2()
\r
472 this.d = new double[rows];
\r
473 this.e = new double[rows];
\r
475 for (i = n - 1; i >= 1; i--)
\r
483 for (k = 0; k < l; k++)
\r
485 scale += Math.abs(value[i][k]);
\r
490 e[i] = value[i][l];
\r
494 for (k = 0; k < l; k++)
\r
496 value[i][k] /= scale;
\r
497 h += (value[i][k] * value[i][k]);
\r
504 g = -1.0 * Math.sqrt(h);
\r
513 value[i][l] = f - g;
\r
516 for (j = 0; j < l; j++)
\r
518 value[j][i] = value[i][j] / h;
\r
521 for (k = 0; k < j; k++)
\r
523 g += (value[j][k] * value[i][k]);
\r
526 for (k = j; k < l; k++)
\r
528 g += (value[k][j] * value[i][k]);
\r
532 f += (e[j] * value[i][j]);
\r
537 for (j = 0; j < l; j++)
\r
540 g = e[j] - (hh * f);
\r
543 for (k = 0; k < j; k++)
\r
545 value[j][k] -= ((f * e[k]) + (g * value[i][k]));
\r
552 e[i] = value[i][l];
\r
561 for (i = 0; i < n; i++)
\r
567 for (j = 0; j < l; j++)
\r
571 for (k = 0; k < l; k++)
\r
573 g += (value[i][k] * value[k][j]);
\r
576 for (k = 0; k < l; k++)
\r
578 value[k][j] -= (g * value[k][i]);
\r
583 d[i] = value[i][i];
\r
586 for (j = 0; j < l; j++)
\r
597 public void tqli2()
\r
617 for (i = 2; i <= n; i++)
\r
619 e[i - 2] = e[i - 1];
\r
624 for (l = 1; l <= n; l++)
\r
630 for (m = l; m <= (n - 1); m++)
\r
632 dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
\r
634 if ((Math.abs(e[m - 1]) + dd) == dd)
\r
646 System.err.print("Too many iterations in tqli");
\r
647 System.exit(0); // JBPNote - same as above - not a graceful exit!
\r
651 // System.out.println("Iteration " + iter);
\r
654 g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
\r
655 r = Math.sqrt((g * g) + 1.0);
\r
656 g = d[m - 1] - d[l - 1] + (e[l - 1] / (g + sign(r, g)));
\r
661 for (i = m - 1; i >= l; i--)
\r
666 if (Math.abs(f) >= Math.abs(g))
\r
669 r = Math.sqrt((c * c) + 1.0);
\r
677 r = Math.sqrt((s * s) + 1.0);
\r
684 r = ((d[i - 1] - g) * s) + (2.0 * c * b);
\r
689 for (k = 1; k <= n; k++)
\r
691 f = value[k - 1][i];
\r
692 value[k - 1][i] = (s * value[k - 1][i - 1]) +
\r
694 value[k - 1][i - 1] = (c * value[k - 1][i - 1]) -
\r
699 d[l - 1] = d[l - 1] - p;
\r
711 * @param a DOCUMENT ME!
\r
712 * @param b DOCUMENT ME!
\r
714 * @return DOCUMENT ME!
\r
716 public double sign(double a, double b)
\r
720 return -Math.abs(a);
\r
724 return Math.abs(a);
\r
731 * @param n DOCUMENT ME!
\r
733 * @return DOCUMENT ME!
\r
735 public double[] getColumn(int n)
\r
737 double[] out = new double[rows];
\r
739 for (int i = 0; i < rows; i++)
\r
741 out[i] = value[i][n];
\r
750 * @param ps DOCUMENT ME!
\r
752 public void printD(PrintStream ps)
\r
754 for (int j = 0; j < rows; j++)
\r
756 Format.print(ps, "%15.4e", d[j]);
\r
763 * @param ps DOCUMENT ME!
\r
765 public void printE(PrintStream ps)
\r
767 for (int j = 0; j < rows; j++)
\r
769 Format.print(ps, "%15.4e", e[j]);
\r
776 * @param args DOCUMENT ME!
\r
778 public static void main(String[] args)
\r
780 int n = Integer.parseInt(args[0]);
\r
781 double[][] in = new double[n][n];
\r
783 for (int i = 0; i < n; i++)
\r
785 for (int j = 0; j < n; j++)
\r
787 in[i][j] = (double) Math.random();
\r
791 Matrix origmat = new Matrix(in, n, n);
\r
793 // System.out.println(" --- Original matrix ---- ");
\r
794 /// origmat.print(System.out);
\r
795 //System.out.println();
\r
796 //System.out.println(" --- transpose matrix ---- ");
\r
797 Matrix trans = origmat.transpose();
\r
799 //trans.print(System.out);
\r
800 //System.out.println();
\r
801 //System.out.println(" --- OrigT * Orig ---- ");
\r
802 Matrix symm = trans.postMultiply(origmat);
\r
804 //symm.print(System.out);
\r
805 //System.out.println();
\r
806 // Copy the symmetric matrix for later
\r
807 //Matrix origsymm = symm.copy();
\r
809 // This produces the tridiagonal transformation matrix
\r
810 //long tstart = System.currentTimeMillis();
\r
813 //long tend = System.currentTimeMillis();
\r
815 //System.out.println("Time take for tred = " + (tend-tstart) + "ms");
\r
816 //System.out.println(" ---Tridiag transform matrix ---");
\r
817 //symm.print(System.out);
\r
818 //System.out.println();
\r
819 //System.out.println(" --- D vector ---");
\r
820 //symm.printD(System.out);
\r
821 //System.out.println();
\r
822 //System.out.println(" --- E vector ---");
\r
823 //symm.printE(System.out);
\r
824 //System.out.println();
\r
825 // Now produce the diagonalization matrix
\r
826 //tstart = System.currentTimeMillis();
\r
828 //tend = System.currentTimeMillis();
\r
830 //System.out.println("Time take for tqli = " + (tend-tstart) + " ms");
\r
831 //System.out.println(" --- New diagonalization matrix ---");
\r
832 //symm.print(System.out);
\r
833 //System.out.println();
\r
834 //System.out.println(" --- D vector ---");
\r
835 //symm.printD(System.out);
\r
836 //System.out.println();
\r
837 //System.out.println(" --- E vector ---");
\r
838 //symm.printE(System.out);
\r
839 //System.out.println();
\r
840 //System.out.println(" --- First eigenvector --- ");
\r
841 //double[] eigenv = symm.getColumn(0);
\r
842 //for (int i=0; i < eigenv.length;i++) {
\r
843 // Format.print(System.out,"%15.4f",eigenv[i]);
\r
845 //System.out.println();
\r
846 //double[] neigenv = origsymm.vectorPostMultiply(eigenv);
\r
847 //for (int i=0; i < neigenv.length;i++) {
\r
848 // Format.print(System.out,"%15.4f",neigenv[i]/symm.d[0]);
\r
850 //System.out.println();
\r