66 where T : Mapping<Loc<F, N>, Codomain = Loc<F, M>> {} |
66 where T : Mapping<Loc<F, N>, Codomain = Loc<F, M>> {} |
67 |
67 |
68 |
68 |
69 /// Trait for calculation the differential of `Self` as a mathematical function on `X`. |
69 /// Trait for calculation the differential of `Self` as a mathematical function on `X`. |
70 pub trait Differentiable<X> : Sized { |
70 pub trait Differentiable<X> : Sized { |
71 type Output; |
71 type Derivative; |
72 |
72 |
73 /// Compute the differential of `self` at `x`. |
73 /// Compute the differential of `self` at `x`. |
74 fn differential(&self, x : X) -> Self::Output; |
74 fn differential(&self, x : X) -> Self::Derivative; |
75 } |
75 } |
76 |
76 |
77 /// A differentiable mapping from `Domain` to [`Mapping::Codomain`], with differentials |
77 /// A differentiable mapping from `Domain` to [`Mapping::Codomain`], with differentials |
78 /// `Differential`. |
78 /// `Differential`. |
79 /// |
79 /// |
80 /// This is automatically implemented when the relevant [`Differentiate`] are implemented. |
80 /// This is automatically implemented when the relevant [`Differentiate`] are implemented. |
81 pub trait DifferentiableMapping<Domain> |
81 pub trait DifferentiableMapping<Domain> |
82 : Mapping<Domain> |
82 : Mapping<Domain> |
83 + Differentiable<Domain, Output=Self::Differential> |
83 + Differentiable<Domain, Derivative=Self::Differential> |
84 + for<'a> Differentiable<&'a Domain, Output=Self::Differential> { |
84 + for<'a> Differentiable<&'a Domain, Derivative=Self::Differential> { |
85 type Differential; |
85 type Differential; |
86 |
86 |
87 /// Form the differential mapping of `self`. |
87 /// Form the differential mapping of `self`. |
88 fn diff(self) -> Differential<Domain, Self> { |
88 fn diff(self) -> Differential<Domain, Self> { |
89 Differential{ g : self, _space : PhantomData } |
89 Differential{ g : self, _space : PhantomData } |
96 } |
96 } |
97 |
97 |
98 |
98 |
99 impl<Domain, Differential, T> DifferentiableMapping<Domain> for T |
99 impl<Domain, Differential, T> DifferentiableMapping<Domain> for T |
100 where T : Mapping<Domain> |
100 where T : Mapping<Domain> |
101 + Differentiable<Domain, Output=Differential> |
101 + Differentiable<Domain, Derivative=Differential> |
102 + for<'a> Differentiable<&'a Domain, Output=Differential> { |
102 + for<'a> Differentiable<&'a Domain, Derivative=Differential> { |
103 type Differential = Differential; |
103 type Differential = Differential; |
104 } |
104 } |
105 |
105 |
106 /// A sum of [`Mapping`]s. |
106 /// A sum of [`Mapping`]s. |
107 #[derive(Serialize, Debug, Clone)] |
107 #[derive(Serialize, Debug, Clone)] |
147 where M : DifferentiableMapping<Domain>, |
147 where M : DifferentiableMapping<Domain>, |
148 M :: Codomain : std::iter::Sum, |
148 M :: Codomain : std::iter::Sum, |
149 M :: Differential : std::iter::Sum, |
149 M :: Differential : std::iter::Sum, |
150 Domain : Copy { |
150 Domain : Copy { |
151 |
151 |
152 type Output = M::Differential; |
152 type Derivative = M::Differential; |
153 |
153 |
154 fn differential(&self, x : Domain) -> Self::Output { |
154 fn differential(&self, x : Domain) -> Self::Derivative { |
155 self.components.iter().map(|c| c.differential(x)).sum() |
155 self.components.iter().map(|c| c.differential(x)).sum() |
156 } |
156 } |
157 } |
157 } |
158 |
158 |
159 /// Container for the differential [`Mapping`] of a [`Differentiable`] mapping. |
159 /// Container for the differential [`Mapping`] of a [`Differentiable`] mapping. |
271 self.0.apply(x) |
271 self.0.apply(x) |
272 } |
272 } |
273 } |
273 } |
274 |
274 |
275 impl<'g, X, G : Differentiable<X>> Differentiable<X> for Ref<'g, G> { |
275 impl<'g, X, G : Differentiable<X>> Differentiable<X> for Ref<'g, G> { |
276 type Output = G::Output; |
276 type Derivative = G::Derivative; |
277 #[inline] |
277 #[inline] |
278 fn differential(&self, x : X) -> Self::Output { |
278 fn differential(&self, x : X) -> Self::Derivative { |
279 self.0.differential(x) |
279 self.0.differential(x) |
280 } |
280 } |
281 } |
281 } |