/** * <p> * Performs a matrix inversion operation on the specified matrix and stores the results * in the same matrix.<br> * <br> * a = a<sup>-1</sup> * </p> * * <p> * If the algorithm could not invert the matrix then false is returned. If it returns true * that just means the algorithm finished. The results could still be bad * because the matrix is singular or nearly singular. * </p> * * @param A The matrix that is to be inverted. Results are stored here. Modified. * @return true if it could invert the matrix false if it could not. */ public static bool invert(CMatrixRMaj A) { LinearSolverDense <CMatrixRMaj> solver = LinearSolverFactory_CDRM.lu(A.numRows); if (solver.setA(A)) { solver.invert(A); } else { return(false); } return(true); }
/** * <p> * Performs a matrix inversion operation that does not modify the original * and stores the results in another matrix. The two matrices must have the * same dimension.<br> * <br> * b = a<sup>-1</sup> * </p> * * <p> * If the algorithm could not invert the matrix then false is returned. If it returns true * that just means the algorithm finished. The results could still be bad * because the matrix is singular or nearly singular. * </p> * * <p> * For medium to large matrices there might be a slight performance boost to using * {@link LinearSolverFactory_CDRM} instead. * </p> * * @param input The matrix that is to be inverted. Not modified. * @param output Where the inverse matrix is stored. Modified. * @return true if it could invert the matrix false if it could not. */ public static bool invert(CMatrixRMaj input, CMatrixRMaj output) { LinearSolverDense <CMatrixRMaj> solver = LinearSolverFactory_CDRM.lu(input.numRows); if (solver.modifiesA()) { input = (CMatrixRMaj)input.copy(); } if (!solver.setA(input)) { return(false); } solver.invert(output); return(true); }
/** * <p> * Solves for x in the following equation:<br> * <br> * A*x = b * </p> * * <p> * If the system could not be solved then false is returned. If it returns true * that just means the algorithm finished operating, but the results could still be bad * because 'A' is singular or nearly singular. * </p> * * <p> * If repeat calls to solve are being made then one should consider using {@link LinearSolverFactory_CDRM} * instead. * </p> * * <p> * It is ok for 'b' and 'x' to be the same matrix. * </p> * * @param a A matrix that is m by n. Not modified. * @param b A matrix that is n by k. Not modified. * @param x A matrix that is m by k. Modified. * * @return true if it could invert the matrix false if it could not. */ public static bool solve(CMatrixRMaj a, CMatrixRMaj b, CMatrixRMaj x) { LinearSolverDense <CMatrixRMaj> solver; if (a.numCols == a.numRows) { solver = LinearSolverFactory_CDRM.lu(a.numRows); } else { solver = LinearSolverFactory_CDRM.qr(a.numRows, a.numCols); } // make sure the inputs 'a' and 'b' are not modified solver = new LinearSolverSafe <CMatrixRMaj>(solver); if (!solver.setA(a)) { return(false); } solver.solve(b, x); return(true); }