diff --git a/src/lib.rs b/src/lib.rs index 0f6b59f..9f20d13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -377,9 +377,12 @@ impl Matrix { data: self.data.clone(), }; + // Inserts indentity matrix for i in 0..inverse.rows { + // create full of 0 column let mut new_column: Vec = vec![Fraction::from(0i64); inverse.rows]; + // at i set to 1 new_column[i] = Fraction::from(1i64); inverse.insert_column(inverse.columns - 1, new_column)?; @@ -393,6 +396,35 @@ impl Matrix { Ok(inverse) } + + pub fn solve_matrix_with_gauss_jordan( + &self, + solution_matrix: Vec, + ) -> Result, 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 {