quad-rs 0.4.0

Adaptive Gauss-Kronrod Integration in Rust
Documentation
use ndarray::{Array1, array};
use quad_rs::{ErrorNorm, Integrable, IntegratorConfig, integrate_real};

struct VectorIntegrand;

impl Integrable for VectorIntegrand {
    type Float = f64;
    type Input = f64;
    type Output = Array1<f64>;

    fn integrand(&self, x: &f64) -> Array1<f64> {
        array![x.sin(), x.cos(), x * x,]
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = IntegratorConfig::default()
        .with_absolute_tolerance(1e-12)
        .with_relative_tolerance(1e-12)
        .with_error_norm(ErrorNorm::Max);

    let result = integrate_real(VectorIntegrand, vec![0.0, std::f64::consts::PI], config)?;

    println!("Integral estimate:");
    println!("{:?}", result.integral);

    println!("Error estimate: {}", result.error);
    println!("Evaluations:    {}", result.evaluations);
    println!("Refinements:    {}", result.refinements);

    let expected = array![2.0, 0.0, std::f64::consts::PI.powi(3) / 3.0,];

    println!("Expected:");
    println!("{expected:?}");

    println!("Difference:");
    println!("{:?}", &result.integral - &expected);

    Ok(())
}