From 4a17c06f22d3236da6f30c397695ef3771a9d393 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 20 Sep 2023 17:23:45 +0200 Subject: support for different vector metrics --- src/embedders/vecmetric.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/embedders/vecmetric.rs (limited to 'src/embedders/vecmetric.rs') diff --git a/src/embedders/vecmetric.rs b/src/embedders/vecmetric.rs new file mode 100644 index 0000000..474a6d0 --- /dev/null +++ b/src/embedders/vecmetric.rs @@ -0,0 +1,43 @@ +use super::MetricElem; +use serde::{Deserialize, Serialize}; + +pub trait VecMetric: MetricElem + From> {} + +#[derive(Deserialize, Serialize)] +pub struct CosineSimilarity(pub Vec); +#[derive(Deserialize, Serialize)] +pub struct EuclidianDistance(pub Vec); +#[derive(Deserialize, Serialize)] +pub struct ManhattenDistance(pub Vec); + +impl VecMetric for CosineSimilarity {} +impl VecMetric for EuclidianDistance {} +impl VecMetric for ManhattenDistance {} +#[rustfmt::skip] impl From> for CosineSimilarity { fn from(value: Vec) -> Self { Self(value) } } +#[rustfmt::skip] impl From> for EuclidianDistance { fn from(value: Vec) -> Self { Self(value) } } +#[rustfmt::skip] impl From> for ManhattenDistance { fn from(value: Vec) -> Self { Self(value) } } + +impl MetricElem for CosineSimilarity { + fn dist(&self, _other: &Self) -> f64 { + todo!() + } +} +impl MetricElem for EuclidianDistance { + fn dist(&self, other: &Self) -> f64 { + self.0 + .iter() + .zip(other.0.iter()) + .map(|(a, b)| (a - b).powf(2.)) + .sum::() + .sqrt() as f64 + } +} +impl MetricElem for ManhattenDistance { + fn dist(&self, other: &Self) -> f64 { + self.0 + .iter() + .zip(other.0.iter()) + .map(|(a, b)| (a - b).abs()) + .sum::() as f64 + } +} -- cgit v1.2.3-70-g09d2