initial commit

This commit is contained in:
2023-12-28 14:07:00 +08:00
commit 6edb6a95b3
16 changed files with 4401 additions and 0 deletions

87
src/hpivot.rs Normal file
View File

@@ -0,0 +1,87 @@
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,
}
}
}