Tue, 31 Dec 2024 08:48:50 -0500
Split out and generalise Weighted
5 | 1 | |
2 | use std::iter::Chain; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
3 | use std::sync::Arc; |
0 | 4 | |
5 | use crate::types::*; | |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
6 | use crate::mapping::{ |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
7 | Instance, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
8 | Mapping, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
9 | DifferentiableImpl, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
10 | DifferentiableMapping, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
11 | Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
12 | }; |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
13 | use crate::iter::{Mappable, MapF, MapZ}; |
5 | 14 | use crate::sets::Cube; |
15 | use crate::loc::Loc; | |
16 | ||
0 | 17 | use super::support::*; |
18 | use super::aggregator::*; | |
19 | ||
5 | 20 | /// A structure for storing two [`SupportGenerator`]s summed/chain together. |
21 | /// | |
22 | /// This is needed to work with sums of different types of [`Support`]s. | |
0 | 23 | #[derive(Debug,Clone)] |
24 | pub struct BothGenerators<A, B>( | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
25 | pub(super) Arc<A>, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
26 | pub(super) Arc<B>, |
0 | 27 | ); |
28 | ||
5 | 29 | /// A structure for a [`Support`] that can be either `A` or `B`. |
30 | /// | |
31 | /// This is needed to work with sums of different types of [`Support`]s. | |
0 | 32 | #[derive(Debug,Clone)] |
33 | pub enum EitherSupport<A, B> { | |
34 | Left(A), | |
35 | Right(B), | |
36 | } | |
37 | ||
38 | // We need type alias bounds to access associate types. | |
39 | #[allow(type_alias_bounds)] | |
40 | type BothAllDataIter< | |
41 | 'a, F, | |
42 | G1 : SupportGenerator<F, N>, | |
43 | G2 : SupportGenerator<F, N>, | |
44 | const N : usize | |
45 | > = Chain< | |
46 | MapF<G1::AllDataIter<'a>, (usize, EitherSupport<G1::SupportType, G2::SupportType>)>, | |
47 | MapZ<G2::AllDataIter<'a>, usize, (usize, EitherSupport<G1::SupportType, G2::SupportType>)>, | |
48 | >; | |
49 | ||
50 | impl<G1, G2> BothGenerators<G1, G2> { | |
5 | 51 | /// Helper for [`all_left_data`]. |
0 | 52 | #[inline] |
53 | fn map_left<F : Float, const N : usize>((d, support) : (G1::Id, G1::SupportType)) | |
54 | -> (usize, EitherSupport<G1::SupportType, G2::SupportType>) | |
55 | where G1 : SupportGenerator<F, N, Id=usize>, | |
56 | G2 : SupportGenerator<F, N, Id=usize> { | |
57 | ||
58 | let id : usize = d.into(); | |
59 | (id.into(), EitherSupport::Left(support)) | |
60 | } | |
61 | ||
5 | 62 | /// Helper for [`all_right_data`]. |
0 | 63 | #[inline] |
64 | fn map_right<F : Float, const N : usize>(n0 : &usize, (d, support) : (G2::Id, G2::SupportType)) | |
65 | -> (usize, EitherSupport<G1::SupportType, G2::SupportType>) | |
66 | where G1 : SupportGenerator<F, N, Id=usize>, | |
67 | G2 : SupportGenerator<F, N, Id=usize> { | |
68 | ||
69 | let id : usize = d.into(); | |
70 | ((n0+id).into(), EitherSupport::Right(support)) | |
71 | } | |
72 | ||
5 | 73 | /// Calls [`SupportGenerator::all_data`] on the “left” support generator. |
74 | /// | |
75 | /// Converts both the id and the [`Support`] into a form that corresponds to `BothGenerators`. | |
0 | 76 | #[inline] |
77 | pub(super) fn all_left_data<F : Float, const N : usize>(&self) | |
78 | -> MapF<G1::AllDataIter<'_>, (usize, EitherSupport<G1::SupportType, G2::SupportType>)> | |
79 | where G1 : SupportGenerator<F, N, Id=usize>, | |
80 | G2 : SupportGenerator<F, N, Id=usize> { | |
81 | self.0.all_data().mapF(Self::map_left) | |
82 | } | |
83 | ||
5 | 84 | /// Calls [`SupportGenerator::all_data`] on the “right” support generator. |
85 | /// | |
86 | /// Converts both the id and the [`Support`] into a form that corresponds to `BothGenerators`. | |
0 | 87 | #[inline] |
88 | pub(super) fn all_right_data<F : Float, const N : usize>(&self) | |
89 | -> MapZ<G2::AllDataIter<'_>, usize, (usize, EitherSupport<G1::SupportType, G2::SupportType>)> | |
90 | where G1 : SupportGenerator<F, N, Id=usize>, | |
91 | G2 : SupportGenerator<F, N, Id=usize> { | |
92 | let n0 = self.0.support_count(); | |
93 | self.1.all_data().mapZ(n0, Self::map_right) | |
94 | } | |
95 | } | |
96 | ||
97 | impl<F : Float, G1, G2, const N : usize> | |
98 | SupportGenerator<F, N> | |
99 | for BothGenerators<G1, G2> | |
100 | where G1 : SupportGenerator<F, N, Id=usize>, | |
101 | G2 : SupportGenerator<F, N, Id=usize> { | |
102 | ||
103 | type Id = usize; | |
104 | type SupportType = EitherSupport<G1::SupportType, G2::SupportType>; | |
105 | type AllDataIter<'a> = BothAllDataIter<'a, F, G1, G2, N> where G1 : 'a, G2 : 'a; | |
106 | ||
107 | #[inline] | |
108 | fn support_for(&self, id : Self::Id) | |
109 | -> Self::SupportType { | |
110 | let n0 = self.0.support_count(); | |
111 | if id < n0 { | |
112 | EitherSupport::Left(self.0.support_for(id.into())) | |
113 | } else { | |
114 | EitherSupport::Right(self.1.support_for((id-n0).into())) | |
115 | } | |
116 | } | |
117 | ||
118 | #[inline] | |
119 | fn support_count(&self) -> usize { | |
120 | self.0.support_count() + self.1.support_count() | |
121 | } | |
122 | ||
123 | #[inline] | |
124 | fn all_data(&self) -> Self::AllDataIter<'_> { | |
125 | self.all_left_data().chain(self.all_right_data()) | |
126 | } | |
127 | } | |
128 | ||
129 | impl<F: Float, S1, S2, const N : usize> Support<F, N> for EitherSupport<S1, S2> | |
130 | where S1 : Support<F, N>, | |
131 | S2 : Support<F, N> { | |
132 | ||
133 | #[inline] | |
134 | fn support_hint(&self) -> Cube<F,N> { | |
135 | match self { | |
136 | EitherSupport::Left(ref a) => a.support_hint(), | |
137 | EitherSupport::Right(ref b) => b.support_hint(), | |
138 | } | |
139 | } | |
140 | ||
141 | #[inline] | |
142 | fn in_support(&self, x : &Loc<F,N>) -> bool { | |
143 | match self { | |
144 | EitherSupport::Left(ref a) => a.in_support(x), | |
145 | EitherSupport::Right(ref b) => b.in_support(x), | |
146 | } | |
147 | } | |
148 | ||
149 | #[inline] | |
150 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
151 | match self { | |
152 | EitherSupport::Left(ref a) => a.bisection_hint(cube), | |
153 | EitherSupport::Right(ref b) => b.bisection_hint(cube), | |
154 | } | |
155 | } | |
156 | } | |
157 | ||
158 | impl<F : Float, A, S1, S2, const N : usize> LocalAnalysis<F, A, N> for EitherSupport<S1, S2> | |
159 | where A : Aggregator, | |
160 | S1 : LocalAnalysis<F, A, N>, | |
161 | S2 : LocalAnalysis<F, A, N>, { | |
162 | ||
163 | #[inline] | |
164 | fn local_analysis(&self, cube : &Cube<F, N>) -> A { | |
165 | match self { | |
166 | EitherSupport::Left(ref a) => a.local_analysis(cube), | |
167 | EitherSupport::Right(ref b) => b.local_analysis(cube), | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
172 | impl<F : Float, A, S1, S2> GlobalAnalysis<F, A> for EitherSupport<S1, S2> | |
173 | where A : Aggregator, | |
174 | S1 : GlobalAnalysis<F, A>, | |
175 | S2 : GlobalAnalysis<F, A>, { | |
176 | ||
177 | #[inline] | |
178 | fn global_analysis(&self) -> A { | |
179 | match self { | |
180 | EitherSupport::Left(ref a) => a.global_analysis(), | |
181 | EitherSupport::Right(ref b) => b.global_analysis(), | |
182 | } | |
183 | } | |
184 | } | |
185 | ||
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
186 | impl<F, S1, S2, X> Mapping<X> for EitherSupport<S1, S2> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
187 | where |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
188 | F : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
189 | X : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
190 | S1 : Mapping<X, Codomain=F>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
191 | S2 : Mapping<X, Codomain=F>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
192 | { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
193 | type Codomain = F; |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
194 | |
0 | 195 | #[inline] |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
196 | fn apply<I : Instance<X>>(&self, x : I) -> F { |
0 | 197 | match self { |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
198 | EitherSupport::Left(ref a) => a.apply(x), |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
199 | EitherSupport::Right(ref b) => b.apply(x), |
0 | 200 | } |
201 | } | |
202 | } | |
203 | ||
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
204 | impl<X, S1, S2, O> DifferentiableImpl<X> for EitherSupport<S1, S2> |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
205 | where |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
206 | O : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
207 | X : Space, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
208 | S1 : DifferentiableMapping<X, DerivativeDomain=O>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
209 | S2 : DifferentiableMapping<X, DerivativeDomain=O>, |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
210 | { |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
211 | type Derivative = O; |
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
212 | |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
213 | #[inline] |
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
214 | fn differential_impl<I : Instance<X>>(&self, x : I) -> O { |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
215 | match self { |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
216 | EitherSupport::Left(ref a) => a.differential(x), |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
217 | EitherSupport::Right(ref b) => b.differential(x), |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
218 | } |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
219 | } |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
220 | } |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
221 | |
0 | 222 | macro_rules! make_either_scalarop_rhs { |
223 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
224 | impl<F : Float, G1, G2> | |
225 | std::ops::$trait_assign<F> | |
226 | for BothGenerators<G1, G2> | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
227 | where G1 : std::ops::$trait_assign<F> + Clone, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
228 | G2 : std::ops::$trait_assign<F> + Clone, { |
0 | 229 | #[inline] |
230 | fn $fn_assign(&mut self, t : F) { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
231 | Arc::make_mut(&mut self.0).$fn_assign(t); |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
232 | Arc::make_mut(&mut self.1).$fn_assign(t); |
0 | 233 | } |
234 | } | |
235 | ||
236 | impl<'a, F : Float, G1, G2> | |
237 | std::ops::$trait<F> | |
238 | for &'a BothGenerators<G1, G2> | |
239 | where &'a G1 : std::ops::$trait<F,Output=G1>, | |
240 | &'a G2 : std::ops::$trait<F,Output=G2> { | |
241 | type Output = BothGenerators<G1, G2>; | |
242 | #[inline] | |
243 | fn $fn(self, t : F) -> BothGenerators<G1, G2> { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
244 | BothGenerators(Arc::new(self.0.$fn(t)), |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
245 | Arc::new(self.1.$fn(t))) |
0 | 246 | } |
247 | } | |
248 | } | |
249 | } | |
250 | ||
251 | make_either_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
252 | make_either_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
253 | ||
254 | impl<G1, G2> std::ops::Neg for BothGenerators<G1, G2> | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
255 | where G1 : std::ops::Neg + Clone, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
256 | G2 : std::ops::Neg + Clone, { |
0 | 257 | type Output = BothGenerators<G1::Output, G2::Output>; |
258 | #[inline] | |
259 | fn neg(self) -> Self::Output { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
260 | BothGenerators(Arc::new(Arc::unwrap_or_clone(self.0).neg()), |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
261 | Arc::new(Arc::unwrap_or_clone(self.1).neg())) |
0 | 262 | } |
263 | } | |
264 | /* | |
265 | impl<'a, G1, G2> std::ops::Neg for &'a BothGenerators<G1, G2> | |
266 | where &'a G1 : std::ops::Neg, &'a G2 : std::ops::Neg, { | |
267 | type Output = BothGenerators<<&'a G1 as std::ops::Neg>::Output, | |
268 | <&'a G2 as std::ops::Neg>::Output>; | |
269 | fn neg(self) -> Self::Output { | |
270 | BothGenerators(self.0.neg(), self.1.neg()) | |
271 | } | |
272 | } | |
273 | */ |