diff -r e09437844ad9 -r f248e1434c3b src/dist.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/dist.rs Sat Oct 19 10:46:13 2024 -0500 @@ -0,0 +1,60 @@ + +use alg_tools::mapping::Apply; +use crate::manifold::ManifoldPoint; +use crate::fb::{Grad, Desc}; + +/// Structure for distance-to functions +pub struct DistTo { + base : M +} + +impl Apply for DistTo { + type Output = f64; + + fn apply(&self, x : M) -> Self::Output { + self.base.dist_to(&x) + } +} + +impl<'a, M : ManifoldPoint> Apply<&'a M> for DistTo { + type Output = f64; + + fn apply(&self, x : &'a M) -> Self::Output { + self.base.dist_to(x) + } +} + +/// Structure for distance-to functions +pub struct DistToSquaredDiv2 { + base : M +} + +impl Apply for DistToSquaredDiv2 { + type Output = f64; + + fn apply(&self, x : M) -> Self::Output { + let d = self.base.dist_to(&x); + d*d / 2.0 + } +} + +impl<'a, M : ManifoldPoint> Apply<&'a M> for DistToSquaredDiv2 { + type Output = f64; + + fn apply(&self, x : &'a M) -> Self::Output { + let d = self.base.dist_to(x); + d*d / 2.0 + } +} + +impl Desc for DistToSquaredDiv2 { + fn desc(&self, τ : f64, x : M) -> M { + x.exp(&(self.grad(&x) * τ)) + } +} + +impl Grad for DistToSquaredDiv2 { + fn grad(&self, x : &M) -> M::Tangent { + x.log(&self.base) + } +}