src/operator_arithmetic.rs

Mon, 23 Dec 2024 23:27:45 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 23 Dec 2024 23:27:45 -0500
branch
dev
changeset 80
f802ddbabcfc
parent 68
c5f70e767511
permissions
-rw-r--r--

Basic arithmetric opt-in hack attempt: not allowed by Rust.

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};
80
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 68
diff changeset
8 use crate::mapping::{Mapping, ArithmeticTrue, DifferentiableImpl, DifferentiableMapping};
68
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;
80
f802ddbabcfc Basic arithmetric opt-in hack attempt: not allowed by Rust.
Tuomo Valkonen <tuomov@iki.fi>
parents: 68
diff changeset
43 type ArithmeticOptIn = ArithmeticTrue;
68
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 #[inline]
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 fn apply<I : Instance<D>>(&self, x : I) -> Self::Codomain {
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 self.base_fn.apply(x) * self.weight.value()
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
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 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
52 where
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 F : Float,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 D : Space,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 T : DifferentiableMapping<D, DerivativeDomain=V>,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56 V : Space + std::ops::Mul<F, Output=V>,
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 C : Constant<Type=F>
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 {
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 type Derivative = V;
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 #[inline]
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 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
63 self.base_fn.differential(x) * self.weight.value()
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 }
c5f70e767511 Split out and generalise Weighted
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65 }

mercurial