1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use alloc::vec::Vec;
use super::Range;
/// Representing the subrange and the integral estimates
#[derive(Clone, Debug)]
pub struct SubRangeInfo {
/// Subrange
pub range: Range,
/// Result of Gauss-Kronrod integration for subrange
pub estimate: f64,
/// Absolute estimation error
pub delta: f64,
/// Recursion depth of subrange
pub level: usize,
}
impl SubRangeInfo {
#[inline]
pub fn new(range: Range, estimate: f64, delta: f64, level: usize) -> Self {
Self {
range,
estimate,
delta,
level,
}
}
}
/// handles the memory for the subrange ranges, results, and error estimates
#[derive(Clone, Debug)]
pub struct WorkSpace {
/// maxerr = `subranges[order[nrmax]].delta`. nrmax is normally 0 but will be
/// positive if subdivision increased error estimate
pub nrmax: usize,
/// the partition index to be devided into sub partitions next
pub i: usize,
/// current maximum recursion depth
pub maximum_level: usize,
/// vector of dimension at least limit, the elements of which are the
/// subranges
pub subranges: Vec<SubRangeInfo>,
/// vector of dimension at least limit, the first k elements of which are
/// indices to the error estimates over the subranges, such that
/// `subranges[order[0]].delta, ..., subranges[order[n - 1]].delta`
/// form a decreasing sequence
pub order: Vec<usize>,
}
impl WorkSpace {
#[inline]
pub const fn new() -> WorkSpace {
WorkSpace {
nrmax: 0,
i: 0,
maximum_level: 0,
subranges: Vec::new(),
order: Vec::new(),
}
}
#[inline]
pub fn with_capacity(n: usize) -> WorkSpace {
WorkSpace {
nrmax: 0,
i: 0,
maximum_level: 0,
subranges: Vec::with_capacity(n),
order: Vec::with_capacity(n),
}
}
/// return the number of subranges
#[inline]
pub fn size(&self) -> usize {
debug_assert_eq!(self.subranges.len(), self.order.len());
self.subranges.len()
}
#[inline]
pub fn capacity(&self) -> usize {
debug_assert_eq!(self.subranges.capacity(), self.order.capacity());
self.subranges.capacity()
}
#[inline]
pub fn reserve(&mut self, n: usize) {
self.subranges.reserve(n);
self.order.reserve(n);
}
#[inline]
pub fn clear(&mut self) {
self.subranges.clear();
self.order.clear();
self.i = 0;
self.nrmax = 0;
self.maximum_level = 0;
}
#[inline]
pub fn push(&mut self, subrange: SubRangeInfo) {
self.subranges.push(subrange);
self.order.push(self.order.len());
}
pub fn sort_results(&mut self) {
debug_assert_eq!(self.subranges.len(), self.order.len());
let nint = self.size();
if nint == 0 {
return;
}
let subranges = &mut self.subranges;
let order = &mut self.order;
for i in 0..nint {
let i1 = order[i];
let mut e1 = subranges[i1].delta;
let mut i_max = i1;
for &i2 in order.iter().skip(i + 1) {
let e2 = subranges[i2].delta;
if e2 >= e1 {
i_max = i2;
e1 = e2;
}
}
if i_max != i1 {
order[i] = order[i_max];
order[i_max] = i1;
}
}
self.i = order[0];
}
/// append the newly-created subranges to the list
pub fn update(self: &mut WorkSpace, s1: SubRangeInfo, s2: SubRangeInfo) {
debug_assert_eq!(self.subranges.len(), self.order.len());
let new_level = self.subranges[self.i].level + 1;
if s2.delta > s1.delta {
self.subranges[self.i] = s2;
self.subranges.push(s1);
} else {
self.subranges[self.i] = s1;
self.subranges.push(s2);
}
self.order.push(self.order.len());
if new_level > self.maximum_level {
self.maximum_level = new_level;
}
self.qpsrt();
debug_assert_eq!(self.subranges.len(), self.order.len());
}
fn qpsrt(&mut self) {
let last = self.size() - 1;
let limit = self.capacity();
let subranges = &self.subranges;
let order = &mut self.order;
let mut i_nrmax = self.nrmax;
let mut i_maxdelta = order[i_nrmax];
// Check whether the list contains more than two error estimates
if last < 2 {
order[0] = 0;
order[1] = 1;
self.i = i_maxdelta;
return;
}
// search the position for inserting the `order[i_nrmax]`
let deltamax = subranges[i_maxdelta].delta;
while i_nrmax > 0 && deltamax > subranges[order[i_nrmax - 1]].delta {
order[i_nrmax] = order[i_nrmax - 1];
i_nrmax -= 1;
}
// If last < (limit / 2 + 2), then the remaining subranges will not
// divided any more, since the algorithm will split the subrange with
// largest delta.
let top = if last < (limit / 2 + 2) {
last
} else {
limit - last + 1
};
// search the position for inserting the `order[i_nrmax]`
let mut i = i_nrmax + 1;
while i < top && deltamax < subranges[order[i]].delta {
order[i - 1] = order[i];
i += 1;
}
order[i - 1] = i_maxdelta;
// search the position for inserting the `order[last]`
let errmin = subranges[last].delta;
let mut k = top as i64 - 1;
while k >= i as i64 - 1 && errmin >= subranges[order[k as usize]].delta {
order[k as usize + 1] = order[k as usize];
k -= 1;
}
order[(k + 1) as usize] = last;
// Set i_max and e_max
i_maxdelta = order[i_nrmax];
self.i = i_maxdelta;
self.nrmax = i_nrmax;
}
#[inline]
pub fn maximum_level(&self) -> usize {
self.maximum_level
}
/// The smallest range has the largest error. Before bisecting decrease the
/// sum of the errors over the larger ranges (error_over_large_ranges)
/// and perform extrapolation.
pub(crate) fn increase_nrmax(&mut self) -> bool {
let id = self.nrmax;
let order = &self.order;
let limit = self.capacity();
let last = self.size() - 1;
let jupbnd = if last > (1 + limit / 2) {
limit + 1 - last
} else {
last
};
// 最小でない部分区間のうち、最も誤差が大きい部分を次に分割する
for _ in id..=jupbnd {
let i_max = order[self.nrmax];
self.i = i_max;
if self.subranges[i_max].level < self.maximum_level {
return true;
}
self.nrmax += 1;
}
// large range not found
false
}
#[inline]
pub(crate) fn reset_nrmax(&mut self) {
self.nrmax = 0;
self.i = self.order[0];
}
/// retrieve the next subrange
#[inline]
pub fn get(&self) -> &SubRangeInfo {
&self.subranges[self.i]
}
/// calculate the sum of integral estimates for all subranges
#[inline]
pub fn sum_results(self: &WorkSpace) -> f64 {
self.subranges.iter().map(|s| s.estimate).sum()
}
pub fn release(&mut self) {
self.clear();
self.subranges.shrink_to_fit();
self.order.shrink_to_fit();
}
}
impl Default for WorkSpace {
#[inline(always)]
fn default() -> WorkSpace {
WorkSpace::new()
}
}