| 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 Differentiate<X> { |
54 pub trait Differentiable<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::Output; |
| 59 } |
59 } |
| 63 /// `Differential`. |
63 /// `Differential`. |
| 64 /// |
64 /// |
| 65 /// This is automatically implemented when the relevant [`Differentiate`] are implemented. |
65 /// This is automatically implemented when the relevant [`Differentiate`] are implemented. |
| 66 pub trait DifferentiableMapping<Domain> |
66 pub trait DifferentiableMapping<Domain> |
| 67 : Mapping<Domain> |
67 : Mapping<Domain> |
| 68 + Differentiate<Domain, Output=Self::Differential> |
68 + Differentiable<Domain, Output=Self::Differential> |
| 69 + for<'a> Differentiate<&'a Domain, Output=Self::Differential>{ |
69 + for<'a> Differentiable<&'a Domain, Output=Self::Differential>{ |
| 70 type Differential; |
70 type Differential; |
| 71 } |
71 } |
| 72 |
72 |
| 73 |
73 |
| 74 impl<Domain, Differential, T> DifferentiableMapping<Domain> for T |
74 impl<Domain, Differential, T> DifferentiableMapping<Domain> for T |
| 75 where T : Mapping<Domain> |
75 where T : Mapping<Domain> |
| 76 + Differentiate<Domain, Output=Differential> |
76 + Differentiable<Domain, Output=Differential> |
| 77 + for<'a> Differentiate<&'a Domain, Output=Differential> { |
77 + for<'a> Differentiable<&'a Domain, Output=Differential> { |
| 78 type Differential = Differential; |
78 type Differential = Differential; |
| 79 } |
79 } |
| 80 |
80 |
| 81 /// A sum of [`Mapping`]s. |
81 /// A sum of [`Mapping`]s. |
| 82 #[derive(Serialize, Debug, Clone)] |
82 #[derive(Serialize, Debug, Clone)] |
| 101 fn apply(&self, x : Domain) -> Self::Output { |
101 fn apply(&self, x : Domain) -> Self::Output { |
| 102 self.components.iter().map(|c| c.apply(x)).sum() |
102 self.components.iter().map(|c| c.apply(x)).sum() |
| 103 } |
103 } |
| 104 } |
104 } |
| 105 |
105 |
| 106 impl<Domain, M> Differentiate<Domain> for Sum<Domain, M> |
106 impl<Domain, M> Differentiable<Domain> for Sum<Domain, M> |
| 107 where M : DifferentiableMapping<Domain>, |
107 where M : DifferentiableMapping<Domain>, |
| 108 M :: Codomain : std::iter::Sum, |
108 M :: Codomain : std::iter::Sum, |
| 109 M :: Differential : std::iter::Sum, |
109 M :: Differential : std::iter::Sum, |
| 110 Domain : Copy { |
110 Domain : Copy { |
| 111 |
111 |