add mat ops

This commit is contained in:
2024-08-19 15:40:09 +05:30
parent 09fac0785e
commit b8ad3e5b66
8 changed files with 165 additions and 15 deletions

View File

@@ -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<f32> {
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));
}

28
rust/day1/src/matOps.rs Normal file
View File

@@ -0,0 +1,28 @@
pub struct Matrix {
mat: Vec<Vec<f32>>,
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
}

View File

@@ -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)
}

27
rust/day1/src/vecOps.rs Normal file
View File

@@ -0,0 +1,27 @@
pub fn mulScalarVector(s: f32, v: &Vec<f32>) -> Vec<f32> {
v.iter().map(|a| a * s).collect()
}
pub fn subVectors(v1: &Vec<f32>, v2: &Vec<f32>) -> Vec<f32> {
v1.iter().zip(v2).map(|(a, b)| a - b).collect()
}
pub fn addVectors(v1: &Vec<f32>, v2: &Vec<f32>) -> Vec<f32> {
v1.iter().zip(v2).map(|(a, b)| a + b).collect()
}
pub fn dotVectors(v1: &Vec<f32>, v2: &Vec<f32>) -> f32 {
v1.iter().zip(v2).map(|(a, b)| a * b).sum()
}
pub fn normVector(v: &Vec<f32>) -> f32 {
dotVectors(v, v).powf(0.5)
}
pub fn normalizeVector(v: &Vec<f32>) -> Vec<f32> {
let vNorm = normVector(&v);
v.iter().map(|a| a / vNorm).collect()
}
pub fn checkEqualLenVectors(v1: &Vec<f32>, v2: &Vec<f32>) -> bool {
v1.len() == v2.len()
}