/** * Returns the determinant of the matrix. If the inverse of the matrix is also * needed, then using {@link org.ejml.interfaces.decomposition.LUDecomposition_F64} directly (or any * similar algorithm) can be more efficient. * * @param A The matrix whose determinant is to be computed. Not modified. * @return The determinant. */ public static double det(DMatrixSparseCSC A) { LUSparseDecomposition_F64 <DMatrixSparseCSC> alg = DecompositionFactory_DSCC.lu(FillReducing.NONE); if (alg.inputModified()) { A = (DMatrixSparseCSC)A.copy(); } if (!alg.decompose(A)) { return(0.0); } return(alg.computeDeterminant().real); }
public static void main(string[] args) { // create a random matrix that can be solved int N = 5; IMersenneTwister rand = new MersenneTwisterFast(234); DMatrixSparseCSC A = RandomMatrices_DSCC.rectangle(N, N, N * N / 4, rand); RandomMatrices_DSCC.ensureNotSingular(A, rand); // Create the LU decomposition LUSparseDecomposition_F64 <DMatrixSparseCSC> decompose = DecompositionFactory_DSCC.lu(FillReducing.NONE); // Decompose the matrix. // If you care about the A matrix being modified call decompose.inputModified() if (!decompose.decompose(A)) { throw new InvalidOperationException("The matrix is singular"); } // Extract new copies of the L and U matrices DMatrixSparseCSC L = decompose.getLower(null); DMatrixSparseCSC U = decompose.getUpper(null); DMatrixSparseCSC P = decompose.getRowPivot(null); // Storage for an intermediate step DMatrixSparseCSC tmp = (DMatrixSparseCSC)A.createLike(); // Storage for the inverse matrix DMatrixSparseCSC Ainv = (DMatrixSparseCSC)A.createLike(); // Solve for the inverse: P*I = L*U*inv(A) TriangularSolver_DSCC.solve(L, true, P, tmp, null, null, null); TriangularSolver_DSCC.solve(U, false, tmp, Ainv, null, null, null); // Make sure the inverse has been found. A*inv(A) = identity should be an identity matrix DMatrixSparseCSC found = (DMatrixSparseCSC)A.createLike(); CommonOps_DSCC.mult(A, Ainv, found); found.print(); }