MCPcopy Create free account
hub / github.com/PerroEngine/Perro / determinant

Method determinant

perro_source/core/perro_structs/src/structs/matrix/mod.rs:1085–1128  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1083
1084 #[inline]
1085 pub fn determinant(self) -> f32 {
1086 if N == 2 {
1087 return Matrix2::from_rows(matrix_rows_2(self.0)).0.determinant();
1088 }
1089 if N == 3 {
1090 return Matrix3::from_rows(matrix_rows_3(self.0)).0.determinant();
1091 }
1092 if N == 4 {
1093 return Matrix4::from_rows(matrix_rows_4(self.0)).0.determinant();
1094 }
1095
1096 let mut rows = self.0;
1097 let mut det = 1.0;
1098 for pivot in 0..N {
1099 let mut best = pivot;
1100 let mut best_abs = rows[pivot][pivot].abs();
1101 for (r, row) in rows.iter().enumerate().skip(pivot + 1) {
1102 let abs = row[pivot].abs();
1103 if abs > best_abs {
1104 best = r;
1105 best_abs = abs;
1106 }
1107 }
1108 if best_abs <= f32::EPSILON {
1109 return 0.0;
1110 }
1111 if best != pivot {
1112 rows.swap(pivot, best);
1113 det = -det;
1114 }
1115
1116 let pivot_value = rows[pivot][pivot];
1117 det *= pivot_value;
1118 let pivot_row = rows[pivot];
1119 for row in rows.iter_mut().skip(pivot + 1) {
1120 let factor = row[pivot] / pivot_value;
1121 row[pivot] = 0.0;
1122 for (c, item) in row.iter_mut().enumerate().skip(pivot + 1) {
1123 *item -= factor * pivot_row[c];
1124 }
1125 }
1126 }
1127 det
1128 }
1129
1130 #[inline]
1131 pub fn inverse(self) -> Option<Self> {

Callers 3

inverseMethod · 0.80
prepareMethod · 0.80
build_instanceFunction · 0.80

Calls 6

matrix_rows_2Function · 0.85
matrix_rows_3Function · 0.85
matrix_rows_4Function · 0.85
absMethod · 0.45
iterMethod · 0.45
iter_mutMethod · 0.45

Tested by

no test coverage detected