Adds draft implementation of LR
This commit is contained in:
+108
-8
@@ -3,7 +3,7 @@ use std::fmt::Debug;
|
||||
|
||||
pub mod naive;
|
||||
|
||||
pub trait Matrix: Into<Vec<f64>> + Clone{
|
||||
pub trait Matrix: Into<Vec<f64>> + Clone + Debug{
|
||||
|
||||
fn get(&self, row: usize, col: usize) -> f64;
|
||||
|
||||
@@ -15,9 +15,11 @@ pub trait Matrix: Into<Vec<f64>> + Clone{
|
||||
|
||||
fn ones(nrows: usize, ncols: usize) -> Self;
|
||||
|
||||
fn from_vector<V:Vector>(v: &V, nrows: usize, ncols: usize) -> Self;
|
||||
|
||||
fn fill(nrows: usize, ncols: usize, value: f64) -> Self;
|
||||
|
||||
fn shape(&self) -> (usize, usize);
|
||||
fn shape(&self) -> (usize, usize);
|
||||
|
||||
fn v_stack(&self, other: &Self) -> Self;
|
||||
|
||||
@@ -29,15 +31,69 @@ pub trait Matrix: Into<Vec<f64>> + Clone{
|
||||
|
||||
fn approximate_eq(&self, other: &Self, error: f64) -> bool;
|
||||
|
||||
fn add_mut(&mut self, other: &Self);
|
||||
fn add_mut(&mut self, other: &Self) -> &Self;
|
||||
|
||||
fn add_scalar_mut(&mut self, scalar: f64);
|
||||
fn sub_mut(&mut self, other: &Self) -> &Self;
|
||||
|
||||
fn sub_scalar_mut(&mut self, scalar: f64);
|
||||
fn mul_mut(&mut self, other: &Self) -> &Self;
|
||||
|
||||
fn mul_scalar_mut(&mut self, scalar: f64);
|
||||
fn div_mut(&mut self, other: &Self) -> &Self;
|
||||
|
||||
fn div_scalar_mut(&mut self, scalar: f64);
|
||||
fn add(&self, other: &Self) -> Self {
|
||||
let mut r = self.clone();
|
||||
r.add_mut(other);
|
||||
r
|
||||
}
|
||||
|
||||
fn sub(&self, other: &Self) -> Self {
|
||||
let mut r = self.clone();
|
||||
r.sub_mut(other);
|
||||
r
|
||||
}
|
||||
|
||||
fn mul(&self, other: &Self) -> Self {
|
||||
let mut r = self.clone();
|
||||
r.mul_mut(other);
|
||||
r
|
||||
}
|
||||
|
||||
fn div(&self, other: &Self) -> Self {
|
||||
let mut r = self.clone();
|
||||
r.div_mut(other);
|
||||
r
|
||||
}
|
||||
|
||||
fn add_scalar_mut(&mut self, scalar: f64) -> &Self;
|
||||
|
||||
fn sub_scalar_mut(&mut self, scalar: f64) -> &Self;
|
||||
|
||||
fn mul_scalar_mut(&mut self, scalar: f64) -> &Self;
|
||||
|
||||
fn div_scalar_mut(&mut self, scalar: f64) -> &Self;
|
||||
|
||||
fn add_scalar(&self, scalar: f64) -> Self{
|
||||
let mut r = self.clone();
|
||||
r.add_scalar_mut(scalar);
|
||||
r
|
||||
}
|
||||
|
||||
fn sub_scalar(&self, scalar: f64) -> Self{
|
||||
let mut r = self.clone();
|
||||
r.sub_scalar_mut(scalar);
|
||||
r
|
||||
}
|
||||
|
||||
fn mul_scalar(&self, scalar: f64) -> Self{
|
||||
let mut r = self.clone();
|
||||
r.mul_scalar_mut(scalar);
|
||||
r
|
||||
}
|
||||
|
||||
fn div_scalar(&self, scalar: f64) -> Self{
|
||||
let mut r = self.clone();
|
||||
r.div_scalar_mut(scalar);
|
||||
r
|
||||
}
|
||||
|
||||
fn transpose(&self) -> Self;
|
||||
|
||||
@@ -47,12 +103,52 @@ pub trait Matrix: Into<Vec<f64>> + Clone{
|
||||
|
||||
fn norm2(&self) -> f64;
|
||||
|
||||
fn norm(&self, p:f64) -> f64;
|
||||
|
||||
fn negative_mut(&mut self);
|
||||
|
||||
fn negative(&self) -> Self {
|
||||
let mut result = self.clone();
|
||||
result.negative_mut();
|
||||
result
|
||||
}
|
||||
|
||||
fn reshape(&self, nrows: usize, ncols: usize) -> Self;
|
||||
|
||||
fn copy_from(&mut self, other: &Self);
|
||||
|
||||
fn abs_mut(&mut self) -> &Self;
|
||||
|
||||
fn abs(&self) -> Self {
|
||||
let mut result = self.clone();
|
||||
result.abs_mut();
|
||||
result
|
||||
}
|
||||
|
||||
fn sum(&self) -> f64;
|
||||
|
||||
fn max_diff(&self, other: &Self) -> f64;
|
||||
|
||||
fn softmax_mut(&mut self);
|
||||
|
||||
fn pow_mut(&mut self, p: f64) -> &Self;
|
||||
|
||||
fn pow(&mut self, p: f64) -> Self {
|
||||
let mut result = self.clone();
|
||||
result.pow_mut(p);
|
||||
result
|
||||
}
|
||||
|
||||
fn argmax(&self) -> Vec<usize>;
|
||||
|
||||
}
|
||||
|
||||
pub trait Vector: Into<Vec<f64>> + Clone + Debug {
|
||||
|
||||
fn from_array(values: &[f64]) -> Self;
|
||||
|
||||
fn from_vec(values: &Vec<f64>) -> Self;
|
||||
|
||||
fn get(&self, i: usize) -> f64;
|
||||
|
||||
fn set(&mut self, i: usize, value: f64);
|
||||
@@ -153,6 +249,10 @@ pub trait Vector: Into<Vec<f64>> + Clone + Debug {
|
||||
r
|
||||
}
|
||||
|
||||
fn max_diff(&self, other: &Self) -> f64;
|
||||
fn max_diff(&self, other: &Self) -> f64;
|
||||
|
||||
fn softmax_mut(&mut self);
|
||||
|
||||
fn unique(&self) -> Vec<f64>;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user