Skip to main content

Adaptive

Struct Adaptive 

Source
pub struct Adaptive<'a, I> { /* private fields */ }
Expand description

An adaptive Gauss-Kronrod integrator.

The Adaptive integrator applies a Gauss-Kronrod integration Rule to approximate the integral of a one-dimensional function. Unlike the Basic routine, the routine implemented by Adaptive is adaptive. After the initial integration, each iteration of the routine picks the previous integration area which has the largest error estimate and bisects this region, updating the estimate to the integal and the total approximated error. The adaptive routine will return the first approximation, result, to the integral which has an absolute error smaller than the tolerance tol encoded through the Tolerance enum, where

  • Tolerance::Absolute specifies absolute tolerance and returns final estimate when error <= tol,
  • Tolerance::Relative specifies a relative error and returns final estimate when error <= tol * abs(result),
  • Tolerance::Either to return a result as soon as either the relative or absolute error bound has been satisfied.

The routine will end when either one of the tolerance conditions have been satisfied or an error has occurred, see Error for more details.

This routine is primarily aimed towards the evaluation of integrals of relatively smooth functions on finite integration domains that are free from singularities. If the function the user wishes to integrate has integrable singularities and/or is defined on an infinite or semi-infinite integration domain then the AdaptiveSingularity integrator should be preferred. In fact, it can often be the case that the AdaptiveSingularity integrator is generally more efficient than the Adaptive routine though this should be benchmarked on a case-by-case basis.

§Example

Here we present a calculation of the integral, $$ I = \int_{0}^{1} x^{\alpha} \ln \frac{1}{x} dx = \frac{1}{(1+\alpha)^{2}} $$ for different values of $\alpha$.

 use rint::{Integrand, Limits, Tolerance};
 use rint::quadrature::{Adaptive, Basic, Rule};

 use std::f64::consts::*;

 struct Function1 {
     alpha: f64,
 }

 impl Integrand for Function1 {
     type Point = f64;
     type Scalar = f64;

     fn evaluate(&self, x: &Self::Point) -> Self::Scalar {
         let alpha = self.alpha;
         x.powf(alpha) * (1.0 / x).ln()
     }
 }

 const TOL: f64 = 1.0e-12;

 let tolerance = Tolerance::Relative(TOL);
 let limits = Limits::new(0.0, 1.0)?;
 let rule = Rule::gk31();
 let max_iterations = 1000;

 let alpha_values = [2.6, PI, EULER_GAMMA, LOG10_E, 100.0, PI.powi(3)];

 for alpha in alpha_values {
     let function = Function1 { alpha };

     let integral = Adaptive::new(&function, &rule, limits, tolerance, max_iterations)?
                     .integrate()?;

     let target = 1.0 / (1.0 + alpha).powi(2);
     let result = integral.result();
     let error = integral.error();
     let abs_actual_error = (result - target).abs();
     let tol = TOL * result.abs();

     assert!(abs_actual_error < error);
     assert!(error < tol);
 }

Implementations§

Source§

impl<'a, I> Adaptive<'a, I>
where I: Integrand<Point = f64>,

Source

pub fn new( function: &'a I, rule: &'a Rule, limits: Limits, tolerance: Tolerance, max_iterations: usize, ) -> Result<Self, InitialisationError>

Generate a new Adaptive integrator.

Initialise an adaptive Gauss-Kronrod integrator. Arguments:

  • function: A user supplied function to be integrated which is something implementing the Integrand trait.
  • rule: An n-point Gauss-Kronrod integration Rule
  • limits: The interval over which the function should be integrated, Limits.
  • tolerance: The tolerance requested by the user. Can be either an absolute tolerance or relative tolerance. Determines the exit condition of the integration routine, see Tolerance.
  • max_iterations: The maximum number of iterations that the adaptive routine should use to try to satisfy the requested tolerance.
§Errors

Function can return an error if it receives bad user input. This is primarily related to using invalid values for the tolerance. The returned error is an InitialisationError. See Tolerance and InitialisationError for more details.

Source

pub fn integrate( &self, ) -> Result<IntegralEstimate<I::Scalar>, IntegrationError<I::Scalar>>

Integrate the function and return a IntegralEstimate integration result.

Adaptively applies the n-point Gauss-Kronrod integration Rule to the user supplied function implementing the Integrand trait to generate an IntegralEstimate upon successful completion.

§Errors

The error type IntegrationError will return both the error IntegrationErrorKind and the IntegralEstimate obtained before an error was encountered. The integration routine has several ways of failing:

Source

pub const fn limits(&self) -> Limits

Return the integration Limits

Trait Implementations§

Source§

impl<'a, I: Clone> Clone for Adaptive<'a, I>

Source§

fn clone(&self) -> Adaptive<'a, I>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, I: Debug> Debug for Adaptive<'a, I>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, I: PartialEq> PartialEq for Adaptive<'a, I>

Source§

fn eq(&self, other: &Adaptive<'a, I>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, I: PartialOrd> PartialOrd for Adaptive<'a, I>

Source§

fn partial_cmp(&self, other: &Adaptive<'a, I>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, I: Copy> Copy for Adaptive<'a, I>

Source§

impl<'a, I> StructuralPartialEq for Adaptive<'a, I>

Auto Trait Implementations§

§

impl<'a, I> Freeze for Adaptive<'a, I>

§

impl<'a, I> RefUnwindSafe for Adaptive<'a, I>
where I: RefUnwindSafe,

§

impl<'a, I> Send for Adaptive<'a, I>
where I: Sync,

§

impl<'a, I> Sync for Adaptive<'a, I>
where I: Sync,

§

impl<'a, I> Unpin for Adaptive<'a, I>

§

impl<'a, I> UnsafeUnpin for Adaptive<'a, I>

§

impl<'a, I> UnwindSafe for Adaptive<'a, I>
where I: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.