Add function scaling by a scalar

Wed, 06 Nov 2024 10:15:38 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 06 Nov 2024 10:15:38 -0500
changeset 18
84923812d220
parent 17
ad68dacabe4f
child 19
bc7f268b324a

Add function scaling by a scalar

src/main.rs file | annotate | diff | comparison | revisions
src/scaled.rs file | annotate | diff | comparison | revisions
--- a/src/main.rs	Wed Nov 06 10:00:06 2024 -0500
+++ b/src/main.rs	Wed Nov 06 10:15:38 2024 -0500
@@ -31,6 +31,7 @@
 mod cube;
 mod dist;
 mod zero;
+mod scaled;
 
 /// Program entry point
 fn main() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/scaled.rs	Wed Nov 06 10:15:38 2024 -0500
@@ -0,0 +1,46 @@
+/*!
+Implementation of scaling of functions on a manifold by a scalar.
+*/
+
+use alg_tools::mapping::Apply;
+use crate::manifold::ManifoldPoint;
+use crate::fb::{Grad, Desc, Prox};
+
+/// Structure for a function of type `G`, scaled by a scalar.
+pub struct Scaled<G> {
+    α : f64,
+    g : G,
+}
+
+impl<G> Scaled<G> {
+    #[allow(dead_code)]
+    pub fn new(α : f64, g : G) -> Self {
+        Scaled{ α, g }
+    }
+}
+
+impl<M, G : Apply<M, Output=f64>> Apply<M> for Scaled< G> {
+    type Output = f64;
+
+    fn apply(&self, x : M) -> Self::Output {
+        self.g.apply(x) * self.α
+    }
+}
+
+impl<M : ManifoldPoint, G : Desc<M>> Desc<M> for Scaled<G> {
+    fn desc(&self, τ : f64, x : M) -> M {
+        self.g.desc(τ * self.α, x)
+    }
+}
+
+impl<M : ManifoldPoint, G : Grad<M>> Grad<M> for Scaled<G> {
+    fn grad(&self, x : &M) -> M::Tangent {
+       self.g.grad(x) * self.α
+    }
+}
+
+impl<M : ManifoldPoint, G : Prox<M>> Prox<M> for Scaled<G> {
+    fn prox(&self, τ : f64, x : M) -> M {
+        self.g.prox(τ * self.α, x)
+    }
+}
\ No newline at end of file

mercurial