src/operator_arithmetic.rs

Tue, 31 Dec 2024 08:48:50 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Tue, 31 Dec 2024 08:48:50 -0500
branch
dev
changeset 68
c5f70e767511
child 69
e5fab0125a8e
child 80
f802ddbabcfc
permissions
-rw-r--r--

Split out and generalise Weighted

68
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 /*!
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 Arithmetic of [`Mapping`]s.
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 */
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 use serde::Serialize;
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 use crate::types::*;
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 use crate::instance::{Space, Instance};
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 use crate::mapping::{Mapping, DifferentiableImpl, DifferentiableMapping};
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 /// A trait for encoding constant [`Float`] values
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 pub trait Constant : Copy + Sync + Send + 'static + std::fmt::Debug + Into<Self::Type> {
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 /// The type of the value
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 type Type : Float;
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 /// Returns the value of the constant
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 fn value(&self) -> Self::Type;
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 }
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 impl<F : Float> Constant for F {
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 type Type = F;
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 #[inline]
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 fn value(&self) -> F { *self }
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 }
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 /// Weighting of a [`Support`] and [`Apply`] by scalar multiplication;
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 /// output of [`Support::weigh`].
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 #[derive(Copy,Clone,Debug,Serialize)]
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 pub struct Weighted<T, C : Constant> {
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 /// The weight
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 pub weight : C,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 /// The base [`Support`] or [`Apply`] being weighted.
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 pub base_fn : T,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 }
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 impl<'a, T, V, D, F, C> Mapping<D> for Weighted<T, C>
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 where
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 F : Float,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 D : Space,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 T : Mapping<D, Codomain=V>,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 V : Space + ClosedMul<F>,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 C : Constant<Type=F>
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 {
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 type Codomain = V;
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 #[inline]
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 fn apply<I : Instance<D>>(&self, x : I) -> Self::Codomain {
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 self.base_fn.apply(x) * self.weight.value()
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 }
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 }
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 impl<'a, T, V, D, F, C> DifferentiableImpl<D> for Weighted<T, C>
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 where
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52 F : Float,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 D : Space,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 T : DifferentiableMapping<D, DerivativeDomain=V>,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 V : Space + std::ops::Mul<F, Output=V>,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 C : Constant<Type=F>
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 {
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 type Derivative = V;
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 #[inline]
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 fn differential_impl<I : Instance<D>>(&self, x : I) -> Self::Derivative {
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 self.base_fn.differential(x) * self.weight.value()
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63 }
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 }

mercurial