Example #1
0
 // Opposite of a matrix [static method]
 public static MatrixError opposite(MatrixError amat) {
   MatrixError opp = MatrixError.copy(amat);
   for (int i = 0; i < amat.nrow; i++) {
     for (int j = 0; j < amat.ncol; j++) {
       opp.matrix[i][j] = -amat.matrix[i][j];
     }
   }
   return opp;
 }
Example #2
0
 // OPPOSITE
 // Opposite of a matrix [instance method]
 public MatrixError opposite() {
   MatrixError opp = MatrixError.copy(this);
   for (int i = 0; i < this.nrow; i++) {
     for (int j = 0; j < this.ncol; j++) {
       opp.matrix[i][j] = -this.matrix[i][j];
     }
   }
   return opp;
 }
Example #3
0
  // LU DECOMPOSITION OF COMPLEX MATRIX A
  // For details of LU decomposition
  // See Numerical Recipes, The Art of Scientific Computing
  // by W H Press, S A Teukolsky, W T Vetterling & B P Flannery
  // Cambridge University Press,   http://www.nr.com/
  // Matrix ludmat is the returned LU decompostion
  // int[] index is the vector of row permutations
  // dswap returns +1.0 for even number of row interchanges
  //       returns -1.0 for odd number of row interchanges
  public MatrixError luDecomp() {
    if (this.nrow != this.ncol) throw new IllegalArgumentException("A matrix is not square");
    int n = this.nrow;
    int imax = 0;
    double dum = 0.0D, temp = 0.0D, big = 0.0D;
    double[] vv = new double[n];
    double sum = 0.0D;
    double dumm = 0.0D;

    this.matrixCheck = true;

    MatrixError ludmat = MatrixError.copy(this);
    double[][] ludarray = ludmat.getArrayReference();

    ludmat.dswap = 1.0D;
    for (int i = 0; i < n; i++) {
      big = 0.0D;
      for (int j = 0; j < n; j++) if ((temp = Math.abs(ludarray[i][j])) > big) big = temp;
      if (big == 0.0D) {
        System.out.println("Attempted LU Decomposition of a singular matrix in Matrix.luDecomp()");
        System.out.println("NaN matrix returned and matrixCheck set to false");
        this.matrixCheck = false;
        for (int k = 0; k < n; k++) for (int j = 0; j < n; j++) ludarray[k][j] = Double.NaN;
        return ludmat;
      }
      vv[i] = 1.0 / big;
    }
    for (int j = 0; j < n; j++) {
      for (int i = 0; i < j; i++) {
        sum = ludarray[i][j];
        for (int k = 0; k < i; k++) sum -= ludarray[i][k] * ludarray[k][j];
        ludarray[i][j] = sum;
      }
      big = 0.0D;
      for (int i = j; i < n; i++) {
        sum = ludarray[i][j];
        for (int k = 0; k < j; k++) sum -= ludarray[i][k] * ludarray[k][j];
        ludarray[i][j] = sum;
        if ((dum = vv[i] * Math.abs(sum)) >= big) {
          big = dum;
          imax = i;
        }
      }
      if (j != imax) {
        for (int k = 0; k < n; k++) {
          dumm = ludarray[imax][k];
          ludarray[imax][k] = ludarray[j][k];
          ludarray[j][k] = dumm;
        }
        ludmat.dswap = -ludmat.dswap;
        vv[imax] = vv[j];
      }
      ludmat.index[j] = imax;

      if (ludarray[j][j] == 0.0D) {
        ludarray[j][j] = TINY;
      }
      if (j != n - 1) {
        dumm = 1.0 / ludarray[j][j];
        for (int i = j + 1; i < n; i++) {
          ludarray[i][j] *= dumm;
        }
      }
    }
    return ludmat;
  }