src/mapping.rs

branch
dev
changeset 69
e5fab0125a8e
parent 68
c5f70e767511
child 75
e9f4550cfa18
equal deleted inserted replaced
68:c5f70e767511 69:e5fab0125a8e
125 fn diff_ref(&self) -> Differential<'_, Domain, Self> { 125 fn diff_ref(&self) -> Differential<'_, Domain, Self> {
126 Differential{ g : Cow::Borrowed(self), _space : PhantomData } 126 Differential{ g : Cow::Borrowed(self), _space : PhantomData }
127 } 127 }
128 } 128 }
129 129
130 /// A sum of [`Mapping`]s.
131 #[derive(Serialize, Debug, Clone)]
132 pub struct Sum<Domain, M> {
133 components : Vec<M>,
134 _domain : PhantomData<Domain>,
135 }
136
137 impl<Domain, M> Sum<Domain, M> {
138 /// Construct from an iterator.
139 pub fn new<I : Iterator<Item = M>>(iter : I) -> Self {
140 Sum { components : iter.collect(), _domain : PhantomData }
141 }
142
143 /// Iterate over the component functions of the sum
144 pub fn iter(&self) -> std::slice::Iter<'_, M> {
145 self.components.iter()
146 }
147 }
148
149
150 impl<Domain, M> Mapping<Domain> for Sum<Domain, M>
151 where
152 Domain : Space + Clone,
153 M : Mapping<Domain>,
154 M::Codomain : std::iter::Sum + Clone
155 {
156 type Codomain = M::Codomain;
157
158 fn apply<I : Instance<Domain>>(&self, x : I) -> Self::Codomain {
159 let xr = x.ref_instance();
160 self.components.iter().map(|c| c.apply(xr)).sum()
161 }
162 }
163
164 impl<Domain, M> DifferentiableImpl<Domain> for Sum<Domain, M>
165 where
166 Domain : Space + Clone,
167 M : DifferentiableMapping<Domain>,
168 M :: DerivativeDomain : std::iter::Sum
169 {
170 type Derivative = M::DerivativeDomain;
171
172 fn differential_impl<I : Instance<Domain>>(&self, x : I) -> Self::Derivative {
173 let xr = x.ref_instance();
174 self.components.iter().map(|c| c.differential(xr)).sum()
175 }
176 }
177 130
178 /// Container for the differential [`Mapping`] of a [`Differentiable`] mapping. 131 /// Container for the differential [`Mapping`] of a [`Differentiable`] mapping.
179 pub struct Differential<'a, X, G : Clone> { 132 pub struct Differential<'a, X, G : Clone> {
180 g : Cow<'a, G>, 133 g : Cow<'a, G>,
181 _space : PhantomData<X> 134 _space : PhantomData<X>

mercurial