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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use crate::gaussian_tables::nodes;
use crate::numerical_integration::integrator::{IntegratorMultiVariable, IntegratorSingleVariable};
use crate::numerical_integration::mode::GaussianQuadratureMethod;
use crate::utils::error_codes::CalcError;
/// Default quadrature order (number of nodes).
pub const DEFAULT_QUADRATURE_ORDERS: usize = 4;
/// Configuration shared by the single- and multi-variable Gaussian integrators.
#[derive(Debug, Clone, Copy)]
pub struct GaussianConfig {
/// Number of quadrature nodes (the order). See [`DEFAULT_QUADRATURE_ORDERS`].
pub order: usize,
/// The quadrature family: GaussLegendre, GaussHermite or GaussLaguerre.
pub integration_method: GaussianQuadratureMethod,
}
impl Default for GaussianConfig {
/// Gauss-Legendre at [`DEFAULT_QUADRATURE_ORDERS`]; optimal for most generic polynomial equations.
fn default() -> Self {
GaussianConfig {
order: DEFAULT_QUADRATURE_ORDERS,
integration_method: GaussianQuadratureMethod::GaussLegendre,
}
}
}
impl GaussianConfig {
/// Builds a config with an explicit order and quadrature family.
pub fn from_parameters(order: usize, integration_method: GaussianQuadratureMethod) -> Self {
GaussianConfig {
order,
integration_method,
}
}
/// Validates each integration limit against the method's fixed domain.
///
/// Gauss-Legendre integrates over a finite `[a, b]`; Gauss-Hermite over `(-inf, +inf)`;
/// Gauss-Laguerre over `[0, +inf)`. The canonical domain is required (not ignored) so a
/// mismatched limit cannot silently return a wrong result. `NaN` comparisons are false and
/// are therefore rejected.
fn check_limits<const NUM_INTEGRATIONS: usize>(
&self,
integration_limit: &[[f64; 2]; NUM_INTEGRATIONS],
) -> Result<(), CalcError> {
for limit in integration_limit {
let ok = match self.integration_method {
GaussianQuadratureMethod::GaussLegendre => {
limit[0].is_finite() && limit[1].is_finite() && limit[0] < limit[1]
}
GaussianQuadratureMethod::GaussHermite => {
limit[0] == f64::NEG_INFINITY && limit[1] == f64::INFINITY
}
GaussianQuadratureMethod::GaussLaguerre => {
limit[0] == 0.0 && limit[1] == f64::INFINITY
}
};
if !ok {
return Err(CalcError::IntegrationLimitsIllDefined);
}
}
Ok(())
}
}
/// Implements the gaussian quadrature methods for numerical integration for single variable functions
#[derive(Debug, Clone, Copy, Default)]
pub struct GaussianSingle {
pub config: GaussianConfig,
}
impl GaussianSingle {
/// custom constructor, optimal for fine-tuning for specific cases
pub fn from_parameters(order: usize, integration_method: GaussianQuadratureMethod) -> Self {
GaussianSingle {
config: GaussianConfig::from_parameters(order, integration_method),
}
}
/// Gauss-Legendre over a finite `[a, b]`: nodes (defined on `[-1, 1]`) are affine-mapped
/// by `(b-a)/2 * x + (b+a)/2` and the result scaled by `(b-a)/2`. Inner folds of a
/// single-variable integral are constant in the outer variable, so the inner result is
/// computed once and reused.
fn integrate_legendre<F: Fn(f64) -> f64, const NUM_INTEGRATIONS: usize>(
&self,
level: usize,
table: &'static [(f64, f64)],
func: &F,
integration_limit: &[[f64; 2]; NUM_INTEGRATIONS],
) -> f64 {
let a = integration_limit[level - 1][0];
let b = integration_limit[level - 1][1];
let half = (b - a) / 2.0;
let mid = (b + a) / 2.0;
if level == 1 {
let mut ans = 0.0;
for &(weight, abscissa) in table {
ans += weight * func(half * abscissa + mid);
}
return half * ans;
}
let inner = self.integrate_legendre(level - 1, table, func, integration_limit);
let mut ans = 0.0;
for &(weight, _) in table {
ans += weight * inner;
}
half * ans
}
/// Gauss-Hermite / Gauss-Laguerre over their fixed domain: nodes are used as-is with no
/// affine map and no exponential factor, since the tabulated weights already carry the
/// `e^{-x^2}` / `e^{-x}` weighting function.
fn integrate_canonical<F: Fn(f64) -> f64>(
&self,
level: usize,
table: &'static [(f64, f64)],
func: &F,
) -> f64 {
if level == 1 {
let mut ans = 0.0;
for &(weight, abscissa) in table {
ans += weight * func(abscissa);
}
return ans;
}
let inner = self.integrate_canonical(level - 1, table, func);
let mut ans = 0.0;
for &(weight, _) in table {
ans += weight * inner;
}
ans
}
}
impl IntegratorSingleVariable for GaussianSingle {
/// Integrates `func` by Gaussian quadrature, once for each limit in `integration_limit`
/// (so the array length sets the number of integrations).
///
/// The integrand is passed bare; the tabulated weights carry the implicit weighting
/// function, and each method has a fixed domain:
/// - `GaussLegendre`: a finite `[a, b]`, computing `∫_a^b f(x) dx`.
/// - `GaussHermite`: `[f64::NEG_INFINITY, f64::INFINITY]`, computing `∫ f(x) e^{-x²} dx`.
/// - `GaussLaguerre`: `[0.0, f64::INFINITY]`, computing `∫_0^∞ f(x) e^{-x} dx`.
///
/// # Arguments
/// * `func` - the bare integrand `f(x)`.
/// * `integration_limit` - the limit for each level of integration; each must match the
/// method's fixed domain.
///
/// # Errors
/// [`CalcError::QuadratureOrderOutOfRange`] if the configured order is unsupported, or
/// [`CalcError::IntegrationLimitsIllDefined`] if any limit does not match the method's domain.
///
/// # Examples
/// ```
/// use multicalc::numerical_integration::integrator::IntegratorSingleVariable;
/// use multicalc::numerical_integration::gaussian_integration::GaussianSingle;
///
/// // Gauss-Legendre is exact for polynomials: integral of 4x^3 - 3x^2 over [0, 2] is 8
/// let my_func = |x: f64| 4.0 * x * x * x - 3.0 * x * x;
/// let integrator = GaussianSingle::default();
/// let val = integrator.get(&my_func, &[[0.0, 2.0]; 1]).unwrap();
/// assert!(f64::abs(val - 8.0) < 1e-7);
/// ```
fn get<F: Fn(f64) -> f64, const NUM_INTEGRATIONS: usize>(
&self,
func: &F,
integration_limit: &[[f64; 2]; NUM_INTEGRATIONS],
) -> Result<f64, CalcError> {
let table = nodes(self.config.integration_method, self.config.order)?;
self.config.check_limits(integration_limit)?;
Ok(match self.config.integration_method {
GaussianQuadratureMethod::GaussLegendre => {
self.integrate_legendre(NUM_INTEGRATIONS, table, func, integration_limit)
}
_ => self.integrate_canonical(NUM_INTEGRATIONS, table, func),
})
}
}
/// Implements the gaussian quadrature methods for numerical integration for multi variable functions
#[derive(Debug, Clone, Copy, Default)]
pub struct GaussianMulti {
pub config: GaussianConfig,
}
impl GaussianMulti {
/// custom constructor, optimal for fine-tuning for specific cases
pub fn from_parameters(order: usize, integration_method: GaussianQuadratureMethod) -> Self {
GaussianMulti {
config: GaussianConfig::from_parameters(order, integration_method),
}
}
/// Gauss-Legendre partial integration over a finite `[a, b]`. The affine-mapped node is
/// written into the integrated variable's slot before recursing; the inner fold depends
/// on the outer node, so it is recomputed for each one.
fn integrate_legendre<
F: Fn(&[f64; NUM_VARS]) -> f64,
const NUM_VARS: usize,
const NUM_INTEGRATIONS: usize,
>(
&self,
level: usize,
idx_to_integrate: [usize; NUM_INTEGRATIONS],
table: &'static [(f64, f64)],
func: &F,
integration_limits: &[[f64; 2]; NUM_INTEGRATIONS],
point: &[f64; NUM_VARS],
) -> f64 {
let a = integration_limits[level - 1][0];
let b = integration_limits[level - 1][1];
let half = (b - a) / 2.0;
let mid = (b + a) / 2.0;
let var = idx_to_integrate[level - 1];
let mut current = *point;
let mut ans = 0.0;
if level == 1 {
for &(weight, abscissa) in table {
current[var] = half * abscissa + mid;
ans += weight * func(¤t);
}
return half * ans;
}
for &(weight, abscissa) in table {
current[var] = half * abscissa + mid;
ans += weight
* self.integrate_legendre(
level - 1,
idx_to_integrate,
table,
func,
integration_limits,
¤t,
);
}
half * ans
}
/// Gauss-Hermite / Gauss-Laguerre partial integration over the fixed domain. The node is
/// written into the integrated variable's slot as-is (no map, no exponential factor) and
/// the recursion stays in the same method.
fn integrate_canonical<
F: Fn(&[f64; NUM_VARS]) -> f64,
const NUM_VARS: usize,
const NUM_INTEGRATIONS: usize,
>(
&self,
level: usize,
idx_to_integrate: [usize; NUM_INTEGRATIONS],
table: &'static [(f64, f64)],
func: &F,
point: &[f64; NUM_VARS],
) -> f64 {
let var = idx_to_integrate[level - 1];
let mut current = *point;
let mut ans = 0.0;
if level == 1 {
for &(weight, abscissa) in table {
current[var] = abscissa;
ans += weight * func(¤t);
}
return ans;
}
for &(weight, abscissa) in table {
current[var] = abscissa;
ans += weight
* self.integrate_canonical(level - 1, idx_to_integrate, table, func, ¤t);
}
ans
}
}
impl IntegratorMultiVariable for GaussianMulti {
/// Partially integrates `func` by Gaussian quadrature over the variables in
/// `idx_to_integrate`, once for each limit in `integration_limits` (so the array length
/// sets the number of integrations).
///
/// The integrand is passed bare; the tabulated weights carry the implicit weighting
/// function (see [`GaussianSingle`] for the per-method domains and integral forms).
///
/// # Arguments
/// * `idx_to_integrate` - the variable index integrated at each level.
/// * `func` - the bare integrand.
/// * `integration_limits` - the limit for each level; each must match the method's domain.
/// * `point` - the value of every variable. A variable being integrated holds its final
/// upper limit; a variable held constant holds that constant.
///
/// # Errors
/// [`CalcError::QuadratureOrderOutOfRange`] if the configured order is unsupported, or
/// [`CalcError::IntegrationLimitsIllDefined`] if any limit does not match the method's domain.
///
/// # Examples
/// ```
/// use multicalc::numerical_integration::integrator::IntegratorMultiVariable;
/// use multicalc::numerical_integration::gaussian_integration::GaussianMulti;
///
/// // f(x, y, z) = 2x + yz, integrated over x in [0, 1] with (y, z) = (2, 3); result is 7
/// let my_func = |args: &[f64; 3]| 2.0 * args[0] + args[1] * args[2];
/// let integrator = GaussianMulti::default();
/// let point = [1.0, 2.0, 3.0];
///
/// let val = integrator.get([0; 1], &my_func, &[[0.0, 1.0]; 1], &point).unwrap();
/// assert!(f64::abs(val - 7.0) < 1e-7);
/// ```
fn get<F: Fn(&[f64; NUM_VARS]) -> f64, const NUM_VARS: usize, const NUM_INTEGRATIONS: usize>(
&self,
idx_to_integrate: [usize; NUM_INTEGRATIONS],
func: &F,
integration_limits: &[[f64; 2]; NUM_INTEGRATIONS],
point: &[f64; NUM_VARS],
) -> Result<f64, CalcError> {
let table = nodes(self.config.integration_method, self.config.order)?;
self.config.check_limits(integration_limits)?;
Ok(match self.config.integration_method {
GaussianQuadratureMethod::GaussLegendre => self.integrate_legendre(
NUM_INTEGRATIONS,
idx_to_integrate,
table,
func,
integration_limits,
point,
),
_ => self.integrate_canonical(NUM_INTEGRATIONS, idx_to_integrate, table, func, point),
})
}
}