From b8ad3e5b66db3a2cfff03a312d95804e389e0bdc Mon Sep 17 00:00:00 2001 From: Phani Pavan K Date: Mon, 19 Aug 2024 15:40:09 +0530 Subject: [PATCH] add mat ops --- rust/day1/src/main.rs | 24 ++++++------- rust/day1/src/matOps.rs | 28 +++++++++++++++ rust/day1/src/planarEigan.rs | 9 +++++ rust/day1/src/vecOps.rs | 27 +++++++++++++++ rust/numbrs/Cargo.toml | 6 +++- rust/numbrs/src/main.rs | 15 ++++++-- rust/numbrs/src/matOps.rs | 67 ++++++++++++++++++++++++++++++++++++ rust/numbrs/src/vecOps.rs | 4 +++ 8 files changed, 165 insertions(+), 15 deletions(-) create mode 100644 rust/day1/src/matOps.rs create mode 100644 rust/day1/src/planarEigan.rs create mode 100644 rust/day1/src/vecOps.rs create mode 100644 rust/numbrs/src/matOps.rs diff --git a/rust/day1/src/main.rs b/rust/day1/src/main.rs index 6e88184..e5095ff 100644 --- a/rust/day1/src/main.rs +++ b/rust/day1/src/main.rs @@ -1,14 +1,14 @@ -fn main() { - println!("{:?}", calEVlas(-5.0, -7.0, 2.0, 4.0)); -} +mod matOps; +mod planarEigan; +mod vecOps; +use crate::planarEigan::getEVals; +// mod planeEigan; -fn calEVlas(a: f32, b: f32, c: f32, d: f32) -> Vec { - let trace = a + d; - let determinant = (a * d) - (b * c); - println!("{determinant}"); - let discri = (trace.powi(2)) - (4.0 * determinant); - println!("{discri}"); - let x1 = (-trace + (discri.powf(0.5))) / 2.0; - let x2 = (-trace - (discri.powf(0.5))) / 2.0; - return vec![x1, x2]; +fn main() { + // println!("Hello, world!"); + let oneVec = vec![-5., -7., 2., 4.]; + // println!("{:?}", getEVals(-5., -7., 2., 4.)); + println!("{:?}", vecOps::mulScalarVector(3.0, &oneVec)); + println!("{:?}", vecOps::subVectors(&oneVec, &oneVec)); + println!("{:?}", vecOps::addVectors(&oneVec, &oneVec)); } diff --git a/rust/day1/src/matOps.rs b/rust/day1/src/matOps.rs new file mode 100644 index 0000000..2591478 --- /dev/null +++ b/rust/day1/src/matOps.rs @@ -0,0 +1,28 @@ +pub struct Matrix { + mat: Vec>, + rows: usize, + cols: usize, +} + +pub fn newZeroMatrix(m: usize, n: usize) -> Matrix { + let mut row = Vec::new(); + row.push(0.0); + row = row.repeat(n); + let mut cols = Vec::new(); + cols.push(row.clone()); + Matrix { + mat: cols, + rows: m, + cols: n, + } +} + +pub fn transpose(m: &Matrix) -> Matrix { + let mut newMat = newZeroMatrix(m.rows, m.cols); + for i in 0..m.rows { + for j in 0..m.cols { + newMat.mat[i][j] = m.mat[j][i]; + } + } + newMat +} diff --git a/rust/day1/src/planarEigan.rs b/rust/day1/src/planarEigan.rs new file mode 100644 index 0000000..4bf3c39 --- /dev/null +++ b/rust/day1/src/planarEigan.rs @@ -0,0 +1,9 @@ +pub mod planeEigan {} +pub fn getEVals(a: f32, b: f32, c: f32, d: f32) -> (f32, f32) { + let trace = a + d; + let determinant = a * d - b * c; + let descriminant = trace.powi(2) - (4. * determinant); + let x1 = (-trace + (descriminant.powf(0.5))) / 2.0; + let x2 = (-trace - (descriminant.powf(0.5))) / 2.0; + (x1, x2) +} diff --git a/rust/day1/src/vecOps.rs b/rust/day1/src/vecOps.rs new file mode 100644 index 0000000..eaeca2a --- /dev/null +++ b/rust/day1/src/vecOps.rs @@ -0,0 +1,27 @@ +pub fn mulScalarVector(s: f32, v: &Vec) -> Vec { + v.iter().map(|a| a * s).collect() +} + +pub fn subVectors(v1: &Vec, v2: &Vec) -> Vec { + v1.iter().zip(v2).map(|(a, b)| a - b).collect() +} +pub fn addVectors(v1: &Vec, v2: &Vec) -> Vec { + v1.iter().zip(v2).map(|(a, b)| a + b).collect() +} + +pub fn dotVectors(v1: &Vec, v2: &Vec) -> f32 { + v1.iter().zip(v2).map(|(a, b)| a * b).sum() +} + +pub fn normVector(v: &Vec) -> f32 { + dotVectors(v, v).powf(0.5) +} + +pub fn normalizeVector(v: &Vec) -> Vec { + let vNorm = normVector(&v); + v.iter().map(|a| a / vNorm).collect() +} + +pub fn checkEqualLenVectors(v1: &Vec, v2: &Vec) -> bool { + v1.len() == v2.len() +} diff --git a/rust/numbrs/Cargo.toml b/rust/numbrs/Cargo.toml index a1df943..147136d 100644 --- a/rust/numbrs/Cargo.toml +++ b/rust/numbrs/Cargo.toml @@ -3,4 +3,8 @@ name = "numbrs" version = "0.1.0" edition = "2021" -[dependencies] +# [dependencies] + + +[workspace.lints.rust] +non_snake_case = "allow" diff --git a/rust/numbrs/src/main.rs b/rust/numbrs/src/main.rs index 71bf5d2..24cf5cf 100644 --- a/rust/numbrs/src/main.rs +++ b/rust/numbrs/src/main.rs @@ -1,9 +1,20 @@ +mod matOps; mod vecOps; +use matOps::Matrix; fn main() { println!("Hello, world!"); + let mut a = Matrix::zeros(2, 2); + a.mat[0] = vec![-5f32, -7f32]; + a.mat[1] = vec![2., 4.]; + println!("{:?}", getEVals(a)); } -fn test() { - let ad = [1, 2, 3]; +pub fn getEVals(m: Matrix) -> (f32, f32) { + let trace = m.trace(); + let determinant = (m.mat[0][0] * m.mat[1][1]) - (m.mat[0][1] * m.mat[1][0]); + let descriminant = trace.powi(2) - (4. * determinant); + let x1 = (-trace + (descriminant.powf(0.5))) / 2.0; + let x2 = (-trace - (descriminant.powf(0.5))) / 2.0; + (x1, x2) } diff --git a/rust/numbrs/src/matOps.rs b/rust/numbrs/src/matOps.rs new file mode 100644 index 0000000..f2d1035 --- /dev/null +++ b/rust/numbrs/src/matOps.rs @@ -0,0 +1,67 @@ +pub struct Matrix { + pub mat: Vec>, + rows: usize, + cols: usize, +} + +pub struct Mat2x2 { + mat: [[f32; 2]; 2], +} + +pub struct Mat3x3 { + mat: [[f32; 3]; 3], +} + +impl Matrix { + pub fn shape(&self) -> (usize, usize) { + (self.rows, self.cols) + } + + pub fn checkShape(&self) -> bool { + if self.mat.len() != self.rows { + return false; + } + + for i in 0..self.rows { + if self.mat.get(i).unwrap().len() != self.cols { + return false; + } + } + return true; + } + + pub fn zeros(row: usize, col: usize) -> Self { + let mut r = vec![0 as f32; col]; + let mut rs = vec![r; row]; + Matrix { + mat: rs, + rows: row, + cols: col, + } + } + + pub fn new(row: usize, col: usize) -> Self { + return Matrix::zeros(row, col); + } + + pub fn getTranspose(&self) -> Self { + let mut t = Matrix::new(self.rows, self.cols); + for i in 0..self.rows { + for j in 0..self.cols { + t.mat[i][j] = self.mat[j][i]; + } + } + t + } + + pub fn isSquare(&self) -> bool { + self.rows == self.cols + } + + pub fn trace(&self) -> f32 { + if self.isSquare() { + return (0..self.rows).map(|a| self.mat[a][a]).sum(); + } + 0. + } +} diff --git a/rust/numbrs/src/vecOps.rs b/rust/numbrs/src/vecOps.rs index 506231c..724f6ca 100644 --- a/rust/numbrs/src/vecOps.rs +++ b/rust/numbrs/src/vecOps.rs @@ -22,3 +22,7 @@ pub fn dotVecVec(v1: &[f32], v2: &[f32]) -> f32 { pub fn mulScalVec<'a>(s: &'a f32, v1: &'a [f32]) -> Vec { v1.iter().map(|a| a * s).collect() } + +pub fn checkEqualLenVec(v1: &[f32], v2: &[f32]) -> bool { + v1.len() == v2.len() +}