1 /*! |
1 /*! |
2 Implementations of the distance function and the distance function squared, on manifolds. |
2 Implementations of the distance function and the distance function squared, on manifolds. |
3 */ |
3 */ |
4 |
4 |
5 use alg_tools::mapping::Apply; |
5 use alg_tools::mapping::{Mapping, Instance}; |
6 use alg_tools::euclidean::Euclidean; |
6 use alg_tools::euclidean::Euclidean; |
7 use crate::manifold::ManifoldPoint; |
7 use crate::manifold::ManifoldPoint; |
8 use crate::fb::{Grad, Desc, Prox}; |
8 use crate::fb::{Grad, Desc, Prox}; |
9 |
9 |
10 /// Structure for distance-to functions |
10 /// Structure for distance-to functions |
11 pub struct DistTo<M : ManifoldPoint>(pub M); |
11 pub struct DistTo<M : ManifoldPoint>(pub M); |
12 |
12 |
13 impl<M : ManifoldPoint> Apply<M> for DistTo<M> { |
13 impl<M : ManifoldPoint> Mapping<M> for DistTo<M> { |
14 type Output = f64; |
14 type Codomain = f64; |
15 |
15 |
16 fn apply(&self, x : M) -> Self::Output { |
16 fn apply<I : Instance<M>>(&self, x : I) -> Self::Codomain { |
17 self.0.dist_to(&x) |
17 x.eval(|x̃| self.0.dist_to(x̃)) |
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 { |
|
25 self.0.dist_to(x) |
|
26 } |
18 } |
27 } |
19 } |
28 |
20 |
29 /// Structure for distance-to functions |
21 /// Structure for distance-to functions |
30 pub struct DistToSquaredDiv2<M : ManifoldPoint>(pub M); |
22 pub struct DistToSquaredDiv2<M : ManifoldPoint>(pub M); |
31 |
23 |
32 impl<M : ManifoldPoint> Apply<M> for DistToSquaredDiv2<M> { |
24 impl<M : ManifoldPoint> Mapping<M> for DistToSquaredDiv2<M> { |
33 type Output = f64; |
25 type Codomain = f64; |
34 |
26 |
35 fn apply(&self, x : M) -> Self::Output { |
27 fn apply<I : Instance<M>>(&self, x : I) -> Self::Codomain { |
36 let d = self.0.dist_to(&x); |
28 let d = x.eval(|x̃| self.0.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.0.dist_to(x); |
|
46 d*d / 2.0 |
29 d*d / 2.0 |
47 } |
30 } |
48 } |
31 } |
49 |
32 |
50 impl<M : ManifoldPoint> Desc<M> for DistToSquaredDiv2<M> { |
33 impl<M : ManifoldPoint> Desc<M> for DistToSquaredDiv2<M> { |