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