refactor: gaussian elimination own funtion
This commit is contained in:
18
src/lib.rs
18
src/lib.rs
@@ -148,11 +148,7 @@ impl Matrix {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_determinant(&self) -> Result<Fraction, MatrixError> {
|
pub fn gaussian_elimination(&self) -> Result<(Matrix, Fraction), MatrixError> {
|
||||||
if self.rows != self.columns {
|
|
||||||
return Err(MatrixError::NotSquared);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut trig_matrix = Matrix {
|
let mut trig_matrix = Matrix {
|
||||||
columns: self.columns,
|
columns: self.columns,
|
||||||
rows: self.rows,
|
rows: self.rows,
|
||||||
@@ -177,7 +173,7 @@ impl Matrix {
|
|||||||
// If there ain't no other thing but 0 then we're
|
// If there ain't no other thing but 0 then we're
|
||||||
// fucked, determinant is zero
|
// fucked, determinant is zero
|
||||||
if max_value.is_zero() {
|
if max_value.is_zero() {
|
||||||
return Ok(Fraction::new(0, 1).unwrap());
|
return Ok((trig_matrix, Fraction::new(0, 1).unwrap()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if max_row != i {
|
if max_row != i {
|
||||||
@@ -204,6 +200,16 @@ impl Matrix {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok((trig_matrix, sign))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_determinant(&self) -> Result<Fraction, MatrixError> {
|
||||||
|
if self.rows != self.columns {
|
||||||
|
return Err(MatrixError::NotSquared);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (trig_matrix, sign) = self.gaussian_elimination()?;
|
||||||
|
|
||||||
// YES, now we got ourselves a triangular matrix, now we just
|
// YES, now we got ourselves a triangular matrix, now we just
|
||||||
// take the product of the diagonal and multiply by sign, that's
|
// take the product of the diagonal and multiply by sign, that's
|
||||||
// the determinant :)
|
// the determinant :)
|
||||||
|
|||||||
Reference in New Issue
Block a user