// 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; }
// 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); }