addtion: matrix solution by gauss jordan
This commit is contained in:
32
src/lib.rs
32
src/lib.rs
@@ -377,9 +377,12 @@ impl Matrix {
|
|||||||
data: self.data.clone(),
|
data: self.data.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Inserts indentity matrix
|
||||||
for i in 0..inverse.rows {
|
for i in 0..inverse.rows {
|
||||||
|
// create full of 0 column
|
||||||
let mut new_column: Vec<Fraction> = vec![Fraction::from(0i64); inverse.rows];
|
let mut new_column: Vec<Fraction> = vec![Fraction::from(0i64); inverse.rows];
|
||||||
|
|
||||||
|
// at i set to 1
|
||||||
new_column[i] = Fraction::from(1i64);
|
new_column[i] = Fraction::from(1i64);
|
||||||
|
|
||||||
inverse.insert_column(inverse.columns - 1, new_column)?;
|
inverse.insert_column(inverse.columns - 1, new_column)?;
|
||||||
@@ -393,6 +396,35 @@ impl Matrix {
|
|||||||
|
|
||||||
Ok(inverse)
|
Ok(inverse)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn solve_matrix_with_gauss_jordan(
|
||||||
|
&self,
|
||||||
|
solution_matrix: Vec<Fraction>,
|
||||||
|
) -> Result<Vec<Fraction>, MatrixError> {
|
||||||
|
if self.columns != self.rows {
|
||||||
|
return Err(MatrixError::NotSquared);
|
||||||
|
}
|
||||||
|
|
||||||
|
if solution_matrix.len() != self.columns {
|
||||||
|
return Err(MatrixError::InvalidDataSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut solved_matrix = Matrix {
|
||||||
|
rows: self.rows,
|
||||||
|
columns: self.columns,
|
||||||
|
data: self.data.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
|
solved_matrix.insert_column(self.columns, solution_matrix)?;
|
||||||
|
let solved_matrix = solved_matrix.gauss_jordan_elimination()?;
|
||||||
|
|
||||||
|
let result = solved_matrix
|
||||||
|
.get_column(solved_matrix.columns)?
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Add for Matrix {
|
impl Add for Matrix {
|
||||||
|
|||||||
Reference in New Issue
Block a user