Skip to main content

Integrand

Trait Integrand 

Source
pub trait Integrand {
    type Point;
    type Scalar: ScalarF64;

    // Required method
    fn evaluate(&self, x: &Self::Point) -> Self::Scalar;
}
Expand description

A real or complex-valued function to be integrated.

The primary entry point for the library is the Integrand trait. A type implementing the Integrand trait represents a real or complex valued function which is to be integrated. The trait requires the definition of two associated types, Integrand::Point and Integrand::Scalar, and an implementation of the method Integrand::evaluate.

  • Integrand::Point: This associated type defines the point at which the function is to be evaluated, and determines the types of numerical integrators which are available to the user to integrate the function. Integrators are provided for univariate functions $f(x)$ through the associated type Point=f64, while integrators for multivariate functions $f(\mathbf{x})$ are provided through the associated type Point=[f64;N] where $N$ is the dimensionality of the point $\mathbf{x}=(x_{1},\dots,x_{N})$ which is limited to $2 \le N \le 15$.

  • Integrand::Scalar: This is the output type of the function to be integrated. A real valued function should have the output type Scalar=f64, while a complex valued function should have output type Scalar=Complex<f64>.

  • Integrand::evaluate: The trait requires an implementation of an evaluate method, which defines how the function takes the input Integrand::Point and turns this into the output type Integrand::Scalar. In other words, this method tells the integrators how to evaluate the function at a point, allowing the integration to be done.

§Examples

§One-dimensional example

For example, consider probability density function of a normal distribution, $$ f(x) = \frac{1}{\sqrt{2 \pi \sigma^{2}}} e^{- \frac{(x - \mu)^{2}}{2 \sigma^{2}}} $$ which has mean $\mu$ and standard deviation $\sigma$. To integrate this function, one first implements the Integrand trait,

 use rint::Integrand;

 struct NormalDist {
     mean: f64,
     standard_dev: f64,
 }

 impl Integrand for NormalDist {
     type Point = f64;
     type Scalar = f64;
     fn evaluate(&self, x: &Self::Point) -> Self::Scalar {
         let mu = self.mean;
         let sigma = self.standard_dev;

         let prefactor = 1.0 / (2.0 * std::f64::consts::PI * sigma.powi(2));
         let exponent = - (x - mu).powi(2) / (2.0 * sigma.powi(2));

         prefactor * exponent.exp()
     }
 }

The type NormalDist can then be used as the function parameter in one of the numerical integration routines provided by the quadrature module.

§Multi-dimensional example

For example, consider a nonlinear 4-dimensional function $f(\mathbf{x})$, $$ f(\mathbf{x}) = a \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})$, which has some amplitude $a$. To integrate this function, one first implements the Integrand trait,

 use rint::Integrand;

 const N: usize = 4;

 struct F {
     amplitude: f64,
 }

 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;
         let a = self.amplitude;
         a * x3.powi(2) * x4 * (x3 * x4).exp() / (x1 + x2 + 1.0).powi(2)
     }
 }

The type F can then be used as the function parameter in one of the numerical integration routines provided by the multi module.

Required Associated Types§

Source

type Point

The input point at which the function is evaluated.

This associated type defines the point at which the function is to be evaluated, and determines the types of numerical integrators which are available to the user to integrate the function. Integrators are provided for univariate functions $f(x)$ through the associated type Point=f64, while integrators for multivariate functions $f(\mathbf{x})$ are provided through the associated type Point=[f64;N] where $N$ is the dimensionality of the point $\mathbf{x}=(x_{1},\dots,x_{N})$ which is limited to $2 \le N \le 15$.

Source

type Scalar: ScalarF64

The type that the function evaluates to.

This is the output type of the function to be integrated. A real valued function should have the output type Scalar=f64, while a complex valued function should have output type Scalar=Complex<f64>.

Required Methods§

Source

fn evaluate(&self, x: &Self::Point) -> Self::Scalar

Evaluate the function at the real point x

The trait requires an implementation of an evaluate method, which defines how the function takes the input Integrand::Point and turns this into the output type Integrand::Scalar. In other words, this method tells the integrators how to evaluate the function at a point, allowing the integration to be done.

Implementations on Foreign Types§

Source§

impl<I: Integrand> Integrand for &I

Source§

type Point = <I as Integrand>::Point

Source§

type Scalar = <I as Integrand>::Scalar

Source§

fn evaluate(&self, x: &Self::Point) -> Self::Scalar

Implementors§