| 81 Self: 'b; |
81 Self: 'b; |
| 82 |
82 |
| 83 /// Calculate differential at `x` |
83 /// Calculate differential at `x` |
| 84 fn differential<I: Instance<Domain>>(&self, x: I) -> Self::DerivativeDomain; |
84 fn differential<I: Instance<Domain>>(&self, x: I) -> Self::DerivativeDomain; |
| 85 |
85 |
| |
86 /// Calculate differential and value at `x` |
| |
87 fn apply_and_differential<I: Instance<Domain>>( |
| |
88 &self, |
| |
89 x: I, |
| |
90 ) -> (Self::Codomain, Self::DerivativeDomain) { |
| |
91 x.eval_ref(|xo| (self.apply(xo), self.differential(xo))) |
| |
92 } |
| |
93 |
| 86 /// Form the differential mapping of `self`. |
94 /// Form the differential mapping of `self`. |
| 87 fn diff(self) -> Self::Differential<'static>; |
95 fn diff(self) -> Self::Differential<'static>; |
| 88 |
96 |
| 89 /// Form the differential mapping of `self`. |
97 /// Form the differential mapping of `self`. |
| 90 fn diff_ref(&self) -> Self::Differential<'_>; |
98 fn diff_ref(&self) -> Self::Differential<'_>; |
| 106 pub trait DifferentiableImpl<X: Space>: Sized { |
114 pub trait DifferentiableImpl<X: Space>: Sized { |
| 107 type Derivative: ClosedSpace; |
115 type Derivative: ClosedSpace; |
| 108 |
116 |
| 109 /// Compute the differential of `self` at `x`, consuming the input. |
117 /// Compute the differential of `self` at `x`, consuming the input. |
| 110 fn differential_impl<I: Instance<X>>(&self, x: I) -> Self::Derivative; |
118 fn differential_impl<I: Instance<X>>(&self, x: I) -> Self::Derivative; |
| |
119 |
| |
120 fn apply_and_differential_impl<I: Instance<X>>( |
| |
121 &self, |
| |
122 x: I, |
| |
123 ) -> (Self::Codomain, Self::Derivative) |
| |
124 where |
| |
125 Self: Mapping<X>, |
| |
126 { |
| |
127 x.eval_ref(|xo| (self.apply(xo), self.differential_impl(xo))) |
| |
128 } |
| 111 } |
129 } |
| 112 |
130 |
| 113 impl<T, Domain> DifferentiableMapping<Domain> for T |
131 impl<T, Domain> DifferentiableMapping<Domain> for T |
| 114 where |
132 where |
| 115 Domain: Space, |
133 Domain: Space, |
| 122 Self: 'b; |
140 Self: 'b; |
| 123 |
141 |
| 124 #[inline] |
142 #[inline] |
| 125 fn differential<I: Instance<Domain>>(&self, x: I) -> Self::DerivativeDomain { |
143 fn differential<I: Instance<Domain>>(&self, x: I) -> Self::DerivativeDomain { |
| 126 self.differential_impl(x) |
144 self.differential_impl(x) |
| |
145 } |
| |
146 |
| |
147 #[inline] |
| |
148 fn apply_and_differential<I: Instance<Domain>>( |
| |
149 &self, |
| |
150 x: I, |
| |
151 ) -> (T::Codomain, Self::DerivativeDomain) { |
| |
152 self.apply_and_differential_impl(x) |
| 127 } |
153 } |
| 128 |
154 |
| 129 fn diff(self) -> Differential<'static, Domain, Self> { |
155 fn diff(self) -> Differential<'static, Domain, Self> { |
| 130 Differential { g: MyCow::Owned(self), _space: PhantomData } |
156 Differential { g: MyCow::Owned(self), _space: PhantomData } |
| 131 } |
157 } |