Added determinant for matrices of any size
This commit is contained in:
@@ -57,12 +57,26 @@ class BoolSquareMatrix {
|
||||
return (_values >> index(row, col)) % 2;
|
||||
}
|
||||
STORAGE get_determinant() {
|
||||
if ( SIZE == 2 ) {
|
||||
if constexpr ( SIZE == 2 ) {
|
||||
return at(0,0) * at(1,1) - at(0,1) * at(1,0);
|
||||
} else if ( SIZE == 3 ) {
|
||||
return at(0,0) * at(1,1) * at(2,2) + at(0,1) * at(1,2) * at(2,0)
|
||||
+ at(0,2) * at(1,0) * at(2,1) - at(0,2) * at(1,1) * at(2,0)
|
||||
- at(0,1) * at(1,0) * at(2,2) - at(0,0) * at(1,2) * at(2,1);
|
||||
} else if constexpr ( SIZE == 4 or SIZE == 3 ) {
|
||||
int cur_modifier = 1;
|
||||
STORAGE res = 0;
|
||||
for (STORAGE remove_ind = 0; remove_ind < SIZE; ++remove_ind) {
|
||||
STORAGE tmp_vector_value = 0;
|
||||
for (STORAGE i = 1; i < SIZE; ++i) {
|
||||
for (STORAGE j = 0; j < SIZE; ++j) {
|
||||
if ( j == remove_ind )
|
||||
continue;
|
||||
tmp_vector_value *= 2;
|
||||
tmp_vector_value += at(i, j);
|
||||
}
|
||||
}
|
||||
BoolSquareMatrix<STORAGE, SIZE - 1> tmp_matix(tmp_vector_value);
|
||||
res += cur_modifier * tmp_matix.get_determinant() * at(0, remove_ind);
|
||||
cur_modifier *= -1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user