diff options
author | Zhongheng Liu <z.liu@outlook.com.gr> | 2025-01-22 12:58:56 +0200 |
---|---|---|
committer | Zhongheng Liu <z.liu@outlook.com.gr> | 2025-01-22 12:58:56 +0200 |
commit | 6357661920a7be21552efdeb15d5f128ce7a5647 (patch) | |
tree | 70fd868457abd952275b39f19ceef0ba8905ad5c | |
parent | 9a3a05ce557f868f4d8db2938d5a1450404ca92b (diff) | |
download | matrix-rs-6357661920a7be21552efdeb15d5f128ce7a5647.tar.gz matrix-rs-6357661920a7be21552efdeb15d5f128ce7a5647.tar.bz2 matrix-rs-6357661920a7be21552efdeb15d5f128ce7a5647.zip |
fix: fixed incorrect determinant sum
-rw-r--r-- | src/types/matrix.rs | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/types/matrix.rs b/src/types/matrix.rs index 371d5df..09277b3 100644 --- a/src/types/matrix.rs +++ b/src/types/matrix.rs @@ -57,25 +57,34 @@ impl Matrix { pub fn splice(&self, at_index: usize) -> Matrix { let mut data: Vec<Vec<i32>> = Vec::new(); for i in 0..self.data.len() { - if i == at_index {continue;} - let mut r: Vec<i32> = Vec::new(); + if i == 0 { + continue; + } + let mut r: Vec<i32> = Vec::new(); for j in 0..self.data[i].len() { - if j == at_index {continue;} + if j == at_index { + continue; + } r.push(self.data[i][j]); } data.push(r); } - Matrix::new(data) + let m = Matrix::new(data); + // println!("Splice at {}: {}", at_index, m); + m } pub fn determinant(&self) -> i32 { + if !self.is_square() { panic!() }; if self.nrows == 2 && self.nrows == 2 { - return &self.data[0][0] * &self.data[0][1] - &self.data[1][0] * &self.data[1][1]; + return &self.data[0][0] * &self.data[1][1] - &self.data[0][1] * &self.data[1][0]; } let mut tmp = 0; for (i, n) in self.data[0].iter().enumerate() { let mult = if i % 2 == 0 { -*n } else { *n }; let eval = self.splice(i).determinant(); + // println!("tmp result: {}", mult * eval); tmp += mult * eval; + // println!("tmp: {}", tmp); } tmp } |