/** * <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(ZMatrixRMaj A) { LinearSolverDense <ZMatrixRMaj> solver = LinearSolverFactory_ZDRM.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_ZDRM} 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(ZMatrixRMaj input, ZMatrixRMaj output) { LinearSolverDense <ZMatrixRMaj> solver = LinearSolverFactory_ZDRM.lu(input.numRows); if (solver.modifiesA()) { input = (ZMatrixRMaj)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_ZDRM} * 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(ZMatrixRMaj a, ZMatrixRMaj b, ZMatrixRMaj x) { LinearSolverDense <ZMatrixRMaj> solver; if (a.numCols == a.numRows) { solver = LinearSolverFactory_ZDRM.lu(a.numRows); } else { solver = LinearSolverFactory_ZDRM.qr(a.numRows, a.numCols); } // make sure the inputs 'a' and 'b' are not modified solver = new LinearSolverSafe <ZMatrixRMaj>(solver); if (!solver.setA(a)) { return(false); } solver.solve(b, x); return(true); }