Skip to main content

TanhSinhEngine

Struct TanhSinhEngine 

Source
pub struct TanhSinhEngine {
    pub tolerance: f64,
    pub max_levels: usize,
    /* private fields */
}
Expand description

Integrates over a finite interval with the substitution x = tanh(π/2 · sinh t) (Takahasi and Mori), which makes the transformed integrand decay double exponentially and lets the trapezoid rule in t converge exponentially fast, even for integrable endpoint singularities such as 1/√x or ln x. The step is halved each level (reusing earlier points) until two successive estimates agree to the tolerance.

Abscissae are formed from the endpoint distance (b − a)/(1 + e^{2u}). Points whose absolute position rounds onto an endpoint are skipped by CanIntegrate1D::integrate; for singularities at endpoints away from zero (such as 1/√(1 − x²)), use TanhSinhEngine::integrate_with_complement, which also passes that exact distance so the integrand can avoid the cancellation in 1 − x.

Fields§

§tolerance: f64

Relative tolerance between successive levels (default 1e-12).

§max_levels: usize

Maximum number of step halvings (default 10, i.e. h = 2⁻¹⁰).

Implementations§

Source§

impl TanhSinhEngine

Source

pub fn new(lower_bound: f64, upper_bound: f64) -> Self

A tanh-sinh integrator over [lower_bound, upper_bound] with the default tolerance and level limit.

use lib_jsl_integration::tanh_sinh::TanhSinhEngine;

// ∫₀¹ ln(x) dx = −1, with a logarithmic singularity at x = 0.
let (value, _) = TanhSinhEngine::new(0.0, 1.0).integrate_with_error(f64::ln).unwrap();
assert!((value + 1.0).abs() < 1e-12);
Source

pub fn integrate_with_error<F: Fn(f64) -> f64>( &self, f: F, ) -> Result<(f64, f64), ErrorsJSL>

The integral together with the difference between the last two levels (an error estimate, typically very pessimistic since convergence is exponential).

Source

pub fn integrate_with_complement<F: Fn(f64, f64) -> f64>( &self, f: F, ) -> Result<(f64, f64), ErrorsJSL>

Integrates f(x, c), where c is the signed offset of x from its nearer endpoint (x − a in the lower half, x − b in the upper half), computed without rounding. Returns the integral and an error estimate.

Trait Implementations§

Source§

impl CanIntegrate1D for TanhSinhEngine

Source§

fn set_lower_bound(&mut self, x: f64)

Sets the lower bound a.
Source§

fn set_upper_bound(&mut self, x: f64)

Sets the upper bound b.
Source§

fn integrate<F>(&self, f: F) -> Result<f64, ErrorsJSL>
where F: Fn(f64) -> f64,

Approximates ∫ₐᵇ f(x) dx.
Source§

impl Clone for TanhSinhEngine

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for TanhSinhEngine

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.