finitediff package

Finite difference weights for any derivative order on arbitrarily spaced grids.

finitediff.derivatives_at_point_by_finite_diff

Esimates of derivatives up to specified order at a point.

Estimates derivatives/function values of requested order at multiple points (xtgt) based on finite difference using provided grid and ydata.

Parameters:

grid : array_like

Grid points: values of the independent variable (“x-data”).

ydata : array_like

Values of the dependent variable. May be two dimensional, in which case the weights of the grid is reused.

xtgt : float

The target value of the independent variable where the the finite difference scheme should be applied.

maxorder : int, optional

Maximum order of derivatives to estimate. The default is 0 (interpolation).

yorder : char

NumPy “order” of ydata.

reshape: bool

Whether to return a 2D array or not. Default: if ydata.ndim != 1.

Returns:

numpy.ndarray

Estimate from applying the finite difference scheme.

References

The underlying algorithm is from: Generation of Finite Difference Formulas on Arbitrarily Spaced Grids, Bengt Fornberg, Mathematics of compuation, 51, 184, 1988, 699-706

Examples

>>> derivatives_at_point_by_finite_diff(np.array([.0, .5, 1.]),
        np.array([.0, .25, 1.]), .5, 2)  # y=x**2
array([.25, 1.0, 2.0])  # (x**2, 2x, 2)
finitediff.interpolate_by_finite_diff

Estimates derivatives of requested order at multiple points.

Estimates derivatives/function values of requested order at multiple points (xtgts) based on finite difference using provided grid and ydata.

Parameters:

grid : array_like

Values of the independent variable (“x-data”).

ydata : array_like

Values of the dependent variable.

xtgts : array_like

Values of the independent variable where the the finite difference scheme should be applied.

maxorder : int, optional

Up to what order derivatives are to be estimated. The default is 0 (interpolation).

ntail : int, optional

how many points in grid before xtgts to inclued (default = 2).

nhead : int, optional

how many points in grid after xtgts to include (default = 2).

yorder : char

NumPy “order” of ydata.

reshape: bool

Whether to return a 3D array or not. Default: if ydata.ndim != 1.

Returns:

array_like

Estimates from applying the finite difference scheme

Notes

It is required that: order >= ntail + nhead Algortithm assumes non-regularly spaced grid. If grid is regularly spaced this algortihm is not optimal from a performance perspective.

References

The underlying algorithm is from: Generation of Finite Difference Formulas on Arbitrarily Spaced Grids, Bengt Fornberg, Mathematics of computation, 51, 184, 1988, 699-706

Examples

>>> import numpy as np
>>> from finitediff import interpolate_by_finite_diff as ifd
>>> x = np.array([0, 1, 2])
>>> y = np.array([[2, 3, 5], [3, 4, 7], [7, 8, 9], [3, 4, 6]])
>>> xout = np.linspace(0.5, 1.5, 5)
>>> r = ifd(x, y, xout, maxorder=2)
>>> r.shape
(5, 4, 3)
finitediff.get_weights(grid, double xtgt, int n=-1, int maxorder=0)

Generates finite differnece weights.

Parameters:

grid: array_like

Grid points.

xtgt: float

Point at which estimates should be accurate.

n: int, optional

Number of points used in xarr. default: -1 (means use length of xarr).

maxorder: int, optional

default: 0 (means interpolation)

Returns:

array_like

2 dimensional array with shape==(n, maxorder+1) with Fortran order (contiguous along columns) with weights for 0:th order in first column.

Submodules

finitediff.util module

finitediff.util.avg_stddev(arr, w)[source]

Calculates the average and standard deviation.

Parameters:

arr : array_like

Values.

w : array_like

Weights.

Returns:

tuple of 2 floats (average & standard deviation)

finitediff.util.interpolate_ahead(x, y, n, direction='fw')[source]