Tue, 18 Jul 2023 15:44:10 +0300
linearisation_error
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 | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
46 | /// A helper trait alias for referring to [`Mapping`]s from [`Loc<F, N>`] to `F` a [`Float`]. |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
47 | pub trait RealMapping<F : Float, const N : usize> : 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
|
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`. |
77 | 54 | pub trait Differentiable<X : Clone> : Apply<X> { |
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`. |
77 | 58 | fn differential(&self, x : X) -> <Self as Differentiable<X>>::Output; |
59 | ||
60 | /// Compute the linearisation error of `self` at `x` for `y`. | |
61 | fn linearisation_error(&self, x : X, y : X) -> <Self as Apply<X>>::Output { | |
62 | let z = x.clone(); | |
63 | self.linearisation_error_gen(x, y, z) | |
64 | } | |
65 | ||
66 | /// Compute the linearisation error of `self` at `x` for `y`, with | |
67 | /// derivative calculated at `z` | |
68 | fn linearisation_error_gen(&self, x : X, y : X, z : X) -> <Self as Apply<X>>::Output; | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
69 | } |
0 | 70 | |
5 | 71 | |
72 | /// A differentiable mapping from `Domain` to [`Mapping::Codomain`], with differentials | |
73 | /// `Differential`. | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
74 | /// |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
75 | /// This is automatically implemented when the relevant [`Differentiate`] are implemented. |
77 | 76 | pub trait DifferentiableMapping<Domain : Clone> |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
77 | : Mapping<Domain> |
29
7fd0984743b5
Rename Differentiate → Differentiable
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
78 | + Differentiable<Domain, Output=Self::Differential> |
77 | 79 | + for<'a> Differentiable<&'a Domain, Output=Self::Differential> { |
0 | 80 | type Differential; |
81 | } | |
82 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
83 | |
77 | 84 | impl<Domain : Clone, Differential, T> DifferentiableMapping<Domain> for T |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
85 | where T : Mapping<Domain> |
29
7fd0984743b5
Rename Differentiate → Differentiable
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
86 | + Differentiable<Domain, Output=Differential> |
7fd0984743b5
Rename Differentiate → Differentiable
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
87 | + 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
|
88 | type Differential = Differential; |
0 | 89 | } |
90 | ||
5 | 91 | /// A sum of [`Mapping`]s. |
0 | 92 | #[derive(Serialize, Debug, Clone)] |
93 | pub struct Sum<Domain, M : Mapping<Domain>> { | |
94 | components : Vec<M>, | |
95 | _domain : PhantomData<Domain>, | |
96 | } | |
97 | ||
5 | 98 | impl<Domain, M : Mapping<Domain>> Sum<Domain, M> { |
99 | /// Construct from an iterator. | |
100 | pub fn new<I : Iterator<Item = M>>(iter : I) -> Self { | |
101 | Sum { components : iter.collect(), _domain : PhantomData } | |
102 | } | |
103 | } | |
104 | ||
105 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
106 | impl<Domain : Copy, M> Apply<Domain> for Sum<Domain, M> |
0 | 107 | 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
|
108 | 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
|
109 | type Output = M::Codomain; |
0 | 110 | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
111 | 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
|
112 | self.components.iter().map(|c| c.apply(x)).sum() |
0 | 113 | } |
114 | } | |
115 | ||
29
7fd0984743b5
Rename Differentiate → Differentiable
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
116 | impl<Domain, M> Differentiable<Domain> for Sum<Domain, M> |
0 | 117 | where M : DifferentiableMapping<Domain>, |
118 | M :: Codomain : std::iter::Sum, | |
119 | M :: Differential : std::iter::Sum, | |
120 | Domain : Copy { | |
121 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
122 | type Output = M::Differential; |
0 | 123 | |
77 | 124 | fn differential(&self, x : Domain) -> M::Differential { |
125 | self.components | |
126 | .iter() | |
127 | .map(|c| c.differential(x)) | |
128 | .sum() | |
129 | } | |
130 | ||
131 | fn linearisation_error(&self, x : Domain, y : Domain) -> M::Codomain { | |
132 | self.components | |
133 | .iter() | |
134 | .map(|c| c.linearisation_error(x, y)) | |
135 | .sum() | |
136 | } | |
137 | ||
138 | fn linearisation_error_gen(&self, x : Domain, y : Domain, z : Domain) -> M::Codomain { | |
139 | self.components | |
140 | .iter() | |
141 | .map(|c| c.linearisation_error_gen(x, y, z)) | |
142 | .sum() | |
0 | 143 | } |
144 | } |