src/operator_arithmetic.rs

branch
dev
changeset 69
e5fab0125a8e
parent 68
c5f70e767511
child 75
e9f4550cfa18
equal deleted inserted replaced
68:c5f70e767511 69:e5fab0125a8e
29 pub weight : C, 29 pub weight : C,
30 /// The base [`Support`] or [`Apply`] being weighted. 30 /// The base [`Support`] or [`Apply`] being weighted.
31 pub base_fn : T, 31 pub base_fn : T,
32 } 32 }
33 33
34 impl<T, C> Weighted<T, C>
35 where
36 C : Constant,
37 {
38 /// Construct from an iterator.
39 pub fn new(weight : C, base_fn : T) -> Self {
40 Weighted{ weight, base_fn }
41 }
42 }
43
34 impl<'a, T, V, D, F, C> Mapping<D> for Weighted<T, C> 44 impl<'a, T, V, D, F, C> Mapping<D> for Weighted<T, C>
35 where 45 where
36 F : Float, 46 F : Float,
37 D : Space, 47 D : Space,
38 T : Mapping<D, Codomain=V>, 48 T : Mapping<D, Codomain=V>,
60 #[inline] 70 #[inline]
61 fn differential_impl<I : Instance<D>>(&self, x : I) -> Self::Derivative { 71 fn differential_impl<I : Instance<D>>(&self, x : I) -> Self::Derivative {
62 self.base_fn.differential(x) * self.weight.value() 72 self.base_fn.differential(x) * self.weight.value()
63 } 73 }
64 } 74 }
75
76 /// A sum of [`Mapping`]s.
77 #[derive(Serialize, Debug, Clone)]
78 pub struct MappingSum<M>(Vec<M>);
79
80 impl< M> MappingSum<M> {
81 /// Construct from an iterator.
82 pub fn new<I : IntoIterator<Item = M>>(iter : I) -> Self {
83 MappingSum(iter.into_iter().collect())
84 }
85
86 /// Iterate over the component functions of the sum
87 pub fn iter(&self) -> std::slice::Iter<'_, M> {
88 self.0.iter()
89 }
90 }
91
92 impl<Domain, M> Mapping<Domain> for MappingSum<M>
93 where
94 Domain : Space + Clone,
95 M : Mapping<Domain>,
96 M::Codomain : std::iter::Sum + Clone
97 {
98 type Codomain = M::Codomain;
99
100 fn apply<I : Instance<Domain>>(&self, x : I) -> Self::Codomain {
101 let xr = x.ref_instance();
102 self.0.iter().map(|c| c.apply(xr)).sum()
103 }
104 }
105
106 impl<Domain, M> DifferentiableImpl<Domain> for MappingSum< M>
107 where
108 Domain : Space + Clone,
109 M : DifferentiableMapping<Domain>,
110 M :: DerivativeDomain : std::iter::Sum
111 {
112 type Derivative = M::DerivativeDomain;
113
114 fn differential_impl<I : Instance<Domain>>(&self, x : I) -> Self::Derivative {
115 let xr = x.ref_instance();
116 self.0.iter().map(|c| c.differential(xr)).sum()
117 }
118 }

mercurial