derivkit.derivatives.fornberg module#
Implementation of Fornberg’s algorithm for numerical derivatives.
The algorithm was publised by Fornberg in: Bengt Fornberg, Calculation of Weights in Finite Difference Formulas, SIAM Review, vol. 40, No. 3, pp. 685–691, September 1998
Typical usage example:
>>> import numpy as np
>>> from derivkit.derivatives.fornberg import FornbergDerivative
>>> x0 = np.pi/4
>>> grid = x0 + np.array([-0.3, -0.25, -0.1, 0, 0.12])
>>> fornberg = FornbergDerivative(lambda x: np.tan(x), x0)
>>> bool(np.isclose(
... fornberg.differentiate(grid=grid, order=1),
... 2.0022106298738143,
... rtol=1e-14,
... atol=0.0,
... ))
True
- class derivkit.derivatives.fornberg.FornbergDerivative(function: Callable, x0: float64)#
Bases:
objectSupplies the Fornberg derivative.
- function#
the function to be differentiated.
- x0#
the evaluation point for the derivative.
Initialises the class.
- Parameters:
function – the function to be differentiated.
x0 – the evaluation point for the derivative.
- differentiate(*, grid: array, order: int = 1) float64#
Constructs the derivative of a given order of a function at a point.
Currently only the derivative of order order is returned. An array of orders of 0 to order can be constructed through get_weights. See section 3 of (Fornberg 1998) for more details.
- Parameters:
grid – a series of points around the evaluation point.
order – the order of the derivative.
- Returns:
The derivative evaluated at the given point.
- get_weights(grid: array, order: int = 1) ndarray#
Constructs the weights needed for derivatives up to a given order.
- Parameters:
grid – a series of points around the evaluation point.
order – the order of the derivative.
- Returns:
A 2D array of numbers representing the derivative weights. The 0th axis corresponds to the order of the derivative, with the 0th row corresponding with the function itself.