Mon, 31 Mar 2025 13:33:14 -0500
README typofix
13 | 1 | /*! |
2 | Implementations of the distance function and the distance function squared, on manifolds. | |
3 | */ | |
5 | 4 | |
55 | 5 | use alg_tools::mapping::{Mapping, Instance}; |
17 | 6 | use alg_tools::euclidean::Euclidean; |
5 | 7 | use crate::manifold::ManifoldPoint; |
17 | 8 | use crate::fb::{Grad, Desc, Prox}; |
5 | 9 | |
10 | /// Structure for distance-to functions | |
7 | 11 | pub struct DistTo<M : ManifoldPoint>(pub M); |
5 | 12 | |
55 | 13 | impl<M : ManifoldPoint> Mapping<M> for DistTo<M> { |
14 | type Codomain = f64; | |
5 | 15 | |
55 | 16 | fn apply<I : Instance<M>>(&self, x : I) -> Self::Codomain { |
17 | x.eval(|x̃| self.0.dist_to(x̃)) | |
5 | 18 | } |
19 | } | |
20 | ||
21 | /// Structure for distance-to functions | |
7 | 22 | pub struct DistToSquaredDiv2<M : ManifoldPoint>(pub M); |
5 | 23 | |
55 | 24 | impl<M : ManifoldPoint> Mapping<M> for DistToSquaredDiv2<M> { |
25 | type Codomain = f64; | |
5 | 26 | |
55 | 27 | fn apply<I : Instance<M>>(&self, x : I) -> Self::Codomain { |
28 | let d = x.eval(|x̃| self.0.dist_to(x̃)); | |
5 | 29 | d*d / 2.0 |
30 | } | |
31 | } | |
32 | ||
33 | impl<M : ManifoldPoint> Desc<M> for DistToSquaredDiv2<M> { | |
34 | fn desc(&self, τ : f64, x : M) -> M { | |
8 | 35 | let t = self.grad(&x) * τ; |
36 | x.exp(&t) | |
5 | 37 | } |
38 | } | |
39 | ||
40 | impl<M : ManifoldPoint> Grad<M> for DistToSquaredDiv2<M> { | |
41 | fn grad(&self, x : &M) -> M::Tangent { | |
7 | 42 | x.log(&self.0) |
5 | 43 | } |
44 | } | |
17 | 45 | |
46 | impl<M : ManifoldPoint> Prox<M> for DistTo<M> { | |
53 | 47 | /// This proximal map is a type of soft-thresholding on manifolds. |
17 | 48 | fn prox(&self, τ : f64, x : M) -> M { |
49 | let v = x.log(&self.0); | |
50 | let d = v.norm2(); | |
51 | if d <= τ { | |
52 | self.0.clone() | |
53 | } else { | |
54 | x.exp( &(v * (τ / d)) ) | |
55 | } | |
56 | } | |
57 | } |