scivex_optim/integrate/mod.rs
1//! Numerical integration (quadrature) algorithms.
2
3mod quad;
4mod simpson;
5mod trapezoid;
6
7pub use quad::quad;
8pub use simpson::simpson;
9pub use trapezoid::trapezoid;
10
11use scivex_core::Float;
12
13/// Result of a numerical integration.
14///
15/// # Examples
16///
17/// ```
18/// # use scivex_optim::integrate::{simpson, QuadResult};
19/// let result = simpson(|x: f64| x * x, 0.0, 1.0, 100).unwrap();
20/// assert!((result.value - 1.0 / 3.0).abs() < 1e-6);
21/// ```
22#[cfg_attr(
23 feature = "serde-support",
24 derive(serde::Serialize, serde::Deserialize)
25)]
26#[derive(Debug, Clone)]
27pub struct QuadResult<T: Float> {
28 /// The estimated value of the integral.
29 pub value: T,
30 /// An estimate of the absolute error.
31 pub error_estimate: T,
32 /// Number of function evaluations.
33 pub n_evals: usize,
34}
35
36/// Options controlling numerical integration.
37///
38/// # Examples
39///
40/// ```
41/// # use scivex_optim::integrate::QuadOptions;
42/// let opts = QuadOptions::<f64>::default();
43/// assert!(opts.abs_tol < 1e-5);
44/// ```
45#[cfg_attr(
46 feature = "serde-support",
47 derive(serde::Serialize, serde::Deserialize)
48)]
49#[derive(Debug, Clone)]
50pub struct QuadOptions<T: Float> {
51 /// Absolute error tolerance.
52 pub abs_tol: T,
53 /// Relative error tolerance.
54 pub rel_tol: T,
55 /// Maximum number of subdivisions (for adaptive methods).
56 pub max_subdivisions: usize,
57}
58
59impl<T: Float> Default for QuadOptions<T> {
60 fn default() -> Self {
61 Self {
62 abs_tol: T::from_f64(1e-10),
63 rel_tol: T::from_f64(1e-10),
64 max_subdivisions: 50,
65 }
66 }
67}