-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdeterminant.rs
37 lines (33 loc) · 884 Bytes
/
determinant.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
pub fn calc_determinant<T>(mut matrix: Vec<Vec<T>>) -> T
where
T: Copy + std::ops::Sub<Output = T> + std::ops::Mul<Output = T> + std::ops::Div<Output = T>,
{
let n = matrix.len();
assert!(
matrix.iter().all(|row| row.len() == n),
"The matrix is not square!"
);
for i in 0..n {
for j in (i + 1)..n {
let b = matrix[j][i] / matrix[i][i];
for k in 0..n {
matrix[j][k] = matrix[j][k] - matrix[i][k] * b;
}
}
}
let mut det = matrix[0][0];
for i in 1..n {
det = det * matrix[i][i];
}
det
}
#[cfg(test)]
mod tests {
use crate::math::determinant::calc_determinant;
#[test]
fn test_calc_determinant() {
let a = vec![vec![1, 2, 3], vec![2, 2, 4], vec![2, 4, 5]];
let det = calc_determinant(a);
assert_eq!(det, 2);
}
}