Sun, 27 Apr 2025 15:45:40 -0500
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
1 | use crate::mapping::{ |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
2 | BasicDecomposition, DifferentiableImpl, DifferentiableMapping, Instance, Mapping, Space, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
3 | }; |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
4 | use crate::types::Float; |
| 0 | 5 | use numeric_literals::replace_float_literals; |
| 6 | use std::iter::Sum; | |
| 7 | use std::marker::PhantomData; | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
8 | use std::sync::Arc; |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
9 | //use crate::linops::{Apply, Linear}; |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
10 | use super::aggregator::*; |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
11 | use super::bt::*; |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
12 | use super::either::*; |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
13 | use super::refine::*; |
| 0 | 14 | use super::support::*; |
| 15 | use crate::fe_model::base::RealLocalModel; | |
| 16 | use crate::fe_model::p2_local_model::*; | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
17 | use crate::loc::Loc; |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
18 | use crate::sets::Cube; |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
19 | use crate::sets::Set; |
| 0 | 20 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
21 | /// Presentation for (mathematical) functions constructed as a sum of components functions with |
| 5 | 22 | /// typically small support. |
| 23 | /// | |
| 24 | /// The domain of the function is [`Loc`]`<F, N>`, where `F` is the type of floating point numbers, | |
| 25 | /// and `N` the dimension. | |
| 26 | /// | |
| 27 | /// The `generator` lists the component functions that have to implement [`Support`]. | |
| 28 | /// Identifiers of the components ([`SupportGenerator::Id`], usually `usize`) are stored stored | |
| 29 | /// in a [bisection tree][BTImpl], when one is provided as `bt`. However `bt` may also be `()` | |
| 30 | /// for a [`PreBTFN`] that is only useful for vector space operations with a full [`BTFN`]. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
31 | #[derive(Clone, Debug)] |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
32 | pub struct BTFN<F: Float, G: SupportGenerator<F, N>, BT /*: BTImpl<F, N>*/, const N: usize> /*where G::SupportType : LocalAnalysis<F, A, N>*/ |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
33 | { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
34 | bt: BT, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
35 | generator: Arc<G>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
36 | _phantoms: PhantomData<F>, |
| 0 | 37 | } |
| 38 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
39 | impl<F: Float, G, BT, const N: usize> Space for BTFN<F, G, BT, N> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
40 | where |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
41 | G: SupportGenerator<F, N, Id = BT::Data>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
42 | G::SupportType: LocalAnalysis<F, BT::Agg, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
43 | BT: BTImpl<F, N>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
44 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
45 | type Decomp = BasicDecomposition; |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
46 | } |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
47 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
48 | impl<F: Float, G, BT, const N: usize> BTFN<F, G, BT, N> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
49 | where |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
50 | G: SupportGenerator<F, N, Id = BT::Data>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
51 | G::SupportType: LocalAnalysis<F, BT::Agg, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
52 | BT: BTImpl<F, N>, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
53 | { |
| 5 | 54 | /// Create a new BTFN from a support generator and a pre-initialised bisection tree. |
| 55 | /// | |
| 56 | /// The bisection tree `bt` should be pre-initialised to correspond to the `generator`. | |
| 57 | /// Use [`Self::construct`] if no preinitialised tree is available. Use [`Self::new_refresh`] | |
| 58 | /// when the aggregators of the tree may need updates. | |
| 59 | /// | |
| 60 | /// See the documentation for [`BTFN`] on the role of the `generator`. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
61 | pub fn new(bt: BT, generator: G) -> Self { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
62 | Self::new_arc(bt, Arc::new(generator)) |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
63 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
64 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
65 | fn new_arc(bt: BT, generator: Arc<G>) -> Self { |
| 0 | 66 | BTFN { |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
67 | bt: bt, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
68 | generator: generator, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
69 | _phantoms: std::marker::PhantomData, |
| 0 | 70 | } |
| 71 | } | |
| 72 | ||
| 5 | 73 | /// Create a new BTFN support generator and a pre-initialised bisection tree, |
| 74 | /// cloning the tree and refreshing aggregators. | |
| 75 | /// | |
| 76 | /// The bisection tree `bt` should be pre-initialised to correspond to the `generator`, but | |
| 77 | /// the aggregator may be out of date. | |
| 78 | /// | |
| 79 | /// See the documentation for [`BTFN`] on the role of the `generator`. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
80 | pub fn new_refresh(bt: &BT, generator: G) -> Self { |
| 0 | 81 | // clone().refresh_aggregator(…) as opposed to convert_aggregator |
| 82 | // ensures that type is maintained. Due to Rc-pointer copy-on-write, | |
| 83 | // the effort is not significantly different. | |
| 84 | let mut btnew = bt.clone(); | |
| 85 | btnew.refresh_aggregator(&generator); | |
| 86 | BTFN::new(btnew, generator) | |
| 87 | } | |
| 88 | ||
| 5 | 89 | /// Create a new BTFN from a support generator, domain, and depth for a new [`BT`]. |
| 90 | /// | |
| 91 | /// The top node of the created [`BT`] will have the given `domain`. | |
| 92 | /// | |
| 93 | /// See the documentation for [`BTFN`] on the role of the `generator`. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
94 | 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
|
95 | Self::construct_arc(domain, depth, Arc::new(generator)) |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
96 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
97 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
98 | fn construct_arc(domain: Cube<F, N>, depth: BT::Depth, generator: Arc<G>) -> Self { |
| 0 | 99 | let mut bt = BT::new(domain, depth); |
| 100 | for (d, support) in generator.all_data() { | |
| 101 | bt.insert(d, &support); | |
| 102 | } | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
103 | Self::new_arc(bt, generator) |
| 0 | 104 | } |
| 105 | ||
| 5 | 106 | /// Convert the aggregator of the [`BTFN`] to a different one. |
| 107 | /// | |
| 108 | /// This will construct a [`BTFN`] with the same components and generator as the (consumed) | |
| 109 | /// `self`, but a new `BT` with [`Aggregator`]s of type `ANew`. | |
| 0 | 110 | pub fn convert_aggregator<ANew>(self) -> BTFN<F, G, BT::Converted<ANew>, N> |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
111 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
112 | ANew: Aggregator, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
113 | G: SupportGenerator<F, N, Id = BT::Data>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
114 | G::SupportType: LocalAnalysis<F, ANew, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
115 | { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
116 | BTFN::new_arc(self.bt.convert_aggregator(&*self.generator), self.generator) |
| 0 | 117 | } |
| 118 | ||
| 119 | /// Change the generator (after, e.g., a scaling of the latter). | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
120 | fn new_generator(&self, generator: G) -> Self { |
| 0 | 121 | BTFN::new_refresh(&self.bt, generator) |
| 122 | } | |
| 123 | ||
| 124 | /// Refresh aggregator after updates to generator | |
| 125 | fn refresh_aggregator(&mut self) { | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
126 | self.bt.refresh_aggregator(&*self.generator); |
| 0 | 127 | } |
| 128 | } | |
| 129 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
130 | impl<F: Float, G, BT, const N: usize> BTFN<F, G, BT, N> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
131 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
132 | G: SupportGenerator<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
133 | { |
| 5 | 134 | /// Change the [bisection tree][BTImpl] of the [`BTFN`] to a different one. |
| 135 | /// | |
| 136 | /// This can be used to convert a [`PreBTFN`] to a full [`BTFN`], or the change | |
| 137 | /// the aggreagator; see also [`self.convert_aggregator`]. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
138 | pub fn instantiate<BTNew: BTImpl<F, N, Data = G::Id>>( |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
139 | self, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
140 | domain: Cube<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
141 | depth: BTNew::Depth, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
142 | ) -> BTFN<F, G, BTNew, N> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
143 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
144 | G::SupportType: LocalAnalysis<F, BTNew::Agg, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
145 | { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
146 | BTFN::construct_arc(domain, depth, self.generator) |
| 5 | 147 | } |
| 148 | } | |
| 149 | ||
| 150 | /// A BTFN with no bisection tree. | |
| 151 | /// | |
| 152 | /// Most BTFN methods are not available, but if a BTFN is going to be summed with another | |
| 153 | /// before other use, it will be more efficient to not construct an unnecessary bisection tree | |
| 154 | /// that would be shortly dropped. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
155 | pub type PreBTFN<F, G, const N: usize> = BTFN<F, G, (), N>; |
| 0 | 156 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
157 | impl<F: Float, G, const N: usize> PreBTFN<F, G, N> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
158 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
159 | G: SupportGenerator<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
160 | { |
| 5 | 161 | /// Create a new [`PreBTFN`] with no bisection tree. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
162 | pub fn new_pre(generator: G) -> Self { |
| 0 | 163 | BTFN { |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
164 | bt: (), |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
165 | generator: Arc::new(generator), |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
166 | _phantoms: std::marker::PhantomData, |
| 0 | 167 | } |
| 168 | } | |
| 169 | } | |
| 170 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
171 | impl<F: Float, G, BT, const N: usize> BTFN<F, G, BT, N> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
172 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
173 | G: SupportGenerator<F, N, Id = usize>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
174 | G::SupportType: LocalAnalysis<F, BT::Agg, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
175 | BT: BTImpl<F, N, Data = usize>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
176 | { |
| 0 | 177 | /// Helper function for implementing [`std::ops::Add`]. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
178 | fn add_another<G2>(&self, g2: Arc<G2>) -> BTFN<F, BothGenerators<G, G2>, BT, N> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
179 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
180 | G2: SupportGenerator<F, N, Id = usize>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
181 | G2::SupportType: LocalAnalysis<F, BT::Agg, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
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 { | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
191 | bt: bt, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
192 | generator: Arc::new(both), |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
193 | _phantoms: std::marker::PhantomData, |
| 0 | 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, ); | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
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, ); | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
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) => { | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
281 | impl<F: Float, G, BT, const N: usize> std::ops::$trait_assign<F> for BTFN<F, G, BT, N> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
282 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
283 | BT: BTImpl<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
284 | G: SupportGenerator<F, N, Id = BT::Data>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
285 | G::SupportType: LocalAnalysis<F, BT::Agg, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
286 | { |
| 0 | 287 | #[inline] |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
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 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
294 | impl<F: Float, G, BT, const N: usize> std::ops::$trait<F> for BTFN<F, G, BT, N> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
295 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
296 | BT: BTImpl<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
297 | G: SupportGenerator<F, N, Id = BT::Data>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
298 | G::SupportType: LocalAnalysis<F, BT::Agg, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
299 | { |
| 0 | 300 | type Output = Self; |
| 301 | #[inline] | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
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 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
309 | impl<'a, F: Float, G, BT, const N: usize> std::ops::$trait<F> for &'a BTFN<F, G, BT, N> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
310 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
311 | BT: BTImpl<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
312 | G: SupportGenerator<F, N, Id = BT::Data>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
313 | G::SupportType: LocalAnalysis<F, BT::Agg, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
314 | &'a G: std::ops::$trait<F, Output = G>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
315 | { |
| 0 | 316 | type Output = BTFN<F, G, BT, N>; |
| 317 | #[inline] | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
318 | fn $fn(self, t: F) -> Self::Output { |
| 0 | 319 | self.new_generator(self.generator.$fn(t)) |
| 320 | } | |
| 321 | } | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
322 | }; |
| 0 | 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) => { | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
371 | impl<F: Float, G, BT, const N: usize> std::ops::$trait for BTFN<F, G, BT, N> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
372 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
373 | BT: BTImpl<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
374 | G: SupportGenerator<F, N, Id = BT::Data>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
375 | G::SupportType: LocalAnalysis<F, BT::Agg, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
376 | { |
| 0 | 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 | }*/ | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
399 | }; |
| 0 | 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 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
408 | impl<F: Float, G, BT, V, const N: usize> Mapping<Loc<F, N>> for BTFN<F, G, BT, N> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
409 | where |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
410 | BT: BTImpl<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
411 | G: SupportGenerator<F, N, Id = BT::Data>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
412 | G::SupportType: LocalAnalysis<F, BT::Agg, N> + Mapping<Loc<F, N>, Codomain = V>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
413 | V: Sum + Space, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
414 | { |
|
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
415 | type Codomain = V; |
| 0 | 416 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
417 | fn apply<I: Instance<Loc<F, N>>>(&self, x: I) -> Self::Codomain { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
418 | let xc = x.cow(); |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
419 | self.bt |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
420 | .iter_at(&*xc) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
421 | .map(|&d| self.generator.support_for(d).apply(&*xc)) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
422 | .sum() |
| 0 | 423 | } |
| 424 | } | |
| 425 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
426 | impl<F: Float, G, BT, V, const N: usize> DifferentiableImpl<Loc<F, N>> for BTFN<F, G, BT, N> |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
427 | where |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
428 | BT: BTImpl<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
429 | G: SupportGenerator<F, N, Id = BT::Data>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
430 | G::SupportType: |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
431 | LocalAnalysis<F, BT::Agg, N> + DifferentiableMapping<Loc<F, N>, DerivativeDomain = V>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
432 | V: Sum + Space, |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
433 | { |
|
47
a0db98c16ab5
Some Differentiable simplifications and clarifications
Tuomo Valkonen <tuomov@iki.fi>
parents:
29
diff
changeset
|
434 | type Derivative = V; |
|
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
435 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
436 | fn differential_impl<I: Instance<Loc<F, N>>>(&self, x: I) -> Self::Derivative { |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
437 | let xc = x.cow(); |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
438 | self.bt |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
439 | .iter_at(&*xc) |
|
59
9226980e45a7
Significantly simplify Mapping / Apply through Instance
Tuomo Valkonen <tuomov@iki.fi>
parents:
47
diff
changeset
|
440 | .map(|&d| self.generator.support_for(d).differential(&*xc)) |
|
27
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
441 | .sum() |
|
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
442 | } |
|
00029c20c0ee
Implement Differentiate for BTFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
443 | } |
|
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 | // GlobalAnalysis |
|
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 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
449 | impl<F: Float, G, BT, const N: usize> GlobalAnalysis<F, BT::Agg> for BTFN<F, G, BT, N> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
450 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
451 | BT: BTImpl<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
452 | G: SupportGenerator<F, N, Id = BT::Data>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
453 | G::SupportType: LocalAnalysis<F, BT::Agg, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
454 | { |
| 0 | 455 | #[inline] |
| 456 | fn global_analysis(&self) -> BT::Agg { | |
| 457 | self.bt.global_analysis() | |
| 458 | } | |
| 459 | } | |
| 460 | ||
| 461 | // | |
| 462 | // Blanket implementation of BTFN as a linear functional over objects | |
| 463 | // that are linear functionals over BTFN. | |
| 464 | // | |
| 465 | ||
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
466 | /* |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
467 | 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
|
468 | 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
|
469 | 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
|
470 | 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
|
471 | 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
|
472 | 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
|
473 | |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
474 | #[inline] |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
475 | 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
|
476 | 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
|
477 | } |
|
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
478 | } |
|
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<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
|
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 : 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 | |
| 0 | 493 | impl<X, F : Float, G, BT, const N : usize> Linear<X> |
| 494 | for BTFN<F, G, BT, N> | |
| 495 | where BT : BTImpl<F, N>, | |
| 496 | G : SupportGenerator<F, N, Id=BT::Data>, | |
| 497 | 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
|
498 | X : for<'a> Apply<&'a BTFN<F, G, BT, N>, F> { |
| 0 | 499 | type Codomain = F; |
| 500 | } | |
|
13
465fa2121ccb
Better Linear and Mapping structure that can provide consuming and reference `apply`.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
501 | */ |
| 0 | 502 | |
| 503 | /// Helper trait for performing approximate minimisation using P2 elements. | |
| 5 | 504 | /// |
| 505 | /// `U` is the domain, generally [`Loc`]`<F, N>`, and `F` the type of floating point numbers. | |
| 506 | /// `Self` is generally a set of `U`, for example, [`Cube`]`<F, N>`. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
507 | pub trait P2Minimise<U: Space, F: Float>: Set<U> { |
| 5 | 508 | /// Minimise `g` over the set presented by `Self`. |
| 509 | /// | |
| 510 | /// The function returns `(x, v)` where `x` is the minimiser `v` an approximation of `g(x)`. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
511 | fn p2_minimise<G: Fn(&U) -> F>(&self, g: G) -> (U, F); |
| 0 | 512 | } |
| 513 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
514 | impl<F: Float> P2Minimise<Loc<F, 1>, F> for Cube<F, 1> { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
515 | fn p2_minimise<G: Fn(&Loc<F, 1>) -> F>(&self, g: G) -> (Loc<F, 1>, F) { |
| 0 | 516 | let interval = Simplex(self.corners()); |
| 517 | interval.p2_model(&g).minimise(&interval) | |
| 518 | } | |
| 519 | } | |
| 520 | ||
| 521 | #[replace_float_literals(F::cast_from(literal))] | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
522 | impl<F: Float> P2Minimise<Loc<F, 2>, F> for Cube<F, 2> { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
523 | fn p2_minimise<G: Fn(&Loc<F, 2>) -> F>(&self, g: G) -> (Loc<F, 2>, F) { |
| 0 | 524 | if false { |
| 525 | // Split into two triangle (simplex) with separate P2 model in each. | |
| 526 | // The six nodes of each triangle are the corners and the edges. | |
| 527 | let [a, b, c, d] = self.corners(); | |
| 528 | let [va, vb, vc, vd] = [g(&a), g(&b), g(&c), g(&d)]; | |
| 529 | ||
| 530 | let ab = midpoint(&a, &b); | |
| 531 | let bc = midpoint(&b, &c); | |
| 532 | let ca = midpoint(&c, &a); | |
| 533 | let cd = midpoint(&c, &d); | |
| 534 | let da = midpoint(&d, &a); | |
| 535 | let [vab, vbc, vca, vcd, vda] = [g(&ab), g(&bc), g(&ca), g(&cd), g(&da)]; | |
| 536 | ||
| 537 | let s1 = Simplex([a, b, c]); | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
538 | let m1 = |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
539 | P2LocalModel::<F, 2, 3>::new(&[a, b, c, ab, bc, ca], &[va, vb, vc, vab, vbc, vca]); |
| 0 | 540 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
541 | let r1 @ (_, v1) = m1.minimise(&s1); |
| 0 | 542 | |
| 543 | let s2 = Simplex([c, d, a]); | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
544 | let m2 = |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
545 | P2LocalModel::<F, 2, 3>::new(&[c, d, a, cd, da, ca], &[vc, vd, va, vcd, vda, vca]); |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
546 | |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
547 | let r2 @ (_, v2) = m2.minimise(&s2); |
| 0 | 548 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
549 | if v1 < v2 { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
550 | r1 |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
551 | } else { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
552 | r2 |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
553 | } |
| 0 | 554 | } else { |
| 555 | // Single P2 model for the entire cube. | |
| 556 | let [a, b, c, d] = self.corners(); | |
| 557 | let [va, vb, vc, vd] = [g(&a), g(&b), g(&c), g(&d)]; | |
| 558 | let [e, f] = match 'r' { | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
559 | 'm' => [(&a + &b + &c) / 3.0, (&c + &d + &a) / 3.0], |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
560 | 'c' => [midpoint(&a, &b), midpoint(&a, &d)], |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
561 | 'w' => [(&a + &b * 2.0) / 3.0, (&a + &d * 2.0) / 3.0], |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
562 | 'r' => { |
| 0 | 563 | // Pseudo-randomise edge midpoints |
| 564 | let Loc([x, y]) = a; | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
565 | let tmp: f64 = (x + y).as_(); |
| 0 | 566 | match tmp.to_bits() % 4 { |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
567 | 0 => [midpoint(&a, &b), midpoint(&a, &d)], |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
568 | 1 => [midpoint(&c, &d), midpoint(&a, &d)], |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
569 | 2 => [midpoint(&a, &b), midpoint(&b, &c)], |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
570 | _ => [midpoint(&c, &d), midpoint(&b, &c)], |
| 0 | 571 | } |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
572 | } |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
573 | _ => [self.center(), (&a + &b) / 2.0], |
| 0 | 574 | }; |
| 575 | let [ve, vf] = [g(&e), g(&f)]; | |
| 576 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
577 | let m1 = P2LocalModel::<F, 2, 3>::new(&[a, b, c, d, e, f], &[va, vb, vc, vd, ve, vf]); |
| 0 | 578 | |
| 579 | m1.minimise(self) | |
| 580 | } | |
| 581 | } | |
| 582 | } | |
| 583 | ||
| 5 | 584 | /// Helper type to use [`P2Refiner`] for maximisation. |
| 0 | 585 | struct RefineMax; |
| 5 | 586 | |
| 587 | /// Helper type to use [`P2Refiner`] for minimisation. | |
| 0 | 588 | struct RefineMin; |
| 589 | ||
| 5 | 590 | /// A bisection tree [`Refiner`] for maximising or minimising a [`BTFN`]. |
| 591 | /// | |
| 0 | 592 | /// The type parameter `T` should be either [`RefineMax`] or [`RefineMin`]. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
593 | struct P2Refiner<F: Float, T> { |
| 5 | 594 | /// The maximum / minimum should be above / below this threshold. |
| 595 | /// If the threshold cannot be satisfied, the refiner will return `None`. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
596 | bound: Option<F>, |
| 5 | 597 | /// Tolerance for function value estimation. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
598 | tolerance: F, |
| 5 | 599 | /// Maximum number of steps to execute the refiner for |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
600 | max_steps: usize, |
| 5 | 601 | /// Either [`RefineMax`] or [`RefineMin`]. Used only for type system purposes. |
| 0 | 602 | #[allow(dead_code)] // `how` is just for type system purposes. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
603 | how: T, |
| 0 | 604 | } |
| 605 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
606 | impl<F: Float, G, const N: usize> Refiner<F, Bounds<F>, G, N> for P2Refiner<F, RefineMax> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
607 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
608 | Cube<F, N>: P2Minimise<Loc<F, N>, F>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
609 | G: SupportGenerator<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
610 | G::SupportType: Mapping<Loc<F, N>, Codomain = F> + LocalAnalysis<F, Bounds<F>, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
611 | { |
| 0 | 612 | type Result = Option<(Loc<F, N>, F)>; |
| 613 | type Sorting = UpperBoundSorting<F>; | |
| 614 | ||
| 615 | fn refine( | |
| 616 | &self, | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
617 | aggregator: &Bounds<F>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
618 | cube: &Cube<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
619 | data: &[G::Id], |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
620 | generator: &G, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
621 | step: usize, |
| 0 | 622 | ) -> RefinerResult<Bounds<F>, Self::Result> { |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
623 | if self |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
624 | .bound |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
625 | .map_or(false, |b| aggregator.upper() <= b + self.tolerance) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
626 | { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
627 | // The upper bound is below the maximisation threshold. Don't bother with this cube. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
628 | return RefinerResult::Uncertain(*aggregator, None); |
|
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 | |
| 0 | 631 | // g gives the negative of the value of the function presented by `data` and `generator`. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
632 | let g = move |x: &Loc<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
|
633 | let f = move |&d| generator.support_for(d).apply(x); |
| 0 | 634 | -data.iter().map(f).sum::<F>() |
| 635 | }; | |
| 636 | // … 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
|
637 | let (x, _neg_v) = cube.p2_minimise(g); |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
638 | //let v = -neg_v; |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
639 | let v = -g(&x); |
| 0 | 640 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
641 | if step < self.max_steps |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
642 | && (aggregator.upper() > v + self.tolerance/*|| aggregator.lower() > v - self.tolerance*/) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
643 | { |
| 0 | 644 | // The function isn't refined enough in `cube`, so return None |
| 645 | // to indicate that further subdivision is required. | |
| 646 | RefinerResult::NeedRefinement | |
| 647 | } else { | |
| 648 | // The data is refined enough, so return new hopefully better bounds | |
| 649 | // and the maximiser. | |
| 650 | let res = (x, v); | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
651 | let bounds = Bounds(v, v); |
| 0 | 652 | RefinerResult::Uncertain(bounds, Some(res)) |
| 653 | } | |
| 654 | } | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
655 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
656 | fn fuse_results(r1: &mut Self::Result, r2: Self::Result) { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
657 | match (*r1, r2) { |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
658 | (Some((_, v1)), Some((_, v2))) => { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
659 | if v1 < v2 { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
660 | *r1 = r2 |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
661 | } |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
662 | } |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
663 | (None, Some(_)) => *r1 = r2, |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
664 | (_, _) => {} |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
665 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
666 | } |
| 0 | 667 | } |
| 668 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
669 | impl<F: Float, G, const N: usize> Refiner<F, Bounds<F>, G, N> for P2Refiner<F, RefineMin> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
670 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
671 | Cube<F, N>: P2Minimise<Loc<F, N>, F>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
672 | G: SupportGenerator<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
673 | G::SupportType: Mapping<Loc<F, N>, Codomain = F> + LocalAnalysis<F, Bounds<F>, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
674 | { |
| 0 | 675 | type Result = Option<(Loc<F, N>, F)>; |
| 676 | type Sorting = LowerBoundSorting<F>; | |
| 677 | ||
| 678 | fn refine( | |
| 679 | &self, | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
680 | aggregator: &Bounds<F>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
681 | cube: &Cube<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
682 | data: &[G::Id], |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
683 | generator: &G, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
684 | step: usize, |
| 0 | 685 | ) -> RefinerResult<Bounds<F>, Self::Result> { |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
686 | if self |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
687 | .bound |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
688 | .map_or(false, |b| aggregator.lower() >= b - self.tolerance) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
689 | { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
690 | // The lower bound is above the minimisation threshold. Don't bother with this cube. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
691 | return RefinerResult::Uncertain(*aggregator, None); |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
692 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
693 | |
| 0 | 694 | // g gives the value of the function presented by `data` and `generator`. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
695 | let g = move |x: &Loc<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
|
696 | let f = move |&d| generator.support_for(d).apply(x); |
| 0 | 697 | data.iter().map(f).sum::<F>() |
| 698 | }; | |
| 699 | // Minimise it. | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
700 | let (x, _v) = cube.p2_minimise(g); |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
701 | let v = g(&x); |
| 0 | 702 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
703 | if step < self.max_steps |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
704 | && (aggregator.lower() < v - self.tolerance/*|| aggregator.upper() < v + self.tolerance*/) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
705 | { |
| 0 | 706 | // The function isn't refined enough in `cube`, so return None |
| 707 | // to indicate that further subdivision is required. | |
| 708 | RefinerResult::NeedRefinement | |
| 709 | } else { | |
| 710 | // The data is refined enough, so return new hopefully better bounds | |
| 711 | // and the minimiser. | |
| 712 | let res = (x, v); | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
713 | let l = aggregator.lower(); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
714 | let bounds = if l > v { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
715 | eprintln!("imprecision!"); |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
716 | Bounds(l, l) |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
717 | } else { |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
718 | Bounds(v, v) |
|
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
719 | }; |
| 0 | 720 | RefinerResult::Uncertain(bounds, Some(res)) |
| 721 | } | |
| 722 | } | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
723 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
724 | fn fuse_results(r1: &mut Self::Result, r2: Self::Result) { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
725 | match (*r1, r2) { |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
726 | (Some((_, v1)), Some((_, v2))) => { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
727 | if v1 > v2 { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
728 | *r1 = r2 |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
729 | } |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
730 | } |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
731 | (_, Some(_)) => *r1 = r2, |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
732 | (_, _) => {} |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
733 | } |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
734 | } |
| 0 | 735 | } |
| 736 | ||
| 5 | 737 | /// A bisection tree [`Refiner`] for checking that a [`BTFN`] is within a stated |
| 738 | //// upper or lower bound. | |
| 739 | /// | |
| 740 | /// The type parameter `T` should be either [`RefineMax`] for upper bound or [`RefineMin`] | |
| 741 | /// for lower bound. | |
| 0 | 742 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
743 | struct BoundRefiner<F: Float, T> { |
| 5 | 744 | /// The upper/lower bound to check for |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
745 | bound: F, |
| 5 | 746 | /// Tolerance for function value estimation. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
747 | tolerance: F, |
| 5 | 748 | /// Maximum number of steps to execute the refiner for |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
749 | max_steps: usize, |
| 0 | 750 | #[allow(dead_code)] // `how` is just for type system purposes. |
| 5 | 751 | /// Either [`RefineMax`] or [`RefineMin`]. Used only for type system purposes. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
752 | how: T, |
| 0 | 753 | } |
| 754 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
755 | impl<F: Float, G, const N: usize> Refiner<F, Bounds<F>, G, N> for BoundRefiner<F, RefineMax> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
756 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
757 | G: SupportGenerator<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
758 | { |
| 0 | 759 | type Result = bool; |
| 760 | type Sorting = UpperBoundSorting<F>; | |
| 761 | ||
| 762 | fn refine( | |
| 763 | &self, | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
764 | aggregator: &Bounds<F>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
765 | _cube: &Cube<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
766 | _data: &[G::Id], |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
767 | _generator: &G, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
768 | step: usize, |
| 0 | 769 | ) -> RefinerResult<Bounds<F>, Self::Result> { |
| 770 | if aggregator.upper() <= self.bound + self.tolerance { | |
| 771 | // Below upper bound within tolerances. Indicate uncertain success. | |
| 772 | RefinerResult::Uncertain(*aggregator, true) | |
| 773 | } else if aggregator.lower() >= self.bound - self.tolerance { | |
| 774 | // Above upper bound within tolerances. Indicate certain failure. | |
| 775 | RefinerResult::Certain(false) | |
| 776 | } else if step < self.max_steps { | |
| 777 | // No decision possible, but within step bounds - further subdivision is required. | |
| 778 | RefinerResult::NeedRefinement | |
| 779 | } else { | |
| 780 | // No decision possible, but past step bounds | |
| 781 | RefinerResult::Uncertain(*aggregator, false) | |
| 782 | } | |
| 783 | } | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
784 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
785 | fn fuse_results(r1: &mut Self::Result, r2: Self::Result) { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
786 | *r1 = *r1 && r2; |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
787 | } |
| 0 | 788 | } |
| 789 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
790 | impl<F: Float, G, const N: usize> Refiner<F, Bounds<F>, G, N> for BoundRefiner<F, RefineMin> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
791 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
792 | G: SupportGenerator<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
793 | { |
| 5 | 794 | type Result = bool; |
| 795 | type Sorting = UpperBoundSorting<F>; | |
| 796 | ||
| 797 | fn refine( | |
| 798 | &self, | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
799 | aggregator: &Bounds<F>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
800 | _cube: &Cube<F, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
801 | _data: &[G::Id], |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
802 | _generator: &G, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
803 | step: usize, |
| 5 | 804 | ) -> RefinerResult<Bounds<F>, Self::Result> { |
| 805 | if aggregator.lower() >= self.bound - self.tolerance { | |
| 806 | // Above lower bound within tolerances. Indicate uncertain success. | |
| 807 | RefinerResult::Uncertain(*aggregator, true) | |
| 808 | } else if aggregator.upper() <= self.bound + self.tolerance { | |
| 809 | // Below lower bound within tolerances. Indicate certain failure. | |
| 810 | RefinerResult::Certain(false) | |
| 811 | } else if step < self.max_steps { | |
| 812 | // No decision possible, but within step bounds - further subdivision is required. | |
| 813 | RefinerResult::NeedRefinement | |
| 814 | } else { | |
| 815 | // No decision possible, but past step bounds | |
| 816 | RefinerResult::Uncertain(*aggregator, false) | |
| 817 | } | |
| 818 | } | |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
819 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
820 | fn fuse_results(r1: &mut Self::Result, r2: Self::Result) { |
|
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
821 | *r1 = *r1 && r2; |
|
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
822 | } |
| 5 | 823 | } |
| 0 | 824 | |
|
9
f40dfaf2166d
Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents:
8
diff
changeset
|
825 | // 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
|
826 | // 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
|
827 | // 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
|
828 | // 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
|
829 | // 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
|
830 | // 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
|
831 | // 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
|
832 | // 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
|
833 | // 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
|
834 | // `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
|
835 | // 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
|
836 | // 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
|
837 | // 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
|
838 | // 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
|
839 | // the queue may run out, and we get “Refiner failure”. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
840 | impl<F: Float, G, BT, const N: usize> BTFN<F, G, BT, N> |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
841 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
842 | BT: BTSearch<F, N, Agg = Bounds<F>>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
843 | G: SupportGenerator<F, N, Id = BT::Data>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
844 | G::SupportType: Mapping<Loc<F, N>, Codomain = F> + LocalAnalysis<F, Bounds<F>, N>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
845 | Cube<F, N>: P2Minimise<Loc<F, N>, F>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
846 | { |
| 5 | 847 | /// Maximise the `BTFN` within stated value `tolerance`. |
| 848 | /// | |
| 849 | /// At most `max_steps` refinement steps are taken. | |
| 850 | /// Returns the approximate maximiser and the corresponding function value. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
851 | pub fn maximise(&mut self, tolerance: F, max_steps: usize) -> (Loc<F, N>, F) { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
852 | let refiner = P2Refiner { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
853 | tolerance, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
854 | max_steps, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
855 | how: RefineMax, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
856 | bound: None, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
857 | }; |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
858 | self.bt |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
859 | .search_and_refine(refiner, &self.generator) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
860 | .expect("Refiner failure.") |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
861 | .unwrap() |
| 0 | 862 | } |
| 863 | ||
| 5 | 864 | /// Maximise the `BTFN` within stated value `tolerance` subject to a lower bound. |
| 865 | /// | |
| 866 | /// At most `max_steps` refinement steps are taken. | |
| 867 | /// Returns the approximate maximiser and the corresponding function value when one is found | |
| 868 | /// above the `bound` threshold, otherwise `None`. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
869 | pub fn maximise_above( |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
870 | &mut self, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
871 | bound: F, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
872 | tolerance: F, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
873 | max_steps: usize, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
874 | ) -> Option<(Loc<F, N>, F)> { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
875 | let refiner = P2Refiner { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
876 | tolerance, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
877 | max_steps, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
878 | how: RefineMax, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
879 | bound: Some(bound), |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
880 | }; |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
881 | self.bt |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
882 | .search_and_refine(refiner, &self.generator) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
883 | .expect("Refiner failure.") |
| 0 | 884 | } |
| 885 | ||
| 5 | 886 | /// Minimise the `BTFN` within stated value `tolerance`. |
| 887 | /// | |
| 888 | /// At most `max_steps` refinement steps are taken. | |
| 889 | /// Returns the approximate minimiser and the corresponding function value. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
890 | pub fn minimise(&mut self, tolerance: F, max_steps: usize) -> (Loc<F, N>, F) { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
891 | let refiner = P2Refiner { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
892 | tolerance, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
893 | max_steps, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
894 | how: RefineMin, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
895 | bound: None, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
896 | }; |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
897 | self.bt |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
898 | .search_and_refine(refiner, &self.generator) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
899 | .expect("Refiner failure.") |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
900 | .unwrap() |
| 0 | 901 | } |
| 902 | ||
| 5 | 903 | /// Minimise the `BTFN` within stated value `tolerance` subject to a lower bound. |
| 904 | /// | |
| 905 | /// At most `max_steps` refinement steps are taken. | |
| 906 | /// Returns the approximate minimiser and the corresponding function value when one is found | |
| 907 | /// above the `bound` threshold, otherwise `None`. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
908 | pub fn minimise_below( |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
909 | &mut self, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
910 | bound: F, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
911 | tolerance: F, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
912 | max_steps: usize, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
913 | ) -> Option<(Loc<F, N>, F)> { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
914 | let refiner = P2Refiner { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
915 | tolerance, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
916 | max_steps, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
917 | how: RefineMin, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
918 | bound: Some(bound), |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
919 | }; |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
920 | self.bt |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
921 | .search_and_refine(refiner, &self.generator) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
922 | .expect("Refiner failure.") |
| 0 | 923 | } |
| 5 | 924 | |
| 925 | /// Verify that the `BTFN` has a given upper `bound` within indicated `tolerance`. | |
| 926 | /// | |
| 927 | /// At most `max_steps` refinement steps are taken. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
928 | pub fn has_upper_bound(&mut self, bound: F, tolerance: F, max_steps: usize) -> bool { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
929 | let refiner = BoundRefiner { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
930 | bound, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
931 | tolerance, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
932 | max_steps, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
933 | how: RefineMax, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
934 | }; |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
935 | self.bt |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
936 | .search_and_refine(refiner, &self.generator) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
937 | .expect("Refiner failure.") |
| 0 | 938 | } |
| 5 | 939 | |
| 940 | /// Verify that the `BTFN` has a given lower `bound` within indicated `tolerance`. | |
| 941 | /// | |
| 942 | /// At most `max_steps` refinement steps are taken. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
943 | pub fn has_lower_bound(&mut self, bound: F, tolerance: F, max_steps: usize) -> bool { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
944 | let refiner = BoundRefiner { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
945 | bound, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
946 | tolerance, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
947 | max_steps, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
948 | how: RefineMin, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
949 | }; |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
950 | self.bt |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
951 | .search_and_refine(refiner, &self.generator) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
952 | .expect("Refiner failure.") |
| 5 | 953 | } |
| 0 | 954 | } |