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