init: first working impl of determinant

types: added initial matrix definition and impl blocks
This commit is contained in:
Zhongheng Liu 2025-01-22 12:32:22 +02:00
commit 8bb435ab10
Signed by: steven
GPG key ID: 805A28B071DAD84B
10 changed files with 279 additions and 0 deletions

9
src/main.rs Normal file
View file

@ -0,0 +1,9 @@
use types::matrix::Matrix;
mod types;
fn main() {
println!("Hello, world!");
let m = Matrix::from_str("1,2\n3,4".to_string());
let d = m.determinant();
println!("{}", d);
}

1
src/types.rs Normal file
View file

@ -0,0 +1 @@
pub mod matrix;

75
src/types/matrix.rs Normal file
View file

@ -0,0 +1,75 @@
use std::str::FromStr;
/// Matrix
pub struct Matrix {
pub nrows: usize,
pub ncols: usize,
pub data: Vec<Vec<i32>>,
}
impl Matrix {
pub fn new(data: Vec<Vec<i32>>) -> Matrix {
Matrix {
nrows: data[0].len(),
ncols: data.len(),
data,
}
}
pub fn from_str(s: String) -> Matrix {
let mut d: Vec<Vec<i32>> = Vec::new();
let rows_iter = s.split('\n');
for (_i, txt) in rows_iter.enumerate() {
let mut r: Vec<i32> = Vec::new();
for (_j, ch) in txt.split(',').enumerate() {
let parsed = match i32::from_str(ch) {
Ok(n) => n,
Err(e) => panic!("Err: {}", e),
};
r.push(parsed);
}
d.push(r);
}
Matrix::new(d)
}
pub fn is_square(&self) -> bool {
&self.nrows == &self.ncols
}
pub fn splice(&self, at_index: usize) -> Matrix {
let mut data: Vec<Vec<i32>> = Vec::new();
for i in 0..self.data.len() {
if i == at_index {continue;}
let mut r: Vec<i32> = Vec::new();
for j in 0..self.data[i].len() {
if j == at_index {continue;}
r.push(self.data[i][j]);
}
data.push(r);
}
Matrix::new(data)
}
pub fn determinant(&self) -> i32 {
if self.nrows == 2 && self.nrows == 2 {
return &self.data[0][0] * &self.data[0][1] - &self.data[1][0] * &self.data[1][1];
}
let mut tmp = 0;
for (i, n) in self.data[0].iter().enumerate() {
let mult = if i % 2 == 0 { -*n } else { *n };
let eval = self.splice(i).determinant();
tmp += mult * eval;
}
tmp
}
pub fn transpose(&self) -> Matrix {
let mut new_data = Vec::<Vec<i32>>::new();
for i in 0..self.nrows {
let mut new_row = Vec::<i32>::new();
for j in 0..self.ncols {
new_row.push(self.data[j][i]);
}
new_data.push(new_row);
}
Matrix {
nrows: self.ncols,
ncols: self.nrows,
data: new_data,
}
}
}