packages

interpolate::diff

Numerical differentiation: gradients of samples on 1-d and N-d grids, finite-difference weights for any stencil (Fornberg 1988), smooth derivatives of uniform samples to a chosen order of accuracy, and derivatives of functions by Ridders' extrapolation.

Structs

EstimateSource

pub struct Estimate

An estimate and its error, from derivative.

nabi
let d = try interpolate::derivative(f64::sin, 0.0);
assert((d.value - 1.0).abs() <= d.error.max(1e-14));
  • pub value: f64§

    Source

    The value.

  • pub error: f64§

    Source

    An estimate of its absolute error.

Functions

gradientSource

pub fn gradient(y: Vector<f64>, x: Vector<f64> | f64 = 1.0, edge_order: usize = 1) throws (InterpError) -> Vector<f64>

The derivative of samples y at positions x (a vector of the same length) or with a uniform step dx (default 1), as NumPy's gradient: second-order central differences inside (the three-point formula for uneven steps), one-sided differences of order edge_order (1 or 2) at the ends. Exact for quadratics inside, and at the ends with edge_order 2. Needs edge_order + 1 samples. O(n).

nabi
use core::array::Vector;

let y: Vector<f64> = [0, 1, 4, 9];               // x^2
let g = try interpolate::gradient(y, edge_order: 2);
assert(g[0] == 0.0 && g[1] == 2.0 && g[3] == 6.0);

gradient_ndSource

pub fn gradient_nd<const N: usize>(a: Array<f64, N>, spacing: List<f64>? = none, edge_order: usize = 1) throws (InterpError) -> List<Array<f64, N>>

The gradient of an array of any rank: one array per axis, each the derivative along that axis as gradient computes it. spacing is the step of every axis (one value), of each axis (one per axis), or 1 when none. Every axis needs edge_order + 1 samples. O(N n).

nabi
use core::array::Matrix;

let a: Matrix<f64> = [[0, 1, 2], [10, 11, 12]];   // 10 i + j
let g = try interpolate::gradient_nd(a);
assert(g[0][0, 1] == 10.0 && g[1][1, 2] == 1.0);
let h = try interpolate::gradient_nd(a, [2.0, 0.5]);
assert(h[0][1, 0] == 5.0 && h[1][0, 0] == 2.0);

fd_weightsSource

pub fn fd_weights<S: AsVector<f64>>(m: usize, x0: f64, stencil: S) throws (InterpError) -> Vector<f64>

The weights w with sum_i w[i] f(stencil[i]) ~ f^(m)(x0), by Fornberg's recursion (Math. Comp. 51, 1988): exact for polynomials of degree below the stencil size, any distinct points, O(n^2 m).

nabi
use core::array::Vector;

let s: Vector<f64> = [-1, 0, 1];
let w = try interpolate::fd_weights(2, 0.0, s);
assert(w[0] == 1.0 && w[1] == -2.0 && w[2] == 1.0);

differentiateSource

pub fn differentiate<Y: AsVector<f64>>(y: Y, dx: f64 = 1.0, deriv: usize = 1, accuracy: usize = 2) throws (InterpError) -> Vector<f64>

The deriv-th derivative of uniform samples y with step dx, with error O(dx^accuracy) (accuracy even, 2 by default): central stencils of 2 floor((deriv + 1) / 2) - 1 + accuracy points inside, one-sided stencils of deriv + accuracy points near the ends, all from fd_weights. Exact for polynomials of degree below the stencil size. O(n (deriv + accuracy)).

nabi
use core::array::Vector;

let y: Vector<f64> = [0, 1, 8, 27, 64, 125];       // x^3
let d = try interpolate::differentiate(y, deriv: 2);
assert((d[0] - 0.0).abs() < 1e-12 && (d[5] - 30.0).abs() < 1e-12);

derivativeSource

pub fn derivative(f: fn(f64) -> f64, x: f64, n: usize = 1, h: f64 = 0.0) throws (InterpError) -> Estimate

The derivative of order n (1 or 2) of f at x by Ridders' method: central differences on steps falling from h (by default 0.1 max(1, |x|)) by 1.4, extrapolated in a Neville table, stopping when the error grows. Usually accurate to about 1e-12 relative for smooth f; error is the table's estimate.

nabi
let d = try interpolate::derivative(f64::exp, 1.0);
assert((d.value - f64::E).abs() < 1e-11 && d.error < 1e-9);
let c = try interpolate::derivative(f64::cos, 0.0, 2);
assert((c.value + 1.0).abs() < 1e-9);