diff --git a/mlpp/lin_alg/mlpp_matrix.cpp b/mlpp/lin_alg/mlpp_matrix.cpp index ef3f158..b676fb4 100644 --- a/mlpp/lin_alg/mlpp_matrix.cpp +++ b/mlpp/lin_alg/mlpp_matrix.cpp @@ -1546,10 +1546,6 @@ real_t MLPPMatrix::detb(const Ref &A, int d) const { ERR_FAIL_COND_V(!A.is_valid(), 0); real_t deter = 0; - Ref B; - B.instance(); - B->resize(Size2i(d, d)); - B->fill(0); /* This is the base case in which the input is a 2x2 square matrix. Recursion is performed unless and until we reach this base case, @@ -1557,6 +1553,11 @@ real_t MLPPMatrix::detb(const Ref &A, int d) const { if (d == 2) { return A->element_get(0, 0) * A->element_get(1, 1) - A->element_get(0, 1) * A->element_get(1, 0); } else { + Ref B; + B.instance(); + B->resize(Size2i(d, d)); + B->fill(0); + for (int i = 0; i < d; i++) { int sub_i = 0; for (int j = 1; j < d; j++) { @@ -1667,9 +1668,13 @@ Ref MLPPMatrix::adjoint() const { Ref cof = cofactor(_size.y, i, j); // 1 if even, -1 if odd int sign = (i + j) % 2 == 0 ? 1 : -1; - adj->element_set(j, i, sign * cof->det(int(_size.y) - 1)); + + real_t d = cof->det(int(_size.y) - 1); + + adj->element_set(j, i, sign * d); } } + return adj; } void MLPPMatrix::adjointo(Ref out) const {