1 package jalview.math;
\r
3 import jalview.util.*;
\r
7 public class Matrix {
\r
12 public double[][] value;
\r
15 public double[] d; // Diagonal
\r
16 public double[] e; // off diagonal
\r
18 public Matrix(double[][] value, int rows, int cols) {
\r
24 public Matrix transpose() {
\r
25 double[][] out = new double[cols][rows];
\r
27 for (int i = 0; i < cols; i++) {
\r
28 for (int j = 0; j < rows ; j++) {
\r
29 out[i][j] = value[j][i];
\r
32 return new Matrix(out,cols,rows);
\r
35 public void print(PrintStream ps) {
\r
37 for (int i = 0; i < rows; i++) {
\r
38 for (int j = 0; j < cols; j++) {
\r
39 Format.print(ps,"%8.2f",value[i][j]);
\r
46 public Matrix preMultiply(Matrix in) {
\r
47 double[][] tmp = new double[in.rows][this.cols];
\r
49 for (int i = 0; i < in.rows; i++) {
\r
50 for (int j = 0; j < this.cols; j++ ) {
\r
53 for (int k = 0; k < in.cols; k++) {
\r
54 tmp[i][j] += in.value[i][k]*this.value[k][j];
\r
60 return new Matrix(tmp,in.rows,this.cols);
\r
63 public double[] vectorPostMultiply(double[] in) {
\r
64 double[] out = new double[in.length];
\r
65 for (int i = 0; i < in.length; i++) {
\r
67 for (int k=0; k < in.length; k++) {
\r
68 out[i] += value[i][k] * in[k];
\r
73 public Matrix postMultiply(Matrix in) {
\r
75 double[][] out = new double[this.rows][in.cols];
\r
76 for (int i = 0; i < this.rows; i++) {
\r
77 for (int j = 0; j < in.cols; j++ ) {
\r
81 for (int k = 0; k < rows; k++) {
\r
82 out[i][j] = out[i][j] + value[i][k]*in.value[k][j];
\r
87 return new Matrix(out,this.cols,in.rows);
\r
90 public Matrix copy() {
\r
91 double[][] newmat = new double[rows][cols];
\r
93 for (int i = 0; i < rows; i++) {
\r
94 for (int j = 0; j < cols; j++) {
\r
95 newmat[i][j] = value[i][j];
\r
98 return new Matrix(newmat,rows,cols);
\r
101 public void tred() {
\r
114 this.d = new double[rows];
\r
115 this.e = new double[rows];
\r
117 for (i=n; i >= 2;i--) {
\r
123 for (k=1;k<=l;k++) {
\r
124 scale += Math.abs(value[i-1][k-1]);
\r
126 if (scale == 0.0) {
\r
127 e[i-1] = value[i-1][l-1];
\r
129 for (k=1; k <= l; k++) {
\r
130 value[i-1][k-1] /= scale;
\r
131 h += value[i-1][k-1]*value[i-1][k-1];
\r
133 f = value[i-1][l-1];
\r
135 g = -1.0*Math.sqrt(h);
\r
141 value[i-1][l-1] = f-g;
\r
143 for (j=1; j <= l; j++) {
\r
144 value[j-1][i-1] = value[i-1][j-1]/h;
\r
146 for (k= 1; k <= j; k++) {
\r
147 g += value[j-1][k-1]*value[i-1][k-1];
\r
149 for (k=j+1; k<=l;k++) {
\r
150 g+= value[k-1][j-1]*value[i-1][k-1];
\r
153 f+=e[j-1]*value[i-1][j-1];
\r
156 for (j=1;j<=l;j++) {
\r
160 for (k=1;k<=j;k++) {
\r
161 value[j-1][k-1] -= (f*e[k-1]+g*value[i-1][k-1]);
\r
166 e[i-1] = value[i-1][l-1];
\r
172 for (i=1;i<=n;i++) {
\r
174 if (d[i-1] != 0.0) {
\r
175 for (j=1;j<=l;j++) {
\r
177 for (k=1;k<=l;k++) {
\r
178 g+= value[i-1][k-1]*value[k-1][j-1];
\r
180 for (k=1;k<=l;k++) {
\r
181 value[k-1][j-1] -= g*value[k-1][i-1];
\r
185 d[i-1] = value[i-1][i-1];
\r
186 value[i-1][i-1] = 1.0;
\r
187 for (j=1;j<=l;j++) {
\r
188 value[j-1][i-1] = 0.0;
\r
189 value[i-1][j-1] = 0.0;
\r
194 public void tqli() {
\r
212 for (i=2;i<=n;i++) {
\r
216 for (l=1;l<=n;l++) {
\r
219 for (m=l;m<=(n-1);m++) {
\r
220 dd=Math.abs(d[m-1]) + Math.abs(d[m]);
\r
221 if (Math.abs(e[m-1]) + dd == dd)
\r
227 System.out.print("Too many iterations in tqli");
\r
230 // System.out.println("Iteration " + iter);
\r
232 g=(d[l]-d[l-1])/(2.0*e[l-1]);
\r
233 r = Math.sqrt((g*g) + 1.0);
\r
234 g=d[m-1]-d[l-1]+e[l-1]/(g + sign(r,g));
\r
238 for (i=m-1;i>=l;i--) {
\r
241 if (Math.abs(f) >= Math.abs(g)) {
\r
243 r = Math.sqrt((c*c)+1.0);
\r
249 r = Math.sqrt((s*s)+1.0);
\r
255 r=(d[i-1]-g)*s + 2.0*c*b;
\r
259 for (k=1; k <= n; k++) {
\r
261 value[k-1][i] = s*value[k-1][i-1] + c*f;
\r
262 value[k-1][i-1] = c*value[k-1][i-1] - s*f;
\r
265 d[l-1] = d[l-1] - p;
\r
272 public void tred2() {
\r
285 this.d = new double[rows];
\r
286 this.e = new double[rows];
\r
288 for (i=n-1; i >= 1;i--) {
\r
294 for (k=0;k<l;k++) {
\r
295 scale += Math.abs(value[i][k]);
\r
297 if (scale == 0.0) {
\r
298 e[i] = value[i][l];
\r
300 for (k=0; k < l; k++) {
\r
301 value[i][k] /= scale;
\r
302 h += value[i][k]*value[i][k];
\r
306 g = -1.0*Math.sqrt(h);
\r
314 for (j=0; j < l; j++) {
\r
315 value[j][i] = value[i][j]/h;
\r
317 for (k= 0; k < j; k++) {
\r
318 g += value[j][k]*value[i][k];
\r
320 for (k=j; k<l;k++) {
\r
321 g+= value[k][j]*value[i][k];
\r
324 f+=e[j]*value[i][j];
\r
327 for (j=0;j<l;j++) {
\r
331 for (k=0;k<j;k++) {
\r
332 value[j][k] -= (f*e[k]+g*value[i][k]);
\r
337 e[i] = value[i][l];
\r
343 for (i=0;i<n;i++) {
\r
346 for (j=0;j<l;j++) {
\r
348 for (k=0;k<l;k++) {
\r
349 g+= value[i][k]*value[k][j];
\r
351 for (k=0;k<l;k++) {
\r
352 value[k][j] -= g*value[k][i];
\r
356 d[i] = value[i][i];
\r
358 for (j=0;j<l;j++) {
\r
365 public void tqli2() {
\r
383 for (i=2;i<=n;i++) {
\r
387 for (l=1;l<=n;l++) {
\r
390 for (m=l;m<=(n-1);m++) {
\r
391 dd=Math.abs(d[m-1]) + Math.abs(d[m]);
\r
392 if (Math.abs(e[m-1]) + dd == dd)
\r
398 System.out.print("Too many iterations in tqli");
\r
401 // System.out.println("Iteration " + iter);
\r
403 g=(d[l]-d[l-1])/(2.0*e[l-1]);
\r
404 r = Math.sqrt((g*g) + 1.0);
\r
405 g=d[m-1]-d[l-1]+e[l-1]/(g + sign(r,g));
\r
409 for (i=m-1;i>=l;i--) {
\r
412 if (Math.abs(f) >= Math.abs(g)) {
\r
414 r = Math.sqrt((c*c)+1.0);
\r
420 r = Math.sqrt((s*s)+1.0);
\r
426 r=(d[i-1]-g)*s + 2.0*c*b;
\r
430 for (k=1; k <= n; k++) {
\r
432 value[k-1][i] = s*value[k-1][i-1] + c*f;
\r
433 value[k-1][i-1] = c*value[k-1][i-1] - s*f;
\r
436 d[l-1] = d[l-1] - p;
\r
444 public double sign(double a, double b) {
\r
446 return -Math.abs(a);
\r
448 return Math.abs(a);
\r
452 public double[] getColumn(int n) {
\r
453 double[] out = new double[rows];
\r
454 for (int i=0;i<rows;i++) {
\r
455 out[i] = value[i][n];
\r
461 public void printD(PrintStream ps) {
\r
463 for (int j = 0; j < rows;j++) {
\r
464 Format.print(ps,"%15.4e",d[j]);
\r
467 public void printE(PrintStream ps) {
\r
469 for (int j = 0; j < rows;j++) {
\r
470 Format.print(ps,"%15.4e",e[j]);
\r
474 public static void main(String[] args) {
\r
475 int n = Integer.parseInt(args[0]);
\r
476 double[][] in = new double[n][n];
\r
478 for (int i = 0;i < n;i++) {
\r
479 for (int j = 0; j < n; j++) {
\r
480 in[i][j] = (double)Math.random();
\r
484 Matrix origmat = new Matrix(in,n,n);
\r
485 // System.out.println(" --- Original matrix ---- ");
\r
486 /// origmat.print(System.out);
\r
487 //System.out.println();
\r
489 //System.out.println(" --- transpose matrix ---- ");
\r
490 Matrix trans = origmat.transpose();
\r
491 //trans.print(System.out);
\r
492 //System.out.println();
\r
494 //System.out.println(" --- OrigT * Orig ---- ");
\r
496 Matrix symm = trans.postMultiply(origmat);
\r
497 //symm.print(System.out);
\r
498 //System.out.println();
\r
500 // Copy the symmetric matrix for later
\r
501 Matrix origsymm = symm.copy();
\r
504 // This produces the tridiagonal transformation matrix
\r
505 long tstart = System.currentTimeMillis();
\r
507 long tend = System.currentTimeMillis();
\r
508 //System.out.println("Time take for tred = " + (tend-tstart) + "ms");
\r
509 //System.out.println(" ---Tridiag transform matrix ---");
\r
510 //symm.print(System.out);
\r
511 //System.out.println();
\r
513 //System.out.println(" --- D vector ---");
\r
514 //symm.printD(System.out);
\r
515 //System.out.println();
\r
516 //System.out.println(" --- E vector ---");
\r
517 //symm.printE(System.out);
\r
518 //System.out.println();
\r
521 // Now produce the diagonalization matrix
\r
522 tstart = System.currentTimeMillis();
\r
524 tend = System.currentTimeMillis();
\r
525 //System.out.println("Time take for tqli = " + (tend-tstart) + " ms");
\r
527 //System.out.println(" --- New diagonalization matrix ---");
\r
528 //symm.print(System.out);
\r
529 //System.out.println();
\r
531 //System.out.println(" --- D vector ---");
\r
532 //symm.printD(System.out);
\r
533 //System.out.println();
\r
534 //System.out.println(" --- E vector ---");
\r
535 //symm.printE(System.out);
\r
536 //System.out.println();
\r
538 //System.out.println(" --- First eigenvector --- ");
\r
539 //double[] eigenv = symm.getColumn(0);
\r
540 //for (int i=0; i < eigenv.length;i++) {
\r
541 // Format.print(System.out,"%15.4f",eigenv[i]);
\r
543 //System.out.println();
\r
545 //double[] neigenv = origsymm.vectorPostMultiply(eigenv);
\r
547 //for (int i=0; i < neigenv.length;i++) {
\r
548 // Format.print(System.out,"%15.4f",neigenv[i]/symm.d[0]);
\r
551 //System.out.println();
\r