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