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