Wed, 04 Oct 2023 08:02:14 -0500
Add Differential struct and DifferentiableMapping.diff
5 | 1 | /*! |
2 | Traits for mathematical functions. | |
3 | */ | |
0 | 4 | |
5 | use std::marker::PhantomData; | |
6 | use crate::types::{Float}; | |
7 | use serde::Serialize; | |
8 | use crate::loc::Loc; | |
9 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
10 | /// Trait for application of `Self` as a mathematical function or operator on `X`. |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
11 | pub trait Apply<X> { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
12 | type Output; |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
13 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
14 | /// Compute the value of `self` at `x`. |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
15 | fn apply(&self, x : X) -> Self::Output; |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
16 | } |
0 | 17 | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
18 | /// This blanket implementation is a workaround helper to Rust trait system limitations. |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
19 | /// |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
20 | /// It is introduced because the trait system does not allow blanket implementations of both |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
21 | /// [`Apply<X>`] and [`Apply<&'a X>`]. With this, the latter is implemented automatically for |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
22 | /// the reference, which can be sufficient to apply the operation in another blanket implementation. |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
23 | impl<'a, T, X> Apply<X> for &'a T where T : Apply<X> { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
24 | type Output = <T as Apply<X>>::Output; |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
25 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
26 | #[inline] |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
27 | fn apply(&self, x : X) -> Self::Output { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
28 | (*self).apply(x) |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
29 | } |
0 | 30 | } |
31 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
32 | /// A mapping from `Domain` to `Codomain`. |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
33 | /// |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
34 | /// This is automatically implemented when the relevant [`Apply`] are implemented. |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
35 | pub trait Mapping<Domain> : Apply<Domain, Output=Self::Codomain> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
36 | + for<'a> Apply<&'a Domain, Output=Self::Codomain> { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
37 | type Codomain; |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
38 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
39 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
40 | impl<Domain, Codomain, T> Mapping<Domain> for T |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
41 | where T : Apply<Domain, Output=Codomain> + for<'a> Apply<&'a Domain, Output=Codomain> { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
42 | type Codomain = Codomain; |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
43 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
44 | |
0 | 45 | |
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
46 | pub trait RealMapping<F : Float, const N : usize> |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
47 | : Mapping<Loc<F, N>, Codomain = F> {} |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
48 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
49 | impl<F : Float, T, const N : usize> RealMapping<F, N> for T |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
50 | where T : Mapping<Loc<F, N>, Codomain = F> {} |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
51 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
52 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
53 | /// Trait for calculation the differential of `Self` as a mathematical function on `X`. |
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
54 | pub trait Differentiable<X> : Sized { |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
55 | type Output; |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
56 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
57 | /// Compute the differential of `self` at `x`. |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
58 | fn differential(&self, x : X) -> Self::Output; |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
59 | } |
0 | 60 | |
5 | 61 | /// A differentiable mapping from `Domain` to [`Mapping::Codomain`], with differentials |
62 | /// `Differential`. | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
63 | /// |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
64 | /// This is automatically implemented when the relevant [`Differentiate`] are implemented. |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
65 | pub trait DifferentiableMapping<Domain> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
66 | : Mapping<Domain> |
29
7fd0984743b5
Rename Differentiate → Differentiable
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
67 | + Differentiable<Domain, Output=Self::Differential> |
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
68 | + for<'a> Differentiable<&'a Domain, Output=Self::Differential> { |
0 | 69 | type Differential; |
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
70 | |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
71 | /// Form the differential mapping of `self`. |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
72 | fn diff(self) -> Differential<Domain, Self> { |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
73 | Differential{ g : self, _space : PhantomData } |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
74 | } |
0 | 75 | } |
76 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
77 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
78 | impl<Domain, Differential, T> DifferentiableMapping<Domain> for T |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
79 | where T : Mapping<Domain> |
29
7fd0984743b5
Rename Differentiate → Differentiable
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
80 | + Differentiable<Domain, Output=Differential> |
7fd0984743b5
Rename Differentiate → Differentiable
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
81 | + for<'a> Differentiable<&'a Domain, Output=Differential> { |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
82 | type Differential = Differential; |
0 | 83 | } |
84 | ||
5 | 85 | /// A sum of [`Mapping`]s. |
0 | 86 | #[derive(Serialize, Debug, Clone)] |
87 | pub struct Sum<Domain, M : Mapping<Domain>> { | |
88 | components : Vec<M>, | |
89 | _domain : PhantomData<Domain>, | |
90 | } | |
91 | ||
5 | 92 | impl<Domain, M : Mapping<Domain>> Sum<Domain, M> { |
93 | /// Construct from an iterator. | |
94 | pub fn new<I : Iterator<Item = M>>(iter : I) -> Self { | |
95 | Sum { components : iter.collect(), _domain : PhantomData } | |
96 | } | |
97 | } | |
98 | ||
99 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
100 | impl<Domain : Copy, M> Apply<Domain> for Sum<Domain, M> |
0 | 101 | where M : Mapping<Domain>, |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
102 | M::Codomain : std::iter::Sum { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
103 | type Output = M::Codomain; |
0 | 104 | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
105 | fn apply(&self, x : Domain) -> Self::Output { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
106 | self.components.iter().map(|c| c.apply(x)).sum() |
0 | 107 | } |
108 | } | |
109 | ||
29
7fd0984743b5
Rename Differentiate → Differentiable
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
110 | impl<Domain, M> Differentiable<Domain> for Sum<Domain, M> |
0 | 111 | where M : DifferentiableMapping<Domain>, |
112 | M :: Codomain : std::iter::Sum, | |
113 | M :: Differential : std::iter::Sum, | |
114 | Domain : Copy { | |
115 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
116 | type Output = M::Differential; |
0 | 117 | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
118 | fn differential(&self, x : Domain) -> Self::Output { |
0 | 119 | self.components.iter().map(|c| c.differential(x)).sum() |
120 | } | |
121 | } | |
34
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
122 | |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
123 | /// Container for the differential [`Mapping`] of a [`Differentiable`] mapping. |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
124 | pub struct Differential<X, G : DifferentiableMapping<X>> { |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
125 | g : G, |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
126 | _space : PhantomData<X> |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
127 | } |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
128 | |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
129 | impl<X, G : DifferentiableMapping<X>> Apply<X> for Differential<X, G> { |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
130 | type Output = G::Differential; |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
131 | |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
132 | #[inline] |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
133 | fn apply(&self, x : X) -> G::Differential { |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
134 | self.g.differential(x) |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
135 | } |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
136 | } |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
137 | |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
138 | impl<'a, X, G : DifferentiableMapping<X>> Apply<&'a X> for Differential<X, G> { |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
139 | type Output = G::Differential; |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
140 | |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
141 | #[inline] |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
142 | fn apply(&self, x : &'a X) -> G::Differential { |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
143 | self.g.differential(x) |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
144 | } |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
145 | } |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
146 | |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
147 | #[inline] |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
148 | fn apply(&self, x : X) -> Self::Output { |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
149 | self.g.differential(x) |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
150 | } |
3dbc04100b09
Add Differential struct and DifferentiableMapping.diff
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
151 | } |