src/dist.rs

Mon, 21 Oct 2024 08:44:23 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 21 Oct 2024 08:44:23 -0500
changeset 6
df9628092285
parent 5
f248e1434c3b
child 7
8979a6638424
permissions
-rw-r--r--

Add a zero function on manifolds

5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 use alg_tools::mapping::Apply;
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 use crate::manifold::ManifoldPoint;
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4 use crate::fb::{Grad, Desc};
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 /// Structure for distance-to functions
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 pub struct DistTo<M : ManifoldPoint> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 base : M
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 impl<M : ManifoldPoint> Apply<M> for DistTo<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 type Output = f64;
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 fn apply(&self, x : M) -> Self::Output {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 self.base.dist_to(&x)
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 }
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 impl<'a, M : ManifoldPoint> Apply<&'a M> for DistTo<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 type Output = f64;
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 fn apply(&self, x : &'a M) -> Self::Output {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 self.base.dist_to(x)
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 }
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 /// Structure for distance-to functions
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 pub struct DistToSquaredDiv2<M : ManifoldPoint> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 base : M
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
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 impl<M : ManifoldPoint> Apply<M> for DistToSquaredDiv2<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 type Output = f64;
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 fn apply(&self, x : M) -> Self::Output {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 let d = self.base.dist_to(&x);
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 d*d / 2.0
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
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 impl<'a, M : ManifoldPoint> Apply<&'a M> for DistToSquaredDiv2<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 type Output = f64;
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 fn apply(&self, x : &'a M) -> Self::Output {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 let d = self.base.dist_to(x);
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 d*d / 2.0
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
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 impl<M : ManifoldPoint> Desc<M> for DistToSquaredDiv2<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 fn desc(&self, τ : f64, x : M) -> M {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 x.exp(&(self.grad(&x) * τ))
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 {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 x.log(&self.base)
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