Skip to main content

RombergEngine

Struct RombergEngine 

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

Romberg integration: trapezoid estimates with 1, 2, 4, … panels, combined by Richardson extrapolation R(k, j) = R(k, j−1) + (R(k, j−1) − R(k−1, j−1)) / (4^j − 1) to cancel the even powers of the step in the Euler-Maclaurin error expansion. Converges very fast for smooth integrands (row k is exact for polynomials of degree 2k + 1); for non-smooth or periodic integrands use an adaptive or plain trapezoid rule instead.

Fields§

§tolerance: f64

Relative tolerance between the last two diagonal entries (default 1e-12).

§max_levels: usize

Maximum number of panel doublings (default 20, about a million evaluations).

Implementations§

Source§

impl RombergEngine

Source

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

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

use lib_jsl_integration::romberg::RombergEngine;

let engine = RombergEngine::new(0.0, std::f64::consts::PI);
let (value, error_estimate, levels) = engine.integrate_with_error(f64::sin).unwrap();
assert!((value - 2.0).abs() < 1e-12 && error_estimate < 1e-10);
assert!(levels <= 8); // at most 2⁸ + 1 = 257 function evaluations
Source

pub fn tableau<F: Fn(f64) -> f64>( &self, f: F, levels: usize, ) -> Result<Vec<Vec<f64>>, ErrorsJSL>

The full Romberg tableau with levels + 1 rows: table[k][0] is the trapezoid rule with 2^k panels and table[k][j] its j-th Richardson extrapolation, accurate to O(h^{2j+2}). The diagonal table[k][k] is what RombergEngine::integrate_with_error returns. Useful for studying how extrapolation removes the error terms.

use lib_jsl_integration::romberg::RombergEngine;

let t = RombergEngine::new(0.0, 1.0).tableau(|x| x.powi(3), 2).unwrap();
assert_eq!(t[0][0], 0.5); // one-panel trapezoid: (f(0) + f(1)) / 2
assert!((t[1][1] - 0.25).abs() < 1e-15); // the first extrapolation is Simpson's rule, exact for cubics
Source

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

The extrapolated integral, the difference between the last two diagonal entries as an error estimate, and the number of levels used.

Trait Implementations§

Source§

impl CanIntegrate1D for RombergEngine

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 RombergEngine

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 RombergEngine

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.