88 lines
1.8 KiB
Rust
88 lines
1.8 KiB
Rust
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<usize>,
|
|
permutations: usize,
|
|
}
|
|
|
|
impl Index<usize> for HPivot {
|
|
fn index(&self, index: usize) -> &Self::Output {
|
|
&self.data[index]
|
|
}
|
|
|
|
type Output = usize;
|
|
}
|
|
|
|
impl IndexMut<usize> for HPivot {
|
|
fn index_mut(&mut self, index: usize) -> &mut <Self as Index<usize>>::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<TYPE> Mul<HMatrix<TYPE>> for HPivot
|
|
where
|
|
TYPE: Clone,
|
|
{
|
|
fn mul(self, matrix: HMatrix<TYPE>) -> Self::Output {
|
|
&self * matrix
|
|
}
|
|
|
|
type Output = HMatrix<TYPE>;
|
|
}
|
|
|
|
impl<TYPE> Mul<HMatrix<TYPE>> for &HPivot
|
|
where
|
|
TYPE: Clone,
|
|
{
|
|
fn mul(self, matrix: HMatrix<TYPE>) -> 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<TYPE>;
|
|
}
|
|
|
|
impl Clone for HPivot {
|
|
fn clone(&self) -> Self {
|
|
HPivot {
|
|
data: self.data.clone(),
|
|
permutations: self.permutations,
|
|
}
|
|
}
|
|
}
|