Optimized MLPPMatrix::det a little.

This commit is contained in:
Relintai 2023-12-27 16:10:45 +01:00
parent 1594cc9c0b
commit dd179e10d6

View File

@ -1546,10 +1546,6 @@ real_t MLPPMatrix::detb(const Ref<MLPPMatrix> &A, int d) const {
ERR_FAIL_COND_V(!A.is_valid(), 0); ERR_FAIL_COND_V(!A.is_valid(), 0);
real_t deter = 0; real_t deter = 0;
Ref<MLPPMatrix> 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. /* 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, Recursion is performed unless and until we reach this base case,
@ -1557,6 +1553,11 @@ real_t MLPPMatrix::detb(const Ref<MLPPMatrix> &A, int d) const {
if (d == 2) { if (d == 2) {
return A->element_get(0, 0) * A->element_get(1, 1) - A->element_get(0, 1) * A->element_get(1, 0); return A->element_get(0, 0) * A->element_get(1, 1) - A->element_get(0, 1) * A->element_get(1, 0);
} else { } else {
Ref<MLPPMatrix> B;
B.instance();
B->resize(Size2i(d, d));
B->fill(0);
for (int i = 0; i < d; i++) { for (int i = 0; i < d; i++) {
int sub_i = 0; int sub_i = 0;
for (int j = 1; j < d; j++) { for (int j = 1; j < d; j++) {
@ -1667,9 +1668,13 @@ Ref<MLPPMatrix> MLPPMatrix::adjoint() const {
Ref<MLPPMatrix> cof = cofactor(_size.y, i, j); Ref<MLPPMatrix> cof = cofactor(_size.y, i, j);
// 1 if even, -1 if odd // 1 if even, -1 if odd
int sign = (i + j) % 2 == 0 ? 1 : -1; 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; return adj;
} }
void MLPPMatrix::adjointo(Ref<MLPPMatrix> out) const { void MLPPMatrix::adjointo(Ref<MLPPMatrix> out) const {