Пример #1
0
  public static void main(String[] args) {
    int n = Integer.parseInt(args[0]);
    double[][] in = new double[n][n];

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        in[i][j] = (double) Math.random();
      }
    }

    Matrix origmat = new Matrix(in, n, n);
    //    System.out.println(" --- Original matrix ---- ");
    ///    origmat.print(System.out);
    // System.out.println();

    // System.out.println(" --- transpose matrix ---- ");
    Matrix trans = origmat.transpose();
    // trans.print(System.out);
    // System.out.println();

    // System.out.println(" --- OrigT * Orig ---- ");

    Matrix symm = trans.postMultiply(origmat);
    // symm.print(System.out);
    // System.out.println();

    // Copy the symmetric matrix for later
    Matrix origsymm = symm.copy();

    // This produces the tridiagonal transformation matrix
    long tstart = System.currentTimeMillis();
    symm.tred();
    long tend = System.currentTimeMillis();
    // System.out.println("Time take for tred = " + (tend-tstart) + "ms");
    // System.out.println(" ---Tridiag transform matrix ---");
    // symm.print(System.out);
    // System.out.println();

    // System.out.println(" --- D vector ---");
    // symm.printD(System.out);
    // System.out.println();
    // System.out.println(" --- E vector ---");
    // symm.printE(System.out);
    // System.out.println();

    // Now produce the diagonalization matrix
    tstart = System.currentTimeMillis();
    symm.tqli();
    tend = System.currentTimeMillis();
    // System.out.println("Time take for tqli = " + (tend-tstart) + " ms");

    // System.out.println(" --- New diagonalization matrix ---");
    // symm.print(System.out);
    // System.out.println();

    // System.out.println(" --- D vector ---");
    // symm.printD(System.out);
    // System.out.println();
    // System.out.println(" --- E vector ---");
    // symm.printE(System.out);
    // System.out.println();

    // System.out.println(" --- First eigenvector --- ");
    // double[] eigenv = symm.getColumn(0);
    // for (int i=0; i < eigenv.length;i++) {
    //  Format.print(System.out,"%15.4f",eigenv[i]);
    // }
    // System.out.println();

    // double[] neigenv = origsymm.vectorPostMultiply(eigenv);

    // for (int i=0; i < neigenv.length;i++) {
    //  Format.print(System.out,"%15.4f",neigenv[i]/symm.d[0]);
    // }

    // System.out.println();
  }
Пример #2
0
  public void tqli2() {
    int n = rows;

    int m;
    int l;
    int iter;
    int i;
    int k;
    double s;
    double r;
    double p;
    ;
    double g;
    double f;
    double dd;
    double c;
    double b;

    for (i = 2; i <= n; i++) {
      e[i - 2] = e[i - 1];
    }
    e[n - 1] = 0.0;
    for (l = 1; l <= n; l++) {
      iter = 0;
      do {
        for (m = l; m <= (n - 1); m++) {
          dd = Math.abs(d[m - 1]) + Math.abs(d[m]);
          if (Math.abs(e[m - 1]) + dd == dd) break;
        }
        if (m != l) {
          iter++;
          if (iter == 30) {
            System.out.print("Too many iterations in tqli");
            // Just dont want this to exit apollo
            if (jalview.gui.AlignFrame.exitOnClose()) System.exit(0);
          } else {
            //	    System.out.println("Iteration " + iter);
          }
          g = (d[l] - d[l - 1]) / (2.0 * e[l - 1]);
          r = Math.sqrt((g * g) + 1.0);
          g = d[m - 1] - d[l - 1] + e[l - 1] / (g + sign(r, g));
          c = 1.0;
          s = c;
          p = 0.0;
          for (i = m - 1; i >= l; i--) {
            f = s * e[i - 1];
            b = c * e[i - 1];
            if (Math.abs(f) >= Math.abs(g)) {
              c = g / f;
              r = Math.sqrt((c * c) + 1.0);
              e[i] = f * r;
              s = 1.0 / r;
              c *= s;
            } else {
              s = f / g;
              r = Math.sqrt((s * s) + 1.0);
              e[i] = g * r;
              c = 1.0 / r;
              s *= c;
            }
            g = d[i] - p;
            r = (d[i - 1] - g) * s + 2.0 * c * b;
            p = s * r;
            d[i] = g + p;
            g = c * r - b;
            for (k = 1; k <= n; k++) {
              f = value[k - 1][i];
              value[k - 1][i] = s * value[k - 1][i - 1] + c * f;
              value[k - 1][i - 1] = c * value[k - 1][i - 1] - s * f;
            }
          }
          d[l - 1] = d[l - 1] - p;
          e[l - 1] = g;
          e[m - 1] = 0.0;
        }
      } while (m != l);
    }
  }