Fri, 13 Oct 2023 13:32:15 -0500
Update Cargo.lock to stop build failures with current nightly rust.
5 | 1 | /*! |
2 | Aggregation / summarisation of information in branches of bisection trees. | |
3 | */ | |
4 | ||
0 | 5 | use crate::types::*; |
6 | use crate::sets::Set; | |
7 | ||
5 | 8 | /// Trait for aggregating information about a branch of a [bisection tree][super::BT]. |
9 | /// | |
10 | /// Currently [`Bounds`] is the only provided aggregator. | |
11 | /// It keeps track of upper and lower bounds of a function representeed by the `BT` by | |
12 | /// summing [`Bounds`] produced by [`LocalAnalysis`][super::support::LocalAnalysis] of the | |
13 | /// [`Support`][super::support::Support]s of the data stored in the tree. | |
14 | /// For the `Bounds` aggregator: | |
15 | /// * [`Self::aggregate`] sums input bounds to the current bound. This provides a conservative | |
16 | /// estimate of the upper and lower bounds of a sum of functions. | |
17 | /// * [`Self::summarise`] takes the maximum of the input bounds. This calculates the bounds | |
18 | /// of a function on a greater domain from bounds on subdomains | |
19 | /// (in practise [`Cube`][crate::sets::Cube]s). | |
20 | /// | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
21 | pub trait Aggregator : Clone + Sync + Send + 'static + std::fmt::Debug { |
5 | 22 | /// Aggregate a new data to current state. |
0 | 23 | fn aggregate<I>(&mut self, aggregates : I) |
24 | where I : Iterator<Item=Self>; | |
25 | ||
5 | 26 | /// Summarise several other aggregators, resetting current state. |
0 | 27 | fn summarise<'a, I>(&'a mut self, aggregates : I) |
28 | where I : Iterator<Item=&'a Self>; | |
29 | ||
5 | 30 | /// Create a new “empty” aggregate data. |
0 | 31 | fn new() -> Self; |
32 | } | |
33 | ||
34 | /// An [`Aggregator`] that doesn't aggregate anything. | |
35 | #[derive(Clone,Debug)] | |
36 | pub struct NullAggregator; | |
37 | ||
38 | impl Aggregator for NullAggregator { | |
39 | fn aggregate<I>(&mut self, _aggregates : I) | |
40 | where I : Iterator<Item=Self> {} | |
41 | ||
42 | fn summarise<'a, I>(&'a mut self, _aggregates : I) | |
43 | where I : Iterator<Item=&'a Self> {} | |
44 | ||
45 | fn new() -> Self { NullAggregator } | |
46 | } | |
47 | ||
48 | /// Upper and lower bounds on an `F`-valued function. | |
49 | #[derive(Copy,Clone,Debug)] | |
5 | 50 | pub struct Bounds<F>( |
51 | /// Lower bound | |
52 | pub F, | |
53 | /// Upper bound | |
54 | pub F | |
55 | ); | |
0 | 56 | |
5 | 57 | impl<F : Copy> Bounds<F> { |
0 | 58 | /// Returns the lower bound |
59 | #[inline] | |
60 | pub fn lower(&self) -> F { self.0 } | |
61 | ||
62 | /// Returns the upper bound | |
63 | #[inline] | |
64 | pub fn upper(&self) -> F { self.1 } | |
65 | } | |
66 | ||
67 | impl<F : Float> Bounds<F> { | |
5 | 68 | /// Returns a uniform bound. |
69 | /// | |
70 | /// This is maximum over the absolute values of the upper and lower bound. | |
0 | 71 | #[inline] |
72 | pub fn uniform(&self) -> F { | |
73 | let &Bounds(lower, upper) = self; | |
74 | lower.abs().max(upper.abs()) | |
75 | } | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
76 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
77 | /// Construct a bounds, making sure `lower` bound is less than `upper` |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
78 | #[inline] |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
79 | pub fn corrected(lower : F, upper : F) -> Self { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
80 | if lower <= upper { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
81 | Bounds(lower, upper) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
82 | } else { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
83 | Bounds(upper, lower) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
84 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
85 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
86 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
87 | /// Refine the lower bound |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
88 | #[inline] |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
89 | pub fn refine_lower(&self, lower : F) -> Self { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
90 | let &Bounds(l, u) = self; |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
91 | debug_assert!(l <= u); |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
92 | Bounds(l.max(lower), u.max(lower)) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
93 | } |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
94 | |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
95 | /// Refine the lower bound |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
96 | #[inline] |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
97 | pub fn refine_upper(&self, upper : F) -> Self { |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
98 | let &Bounds(l, u) = self; |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
99 | debug_assert!(l <= u); |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
100 | Bounds(l.min(upper), u.min(upper)) |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
101 | } |
0 | 102 | } |
103 | ||
104 | impl<'a, F : Float> std::ops::Add<Self> for Bounds<F> { | |
105 | type Output = Self; | |
106 | #[inline] | |
107 | fn add(self, Bounds(l2, u2) : Self) -> Self::Output { | |
108 | let Bounds(l1, u1) = self; | |
109 | debug_assert!(l1 <= u1 && l2 <= u2); | |
110 | Bounds(l1 + l2, u1 + u2) | |
111 | } | |
112 | } | |
113 | ||
114 | impl<'a, F : Float> std::ops::Mul<Self> for Bounds<F> { | |
115 | type Output = Self; | |
116 | #[inline] | |
117 | fn mul(self, Bounds(l2, u2) : Self) -> Self::Output { | |
118 | let Bounds(l1, u1) = self; | |
119 | debug_assert!(l1 <= u1 && l2 <= u2); | |
120 | let a = l1 * l2; | |
121 | let b = u1 * u2; | |
122 | // The order may flip when negative numbers are involved, so need min/max | |
123 | Bounds(a.min(b), a.max(b)) | |
124 | } | |
125 | } | |
126 | ||
127 | impl<F : Float> std::iter::Product for Bounds<F> { | |
128 | #[inline] | |
129 | fn product<I>(mut iter: I) -> Self | |
130 | where I: Iterator<Item = Self> { | |
131 | match iter.next() { | |
132 | None => Bounds(F::ZERO, F::ZERO), | |
133 | Some(init) => iter.fold(init, |a, b| a*b) | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | impl<F : Float> Set<F> for Bounds<F> { | |
139 | fn contains(&self, item : &F) -> bool { | |
140 | let &Bounds(l, u) = self; | |
141 | debug_assert!(l <= u); | |
142 | l <= *item && *item <= u | |
143 | } | |
144 | } | |
145 | ||
146 | impl<F : Float> Bounds<F> { | |
147 | /// Calculate a common bound (glb, lub) for two bounds. | |
148 | #[inline] | |
149 | pub fn common(&self, &Bounds(l2, u2) : &Self) -> Self { | |
150 | let &Bounds(l1, u1) = self; | |
151 | debug_assert!(l1 <= u1 && l2 <= u2); | |
152 | Bounds(l1.min(l2), u1.max(u2)) | |
153 | } | |
154 | ||
5 | 155 | /// Indicates whether `Self` is a superset of the argument bound. |
0 | 156 | #[inline] |
157 | pub fn superset(&self, &Bounds(l2, u2) : &Self) -> bool { | |
158 | let &Bounds(l1, u1) = self; | |
159 | debug_assert!(l1 <= u1 && l2 <= u2); | |
160 | l1 <= l2 && u2 <= u1 | |
161 | } | |
162 | ||
5 | 163 | /// Returns the greatest bound contained by both argument bounds, if one exists. |
0 | 164 | #[inline] |
165 | pub fn glb(&self, &Bounds(l2, u2) : &Self) -> Option<Self> { | |
166 | let &Bounds(l1, u1) = self; | |
167 | debug_assert!(l1 <= u1 && l2 <= u2); | |
168 | let l = l1.max(l2); | |
169 | let u = u1.min(u2); | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
170 | debug_assert!(l <= u); |
0 | 171 | if l < u { |
172 | Some(Bounds(l, u)) | |
173 | } else { | |
174 | None | |
175 | } | |
176 | } | |
177 | } | |
178 | ||
179 | impl<F : Float> Aggregator for Bounds<F> { | |
180 | #[inline] | |
181 | fn aggregate<I>(&mut self, aggregates : I) | |
182 | where I : Iterator<Item=Self> { | |
183 | *self = aggregates.fold(*self, |a, b| a + b); | |
184 | } | |
185 | ||
186 | #[inline] | |
187 | fn summarise<'a, I>(&'a mut self, mut aggregates : I) | |
188 | where I : Iterator<Item=&'a Self> { | |
189 | *self = match aggregates.next() { | |
190 | None => Bounds(F::ZERO, F::ZERO), // No parts in this cube; the function is zero | |
191 | Some(&bounds) => { | |
192 | aggregates.fold(bounds, |a, b| a.common(b)) | |
193 | } | |
194 | } | |
195 | } | |
196 | ||
197 | #[inline] | |
198 | fn new() -> Self { | |
199 | Bounds(F::ZERO, F::ZERO) | |
200 | } | |
201 | } |