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
291
292
293
294
295
296
297
use core::fmt::Debug;
macro_rules! impl_error {
(
$(#[$outer:meta])*
pub enum $name:ident {
$($chunk:tt)*
}
) => {
impl_error!(
@PARSE_INNER
meta $(#[$outer])*
name $name
queue [ $($chunk)* ]
items []
);
};
(
@PARSE_INNER
meta $(#[$outer:meta])*
name $name:ident
queue []
items [ $($item:tt)* ]
) => {
impl_error!(
@WRITE
meta $(#[$outer])*
name $name
items [ $($item)* ]
);
};
(
@PARSE_INNER
meta $(#[$outer:meta])*
name $name:ident
queue [ $var:ident { $desc:expr, $msg:expr, }, $($chunk:tt)* ]
items [ $($item:tt)* ]
) => {
impl_error!(
@PARSE_INNER
meta $(#[$outer])*
name $name
queue [ $($chunk)* ]
items [
$($item)*
{
var $var
args []
args2 []
args3 []
desc ($desc)
msg ($msg)
}
]
);
};
(
@PARSE_INNER
meta $(#[$outer:meta])*
name $name:ident
queue [ $var:ident ($($arg:ident: $argtype:ty),*) { $desc:expr, $msg:expr, }, $($chunk:tt)* ]
items [ $($item:tt)* ]
) => {
impl_error!(
@PARSE_INNER
meta $(#[$outer])*
name $name
queue [ $($chunk)* ]
items [
$($item)*
{
var $var
args [
$({
arg $arg
argtype $argtype
})*
]
args2 [ ($($arg),*) ]
args3 [ ($($argtype),*) ]
desc ($desc)
msg ($msg)
}
]
);
};
(
@WRITE
meta $(#[$outer:meta])*
name $name:ident
items [
$({
var $var:ident
args [
$({
arg $arg:ident
argtype $argtype:ty
})*
]
args2 [ $($args2:tt)* ]
args3 [ $($args3:tt)* ]
desc ($desc:expr)
msg ($msg:expr)
})*
]
) => {
$(#[$outer])*
pub enum $name {
$(
#[doc=$msg]
$var $($args3)*,
)*
}
impl ::core::fmt::Display for $name {
#[allow(unused_variables)]
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
match self {
$(
Self::$var $($args2)* => write!(f, $desc),
)*
}
}
}
impl ::core::fmt::Debug for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
match self {
$(
Self::$var $($args2)* => write!(f, $msg $(,$arg)*),
)*
}
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl ::std::error::Error for $name {}
};
}
impl_error!(
#[doc = "calculation error information occured during integration."]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RuntimeError {
InsufficientIteration {
"number of iteration was insufficient",
"The maximum number of subdivisions has been achieved.\n\
If increasing the limit results in no further improvement, \
check the integrand in order to determine the difficulties. \
If the function contains the singular points in the range, \
you should specify the singular points by hand, or transform \
the function to eliminate the singular points.",
},
RoundoffError {
"cannot reach tolerance because of roundoff error",
"Cannot reach tolerance because of roundoff error, \
which prevents the given tolerance from being achieved.\n\
It is assumed that the requested tolerance cannot be achieved, and \
that the returned result is the bst which can be obtained.",
},
SubrangeTooSmall {
"subrange is too small to calculate the integral",
"Subrange was too small to calculate the integral.\n\
Maybe you should specify the singular points, or transform the \
function to eliminate the singular points.",
},
Divergent {
"integral is divergent, or slowly convergent",
"Integral is divergent, or slowly convergent.\n\
Delta (estimation of absolute error) may be underestimated.",
},
NanValueEncountered {
"integrand has returned a NAN value",
"Integrand has returned a NAN value, so the algorithm cannot \
continue the calculation.",
},
}
);
/// Store the estimation of integral and estimated absolute error (delta) of
/// calculation.
#[derive(Debug, Clone)]
pub struct IntegrationResult {
pub(crate) estimate: f64,
pub(crate) delta: f64,
pub(crate) error: Option<RuntimeError>,
}
impl IntegrationResult {
/// Create `IntegrationResult` object
#[inline]
pub fn new(estimate: f64, delta: f64, error: Option<RuntimeError>) -> Self {
Self {
estimate,
delta,
error,
}
}
/// Get the estimation of integral.
///
/// Returns `Err` if integration error has occured.
#[inline]
pub fn estimate(&self) -> Result<f64, RuntimeError> {
match self.error {
Some(error) => Err(error),
None => Ok(self.estimate),
}
}
/// Get the estimated absolute error of integral.
///
/// Returns `Err` if integration error has occured.
#[inline]
pub fn delta(&self) -> Result<f64, RuntimeError> {
match self.error {
Some(error) => Err(error),
None => Ok(self.delta),
}
}
/// Get the estimation of integral and absolute error.
///
/// Returns `Err` if integration error has occured.
///
/// # Examples
///
/// ```
/// use gkquad::single::integral;
/// use core::f64::NEG_INFINITY;
///
/// let result = integral(|x: f64| x.exp(), NEG_INFINITY..0.0);
/// let (estimate, delta) = result.estimate_delta().unwrap();
/// ```
#[inline]
pub fn estimate_delta(&self) -> Result<(f64, f64), RuntimeError> {
match self.error {
Some(error) => Err(error),
None => Ok((self.estimate, self.delta)),
}
}
/// Get the estimation of integral even if integration error occured.
///
/// # Safety
///
/// If integration error occured during computation, integration function
/// will return the current estimation of integral anyway.
/// In this case the estimation does not achieve the given tolerance.
///
/// You should use this function **only if** you don't care about the
/// estimation error.
#[inline]
pub unsafe fn estimate_unchecked(&self) -> f64 {
self.estimate
}
/// Get the estimated absolute error even if integration error has occured.
///
/// # Safety
///
/// If integration error occured during computation, integration function
/// will return the current estimation of integral anyway.
/// In this case the estimation does not achieve the given tolerance, and
/// the error estimation (delta) may not be reliable.
#[inline]
pub unsafe fn delta_unchecked(&self) -> f64 {
self.delta
}
/// Get the estimation of integral and absolute error even if integration
/// error has occured.
///
/// # Safety
///
/// See `Safety secion` of `estimate_unchecked` method and `delta_unchecked` method.
#[inline]
pub unsafe fn estimate_delta_unchecked(&self) -> (f64, f64) {
(self.estimate, self.delta)
}
/// Return true if integration error has occured.
#[inline]
pub fn has_err(&self) -> bool {
self.error.is_some()
}
/// Get integration error.
///
/// Return `None` if any integration error has not occured.
#[inline]
pub fn err(&self) -> Option<RuntimeError> {
self.error
}
}