src/dist.rs

Tue, 22 Oct 2024 08:27:45 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Tue, 22 Oct 2024 08:27:45 -0500
changeset 12
3b05a8b45b95
parent 8
17d71ca4ce84
child 13
f67949050a32
permissions
-rw-r--r--

Save log to a CSV file

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
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
7 pub struct DistTo<M : ManifoldPoint>(pub M);
5
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 impl<M : ManifoldPoint> Apply<M> for DistTo<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 type Output = f64;
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 fn apply(&self, x : M) -> Self::Output {
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
13 self.0.dist_to(&x)
5
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 }
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 impl<'a, M : ManifoldPoint> Apply<&'a M> for DistTo<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 type Output = f64;
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 fn apply(&self, x : &'a M) -> Self::Output {
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
21 self.0.dist_to(x)
5
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 }
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 /// Structure for distance-to functions
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
26 pub struct DistToSquaredDiv2<M : ManifoldPoint>(pub M);
5
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 impl<M : ManifoldPoint> Apply<M> for DistToSquaredDiv2<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 type Output = f64;
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 fn apply(&self, x : M) -> Self::Output {
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
32 let d = self.0.dist_to(&x);
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 d*d / 2.0
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 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 impl<'a, M : ManifoldPoint> Apply<&'a M> for DistToSquaredDiv2<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 type Output = f64;
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 fn apply(&self, x : &'a M) -> Self::Output {
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
41 let d = self.0.dist_to(x);
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 d*d / 2.0
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 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 impl<M : ManifoldPoint> Desc<M> for DistToSquaredDiv2<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 fn desc(&self, τ : f64, x : M) -> M {
8
17d71ca4ce84 Make exp consuming
Tuomo Valkonen <tuomov@iki.fi>
parents: 7
diff changeset
48 let t = self.grad(&x) * τ;
17d71ca4ce84 Make exp consuming
Tuomo Valkonen <tuomov@iki.fi>
parents: 7
diff changeset
49 x.exp(&t)
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 impl<M : ManifoldPoint> Grad<M> for DistToSquaredDiv2<M> {
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 fn grad(&self, x : &M) -> M::Tangent {
7
8979a6638424 A simple test
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
55 x.log(&self.0)
5
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 }
f248e1434c3b Some distance functions etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 }

mercurial