feat: extends interface of Matrix to support for broad range of types
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
pub fn distance(x: &Vec<f64>, y: &Vec<f64>) -> f64 {
|
||||
use crate::math::num::FloatExt;
|
||||
|
||||
pub fn distance<T: FloatExt>(x: &Vec<T>, y: &Vec<T>) -> T {
|
||||
return squared_distance(x, y).sqrt();
|
||||
}
|
||||
|
||||
pub fn squared_distance(x: &Vec<f64>,y: &Vec<f64>) -> f64 {
|
||||
pub fn squared_distance<T: FloatExt>(x: &Vec<T>,y: &Vec<T>) -> T {
|
||||
if x.len() != y.len() {
|
||||
panic!("Input vector sizes are different.");
|
||||
}
|
||||
|
||||
let mut sum = 0f64;
|
||||
let mut sum = T::zero();
|
||||
for i in 0..x.len() {
|
||||
sum += (x[i] - y[i]).powf(2.);
|
||||
sum = sum + (x[i] - y[i]).powf(T::two());
|
||||
}
|
||||
|
||||
return sum;
|
||||
@@ -25,7 +27,7 @@ mod tests {
|
||||
let a = vec![1., 2., 3.];
|
||||
let b = vec![4., 5., 6.];
|
||||
|
||||
let d_arr = distance(&a, &b);
|
||||
let d_arr: f64 = distance(&a, &b);
|
||||
|
||||
assert!((d_arr - 5.19615242).abs() < 1e-8);
|
||||
}
|
||||
|
||||
+1
-43
@@ -1,44 +1,2 @@
|
||||
pub mod distance;
|
||||
|
||||
pub static EPSILON:f64 = 2.2204460492503131e-16_f64;
|
||||
|
||||
pub trait NumericExt {
|
||||
fn ln_1pe(&self) -> f64;
|
||||
fn sigmoid(&self) -> f64;
|
||||
}
|
||||
|
||||
impl NumericExt for f64 {
|
||||
|
||||
fn ln_1pe(&self) -> f64{
|
||||
if *self > 15. {
|
||||
return *self;
|
||||
} else {
|
||||
return self.exp().ln_1p();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn sigmoid(&self) -> f64 {
|
||||
|
||||
if *self < -40. {
|
||||
return 0.;
|
||||
} else if *self > 40. {
|
||||
return 1.;
|
||||
} else {
|
||||
return 1. / (1. + f64::exp(-self))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sigmoid() {
|
||||
assert_eq!(1.0.sigmoid(), 0.7310585786300049);
|
||||
assert_eq!(41.0.sigmoid(), 1.);
|
||||
assert_eq!((-41.0).sigmoid(), 0.);
|
||||
}
|
||||
}
|
||||
pub mod num;
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
use num_traits::{Float, FromPrimitive};
|
||||
use rand::prelude::*;
|
||||
|
||||
pub trait FloatExt: Float + FromPrimitive {
|
||||
|
||||
fn copysign(self, sign: Self) -> Self;
|
||||
|
||||
fn ln_1pe(self) -> Self;
|
||||
|
||||
fn sigmoid(self) -> Self;
|
||||
|
||||
fn rand() -> Self;
|
||||
|
||||
fn two() -> Self;
|
||||
|
||||
fn half() -> Self;
|
||||
|
||||
}
|
||||
|
||||
impl FloatExt for f64 {
|
||||
fn copysign(self, sign: Self) -> Self{
|
||||
self.copysign(sign)
|
||||
}
|
||||
|
||||
fn ln_1pe(self) -> f64{
|
||||
if self > 15. {
|
||||
return self;
|
||||
} else {
|
||||
return self.exp().ln_1p();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn sigmoid(self) -> f64 {
|
||||
|
||||
if self < -40. {
|
||||
return 0.;
|
||||
} else if self > 40. {
|
||||
return 1.;
|
||||
} else {
|
||||
return 1. / (1. + f64::exp(-self))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn rand() -> f64 {
|
||||
let mut rng = rand::thread_rng();
|
||||
rng.gen()
|
||||
}
|
||||
|
||||
fn two() -> Self {
|
||||
2f64
|
||||
}
|
||||
|
||||
fn half() -> Self {
|
||||
0.5f64
|
||||
}
|
||||
}
|
||||
|
||||
impl FloatExt for f32 {
|
||||
fn copysign(self, sign: Self) -> Self{
|
||||
self.copysign(sign)
|
||||
}
|
||||
|
||||
fn ln_1pe(self) -> f32{
|
||||
if self > 15. {
|
||||
return self;
|
||||
} else {
|
||||
return self.exp().ln_1p();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn sigmoid(self) -> f32 {
|
||||
|
||||
if self < -40. {
|
||||
return 0.;
|
||||
} else if self > 40. {
|
||||
return 1.;
|
||||
} else {
|
||||
return 1. / (1. + f32::exp(-self))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn rand() -> f32 {
|
||||
let mut rng = rand::thread_rng();
|
||||
rng.gen()
|
||||
}
|
||||
|
||||
fn two() -> Self {
|
||||
2f32
|
||||
}
|
||||
|
||||
fn half() -> Self {
|
||||
0.5f32
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sigmoid() {
|
||||
assert_eq!(1.0.sigmoid(), 0.7310585786300049);
|
||||
assert_eq!(41.0.sigmoid(), 1.);
|
||||
assert_eq!((-41.0).sigmoid(), 0.);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user