src/dist.rs

Tue, 22 Oct 2024 08:39:46 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Tue, 22 Oct 2024 08:39:46 -0500
changeset 13
f67949050a32
parent 8
17d71ca4ce84
child 17
ad68dacabe4f
permissions
-rw-r--r--

documentation

13
f67949050a32 documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
1 /*!
f67949050a32 documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
2 Implementations of the distance function and the distance function squared, on manifolds.
f67949050a32 documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
3 */
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 use alg_tools::mapping::Apply;
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 use crate::manifold::ManifoldPoint;
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 use crate::fb::{Grad, Desc};
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 /// Structure for distance-to functions
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
10 pub struct DistTo<M : ManifoldPoint>(pub M);
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 impl<M : ManifoldPoint> Apply<M> for DistTo<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 type Output = f64;
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 fn apply(&self, x : M) -> Self::Output {
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
16 self.0.dist_to(&x)
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 impl<'a, M : ManifoldPoint> Apply<&'a M> for DistTo<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 type Output = f64;
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 fn apply(&self, x : &'a M) -> Self::Output {
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
24 self.0.dist_to(x)
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 /// Structure for distance-to functions
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
29 pub struct DistToSquaredDiv2<M : ManifoldPoint>(pub M);
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 impl<M : ManifoldPoint> Apply<M> for DistToSquaredDiv2<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 type Output = f64;
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 fn apply(&self, x : M) -> Self::Output {
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
35 let d = self.0.dist_to(&x);
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 d*d / 2.0
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 impl<'a, M : ManifoldPoint> Apply<&'a M> for DistToSquaredDiv2<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 type Output = f64;
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 fn apply(&self, x : &'a M) -> Self::Output {
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
44 let d = self.0.dist_to(x);
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 d*d / 2.0
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 impl<M : ManifoldPoint> Desc<M> for DistToSquaredDiv2<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 fn desc(&self, τ : f64, x : M) -> M {
8
17d71ca4ce84 Make exp consuming
Tuomo Valkonen <tuomov@iki.fi>
parents: 7
diff changeset
51 let t = self.grad(&x) * τ;
17d71ca4ce84 Make exp consuming
Tuomo Valkonen <tuomov@iki.fi>
parents: 7
diff changeset
52 x.exp(&t)
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 impl<M : ManifoldPoint> Grad<M> for DistToSquaredDiv2<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 fn grad(&self, x : &M) -> M::Tangent {
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
58 x.log(&self.0)
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 }

mercurial