use std::ops::Index; use std::ops::IndexMut; use std::ops::Mul; use super::hmatrix::HMatrix; use super::matrix::MatrixImpl; use super::Matrix; use super::Pivot; pub struct HPivot { data: Vec, permutations: usize, } impl Index for HPivot { fn index(&self, index: usize) -> &Self::Output { &self.data[index] } type Output = usize; } impl IndexMut for HPivot { fn index_mut(&mut self, index: usize) -> &mut >::Output { &mut self.data[index] } } impl Pivot for HPivot { fn swap(&mut self, i1: usize, i2: usize) { self.data.swap(i1, i2); self.permutations += 1; } fn permutations(&self) -> usize { self.permutations } fn new(size: usize) -> Self { HPivot { data: (0..size).collect(), permutations: 0, } } } impl Mul> for HPivot where TYPE: Clone, { fn mul(self, matrix: HMatrix) -> Self::Output { &self * matrix } type Output = HMatrix; } impl Mul> for &HPivot where TYPE: Clone, { fn mul(self, matrix: HMatrix) -> Self::Output { if self.data.len() != matrix.rows() { panic!("Pivot len must equal matrix rows") } let mut result = matrix.clone(); let mut pclone = self.clone(); for i in 0..matrix.rows() { while i != pclone[i] { let j = pclone[i]; result.swap_rows_pivot(&mut pclone, i, j); } } result } type Output = HMatrix; } impl Clone for HPivot { fn clone(&self) -> Self { HPivot { data: self.data.clone(), permutations: self.permutations, } } }