Wed, 26 Oct 2022 22:16:57 +0300
Added tag unthreaded for changeset d80b87b8acd0
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 | /// | |
0 | 21 | pub trait Aggregator : Clone + 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 | // TODO: these should probabably also take a Cube as argument to | |
40 | // allow integrating etc. | |
41 | fn aggregate<I>(&mut self, _aggregates : I) | |
42 | where I : Iterator<Item=Self> {} | |
43 | ||
44 | fn summarise<'a, I>(&'a mut self, _aggregates : I) | |
45 | where I : Iterator<Item=&'a Self> {} | |
46 | ||
47 | fn new() -> Self { NullAggregator } | |
48 | } | |
49 | ||
50 | /// Upper and lower bounds on an `F`-valued function. | |
51 | #[derive(Copy,Clone,Debug)] | |
5 | 52 | pub struct Bounds<F>( |
53 | /// Lower bound | |
54 | pub F, | |
55 | /// Upper bound | |
56 | pub F | |
57 | ); | |
0 | 58 | |
5 | 59 | impl<F : Copy> Bounds<F> { |
0 | 60 | /// Returns the lower bound |
61 | #[inline] | |
62 | pub fn lower(&self) -> F { self.0 } | |
63 | ||
64 | /// Returns the upper bound | |
65 | #[inline] | |
66 | pub fn upper(&self) -> F { self.1 } | |
67 | } | |
68 | ||
69 | impl<F : Float> Bounds<F> { | |
5 | 70 | /// Returns a uniform bound. |
71 | /// | |
72 | /// This is maximum over the absolute values of the upper and lower bound. | |
0 | 73 | #[inline] |
74 | pub fn uniform(&self) -> F { | |
75 | let &Bounds(lower, upper) = self; | |
76 | lower.abs().max(upper.abs()) | |
77 | } | |
78 | } | |
79 | ||
80 | impl<'a, F : Float> std::ops::Add<Self> for Bounds<F> { | |
81 | type Output = Self; | |
82 | #[inline] | |
83 | fn add(self, Bounds(l2, u2) : Self) -> Self::Output { | |
84 | let Bounds(l1, u1) = self; | |
85 | debug_assert!(l1 <= u1 && l2 <= u2); | |
86 | Bounds(l1 + l2, u1 + u2) | |
87 | } | |
88 | } | |
89 | ||
90 | impl<'a, F : Float> std::ops::Mul<Self> for Bounds<F> { | |
91 | type Output = Self; | |
92 | #[inline] | |
93 | fn mul(self, Bounds(l2, u2) : Self) -> Self::Output { | |
94 | let Bounds(l1, u1) = self; | |
95 | debug_assert!(l1 <= u1 && l2 <= u2); | |
96 | let a = l1 * l2; | |
97 | let b = u1 * u2; | |
98 | // The order may flip when negative numbers are involved, so need min/max | |
99 | Bounds(a.min(b), a.max(b)) | |
100 | } | |
101 | } | |
102 | ||
103 | impl<F : Float> std::iter::Product for Bounds<F> { | |
104 | #[inline] | |
105 | fn product<I>(mut iter: I) -> Self | |
106 | where I: Iterator<Item = Self> { | |
107 | match iter.next() { | |
108 | None => Bounds(F::ZERO, F::ZERO), | |
109 | Some(init) => iter.fold(init, |a, b| a*b) | |
110 | } | |
111 | } | |
112 | } | |
113 | ||
114 | impl<F : Float> Set<F> for Bounds<F> { | |
115 | fn contains(&self, item : &F) -> bool { | |
116 | let &Bounds(l, u) = self; | |
117 | debug_assert!(l <= u); | |
118 | l <= *item && *item <= u | |
119 | } | |
120 | } | |
121 | ||
122 | impl<F : Float> Bounds<F> { | |
123 | /// Calculate a common bound (glb, lub) for two bounds. | |
124 | #[inline] | |
125 | pub fn common(&self, &Bounds(l2, u2) : &Self) -> Self { | |
126 | let &Bounds(l1, u1) = self; | |
127 | debug_assert!(l1 <= u1 && l2 <= u2); | |
128 | Bounds(l1.min(l2), u1.max(u2)) | |
129 | } | |
130 | ||
5 | 131 | /// Indicates whether `Self` is a superset of the argument bound. |
0 | 132 | #[inline] |
133 | pub fn superset(&self, &Bounds(l2, u2) : &Self) -> bool { | |
134 | let &Bounds(l1, u1) = self; | |
135 | debug_assert!(l1 <= u1 && l2 <= u2); | |
136 | l1 <= l2 && u2 <= u1 | |
137 | } | |
138 | ||
5 | 139 | /// Returns the greatest bound contained by both argument bounds, if one exists. |
0 | 140 | #[inline] |
141 | pub fn glb(&self, &Bounds(l2, u2) : &Self) -> Option<Self> { | |
142 | let &Bounds(l1, u1) = self; | |
143 | debug_assert!(l1 <= u1 && l2 <= u2); | |
144 | let l = l1.max(l2); | |
145 | let u = u1.min(u2); | |
146 | if l < u { | |
147 | Some(Bounds(l, u)) | |
148 | } else { | |
149 | None | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
154 | impl<F : Float> Aggregator for Bounds<F> { | |
155 | #[inline] | |
156 | fn aggregate<I>(&mut self, aggregates : I) | |
157 | where I : Iterator<Item=Self> { | |
158 | *self = aggregates.fold(*self, |a, b| a + b); | |
159 | } | |
160 | ||
161 | #[inline] | |
162 | fn summarise<'a, I>(&'a mut self, mut aggregates : I) | |
163 | where I : Iterator<Item=&'a Self> { | |
164 | *self = match aggregates.next() { | |
165 | None => Bounds(F::ZERO, F::ZERO), // No parts in this cube; the function is zero | |
166 | Some(&bounds) => { | |
167 | aggregates.fold(bounds, |a, b| a.common(b)) | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
172 | #[inline] | |
173 | fn new() -> Self { | |
174 | Bounds(F::ZERO, F::ZERO) | |
175 | } | |
176 | } |