void solve(size_t n, real **A, real *x, real *b) { size_t *P; /* Construct LUP decomposition */ P = safeMalloc(n*sizeof(size_t)); decomposeLUP(n, A, P); /* Solve by forward and backward substitution */ LUPsolve(n, A, P, x, b); free(P); }
// based on LUP algorithm from Cormen et al "Introduction to Algorithms" Matrix4 Matrix4::inverse() const { double lu[4][4]; int perm[4]; decomposeLUP(lu, perm); double result[4][4]; for (int i = 0; i < 4; i++) { double x[4]; double b[4] = {0}; b[i] = 1.0; solveLUP(x, lu, perm, b); for (int j=0; j < 4; j++) { result[j][i] = x[j]; } } return Matrix4(result); }