diff options
Diffstat (limited to 'src/embedders/vecmetric.rs')
-rw-r--r-- | src/embedders/vecmetric.rs | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/embedders/vecmetric.rs b/src/embedders/vecmetric.rs index 65d71df..2ebd170 100644 --- a/src/embedders/vecmetric.rs +++ b/src/embedders/vecmetric.rs @@ -6,14 +6,18 @@ pub trait VecMetric: MetricElem + From<Vec<f32>> {} #[derive(Decode, Encode)] pub struct AngularDistance(pub Vec<f32>); #[derive(Decode, Encode)] +pub struct CosineDistance(pub Vec<f32>); +#[derive(Decode, Encode)] pub struct EuclidianDistance(pub Vec<f32>); #[derive(Decode, Encode)] pub struct ManhattenDistance(pub Vec<f32>); impl VecMetric for AngularDistance {} +impl VecMetric for CosineDistance {} impl VecMetric for EuclidianDistance {} impl VecMetric for ManhattenDistance {} #[rustfmt::skip] impl From<Vec<f32>> for AngularDistance { fn from(value: Vec<f32>) -> Self { Self(value) } } +#[rustfmt::skip] impl From<Vec<f32>> for CosineDistance { fn from(value: Vec<f32>) -> Self { Self(value) } } #[rustfmt::skip] impl From<Vec<f32>> for EuclidianDistance { fn from(value: Vec<f32>) -> Self { Self(value) } } #[rustfmt::skip] impl From<Vec<f32>> for ManhattenDistance { fn from(value: Vec<f32>) -> Self { Self(value) } } @@ -28,7 +32,18 @@ impl MetricElem for AngularDistance { let mag_a = self.0.iter().map(|x| x.powi(2)).sum::<f32>(); let mag_b = other.0.iter().map(|x| x.powi(2)).sum::<f32>(); let cossim = x / (mag_a * mag_b).sqrt(); - cossim.acos() as f64 + // clamp is require because floating point errors + cossim.clamp(-1., 1.).acos() as f64 + } +} +impl MetricElem for CosineDistance { + fn dist(&self, other: &Self) -> f64 { + self.0 + .iter() + .zip(other.0.iter()) + .map(|(a, b)| (*a - *b) * (*b - *a)) + .sum::<f32>() + .sqrt() as f64 } } impl MetricElem for EuclidianDistance { |