ocas_eval/numeric/mod.rs
1//! Numerical integration (adaptive Monte Carlo / Vegas) and deterministic
2//! quadrature bridges.
3//!
4//! This module provides a [`Vegas`] integrator implementing the classic
5//! Leppler/Kleisser/R. (Vegas) adaptive-grid Monte Carlo algorithm, together
6//! with a convenience [`integrate_1d`] entry point. Both share the
7//! [`Integrator`] trait so callers can swap methods uniformly.
8//!
9//! The integrands are plain `Fn(&[f64]) -> f64` closures; combine with the
10//! crate's [`ExpressionEvaluator`](crate::ExpressionEvaluator) to integrate
11//! symbolic expressions by wrapping `evaluate` in a closure.
12
13pub mod statistics;
14pub mod vegas;
15
16pub use statistics::StatisticsAccumulator;
17pub use vegas::{IntegrateResult, Integrator, Vegas, VegasOptions};
18
19/// Numerically integrate a one-dimensional function `f` over `[a, b]` using
20/// Vegas with default options. Returns the estimate and standard error.
21///
22/// The integrand receives `x` directly (not a unit-hypercube coordinate): the
23/// linear change of variables is applied internally, so `jacobian = (b − a)`
24/// is folded into the result.
25///
26/// ```
27/// use ocas_eval::numeric::integrate_1d;
28///
29/// let r = integrate_1d(|x| x, 0.0, 1.0, Default::default());
30/// assert!((r.integral - 0.5).abs() < 0.01);
31/// ```
32pub fn integrate_1d<F: Fn(f64) -> f64>(
33 f: F,
34 a: f64,
35 b: f64,
36 opts: VegasOptions,
37) -> IntegrateResult {
38 let width = b - a;
39 let wrapped = move |u: &[f64]| f(a + u[0] * width) * width;
40 let mut vegas = Vegas::new(1, opts);
41 vegas.integrate(&wrapped)
42}