Fri, 06 Dec 2024 13:53:09 -0500
Cylinder pgfplots code reuse
13 | 1 | /*! |
2 | Implementations of the distance function and the distance function squared, on manifolds. | |
3 | */ | |
5 | 4 | |
5 | use alg_tools::mapping::Apply; | |
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 | |
13 | impl<M : ManifoldPoint> Apply<M> for DistTo<M> { | |
14 | type Output = f64; | |
15 | ||
16 | fn apply(&self, x : M) -> Self::Output { | |
7 | 17 | self.0.dist_to(&x) |
5 | 18 | } |
19 | } | |
20 | ||
21 | impl<'a, M : ManifoldPoint> Apply<&'a M> for DistTo<M> { | |
22 | type Output = f64; | |
23 | ||
24 | fn apply(&self, x : &'a M) -> Self::Output { | |
7 | 25 | self.0.dist_to(x) |
5 | 26 | } |
27 | } | |
28 | ||
29 | /// Structure for distance-to functions | |
7 | 30 | pub struct DistToSquaredDiv2<M : ManifoldPoint>(pub M); |
5 | 31 | |
32 | impl<M : ManifoldPoint> Apply<M> for DistToSquaredDiv2<M> { | |
33 | type Output = f64; | |
34 | ||
35 | fn apply(&self, x : M) -> Self::Output { | |
7 | 36 | let d = self.0.dist_to(&x); |
5 | 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 { | |
7 | 45 | let d = self.0.dist_to(x); |
5 | 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 { | |
8 | 52 | let t = self.grad(&x) * τ; |
53 | x.exp(&t) | |
5 | 54 | } |
55 | } | |
56 | ||
57 | impl<M : ManifoldPoint> Grad<M> for DistToSquaredDiv2<M> { | |
58 | fn grad(&self, x : &M) -> M::Tangent { | |
7 | 59 | x.log(&self.0) |
5 | 60 | } |
61 | } | |
17 | 62 | |
63 | impl<M : ManifoldPoint> Prox<M> for DistTo<M> { | |
64 | /// This is proximal map is a type of soft-thresholding on manifolds. | |
65 | fn prox(&self, τ : f64, x : M) -> M { | |
66 | let v = x.log(&self.0); | |
67 | let d = v.norm2(); | |
68 | if d <= τ { | |
69 | self.0.clone() | |
70 | } else { | |
71 | x.exp( &(v * (τ / d)) ) | |
72 | } | |
73 | } | |
74 | } |