Tue, 31 Dec 2024 09:12:43 -0500
Try to have Field as member type in Mappings etc.
0 | 1 | |
2 | use numeric_literals::replace_float_literals; | |
3 | use std::iter::Sum; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
4 | use std::sync::Arc; |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
5 | use crate::types::{Float, HasScalarField, HasRealField}; |
29
7fd0984743b5
Rename Differentiate → Differentiable
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
6 | use crate::mapping::{Apply, Mapping, Differentiable}; |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
7 | //use crate::linops::{Apply, Linear}; |
0 | 8 | use crate::sets::Set; |
5 | 9 | use crate::sets::Cube; |
10 | use crate::loc::Loc; | |
0 | 11 | use super::support::*; |
12 | use super::bt::*; | |
13 | use super::refine::*; | |
14 | use super::aggregator::*; | |
15 | use super::either::*; | |
16 | use crate::fe_model::base::RealLocalModel; | |
17 | use crate::fe_model::p2_local_model::*; | |
18 | ||
5 | 19 | /// Presentation for (mathematical) functions constructed as a sum of components functions with |
20 | /// typically small support. | |
21 | /// | |
22 | /// The domain of the function is [`Loc`]`<F, N>`, where `F` is the type of floating point numbers, | |
23 | /// and `N` the dimension. | |
24 | /// | |
25 | /// The `generator` lists the component functions that have to implement [`Support`]. | |
26 | /// Identifiers of the components ([`SupportGenerator::Id`], usually `usize`) are stored stored | |
27 | /// in a [bisection tree][BTImpl], when one is provided as `bt`. However `bt` may also be `()` | |
28 | /// for a [`PreBTFN`] that is only useful for vector space operations with a full [`BTFN`]. | |
0 | 29 | #[derive(Clone,Debug)] |
30 | pub struct BTFN< | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
31 | G : SupportGenerator<N>, |
0 | 32 | BT /*: BTImpl<F, N>*/, |
33 | const N : usize | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
34 | > { |
0 | 35 | bt : BT, |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
36 | generator : Arc<G>, |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
37 | } |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
38 | |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
39 | impl<G : SupportGenerator<N>, BT, const N : usize> HasScalarField for BTFN<G, BT, N> { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
40 | type Field = G::Field; |
0 | 41 | } |
42 | ||
43 | impl<F : Float, G, BT, const N : usize> | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
44 | BTFN<G, BT, N> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
45 | where G : SupportGenerator<N, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
46 | G::SupportType : LocalAnalysis<BT::Agg, Cube<F, N>>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
47 | BT : BTImpl<F, N, Data=G::Id> { |
0 | 48 | |
5 | 49 | /// Create a new BTFN from a support generator and a pre-initialised bisection tree. |
50 | /// | |
51 | /// The bisection tree `bt` should be pre-initialised to correspond to the `generator`. | |
52 | /// Use [`Self::construct`] if no preinitialised tree is available. Use [`Self::new_refresh`] | |
53 | /// when the aggregators of the tree may need updates. | |
54 | /// | |
55 | /// See the documentation for [`BTFN`] on the role of the `generator`. | |
0 | 56 | pub fn new(bt : BT, generator : G) -> Self { |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
57 | Self::new_arc(bt, Arc::new(generator)) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
58 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
59 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
60 | fn new_arc(bt : BT, generator : Arc<G>) -> Self { |
0 | 61 | BTFN { |
62 | bt : bt, | |
63 | generator : generator, | |
64 | } | |
65 | } | |
66 | ||
5 | 67 | /// Create a new BTFN support generator and a pre-initialised bisection tree, |
68 | /// cloning the tree and refreshing aggregators. | |
69 | /// | |
70 | /// The bisection tree `bt` should be pre-initialised to correspond to the `generator`, but | |
71 | /// the aggregator may be out of date. | |
72 | /// | |
73 | /// See the documentation for [`BTFN`] on the role of the `generator`. | |
0 | 74 | pub fn new_refresh(bt : &BT, generator : G) -> Self { |
75 | // clone().refresh_aggregator(…) as opposed to convert_aggregator | |
76 | // ensures that type is maintained. Due to Rc-pointer copy-on-write, | |
77 | // the effort is not significantly different. | |
78 | let mut btnew = bt.clone(); | |
79 | btnew.refresh_aggregator(&generator); | |
80 | BTFN::new(btnew, generator) | |
81 | } | |
82 | ||
5 | 83 | /// Create a new BTFN from a support generator, domain, and depth for a new [`BT`]. |
84 | /// | |
85 | /// The top node of the created [`BT`] will have the given `domain`. | |
86 | /// | |
87 | /// See the documentation for [`BTFN`] on the role of the `generator`. | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
88 | pub fn construct(domain : Cube<G::Field, N>, depth : BT::Depth, generator : G) -> Self { |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
89 | Self::construct_arc(domain, depth, Arc::new(generator)) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
90 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
91 | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
92 | fn construct_arc(domain : Cube<G::Field, N>, depth : BT::Depth, generator : Arc<G>) -> Self { |
0 | 93 | let mut bt = BT::new(domain, depth); |
94 | for (d, support) in generator.all_data() { | |
95 | bt.insert(d, &support); | |
96 | } | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
97 | Self::new_arc(bt, generator) |
0 | 98 | } |
99 | ||
5 | 100 | /// Convert the aggregator of the [`BTFN`] to a different one. |
101 | /// | |
102 | /// This will construct a [`BTFN`] with the same components and generator as the (consumed) | |
103 | /// `self`, but a new `BT` with [`Aggregator`]s of type `ANew`. | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
104 | pub fn convert_aggregator<ANew>(self) -> BTFN<G, BT::Converted<ANew>, N> |
0 | 105 | where ANew : Aggregator, |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
106 | G : SupportGenerator<N>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
107 | G::SupportType : LocalAnalysis<ANew, Cube<G::RealField, N>> { |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
108 | BTFN::new_arc(self.bt.convert_aggregator(&*self.generator), self.generator) |
0 | 109 | } |
110 | ||
111 | /// Change the generator (after, e.g., a scaling of the latter). | |
5 | 112 | fn new_generator(&self, generator : G) -> Self { |
0 | 113 | BTFN::new_refresh(&self.bt, generator) |
114 | } | |
115 | ||
116 | /// Refresh aggregator after updates to generator | |
117 | fn refresh_aggregator(&mut self) { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
118 | self.bt.refresh_aggregator(&*self.generator); |
0 | 119 | } |
120 | ||
121 | } | |
122 | ||
5 | 123 | impl<F : Float, G, BT, const N : usize> |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
124 | BTFN<G, BT, N> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
125 | where G : SupportGenerator<N, RealField = F> { |
5 | 126 | /// Change the [bisection tree][BTImpl] of the [`BTFN`] to a different one. |
127 | /// | |
128 | /// This can be used to convert a [`PreBTFN`] to a full [`BTFN`], or the change | |
129 | /// the aggreagator; see also [`self.convert_aggregator`]. | |
130 | pub fn instantiate< | |
131 | BTNew : BTImpl<F, N, Data=G::Id>, | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
132 | > (self, domain : Cube<F, N>, depth : BTNew::Depth) -> BTFN<G, BTNew, N> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
133 | where G::SupportType : LocalAnalysis<BTNew::Agg, Cube<F, N>> { |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
134 | BTFN::construct_arc(domain, depth, self.generator) |
5 | 135 | } |
136 | } | |
137 | ||
138 | /// A BTFN with no bisection tree. | |
139 | /// | |
140 | /// Most BTFN methods are not available, but if a BTFN is going to be summed with another | |
141 | /// before other use, it will be more efficient to not construct an unnecessary bisection tree | |
142 | /// that would be shortly dropped. | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
143 | pub type PreBTFN<G, const N : usize> = BTFN<G, (), N>; |
0 | 144 | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
145 | impl<G, const N : usize> PreBTFN<G, N> where G : SupportGenerator<N> { |
0 | 146 | |
5 | 147 | /// Create a new [`PreBTFN`] with no bisection tree. |
0 | 148 | pub fn new_pre(generator : G) -> Self { |
149 | BTFN { | |
150 | bt : (), | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
151 | generator : Arc::new(generator), |
0 | 152 | } |
153 | } | |
154 | } | |
155 | ||
156 | impl<F : Float, G, BT, const N : usize> | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
157 | BTFN<G, BT, N> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
158 | where G : SupportGenerator<N, Id=usize, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
159 | G::SupportType : LocalAnalysis<BT::Agg, Cube<F, N>>, |
0 | 160 | BT : BTImpl<F, N, Data=usize> { |
161 | ||
162 | /// Helper function for implementing [`std::ops::Add`]. | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
163 | fn add_another<G2>(&self, g2 : Arc<G2>) -> BTFN<BothGenerators<G, G2>, BT, N> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
164 | where G2 : SupportGenerator<N, Id=usize, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
165 | G2::SupportType : LocalAnalysis<BT::Agg, Cube<F, N>> { |
0 | 166 | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
167 | let mut bt = self.bt.clone(); |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
168 | let both = BothGenerators(Arc::clone(&self.generator), g2); |
0 | 169 | |
170 | for (d, support) in both.all_right_data() { | |
171 | bt.insert(d, &support); | |
172 | } | |
173 | ||
174 | BTFN { | |
175 | bt : bt, | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
176 | generator : Arc::new(both), |
0 | 177 | } |
178 | } | |
179 | } | |
180 | ||
181 | macro_rules! make_btfn_add { | |
182 | ($lhs:ty, $preprocess:path, $($extra_trait:ident)?) => { | |
183 | impl<'a, F : Float, G1, G2, BT1, BT2, const N : usize> | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
184 | std::ops::Add<BTFN<G2, BT2, N>> for |
0 | 185 | $lhs |
186 | where BT1 : BTImpl<F, N, Data=usize>, | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
187 | G1 : SupportGenerator<N, Id=usize, RealField = F> + $($extra_trait)?, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
188 | G2 : SupportGenerator<N, Id=usize, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
189 | G1::SupportType : LocalAnalysis<BT1::Agg, Cube<F, N>>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
190 | G2::SupportType : LocalAnalysis<BT1::Agg, Cube<F, N>> { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
191 | type Output = BTFN<BothGenerators<G1, G2>, BT1, N>; |
0 | 192 | #[inline] |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
193 | fn add(self, other : BTFN<G2, BT2, N>) -> Self::Output { |
0 | 194 | $preprocess(self).add_another(other.generator) |
195 | } | |
196 | } | |
197 | ||
198 | impl<'a, 'b, F : Float, G1, G2, BT1, BT2, const N : usize> | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
199 | std::ops::Add<&'b BTFN<G2, BT2, N>> for |
0 | 200 | $lhs |
201 | where BT1 : BTImpl<F, N, Data=usize>, | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
202 | G1 : SupportGenerator<N, Id=usize, RealField = F> + $($extra_trait)?, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
203 | G2 : SupportGenerator<N, Id=usize, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
204 | G1::SupportType : LocalAnalysis<BT1::Agg, Cube<F, N>>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
205 | G2::SupportType : LocalAnalysis<BT1::Agg, Cube<F, N>> { |
0 | 206 | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
207 | type Output = BTFN<BothGenerators<G1, G2>, BT1, N>; |
0 | 208 | #[inline] |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
209 | fn add(self, other : &'b BTFN<G2, BT2, N>) -> Self::Output { |
0 | 210 | $preprocess(self).add_another(other.generator.clone()) |
211 | } | |
212 | } | |
213 | } | |
214 | } | |
215 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
216 | make_btfn_add!(BTFN<G1, BT1, N>, std::convert::identity, ); |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
217 | make_btfn_add!(&'a BTFN<G1, BT1, N>, Clone::clone, ); |
0 | 218 | |
219 | macro_rules! make_btfn_sub { | |
220 | ($lhs:ty, $preprocess:path, $($extra_trait:ident)?) => { | |
221 | impl<'a, F : Float, G1, G2, BT1, BT2, const N : usize> | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
222 | std::ops::Sub<BTFN<G2, BT2, N>> for |
0 | 223 | $lhs |
224 | where BT1 : BTImpl<F, N, Data=usize>, | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
225 | G1 : SupportGenerator<N, Id=usize, RealField = F> + $($extra_trait)?, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
226 | G2 : SupportGenerator<N, Id=usize, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
227 | G1::SupportType : LocalAnalysis<BT1::Agg, Cube<F, N>>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
228 | G2::SupportType : LocalAnalysis<BT1::Agg, Cube<F, N>> { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
229 | type Output = BTFN<BothGenerators<G1, G2>, BT1, N>; |
0 | 230 | #[inline] |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
231 | fn sub(self, other : BTFN<G2, BT2, N>) -> Self::Output { |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
232 | $preprocess(self).add_another(Arc::new( |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
233 | Arc::try_unwrap(other.generator) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
234 | .unwrap_or_else(|arc| (*arc).clone()) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
235 | .neg() |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
236 | )) |
0 | 237 | } |
238 | } | |
239 | ||
240 | impl<'a, 'b, F : Float, G1, G2, BT1, BT2, const N : usize> | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
241 | std::ops::Sub<&'b BTFN<G2, BT2, N>> for |
0 | 242 | $lhs |
243 | where BT1 : BTImpl<F, N, Data=usize>, | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
244 | G1 : SupportGenerator<N, Id=usize, RealField = F> + $($extra_trait)?, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
245 | G2 : SupportGenerator<N, Id=usize, RealField = F> + Clone, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
246 | G1::SupportType : LocalAnalysis<BT1::Agg, Cube<F, N>>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
247 | G2::SupportType : LocalAnalysis<BT1::Agg, Cube<F, N>>, |
5 | 248 | &'b G2 : std::ops::Neg<Output=G2> { |
0 | 249 | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
250 | type Output = BTFN<BothGenerators<G1, G2>, BT1, N>; |
0 | 251 | #[inline] |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
252 | fn sub(self, other : &'b BTFN<G2, BT2, N>) -> Self::Output { |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
253 | $preprocess(self).add_another(Arc::new((*other.generator).clone().neg())) |
0 | 254 | } |
255 | } | |
256 | } | |
257 | } | |
258 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
259 | make_btfn_sub!(BTFN<G1, BT1, N>, std::convert::identity, ); |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
260 | make_btfn_sub!(&'a BTFN<G1, BT1, N>, std::convert::identity, ); |
0 | 261 | |
262 | macro_rules! make_btfn_scalarop_rhs { | |
263 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
264 | impl<F : Float, G, BT, const N : usize> | |
265 | std::ops::$trait_assign<F> | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
266 | for BTFN<G, BT, N> |
0 | 267 | where BT : BTImpl<F, N>, |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
268 | G : SupportGenerator<N, Id=BT::Data, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
269 | G::SupportType : LocalAnalysis<BT::Agg, Cube<F, N>> { |
0 | 270 | #[inline] |
271 | fn $fn_assign(&mut self, t : F) { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
272 | Arc::make_mut(&mut self.generator).$fn_assign(t); |
0 | 273 | self.refresh_aggregator(); |
274 | } | |
275 | } | |
276 | ||
277 | impl<F : Float, G, BT, const N : usize> | |
278 | std::ops::$trait<F> | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
279 | for BTFN<G, BT, N> |
0 | 280 | where BT : BTImpl<F, N>, |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
281 | G : SupportGenerator<N, Id=BT::Data, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
282 | G::SupportType : LocalAnalysis<BT::Agg, Cube<F, N>> { |
0 | 283 | type Output = Self; |
284 | #[inline] | |
285 | fn $fn(mut self, t : F) -> Self::Output { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
286 | Arc::make_mut(&mut self.generator).$fn_assign(t); |
0 | 287 | self.refresh_aggregator(); |
288 | self | |
289 | } | |
290 | } | |
291 | ||
292 | impl<'a, F : Float, G, BT, const N : usize> | |
293 | std::ops::$trait<F> | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
294 | for &'a BTFN<G, BT, N> |
0 | 295 | where BT : BTImpl<F, N>, |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
296 | G : SupportGenerator<N, Id=BT::Data, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
297 | G::SupportType : LocalAnalysis<BT::Agg, Cube<F, N>>, |
0 | 298 | &'a G : std::ops::$trait<F,Output=G> { |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
299 | type Output = BTFN<G, BT, N>; |
0 | 300 | #[inline] |
301 | fn $fn(self, t : F) -> Self::Output { | |
302 | self.new_generator(self.generator.$fn(t)) | |
303 | } | |
304 | } | |
305 | } | |
306 | } | |
307 | ||
308 | make_btfn_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
309 | make_btfn_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
310 | ||
311 | macro_rules! make_btfn_scalarop_lhs { | |
312 | ($trait:ident, $fn:ident, $fn_assign:ident, $($f:ident)+) => { $( | |
313 | impl<G, BT, const N : usize> | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
314 | std::ops::$trait<BTFN<G, BT, N>> |
0 | 315 | for $f |
316 | where BT : BTImpl<$f, N>, | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
317 | G : SupportGenerator<N, Id=BT::Data, RealField = $f>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
318 | G::SupportType : LocalAnalysis<BT::Agg, Cube<$f, N>> { |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
319 | type Output = BTFN<G, BT, N>; |
0 | 320 | #[inline] |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
321 | fn $fn(self, mut a : BTFN<G, BT, N>) -> Self::Output { |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
322 | Arc::make_mut(&mut a.generator).$fn_assign(self); |
0 | 323 | a.refresh_aggregator(); |
324 | a | |
325 | } | |
326 | } | |
327 | ||
328 | impl<'a, G, BT, const N : usize> | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
329 | std::ops::$trait<&'a BTFN<G, BT, N>> |
0 | 330 | for $f |
331 | where BT : BTImpl<$f, N>, | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
332 | G : SupportGenerator<N, Id=BT::Data, RealField = $f> + Clone, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
333 | G::SupportType : LocalAnalysis<BT::Agg, Cube<$f, N>>, |
0 | 334 | // FIXME: This causes compiler overflow |
335 | /*&'a G : std::ops::$trait<$f,Output=G>*/ { | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
336 | type Output = BTFN<G, BT, N>; |
0 | 337 | #[inline] |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
338 | fn $fn(self, a : &'a BTFN<G, BT, N>) -> Self::Output { |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
339 | let mut tmp = (*a.generator).clone(); |
0 | 340 | tmp.$fn_assign(self); |
341 | a.new_generator(tmp) | |
342 | // FIXME: Prevented by the compiler overflow above. | |
343 | //a.new_generator(a.generator.$fn(a)) | |
344 | } | |
345 | } | |
346 | )+ } | |
347 | } | |
348 | ||
349 | make_btfn_scalarop_lhs!(Mul, mul, mul_assign, f32 f64); | |
350 | make_btfn_scalarop_lhs!(Div, div, div_assign, f32 f64); | |
351 | ||
352 | macro_rules! make_btfn_unaryop { | |
353 | ($trait:ident, $fn:ident) => { | |
354 | impl<F : Float, G, BT, const N : usize> | |
355 | std::ops::$trait | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
356 | for BTFN<G, BT, N> |
0 | 357 | where BT : BTImpl<F, N>, |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
358 | G : SupportGenerator<N, Id=BT::Data, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
359 | G::SupportType : LocalAnalysis<BT::Agg, Cube<F, N>> { |
0 | 360 | type Output = Self; |
361 | #[inline] | |
362 | fn $fn(mut self) -> Self::Output { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
363 | self.generator = Arc::new(Arc::unwrap_or_clone(self.generator).$fn()); |
0 | 364 | self.refresh_aggregator(); |
365 | self | |
366 | } | |
367 | } | |
368 | ||
369 | /*impl<'a, F : Float, G, BT, const N : usize> | |
370 | std::ops::$trait | |
371 | for &'a BTFN<F, G, BT, N> | |
372 | where BT : BTImpl<F, N>, | |
373 | G : SupportGenerator<F, N, Id=BT::Data>, | |
374 | G::SupportType : LocalAnalysis<F, BT::Agg, N>, | |
375 | &'a G : std::ops::$trait<Output=G> { | |
376 | type Output = BTFN<F, G, BT, N>; | |
377 | #[inline] | |
378 | fn $fn(self) -> Self::Output { | |
379 | self.new_generator(std::ops::$trait::$fn(&self.generator)) | |
380 | } | |
381 | }*/ | |
382 | } | |
383 | } | |
384 | ||
385 | make_btfn_unaryop!(Neg, neg); | |
386 | ||
387 | // | |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
388 | // Apply, Mapping, Differentiate |
0 | 389 | // |
390 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
391 | impl<'a, F : Float, G, BT, V, const N : usize> Apply<&'a Loc<F, N>> |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
392 | for BTFN<G, BT, N> |
0 | 393 | where BT : BTImpl<F, N>, |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
394 | G : SupportGenerator<N, Id=BT::Data, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
395 | G::SupportType : LocalAnalysis<BT::Agg, Cube<F, N>> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
396 | + Apply<&'a Loc<F, N>, Output = V>, |
0 | 397 | V : Sum { |
398 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
399 | type Output = V; |
0 | 400 | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
401 | fn apply(&self, x : &'a Loc<F, N>) -> Self::Output { |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
402 | self.bt.iter_at(x) |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
403 | .map(|&d| self.generator.support_for(d).apply(x)).sum() |
0 | 404 | } |
405 | } | |
406 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
407 | impl<F : Float, G, BT, V, const N : usize> Apply<Loc<F, N>> |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
408 | for BTFN<G, BT, N> |
0 | 409 | where BT : BTImpl<F, N>, |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
410 | G : SupportGenerator<N, Id=BT::Data, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
411 | G::SupportType : LocalAnalysis<BT::Agg, Cube<F, N>> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
412 | + Apply<Loc<F, N>, Output = V>, |
0 | 413 | V : Sum { |
414 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
415 | type Output = V; |
0 | 416 | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
417 | fn apply(&self, x : Loc<F, N>) -> Self::Output { |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
418 | self.bt.iter_at(&x) |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
419 | .map(|&d| self.generator.support_for(d).apply(x)).sum() |
0 | 420 | } |
421 | } | |
422 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
423 | impl<'a, F : Float, G, BT, V : HasRealField<RealField=F>, const N : usize> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
424 | Differentiable<&'a Loc<F, N>> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
425 | for BTFN<G, BT, N> |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
426 | where BT : BTImpl<F, N>, |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
427 | G : SupportGenerator<N, Id=BT::Data, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
428 | G::SupportType : LocalAnalysis<BT::Agg, Cube<F, N>> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
429 | + Differentiable<&'a Loc<F, N>, Derivative = V>, |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
430 | V : Sum { |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
431 | |
47
a0db98c16ab5
Some Differentiable simplifications and clarifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
432 | type Derivative = V; |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
433 | |
47
a0db98c16ab5
Some Differentiable simplifications and clarifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
434 | fn differential(&self, x : &'a Loc<F, N>) -> Self::Derivative { |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
435 | self.bt.iter_at(x) |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
436 | .map(|&d| self.generator.support_for(d).differential(x)) |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
437 | .sum() |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
438 | } |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
439 | } |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
440 | |
29
7fd0984743b5
Rename Differentiate → Differentiable
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
441 | impl<F : Float, G, BT, V, const N : usize> Differentiable<Loc<F, N>> |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
442 | for BTFN<G, BT, N> |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
443 | where BT : BTImpl<F, N>, |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
444 | G : SupportGenerator<N, Id=BT::Data, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
445 | G::SupportType : LocalAnalysis<BT::Agg, Cube<F, N>> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
446 | + Differentiable<Loc<F, N>, Derivative = V>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
447 | V : Sum + HasRealField<RealField=F> { |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
448 | |
47
a0db98c16ab5
Some Differentiable simplifications and clarifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
449 | type Derivative = V; |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
450 | |
47
a0db98c16ab5
Some Differentiable simplifications and clarifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
451 | fn differential(&self, x : Loc<F, N>) -> Self::Derivative { |
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
452 | self.bt.iter_at(&x) |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
453 | .map(|&d| self.generator.support_for(d).differential(x)) |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
454 | .sum() |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
455 | } |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
456 | } |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
457 | |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
458 | // |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
459 | // GlobalAnalysis |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
460 | // |
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
461 | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
462 | impl<F : Float, G, BT, const N : usize> GlobalAnalysis<BT::Agg> |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
463 | for BTFN<G, BT, N> |
0 | 464 | where BT : BTImpl<F, N>, |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
465 | G : SupportGenerator<N, Id=BT::Data, RealField = F>, |
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
466 | G::SupportType : LocalAnalysis<BT::Agg, Cube<F, N>> { |
0 | 467 | |
468 | #[inline] | |
469 | fn global_analysis(&self) -> BT::Agg { | |
470 | self.bt.global_analysis() | |
471 | } | |
472 | } | |
473 | ||
474 | // | |
475 | // Blanket implementation of BTFN as a linear functional over objects | |
476 | // that are linear functionals over BTFN. | |
477 | // | |
478 | ||
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
479 | /* |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
480 | impl<'b, X, F : Float, G, BT, const N : usize> Apply<&'b X, F> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
481 | for BTFN<F, G, BT, N> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
482 | where BT : BTImpl<F, N>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
483 | G : SupportGenerator<F, N, Id=BT::Data>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
484 | G::SupportType : LocalAnalysis<F, BT::Agg, N>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
485 | X : for<'a> Apply<&'a BTFN<F, G, BT, N>, F> { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
486 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
487 | #[inline] |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
488 | fn apply(&self, x : &'b X) -> F { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
489 | x.apply(&self) |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
490 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
491 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
492 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
493 | impl<X, F : Float, G, BT, const N : usize> Apply<X, F> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
494 | for BTFN<F, G, BT, N> |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
495 | where BT : BTImpl<F, N>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
496 | G : SupportGenerator<F, N, Id=BT::Data>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
497 | G::SupportType : LocalAnalysis<F, BT::Agg, N>, |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
498 | X : for<'a> Apply<&'a BTFN<F, G, BT, N>, F> { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
499 | |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
500 | #[inline] |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
501 | fn apply(&self, x : X) -> F { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
502 | x.apply(&self) |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
503 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
504 | } |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
505 | |
0 | 506 | impl<X, F : Float, G, BT, const N : usize> Linear<X> |
507 | for BTFN<F, G, BT, N> | |
508 | where BT : BTImpl<F, N>, | |
509 | G : SupportGenerator<F, N, Id=BT::Data>, | |
510 | G::SupportType : LocalAnalysis<F, BT::Agg, N>, | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
511 | X : for<'a> Apply<&'a BTFN<F, G, BT, N>, F> { |
0 | 512 | type Codomain = F; |
513 | } | |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
514 | */ |
0 | 515 | |
516 | /// Helper trait for performing approximate minimisation using P2 elements. | |
5 | 517 | /// |
518 | /// `U` is the domain, generally [`Loc`]`<F, N>`, and `F` the type of floating point numbers. | |
519 | /// `Self` is generally a set of `U`, for example, [`Cube`]`<F, N>`. | |
0 | 520 | pub trait P2Minimise<U, F : Float> : Set<U> { |
5 | 521 | /// Minimise `g` over the set presented by `Self`. |
522 | /// | |
523 | /// The function returns `(x, v)` where `x` is the minimiser `v` an approximation of `g(x)`. | |
0 | 524 | fn p2_minimise<G : Fn(&U) -> F>(&self, g : G) -> (U, F); |
525 | ||
526 | } | |
527 | ||
528 | impl<F : Float> P2Minimise<Loc<F, 1>, F> for Cube<F, 1> { | |
529 | fn p2_minimise<G : Fn(&Loc<F, 1>) -> F>(&self, g : G) -> (Loc<F, 1>, F) { | |
530 | let interval = Simplex(self.corners()); | |
531 | interval.p2_model(&g).minimise(&interval) | |
532 | } | |
533 | } | |
534 | ||
535 | #[replace_float_literals(F::cast_from(literal))] | |
536 | impl<F : Float> P2Minimise<Loc<F, 2>, F> for Cube<F, 2> { | |
537 | fn p2_minimise<G : Fn(&Loc<F, 2>) -> F>(&self, g : G) -> (Loc<F, 2>, F) { | |
538 | if false { | |
539 | // Split into two triangle (simplex) with separate P2 model in each. | |
540 | // The six nodes of each triangle are the corners and the edges. | |
541 | let [a, b, c, d] = self.corners(); | |
542 | let [va, vb, vc, vd] = [g(&a), g(&b), g(&c), g(&d)]; | |
543 | ||
544 | let ab = midpoint(&a, &b); | |
545 | let bc = midpoint(&b, &c); | |
546 | let ca = midpoint(&c, &a); | |
547 | let cd = midpoint(&c, &d); | |
548 | let da = midpoint(&d, &a); | |
549 | let [vab, vbc, vca, vcd, vda] = [g(&ab), g(&bc), g(&ca), g(&cd), g(&da)]; | |
550 | ||
551 | let s1 = Simplex([a, b, c]); | |
5 | 552 | let m1 = P2LocalModel::<F, 2, 3>::new( |
0 | 553 | &[a, b, c, ab, bc, ca], |
554 | &[va, vb, vc, vab, vbc, vca] | |
555 | ); | |
556 | ||
557 | let r1@(_, v1) = m1.minimise(&s1); | |
558 | ||
559 | let s2 = Simplex([c, d, a]); | |
5 | 560 | let m2 = P2LocalModel::<F, 2, 3>::new( |
0 | 561 | &[c, d, a, cd, da, ca], |
562 | &[vc, vd, va, vcd, vda, vca] | |
563 | ); | |
564 | ||
565 | let r2@(_, v2) = m2.minimise(&s2); | |
566 | ||
567 | if v1 < v2 { r1 } else { r2 } | |
568 | } else { | |
569 | // Single P2 model for the entire cube. | |
570 | let [a, b, c, d] = self.corners(); | |
571 | let [va, vb, vc, vd] = [g(&a), g(&b), g(&c), g(&d)]; | |
572 | let [e, f] = match 'r' { | |
573 | 'm' => [(&a + &b + &c) / 3.0, (&c + &d + &a) / 3.0], | |
574 | 'c' => [midpoint(&a, &b), midpoint(&a, &d)], | |
575 | 'w' => [(&a + &b * 2.0) / 3.0, (&a + &d * 2.0) / 3.0], | |
576 | 'r' => { | |
577 | // Pseudo-randomise edge midpoints | |
578 | let Loc([x, y]) = a; | |
579 | let tmp : f64 = (x+y).as_(); | |
580 | match tmp.to_bits() % 4 { | |
581 | 0 => [midpoint(&a, &b), midpoint(&a, &d)], | |
582 | 1 => [midpoint(&c, &d), midpoint(&a, &d)], | |
583 | 2 => [midpoint(&a, &b), midpoint(&b, &c)], | |
584 | _ => [midpoint(&c, &d), midpoint(&b, &c)], | |
585 | } | |
586 | }, | |
587 | _ => [self.center(), (&a + &b) / 2.0], | |
588 | }; | |
589 | let [ve, vf] = [g(&e), g(&f)]; | |
590 | ||
5 | 591 | let m1 = P2LocalModel::<F, 2, 3>::new( |
0 | 592 | &[a, b, c, d, e, f], |
593 | &[va, vb, vc, vd, ve, vf], | |
594 | ); | |
595 | ||
596 | m1.minimise(self) | |
597 | } | |
598 | } | |
599 | } | |
600 | ||
5 | 601 | /// Helper type to use [`P2Refiner`] for maximisation. |
0 | 602 | struct RefineMax; |
5 | 603 | |
604 | /// Helper type to use [`P2Refiner`] for minimisation. | |
0 | 605 | struct RefineMin; |
606 | ||
5 | 607 | /// A bisection tree [`Refiner`] for maximising or minimising a [`BTFN`]. |
608 | /// | |
0 | 609 | /// The type parameter `T` should be either [`RefineMax`] or [`RefineMin`]. |
610 | struct P2Refiner<F : Float, T> { | |
5 | 611 | /// The maximum / minimum should be above / below this threshold. |
612 | /// If the threshold cannot be satisfied, the refiner will return `None`. | |
0 | 613 | bound : Option<F>, |
5 | 614 | /// Tolerance for function value estimation. |
0 | 615 | tolerance : F, |
5 | 616 | /// Maximum number of steps to execute the refiner for |
0 | 617 | max_steps : usize, |
5 | 618 | /// Either [`RefineMax`] or [`RefineMin`]. Used only for type system purposes. |
0 | 619 | #[allow(dead_code)] // `how` is just for type system purposes. |
620 | how : T, | |
621 | } | |
622 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
623 | impl<F : Float, G, const N : usize> Refiner<Bounds<F>, G, N> |
0 | 624 | for P2Refiner<F, RefineMax> |
625 | where Cube<F, N> : P2Minimise<Loc<F, N>, F>, | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
626 | G : SupportGenerator<N, RealField = F>, |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
627 | G::SupportType : Mapping<Loc<F, N>, Codomain=F> |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
628 | + LocalAnalysis<Bounds<F>, Cube<F, N>> { |
0 | 629 | type Result = Option<(Loc<F, N>, F)>; |
630 | type Sorting = UpperBoundSorting<F>; | |
631 | ||
632 | fn refine( | |
633 | &self, | |
634 | aggregator : &Bounds<F>, | |
635 | cube : &Cube<F, N>, | |
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
636 | data : &[G::Id], |
0 | 637 | generator : &G, |
638 | step : usize | |
639 | ) -> RefinerResult<Bounds<F>, Self::Result> { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
640 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
641 | if self.bound.map_or(false, |b| aggregator.upper() <= b + self.tolerance) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
642 | // The upper bound is below the maximisation threshold. Don't bother with this cube. |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
643 | return RefinerResult::Uncertain(*aggregator, None) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
644 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
645 | |
0 | 646 | // g gives the negative of the value of the function presented by `data` and `generator`. |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
647 | let g = move |x : &Loc<F, N>| { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
648 | let f = move |&d| generator.support_for(d).apply(x); |
0 | 649 | -data.iter().map(f).sum::<F>() |
650 | }; | |
651 | // … so the negative of the minimum is the maximm we want. | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
652 | let (x, _neg_v) = cube.p2_minimise(g); |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
653 | //let v = -neg_v; |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
654 | let v = -g(&x); |
0 | 655 | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
656 | if step < self.max_steps && (aggregator.upper() > v + self.tolerance |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
657 | /*|| aggregator.lower() > v - self.tolerance*/) { |
0 | 658 | // The function isn't refined enough in `cube`, so return None |
659 | // to indicate that further subdivision is required. | |
660 | RefinerResult::NeedRefinement | |
661 | } else { | |
662 | // The data is refined enough, so return new hopefully better bounds | |
663 | // and the maximiser. | |
664 | let res = (x, v); | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
665 | let bounds = Bounds(v, v); |
0 | 666 | RefinerResult::Uncertain(bounds, Some(res)) |
667 | } | |
668 | } | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
669 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
670 | fn fuse_results(r1 : &mut Self::Result, r2 : Self::Result) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
671 | match (*r1, r2) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
672 | (Some((_, v1)), Some((_, v2))) => if v1 < v2 { *r1 = r2 } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
673 | (None, Some(_)) => *r1 = r2, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
674 | (_, _) => {}, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
675 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
676 | } |
0 | 677 | } |
678 | ||
679 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
680 | impl<F : Float, G, const N : usize> Refiner<Bounds<F>, G, N> |
0 | 681 | for P2Refiner<F, RefineMin> |
682 | where Cube<F, N> : P2Minimise<Loc<F, N>, F>, | |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
683 | G : SupportGenerator<N, RealField = F>, |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
684 | G::SupportType : Mapping<Loc<F, N>, Codomain=F> |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
685 | + LocalAnalysis<Bounds<F>, Cube<F, N>> { |
0 | 686 | type Result = Option<(Loc<F, N>, F)>; |
687 | type Sorting = LowerBoundSorting<F>; | |
688 | ||
689 | fn refine( | |
690 | &self, | |
691 | aggregator : &Bounds<F>, | |
692 | cube : &Cube<F, N>, | |
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
693 | data : &[G::Id], |
0 | 694 | generator : &G, |
695 | step : usize | |
696 | ) -> RefinerResult<Bounds<F>, Self::Result> { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
697 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
698 | if self.bound.map_or(false, |b| aggregator.lower() >= b - self.tolerance) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
699 | // The lower bound is above the minimisation threshold. Don't bother with this cube. |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
700 | return RefinerResult::Uncertain(*aggregator, None) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
701 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
702 | |
0 | 703 | // g gives the value of the function presented by `data` and `generator`. |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
704 | let g = move |x : &Loc<F, N>| { |
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
705 | let f = move |&d| generator.support_for(d).apply(x); |
0 | 706 | data.iter().map(f).sum::<F>() |
707 | }; | |
708 | // Minimise it. | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
709 | let (x, _v) = cube.p2_minimise(g); |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
710 | let v = g(&x); |
0 | 711 | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
712 | if step < self.max_steps && (aggregator.lower() < v - self.tolerance |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
713 | /*|| aggregator.upper() < v + self.tolerance*/) { |
0 | 714 | // The function isn't refined enough in `cube`, so return None |
715 | // to indicate that further subdivision is required. | |
716 | RefinerResult::NeedRefinement | |
717 | } else { | |
718 | // The data is refined enough, so return new hopefully better bounds | |
719 | // and the minimiser. | |
720 | let res = (x, v); | |
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
721 | let l = aggregator.lower(); |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
722 | let bounds = if l > v { |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
723 | eprintln!("imprecision!"); |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
724 | Bounds(l, l) |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
725 | } else { |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
726 | Bounds(v, v) |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
727 | }; |
0 | 728 | RefinerResult::Uncertain(bounds, Some(res)) |
729 | } | |
730 | } | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
731 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
732 | fn fuse_results(r1 : &mut Self::Result, r2 : Self::Result) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
733 | match (*r1, r2) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
734 | (Some((_, v1)), Some((_, v2))) => if v1 > v2 { *r1 = r2 } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
735 | (_, Some(_)) => *r1 = r2, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
736 | (_, _) => {}, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
737 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
738 | } |
0 | 739 | } |
740 | ||
741 | ||
5 | 742 | /// A bisection tree [`Refiner`] for checking that a [`BTFN`] is within a stated |
743 | //// upper or lower bound. | |
744 | /// | |
745 | /// The type parameter `T` should be either [`RefineMax`] for upper bound or [`RefineMin`] | |
746 | /// for lower bound. | |
0 | 747 | |
748 | struct BoundRefiner<F : Float, T> { | |
5 | 749 | /// The upper/lower bound to check for |
0 | 750 | bound : F, |
5 | 751 | /// Tolerance for function value estimation. |
0 | 752 | tolerance : F, |
5 | 753 | /// Maximum number of steps to execute the refiner for |
0 | 754 | max_steps : usize, |
755 | #[allow(dead_code)] // `how` is just for type system purposes. | |
5 | 756 | /// Either [`RefineMax`] or [`RefineMin`]. Used only for type system purposes. |
0 | 757 | how : T, |
758 | } | |
759 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
760 | impl<F : Float, G, const N : usize> Refiner<Bounds<F>, G, N> |
0 | 761 | for BoundRefiner<F, RefineMax> |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
762 | where G : SupportGenerator<N, RealField = F> { |
0 | 763 | type Result = bool; |
764 | type Sorting = UpperBoundSorting<F>; | |
765 | ||
766 | fn refine( | |
767 | &self, | |
768 | aggregator : &Bounds<F>, | |
769 | _cube : &Cube<F, N>, | |
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
770 | _data : &[G::Id], |
0 | 771 | _generator : &G, |
772 | step : usize | |
773 | ) -> RefinerResult<Bounds<F>, Self::Result> { | |
774 | if aggregator.upper() <= self.bound + self.tolerance { | |
775 | // Below upper bound within tolerances. Indicate uncertain success. | |
776 | RefinerResult::Uncertain(*aggregator, true) | |
777 | } else if aggregator.lower() >= self.bound - self.tolerance { | |
778 | // Above upper bound within tolerances. Indicate certain failure. | |
779 | RefinerResult::Certain(false) | |
780 | } else if step < self.max_steps { | |
781 | // No decision possible, but within step bounds - further subdivision is required. | |
782 | RefinerResult::NeedRefinement | |
783 | } else { | |
784 | // No decision possible, but past step bounds | |
785 | RefinerResult::Uncertain(*aggregator, false) | |
786 | } | |
787 | } | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
788 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
789 | fn fuse_results(r1 : &mut Self::Result, r2 : Self::Result) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
790 | *r1 = *r1 && r2; |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
791 | } |
0 | 792 | } |
793 | ||
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
794 | impl<F : Float, G, const N : usize> Refiner<Bounds<F>, G, N> |
5 | 795 | for BoundRefiner<F, RefineMin> |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
796 | where G : SupportGenerator<N, RealField = F> { |
5 | 797 | type Result = bool; |
798 | type Sorting = UpperBoundSorting<F>; | |
799 | ||
800 | fn refine( | |
801 | &self, | |
802 | aggregator : &Bounds<F>, | |
803 | _cube : &Cube<F, N>, | |
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
804 | _data : &[G::Id], |
5 | 805 | _generator : &G, |
806 | step : usize | |
807 | ) -> RefinerResult<Bounds<F>, Self::Result> { | |
808 | if aggregator.lower() >= self.bound - self.tolerance { | |
809 | // Above lower bound within tolerances. Indicate uncertain success. | |
810 | RefinerResult::Uncertain(*aggregator, true) | |
811 | } else if aggregator.upper() <= self.bound + self.tolerance { | |
812 | // Below lower bound within tolerances. Indicate certain failure. | |
813 | RefinerResult::Certain(false) | |
814 | } else if step < self.max_steps { | |
815 | // No decision possible, but within step bounds - further subdivision is required. | |
816 | RefinerResult::NeedRefinement | |
817 | } else { | |
818 | // No decision possible, but past step bounds | |
819 | RefinerResult::Uncertain(*aggregator, false) | |
820 | } | |
821 | } | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
822 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
823 | fn fuse_results(r1 : &mut Self::Result, r2 : Self::Result) { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
824 | *r1 = *r1 && r2; |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
825 | } |
5 | 826 | } |
0 | 827 | |
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
828 | // FIXME: The most likely reason for the “Refiner failure” expectation in the methods below |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
829 | // is numerical inaccuracy: the `glb` maintained in `HeapContainer` (`refine.rs`) becomes bigger |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
830 | // than the *upper bound* of nodes attempted to be inserted into the `heap` in the container. |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
831 | // But the `glb` is there exactly to prevent that. Due to numerical inaccuracy, however, a |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
832 | // newly subdivided node may have lower upper bound than the original lower bound that should |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
833 | // have been above the `glb` since the node was picked from the queue. Due to the subdivision |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
834 | // process, if a node whose lower bound is at the `glb` is picked, all of its refined subnodes |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
835 | // should have lower bound at least the old `glb`, so in a single-threaded situation there should |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
836 | // always be nodes above the `glb` in the queue. In a multi-threaded situation a node below the |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
837 | // `glb` may be picked by some thread. When that happens, that thread inserts no new nodes into |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
838 | // the queue. If the queue empties as a result of that, the thread goes to wait for other threads |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
839 | // to produce results. Since some node had a node whose lower bound was above the `glb`, eventually |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
840 | // there should be a result, or new nodes above the `glb` inserted into the queue. Then the waiting |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
841 | // threads can also continue processing. If, however, numerical inaccuracy destroyes the `glb`, |
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
842 | // the queue may run out, and we get “Refiner failure”. |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
843 | impl<F : Float, G, BT, const N : usize> BTFN<G, BT, N> |
0 | 844 | where BT : BTSearch<F, N, Agg=Bounds<F>>, |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
845 | G : SupportGenerator<N, Id=BT::Data, RealField = F>, |
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
846 | G::SupportType : Mapping<Loc<F, N>,Codomain=F> |
81
d2acaaddd9af
Try to have Field as member type in Mappings etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
847 | + LocalAnalysis<Bounds<F>, Cube<F, N>>, |
0 | 848 | Cube<F, N> : P2Minimise<Loc<F, N>, F> { |
849 | ||
5 | 850 | /// Maximise the `BTFN` within stated value `tolerance`. |
851 | /// | |
852 | /// At most `max_steps` refinement steps are taken. | |
853 | /// Returns the approximate maximiser and the corresponding function value. | |
0 | 854 | pub fn maximise(&mut self, tolerance : F, max_steps : usize) -> (Loc<F, N>, F) { |
855 | let refiner = P2Refiner{ tolerance, max_steps, how : RefineMax, bound : None }; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
856 | self.bt.search_and_refine(refiner, &self.generator).expect("Refiner failure.").unwrap() |
0 | 857 | } |
858 | ||
5 | 859 | /// Maximise the `BTFN` within stated value `tolerance` subject to a lower bound. |
860 | /// | |
861 | /// At most `max_steps` refinement steps are taken. | |
862 | /// Returns the approximate maximiser and the corresponding function value when one is found | |
863 | /// above the `bound` threshold, otherwise `None`. | |
0 | 864 | pub fn maximise_above(&mut self, bound : F, tolerance : F, max_steps : usize) |
865 | -> Option<(Loc<F, N>, F)> { | |
866 | let refiner = P2Refiner{ tolerance, max_steps, how : RefineMax, bound : Some(bound) }; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
867 | self.bt.search_and_refine(refiner, &self.generator).expect("Refiner failure.") |
0 | 868 | } |
869 | ||
5 | 870 | /// Minimise the `BTFN` within stated value `tolerance`. |
871 | /// | |
872 | /// At most `max_steps` refinement steps are taken. | |
873 | /// Returns the approximate minimiser and the corresponding function value. | |
0 | 874 | pub fn minimise(&mut self, tolerance : F, max_steps : usize) -> (Loc<F, N>, F) { |
875 | let refiner = P2Refiner{ tolerance, max_steps, how : RefineMin, bound : None }; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
876 | self.bt.search_and_refine(refiner, &self.generator).expect("Refiner failure.").unwrap() |
0 | 877 | } |
878 | ||
5 | 879 | /// Minimise the `BTFN` within stated value `tolerance` subject to a lower bound. |
880 | /// | |
881 | /// At most `max_steps` refinement steps are taken. | |
882 | /// Returns the approximate minimiser and the corresponding function value when one is found | |
883 | /// above the `bound` threshold, otherwise `None`. | |
0 | 884 | pub fn minimise_below(&mut self, bound : F, tolerance : F, max_steps : usize) |
885 | -> Option<(Loc<F, N>, F)> { | |
886 | let refiner = P2Refiner{ tolerance, max_steps, how : RefineMin, bound : Some(bound) }; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
887 | self.bt.search_and_refine(refiner, &self.generator).expect("Refiner failure.") |
0 | 888 | } |
5 | 889 | |
890 | /// Verify that the `BTFN` has a given upper `bound` within indicated `tolerance`. | |
891 | /// | |
892 | /// At most `max_steps` refinement steps are taken. | |
0 | 893 | pub fn has_upper_bound(&mut self, bound : F, tolerance : F, max_steps : usize) -> bool { |
894 | let refiner = BoundRefiner{ bound, tolerance, max_steps, how : RefineMax }; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
895 | self.bt.search_and_refine(refiner, &self.generator).expect("Refiner failure.") |
0 | 896 | } |
5 | 897 | |
898 | /// Verify that the `BTFN` has a given lower `bound` within indicated `tolerance`. | |
899 | /// | |
900 | /// At most `max_steps` refinement steps are taken. | |
901 | pub fn has_lower_bound(&mut self, bound : F, tolerance : F, max_steps : usize) -> bool { | |
902 | let refiner = BoundRefiner{ bound, tolerance, max_steps, how : RefineMin }; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
903 | self.bt.search_and_refine(refiner, &self.generator).expect("Refiner failure.") |
5 | 904 | } |
0 | 905 | } |