|
|
@ -1,9 +1,18 @@
|
|
|
|
/// Trait defining the data structure that must be implemented for the independent variables used
|
|
|
|
/// Trait defining the data structure that must be implemented for the independent variables used
|
|
|
|
/// in the objective function. The generic type denotes the type of the prime of that variable
|
|
|
|
/// in the objective function. The generic type denotes the type of the prime of that variable
|
|
|
|
///
|
|
|
|
/// NOTE: This trait also defines some functions that are required to operate on the prime data
|
|
|
|
|
|
|
|
/// type. It should be noted that we are unable to just require T to implement Mul<f64> or Add<f64>
|
|
|
|
|
|
|
|
/// Bbecause then we wouldn't be able to implement XVar for plain Vec types which seems
|
|
|
|
|
|
|
|
/// inconvenient
|
|
|
|
pub trait XVar<T>: Clone {
|
|
|
|
pub trait XVar<T>: Clone {
|
|
|
|
/// Update the current Xvariable based on the prime
|
|
|
|
/// Update the current Xvariable based on the prime
|
|
|
|
fn update(&self, alpha: f64, prime: &T) -> Self;
|
|
|
|
fn update(&self, alpha: f64, prime: &T) -> Self;
|
|
|
|
|
|
|
|
/// Multiply the prime by a float
|
|
|
|
|
|
|
|
fn scale_prime(prime: &T, rhs: f64) -> T;
|
|
|
|
|
|
|
|
/// Add a float to the prime
|
|
|
|
|
|
|
|
fn add_prime(prime: &T, rhs: f64) -> T;
|
|
|
|
|
|
|
|
/// Inner Produce
|
|
|
|
|
|
|
|
fn prime_inner_product(prime: &T, rhs: &T) -> f64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Implementation of XVar for an f64 type
|
|
|
|
/// Implementation of XVar for an f64 type
|
|
|
@ -11,6 +20,18 @@ impl XVar<f64> for f64 {
|
|
|
|
fn update(&self, alpha: f64, prime: &f64) -> Self {
|
|
|
|
fn update(&self, alpha: f64, prime: &f64) -> Self {
|
|
|
|
self + alpha * prime
|
|
|
|
self + alpha * prime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn scale_prime(prime: &f64, rhs: f64) -> f64 {
|
|
|
|
|
|
|
|
prime * rhs
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn add_prime(prime: &f64, rhs: f64) -> f64 {
|
|
|
|
|
|
|
|
prime + rhs
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn prime_inner_product(prime: &f64, rhs: &f64) -> f64 {
|
|
|
|
|
|
|
|
prime * rhs
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Implementation of XVar for a Vec<f64> type
|
|
|
|
/// Implementation of XVar for a Vec<f64> type
|
|
|
@ -21,4 +42,19 @@ impl XVar<Vec<f64>> for Vec<f64> {
|
|
|
|
.map(|(x, xprime)| x + alpha * xprime)
|
|
|
|
.map(|(x, xprime)| x + alpha * xprime)
|
|
|
|
.collect()
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn scale_prime(prime: &Vec<f64>, rhs: f64) -> Vec<f64> {
|
|
|
|
|
|
|
|
prime.iter().map(|val| val * rhs).collect()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn add_prime(prime: &Vec<f64>, rhs: f64) -> Vec<f64> {
|
|
|
|
|
|
|
|
prime.iter().map(|val| val + rhs).collect()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn prime_inner_product(prime: &Vec<f64>, rhs: &Vec<f64>) -> f64 {
|
|
|
|
|
|
|
|
prime
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.zip(rhs.iter())
|
|
|
|
|
|
|
|
.fold(0.0, |acc, a| acc + a.0 * a.1)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|