Mon, 21 Oct 2024 08:44:23 -0500
Add a zero function on manifolds
| 5 | 1 | |
| 2 | use alg_tools::mapping::Apply; | |
| 3 | use crate::manifold::ManifoldPoint; | |
| 4 | use crate::fb::{Grad, Desc}; | |
| 5 | ||
| 6 | /// Structure for distance-to functions | |
| 7 | pub struct DistTo<M : ManifoldPoint> { | |
| 8 | base : M | |
| 9 | } | |
| 10 | ||
| 11 | impl<M : ManifoldPoint> Apply<M> for DistTo<M> { | |
| 12 | type Output = f64; | |
| 13 | ||
| 14 | fn apply(&self, x : M) -> Self::Output { | |
| 15 | self.base.dist_to(&x) | |
| 16 | } | |
| 17 | } | |
| 18 | ||
| 19 | impl<'a, M : ManifoldPoint> Apply<&'a M> for DistTo<M> { | |
| 20 | type Output = f64; | |
| 21 | ||
| 22 | fn apply(&self, x : &'a M) -> Self::Output { | |
| 23 | self.base.dist_to(x) | |
| 24 | } | |
| 25 | } | |
| 26 | ||
| 27 | /// Structure for distance-to functions | |
| 28 | pub struct DistToSquaredDiv2<M : ManifoldPoint> { | |
| 29 | base : M | |
| 30 | } | |
| 31 | ||
| 32 | impl<M : ManifoldPoint> Apply<M> for DistToSquaredDiv2<M> { | |
| 33 | type Output = f64; | |
| 34 | ||
| 35 | fn apply(&self, x : M) -> Self::Output { | |
| 36 | let d = self.base.dist_to(&x); | |
| 37 | d*d / 2.0 | |
| 38 | } | |
| 39 | } | |
| 40 | ||
| 41 | impl<'a, M : ManifoldPoint> Apply<&'a M> for DistToSquaredDiv2<M> { | |
| 42 | type Output = f64; | |
| 43 | ||
| 44 | fn apply(&self, x : &'a M) -> Self::Output { | |
| 45 | let d = self.base.dist_to(x); | |
| 46 | d*d / 2.0 | |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 50 | impl<M : ManifoldPoint> Desc<M> for DistToSquaredDiv2<M> { | |
| 51 | fn desc(&self, τ : f64, x : M) -> M { | |
| 52 | x.exp(&(self.grad(&x) * τ)) | |
| 53 | } | |
| 54 | } | |
| 55 | ||
| 56 | impl<M : ManifoldPoint> Grad<M> for DistToSquaredDiv2<M> { | |
| 57 | fn grad(&self, x : &M) -> M::Tangent { | |
| 58 | x.log(&self.base) | |
| 59 | } | |
| 60 | } |