src/scaled.rs

Fri, 06 Dec 2024 14:27:14 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 06 Dec 2024 14:27:14 -0500
changeset 45
cac6978dc7dd
parent 18
84923812d220
child 46
90cc221eb52b
permissions
-rw-r--r--

Adjust plot style

18
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 /*!
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 Implementation of scaling of functions on a manifold by a scalar.
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 */
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 use alg_tools::mapping::Apply;
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 use crate::manifold::ManifoldPoint;
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 use crate::fb::{Grad, Desc, Prox};
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 /// Structure for a function of type `G`, scaled by a scalar.
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 pub struct Scaled<G> {
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 α : f64,
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 g : G,
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 }
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 impl<G> Scaled<G> {
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 #[allow(dead_code)]
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 pub fn new(α : f64, g : G) -> Self {
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 Scaled{ α, g }
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 }
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 }
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 impl<M, G : Apply<M, Output=f64>> Apply<M> for Scaled< G> {
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 type Output = f64;
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 fn apply(&self, x : M) -> Self::Output {
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 self.g.apply(x) * self.α
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 }
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 }
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 impl<M : ManifoldPoint, G : Desc<M>> Desc<M> for Scaled<G> {
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 fn desc(&self, τ : f64, x : M) -> M {
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 self.g.desc(τ * self.α, x)
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 }
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 }
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 impl<M : ManifoldPoint, G : Grad<M>> Grad<M> for Scaled<G> {
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 fn grad(&self, x : &M) -> M::Tangent {
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 self.g.grad(x) * self.α
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 }
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 }
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 impl<M : ManifoldPoint, G : Prox<M>> Prox<M> for Scaled<G> {
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 fn prox(&self, τ : f64, x : M) -> M {
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 self.g.prox(τ * self.α, x)
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 }
84923812d220 Add function scaling by a scalar
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 }

mercurial