// TRANSPOSE // Transpose of a complex matrix [instance method] public MatrixError transpose() { MatrixError tmat = new MatrixError(this.ncol, this.nrow); double[][] tarray = tmat.getArrayReference(); for (int i = 0; i < this.ncol; i++) { for (int j = 0; j < this.nrow; j++) { tarray[i][j] = this.matrix[j][i]; } } return tmat; }
// Transpose of a matrix [static method] public static MatrixError transpose(MatrixError amat) { MatrixError tmat = new MatrixError(amat.ncol, amat.nrow); double[][] tarray = tmat.getArrayReference(); for (int i = 0; i < amat.ncol; i++) { for (int j = 0; j < amat.nrow; j++) { tarray[i][j] = amat.matrix[j][i]; } } return tmat; }
// Construct a diagonal matrix public static MatrixError diagonalMatrix(int nrow, double[] diag) { if (diag.length != nrow) throw new IllegalArgumentException("matrix dimension differs from diagonal array length"); MatrixError u = new MatrixError(nrow, nrow); double[][] uarray = u.getArrayReference(); for (int i = 0; i < nrow; i++) { uarray[i][i] = diag[0]; } return u; }
// Multiply a matrix by a constant [static method] public static MatrixError times(MatrixError amat, double constant) { MatrixError cmat = new MatrixError(amat.nrow, amat.ncol); double[][] carray = cmat.getArrayReference(); for (int i = 0; i < amat.nrow; i++) { for (int j = 0; j < amat.ncol; j++) { carray[i][j] = amat.matrix[i][j] * constant; } } return cmat; }
// Multiply this matrix by a constant [instance method] // This matrix remains unaltered public MatrixError times(double constant) { MatrixError cmat = new MatrixError(this.nrow, this.ncol); double[][] carray = cmat.getArrayReference(); for (int i = 0; i < this.nrow; i++) { for (int j = 0; j < this.ncol; j++) { carray[i][j] = this.matrix[i][j] * constant; } } return cmat; }
// Construct a complex scalar matrix public static MatrixError scalarMatrix(int nrow, double diagconst) { MatrixError u = new MatrixError(nrow, nrow); double[][] uarray = u.getArrayReference(); for (int i = 0; i < nrow; i++) { for (int j = i; j < nrow; j++) { if (i == j) { uarray[i][j] = diagconst; } } } return u; }
// Subtract matrix B from matrix A [static method] public static MatrixError minus(MatrixError amat, MatrixError bmat) { if ((amat.nrow != bmat.nrow) || (amat.ncol != bmat.ncol)) { throw new IllegalArgumentException("Array dimensions do not agree"); } int nr = amat.nrow; int nc = amat.ncol; MatrixError cmat = new MatrixError(nr, nc); double[][] carray = cmat.getArrayReference(); for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) { carray[i][j] = amat.matrix[i][j] - bmat.matrix[i][j]; } } return cmat; }
// 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; }
// 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; }
// Inverse of a square matrix [static method] public static MatrixError inverse(MatrixError amat) { int n = amat.nrow; if (n != amat.ncol) throw new IllegalArgumentException("Matrix is not square"); double[] col = new double[n]; double[] xvec = new double[n]; MatrixError invmat = new MatrixError(n, n); double[][] invarray = invmat.getArrayReference(); MatrixError ludmat; ludmat = amat.luDecomp(); for (int j = 0; j < n; j++) { for (int i = 0; i < n; i++) col[i] = 0.0D; col[j] = 1.0; xvec = ludmat.luBackSub(col); for (int i = 0; i < n; i++) invarray[i][j] = xvec[i]; } return invmat; }
// Multiply two complex matrices {static method] public static MatrixError times(MatrixError amat, MatrixError bmat) { if (amat.ncol != bmat.nrow) throw new IllegalArgumentException("Nonconformable matrices"); MatrixError cmat = new MatrixError(amat.nrow, bmat.ncol); double[][] carray = cmat.getArrayReference(); double sum = 0.0D; for (int i = 0; i < amat.nrow; i++) { for (int j = 0; j < bmat.ncol; j++) { sum = 0.0D; for (int k = 0; k < amat.ncol; k++) { sum += (amat.matrix[i][k] * bmat.matrix[k][j]); } carray[i][j] = sum; } } return cmat; }
// Clone a Matrix public Object clone() { if (this == null) { return null; } else { int nr = this.nrow; int nc = this.ncol; MatrixError b = new MatrixError(nr, nc); double[][] barray = b.getArrayReference(); b.nrow = nr; b.ncol = nc; for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) { barray[i][j] = this.matrix[i][j]; } } for (int i = 0; i < nr; i++) b.index[i] = this.index[i]; return (Object) b; } }
// Returns the determinant of a square matrix [static method] public static double determinant(MatrixError amat) { int n = amat.nrow; if (n == amat.ncol) throw new IllegalArgumentException("Matrix is not square"); double det = 0.0D; MatrixError ludmat = amat.luDecomp(); det = ludmat.dswap; for (int j = 0; j < n; j++) { det *= (ludmat.matrix[j][j]); } return det; }
// COPY // Copy a Matrix [static method] public static MatrixError copy(MatrixError a) { if (a == null) { return null; } else { int nr = a.getNrow(); int nc = a.getNcol(); double[][] aarray = a.getArrayReference(); MatrixError b = new MatrixError(nr, nc); b.nrow = nr; b.ncol = nc; double[][] barray = b.getArrayReference(); for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) { barray[i][j] = aarray[i][j]; } } for (int i = 0; i < nr; i++) b.index[i] = a.index[i]; return b; } }
// Solves the set of n linear equations A.X=B // bvec is the vector B (input) // xvec is the vector X (output) public double[] solveLinearSet(double[] bvec) { MatrixError ludmat = this.luDecomp(); return ludmat.luBackSub(bvec); }
// 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; }