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
//! Numerical integration routines for multi-dimensional functions.
//!
//!
//! # Overview
//!
//! This module provides numerical integration routines for approximating the $N$-dimensional
//! integrals of the form,
//! $$
//! I = \int_{\Sigma_{N}} f(\mathbf{x}) d\mathbf{x}
//! $$
//! where $\mathbf{x} = (x_{1}, x_{2}, \dots, x_{N})$ and $\Sigma_{N}$ is an $N$-dimensional
//! hypercube. The dimensionality is limited to $2 \le N \le 15$. The functions $f(x)$ can be
//! either real- or complex-valued and implement the [`Integrand`] trait with associated type
//! [`Integrand::Point`]`=`[`[f64;N]`]. The routines are based primarily on the [DCUHRE] FORTRAN
//! library (Bernsten, Espelid, Genz), however unlike the original algorithm the routines presented
//! in currently only operate on a single function _not_ a vector of functions. The routines use
//! fully symmetric integration [`Rule`]s, with each rule of a particular order $n$ there is a set
//! of five fully symmetric rules used, where one rule of degree $n=2m+1$ is used to obtain an
//! estimate of the integral, $R\[f\]$,
//! $$
//! I = \int_{\Sigma_{N}} f(\mathbf{x}) d\mathbf{x}
//! \approx R\[f\] = \sum_{i = 1}^{L} w_{i} f(\mathbf{x}\_{i})
//! $$
//! where $L$ is the total number of evaluation points $\mathbf{x}\_{i} = (x_{1},\dots,x_{N})$ and
//! $w_{i}$ are the rule weights. In adition there are four _null rules_ of order $2m-1$, $2m-1$,
//! $2m-3$, and $2m-5$, used to calculate,
//! $$
//! N_{j}\[f\] = \sum_{i = 1}^{L} w_{i}^{j} f(\mathbf{x}\_{i}) ~~~~~~ (j = 1,2,3,4)
//! $$
//! evaluated with the same set of points $\mathbf{x}\_{i}$, however the weights $w_{i}^{j}$ are
//! such that a null rule of degree $d$ will integrate to zero all monomials of degree $\le d$.
//! These are used in the estimation of the error.
//!
//! The algorithm and integration rules are outlined in,
//! - Bernsten, Espelid, & Genz. 1991. Algorithm 698: DCUHRE: an adaptive multidemensional
//! integration routine for a vector of integrals. ACM Trans. Math. Softw. 17, 4 (Dec. 1991),
//! 452–456. <https://doi.org/10.1145/210232.210234>
//! - Bernsten, Espelid, & Genz. 1991. An adaptive algorithm for the approximate calculation of
//! multiple integrals. ACM Trans. Math. Softw. 17, 4 (Dec. 1991), 437–451.
//! <https://doi.org/10.1145/210232.210233>
//!
//! [`Integrand`]: crate::Integrand
//! [`Integrand::Point`]: crate::Integrand::Point
//!
//! # Available integrator routines
//!
//! The module provides two classes of routine:
//!
//! - [`Basic`]: A non-adaptive routine which applies a fully-symmetric integration [`Rule`] to a
//! function exactly once. Rules of different order are available, and are generated through the
//! `Rule*::generate` constructors of specific type alias' for each rule.
//!
//! - [`Adaptive`]: A $2 \le N \le 15$ dimensional adaptive routine. On each iteration of the
//! algorithm the axis along which the largest contribution to the error estimate was obtained is
//! used as the bisection axis to bisect the integration region and then calculate new estimates
//! for these newly bisected volumes. This concentrates the integration refinement to the regions
//! with highest error, rapidly reducing the numerical error of the routine. The algorithm uses
//! fully-symmetric integration rules, [`Rule`], of varying order and generality.
//!
//! [DCUHRE]: <https://dl.acm.org/doi/10.1145/210232.210234>
//!
//! # Examples
//!
//! ## [`Basic`] integrator example
//!
//! Here we present a calculation of [Catalan's constant] $G$ using the integral representation:
//! $$
//! G = \int_{0}^{1} \int_{0}^{1} \frac{dxdy}{1 + x^{2} y^{2}},
//! $$
//! which is a smooth integral over the integration region and can be easily integrated with the
//! [`Basic`] routine.
//!
//!```rust
//! use rint::{Limits, Integrand, Tolerance};
//! use rint::multi::{Basic, Rule13};
//!
//! const N: usize = 2;
//! const G: f64 = 0.915_965_594_177_219_015_054_603_514_932_384_110_774;
//!
//! struct Catalan;
//!
//! impl Integrand for Catalan {
//! type Point = [f64; N];
//! type Scalar = f64;
//! fn evaluate(&self, coordinates: &[f64; N]) -> Self::Scalar {
//! let [x, y] = coordinates;
//! 1.0 / (1.0 + x.powi(2) * y.powi(2))
//! }
//! }
//!
//! # use std::error::Error;
//! # fn main() -> Result<(), Box<dyn Error>> {
//! let catalan = Catalan;
//! let limits = [Limits::new(0.0,1.0)?;N];
//! let rule = Rule13::generate();
//! let integral = Basic::new(&catalan, &rule, limits)?.integrate();
//!
//! let result = integral.result();
//! let error = integral.error();
//! let abs_actual_error = (G - result).abs();
//! let iters = integral.iterations();
//! assert_eq!(iters, 1);
//! assert!(abs_actual_error < error);
//! # Ok(())
//! # }
//!```
//!
//!
//! ## [`Adaptive`] integrator example
//!
//! The following example integtates a 4-dimensional function $f(\mathbf{x})$,
//! $$
//! f(\mathbf{x}) = \frac{x_{3}^{2} x_{4} e^{x_{3} x_{4}}}{(1 + x_{1} + x_{2})^{2}}
//! $$
//! where $\mathbf{x} = (x_{1}, x_{2}, x_{3}, x_{4})$ over an $N=4$ dimensional hypercube
//! $((0,1),(0,1),(0,2),(0,1))$ using a fully-symmetric 7-point adaptive algorithm.
//! Adapted from P. van Dooren & L. de Ridder, "An adaptive algorithm for numerical integration over
//! an n-dimensional cube", J. Comp. App. Math., Vol. 2, (1976) 207-217
//!
//!
//!```rust
//! use rint::{Limits, Integrand, Tolerance};
//! use rint::multi::{Adaptive, Rule07};
//!
//! const N: usize = 4;
//!
//! struct F;
//!
//! impl Integrand for F {
//! type Point = [f64; N];
//! type Scalar = f64;
//! fn evaluate(&self, coordinates: &[f64; N]) -> Self::Scalar {
//! let [x1, x2, x3, x4] = coordinates;
//! x3.powi(2) * x4 * (x3 * x4).exp() / (x1 + x2 + 1.0).powi(2)
//! }
//! }
//!
//! # use std::error::Error;
//! # fn main() -> Result<(), Box<dyn Error>> {
//! const TARGET: f64 = 5.753_641_449_035_616e-1;
//! const TOL: f64 = 1e-2;
//!
//! let function = F;
//! let limits = [
//! Limits::new(0.0, 1.0)?,
//! Limits::new(0.0, 1.0)?,
//! Limits::new(0.0, 1.0)?,
//! Limits::new(0.0, 2.0)?
//! ];
//! let rule = Rule07::<N>::generate()?;
//! let tolerance = Tolerance::Relative(TOL);
//!
//! let integral = Adaptive::new(&function, &rule, limits, tolerance, 10000)?.integrate()?;
//!
//! let result = integral.result();
//! let error = integral.error();
//! let actual_error = (result - TARGET).abs();
//! let requested_error = TOL * result.abs();
//!
//! assert!(actual_error < error);
//! assert!(error < requested_error);
//! # Ok(())
//! # }
//!```
pub
pub use Adaptive;
pub use Basic;
pub use Integrator;
pub use Region;
pub use ;
pub const
pub const