Sat, 06 Sep 2025 23:29:34 -0500
wrap guard interface
|
97
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | /*! |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | Bounded and minimizable/maximizable mappings. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | */ |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | |
|
125
25b6c8b20d1b
Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
5 | use crate::instance::{Instance, Space}; |
|
25b6c8b20d1b
Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
6 | use crate::mapping::Mapping; |
|
97
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
7 | use crate::sets::{Cube, Set}; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
8 | use crate::types::{Float, Num}; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
9 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
10 | /// Trait for globally analysing a property `A` of a [`Mapping`]. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
11 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
12 | /// Typically `A` is an [`Aggregator`][super::aggregator::Aggregator] such as |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
13 | /// [`Bounds`][super::aggregator::Bounds]. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
14 | pub trait GlobalAnalysis<F: Num, A> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
15 | /// Perform global analysis of the property `A` of `Self`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
16 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
17 | /// As an example, in the case of `A` being [`Bounds`][super::aggregator::Bounds], |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
18 | /// this function will return global upper and lower bounds for the mapping |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
19 | /// represented by `self`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
20 | fn global_analysis(&self) -> A; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
21 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
22 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
23 | // default impl<F, A, N, L> GlobalAnalysis<F, A, N> for L |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
24 | // where L : LocalAnalysis<F, A, N> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
25 | // #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
26 | // fn global_analysis(&self) -> Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
27 | // self.local_analysis(&self.support_hint()) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
28 | // } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
29 | // } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
30 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
31 | /// Trait for locally analysing a property `A` of a [`Mapping`] (implementing [`Support`]) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
32 | /// within a [`Cube`]. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
33 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
34 | /// Typically `A` is an [`Aggregator`][super::aggregator::Aggregator] such as |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
35 | /// [`Bounds`][super::aggregator::Bounds]. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
36 | pub trait LocalAnalysis<F: Num, A, const N: usize>: GlobalAnalysis<F, A> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
37 | /// Perform local analysis of the property `A` of `Self`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
38 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
39 | /// As an example, in the case of `A` being [`Bounds`][super::aggregator::Bounds], |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
40 | /// this function will return upper and lower bounds within `cube` for the mapping |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
41 | /// represented by `self`. |
|
124
6aa955ad8122
Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
42 | fn local_analysis(&self, cube: &Cube<N, F>) -> A; |
|
97
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
43 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
44 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
45 | /// Trait for determining the upper and lower bounds of an float-valued [`Mapping`]. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
46 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
47 | /// This is a blanket-implemented alias for [`GlobalAnalysis`]`<F, Bounds<F>>` |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
48 | /// [`Mapping`] is not a supertrait to allow flexibility in the implementation of either |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
49 | /// reference or non-reference arguments. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
50 | pub trait Bounded<F: Float>: GlobalAnalysis<F, Bounds<F>> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
51 | /// Return lower and upper bounds for the values of of `self`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
52 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
53 | fn bounds(&self) -> Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
54 | self.global_analysis() |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
55 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
56 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
57 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
58 | impl<F: Float, T: GlobalAnalysis<F, Bounds<F>>> Bounded<F> for T {} |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
59 | |
|
126
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
60 | /// A real-valued [`Mapping`] that provides rough bounds as well as minimisation and maximisation. |
|
125
25b6c8b20d1b
Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
61 | pub trait MinMaxMapping<Domain: Space, F: Float = f64>: |
|
25b6c8b20d1b
Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
62 | Mapping<Domain, Codomain = F> + Bounded<F> |
|
25b6c8b20d1b
Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
63 | { |
|
97
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
64 | /// Maximise the mapping within stated value `tolerance`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
65 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
66 | /// At most `max_steps` refinement steps are taken. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
67 | /// Returns the approximate maximiser and the corresponding function value. |
|
125
25b6c8b20d1b
Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
68 | fn maximise(&mut self, tolerance: F, max_steps: usize) -> (Domain, F); |
|
97
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
69 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
70 | /// Maximise the mapping within stated value `tolerance` subject to a lower bound. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
71 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
72 | /// At most `max_steps` refinement steps are taken. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
73 | /// Returns the approximate maximiser and the corresponding function value when one is found |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
74 | /// above the `bound` threshold, otherwise `None`. |
|
126
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
75 | fn maximise_above(&mut self, bound: F, tolerance: F, max_steps: usize) -> Option<(Domain, F)> { |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
76 | let res @ (_, v) = self.maximise(tolerance, max_steps); |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
77 | (v > bound).then_some(res) |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
78 | } |
|
97
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
79 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
80 | /// Minimise the mapping within stated value `tolerance`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
81 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
82 | /// At most `max_steps` refinement steps are taken. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
83 | /// Returns the approximate minimiser and the corresponding function value. |
|
125
25b6c8b20d1b
Allow MinMaxMappings on any domain
Tuomo Valkonen <tuomov@iki.fi>
parents:
124
diff
changeset
|
84 | fn minimise(&mut self, tolerance: F, max_steps: usize) -> (Domain, F); |
|
97
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
85 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
86 | /// Minimise the mapping within stated value `tolerance` subject to a lower bound. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
87 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
88 | /// At most `max_steps` refinement steps are taken. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
89 | /// Returns the approximate minimiser and the corresponding function value when one is found |
|
126
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
90 | /// below the `bound` threshold, otherwise `None`. |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
91 | fn minimise_below(&mut self, bound: F, tolerance: F, max_steps: usize) -> Option<(Domain, F)> { |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
92 | let res @ (_, v) = self.minimise(tolerance, max_steps); |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
93 | (v < bound).then_some(res) |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
94 | } |
|
97
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
95 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
96 | /// Verify that the mapping has a given upper `bound` within indicated `tolerance`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
97 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
98 | /// At most `max_steps` refinement steps are taken. |
|
126
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
99 | fn has_upper_bound(&mut self, bound: F, tolerance: F, max_steps: usize) -> bool { |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
100 | match self.maximise_above(bound, tolerance, max_steps) { |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
101 | None => true, |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
102 | Some((_, v)) => v <= bound, |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
103 | } |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
104 | } |
|
97
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
105 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
106 | /// Verify that the mapping has a given lower `bound` within indicated `tolerance`. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
107 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
108 | /// At most `max_steps` refinement steps are taken. |
|
126
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
109 | fn has_lower_bound(&mut self, bound: F, tolerance: F, max_steps: usize) -> bool { |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
110 | match self.minimise_below(bound, tolerance, max_steps) { |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
111 | None => true, |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
112 | Some((_, v)) => v >= bound, |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
113 | } |
|
0ccad3ee8e95
MinMaxMapping default implementations
Tuomo Valkonen <tuomov@iki.fi>
parents:
125
diff
changeset
|
114 | } |
|
97
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
115 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
116 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
117 | /// Upper and lower bounds on an `F`-valued function. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
118 | #[derive(Copy, Clone, Debug)] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
119 | pub struct Bounds<F>( |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
120 | /// Lower bound |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
121 | pub F, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
122 | /// Upper bound |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
123 | pub F, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
124 | ); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
125 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
126 | impl<F: Copy> Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
127 | /// Returns the lower bound |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
128 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
129 | pub fn lower(&self) -> F { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
130 | self.0 |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
131 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
132 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
133 | /// Returns the upper bound |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
134 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
135 | pub fn upper(&self) -> F { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
136 | self.1 |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
137 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
138 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
139 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
140 | impl<F: Float> Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
141 | /// Returns a uniform bound. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
142 | /// |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
143 | /// This is maximum over the absolute values of the upper and lower bound. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
144 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
145 | pub fn uniform(&self) -> F { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
146 | let &Bounds(lower, upper) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
147 | lower.abs().max(upper.abs()) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
148 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
149 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
150 | /// Construct a bounds, making sure `lower` bound is less than `upper` |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
151 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
152 | pub fn corrected(lower: F, upper: F) -> Self { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
153 | if lower <= upper { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
154 | Bounds(lower, upper) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
155 | } else { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
156 | Bounds(upper, lower) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
157 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
158 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
159 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
160 | /// Refine the lower bound |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
161 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
162 | pub fn refine_lower(&self, lower: F) -> Self { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
163 | let &Bounds(l, u) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
164 | debug_assert!(l <= u); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
165 | Bounds(l.max(lower), u.max(lower)) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
166 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
167 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
168 | /// Refine the lower bound |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
169 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
170 | pub fn refine_upper(&self, upper: F) -> Self { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
171 | let &Bounds(l, u) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
172 | debug_assert!(l <= u); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
173 | Bounds(l.min(upper), u.min(upper)) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
174 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
175 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
176 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
177 | impl<'a, F: Float> std::ops::Add<Self> for Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
178 | type Output = Self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
179 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
180 | fn add(self, Bounds(l2, u2): Self) -> Self::Output { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
181 | let Bounds(l1, u1) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
182 | debug_assert!(l1 <= u1 && l2 <= u2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
183 | Bounds(l1 + l2, u1 + u2) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
184 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
185 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
186 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
187 | impl<'a, F: Float> std::ops::Mul<Self> for Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
188 | type Output = Self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
189 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
190 | fn mul(self, Bounds(l2, u2): Self) -> Self::Output { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
191 | let Bounds(l1, u1) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
192 | debug_assert!(l1 <= u1 && l2 <= u2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
193 | let a = l1 * l2; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
194 | let b = u1 * u2; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
195 | // The order may flip when negative numbers are involved, so need min/max |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
196 | Bounds(a.min(b), a.max(b)) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
197 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
198 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
199 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
200 | impl<F: Float> std::iter::Product for Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
201 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
202 | fn product<I>(mut iter: I) -> Self |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
203 | where |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
204 | I: Iterator<Item = Self>, |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
205 | { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
206 | match iter.next() { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
207 | None => Bounds(F::ZERO, F::ZERO), |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
208 | Some(init) => iter.fold(init, |a, b| a * b), |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
209 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
210 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
211 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
212 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
213 | impl<F: Float> Set<F> for Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
214 | fn contains<I: Instance<F>>(&self, item: I) -> bool { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
215 | let v = item.own(); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
216 | let &Bounds(l, u) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
217 | debug_assert!(l <= u); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
218 | l <= v && v <= u |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
219 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
220 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
221 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
222 | impl<F: Float> Bounds<F> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
223 | /// Calculate a common bound (glb, lub) for two bounds. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
224 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
225 | pub fn common(&self, &Bounds(l2, u2): &Self) -> Self { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
226 | let &Bounds(l1, u1) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
227 | debug_assert!(l1 <= u1 && l2 <= u2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
228 | Bounds(l1.min(l2), u1.max(u2)) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
229 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
230 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
231 | /// Indicates whether `Self` is a superset of the argument bound. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
232 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
233 | pub fn superset(&self, &Bounds(l2, u2): &Self) -> bool { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
234 | let &Bounds(l1, u1) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
235 | debug_assert!(l1 <= u1 && l2 <= u2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
236 | l1 <= l2 && u2 <= u1 |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
237 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
238 | |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
239 | /// Returns the greatest bound contained by both argument bounds, if one exists. |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
240 | #[inline] |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
241 | pub fn glb(&self, &Bounds(l2, u2): &Self) -> Option<Self> { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
242 | let &Bounds(l1, u1) = self; |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
243 | debug_assert!(l1 <= u1 && l2 <= u2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
244 | let l = l1.max(l2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
245 | let u = u1.min(u2); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
246 | debug_assert!(l <= u); |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
247 | if l < u { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
248 | Some(Bounds(l, u)) |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
249 | } else { |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
250 | None |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
251 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
252 | } |
|
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
253 | } |