Listing 11-0 shows class InvertibleMatrix in package numbercruncher.matrix . This is a subclass of LinearSystem , and we add methods to compute the inverse of the matrix, its determinant, its norm, and its condition number. Listing 11-0 Class InvertibleMatrix . package numbercruncher.matrix; import numbercruncher.mathutils.Epsilon; /** * A matrix that can be inverted. Also, compute its determinant, * norm, and condition number. */ public class InvertibleMatrix extends LinearSystem { /** * Constructor. * @param n the number of rows = the number of columns */ public InvertibleMatrix(int n) { super(n); } /** * Constructor. * @param values the array of values */ public InvertibleMatrix(float values[][]) { super(values); } /** * Compute the inverse of this matrix. * @return the inverse matrix * @throws matrix.MatrixException if an error occurred */ public InvertibleMatrix inverse() throws MatrixException { InvertibleMatrix inverse = new InvertibleMatrix(nRows); IdentityMatrix identity = new IdentityMatrix(nRows); // Compute each column of the inverse matrix // using columns of the identity matrix. for (int c = 0; c < nCols; ++c) { ColumnVector col = solve(identity.getColumn(c), true); inverse.setColumn(col, c); } return inverse; } /** * Compute the determinant. * @return the determinant * @throws matrix.MatrixException if an error occurred */ public float determinant() throws MatrixException { decompose(); // Each row exchange during forward elimination flips the sign // of the determinant, so check for an odd number of exchanges. float determinant = ((exchangeCount & 1) == 0) ? 1 : -1; // Form the product of the diagonal elements of matrix U. for (int i = 0; i < nRows; ++i) { int pi = permutation[i]; // permuted index determinant *= LU.at(pi, i); } return determinant; } /** * Compute the Euclidean norm of this matrix. * @return the norm */ public float norm() { float sum = 0; for (int r = 0; r < nRows; ++r) { for (int c = 0; c < nCols; ++c) { float v = values[r][c]; sum += v*v; } } return (float) Math.sqrt(sum); } /** * Compute the condition number based on the Euclidean norm. * @return the condition number */ public float condition() throws MatrixException { return norm() * inverse().norm(); } } Using the algorithm described in Section 11.2 method inverse() computes the columns of the matrix inverse. It repeatedly solves the system with the corresponding columns of the appropriate identity matrix as the right-hand side. Method determinant() forms the product of the diagonal elements of LU. It obtains diagonal elements via the permutation vector. If the number of row exchanges during decomposition was odd, it negates the product. Method norm() computes and returns the Euclidean norm of the matrix, and matrix condition() computes and returns its condition number. |