| 49 impl<F : Float, T, const N : usize> RealMapping<F, N> for T |
49 impl<F : Float, T, const N : usize> RealMapping<F, N> for T |
| 50 where T : Mapping<Loc<F, N>, Codomain = F> {} |
50 where T : Mapping<Loc<F, N>, Codomain = F> {} |
| 51 |
51 |
| 52 |
52 |
| 53 /// Trait for calculation the differential of `Self` as a mathematical function on `X`. |
53 /// Trait for calculation the differential of `Self` as a mathematical function on `X`. |
| 54 pub trait Differentiable<X> { |
54 pub trait Differentiable<X : Clone> : Apply<X> { |
| 55 type Output; |
55 type Output; |
| 56 |
56 |
| 57 /// Compute the differential of `self` at `x`. |
57 /// Compute the differential of `self` at `x`. |
| 58 fn differential(&self, x : X) -> Self::Output; |
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; |
| 59 } |
69 } |
| 60 |
70 |
| 61 |
71 |
| 62 /// A differentiable mapping from `Domain` to [`Mapping::Codomain`], with differentials |
72 /// A differentiable mapping from `Domain` to [`Mapping::Codomain`], with differentials |
| 63 /// `Differential`. |
73 /// `Differential`. |
| 64 /// |
74 /// |
| 65 /// This is automatically implemented when the relevant [`Differentiate`] are implemented. |
75 /// This is automatically implemented when the relevant [`Differentiate`] are implemented. |
| 66 pub trait DifferentiableMapping<Domain> |
76 pub trait DifferentiableMapping<Domain : Clone> |
| 67 : Mapping<Domain> |
77 : Mapping<Domain> |
| 68 + Differentiable<Domain, Output=Self::Differential> |
78 + Differentiable<Domain, Output=Self::Differential> |
| 69 + for<'a> Differentiable<&'a Domain, Output=Self::Differential>{ |
79 + for<'a> Differentiable<&'a Domain, Output=Self::Differential> { |
| 70 type Differential; |
80 type Differential; |
| 71 } |
81 } |
| 72 |
82 |
| 73 |
83 |
| 74 impl<Domain, Differential, T> DifferentiableMapping<Domain> for T |
84 impl<Domain : Clone, Differential, T> DifferentiableMapping<Domain> for T |
| 75 where T : Mapping<Domain> |
85 where T : Mapping<Domain> |
| 76 + Differentiable<Domain, Output=Differential> |
86 + Differentiable<Domain, Output=Differential> |
| 77 + for<'a> Differentiable<&'a Domain, Output=Differential> { |
87 + for<'a> Differentiable<&'a Domain, Output=Differential> { |
| 78 type Differential = Differential; |
88 type Differential = Differential; |
| 79 } |
89 } |
| 109 M :: Differential : std::iter::Sum, |
119 M :: Differential : std::iter::Sum, |
| 110 Domain : Copy { |
120 Domain : Copy { |
| 111 |
121 |
| 112 type Output = M::Differential; |
122 type Output = M::Differential; |
| 113 |
123 |
| 114 fn differential(&self, x : Domain) -> Self::Output { |
124 fn differential(&self, x : Domain) -> M::Differential { |
| 115 self.components.iter().map(|c| c.differential(x)).sum() |
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() |
| 116 } |
143 } |
| 117 } |
144 } |