derivkit.derivatives.fornberg module#

Implementation of Fornberg’s algorithm for numerical derivatives.

The algorithm was published by Fornberg in: Bengt Fornberg, Calculation of Weights in Finite Difference Formulas, SIAM Review, vol. 40, No. 3, pp. 685–691, September 1998

Examples:#

Calculating the derivative at a single value:: >>> import numpy as np >>> from derivkit.derivatives.fornberg import FornbergDerivative >>> x0 = np.pi/4 >>> grid = 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

Calculating the derivative at an array of values using uniform offsets:: >>> import numpy as np >>> from derivkit.derivatives.fornberg import FornbergDerivative >>> x0 = np.array([ … [[1, 2], … [3, 4]], … [[5, 6], … [7,8]] … ]) >>> grid = np.array([-0.34, -0.02, 0.1, 0.34, 0.98]) >>> fornberg = FornbergDerivative(lambda x: np.cos(x), x0) >>> np.allclose( … fornberg.differentiate(grid=grid, order=1), … -np.sin(x0), … rtol=1e-4, … atol=0.0, … ) True

Calculating the derivative at an array of values using 5 unique offsets for each evaluation point:: >>> import numpy as np >>> from derivkit.derivatives.fornberg import FornbergDerivative >>> x0 = np.array([2, 7, 10, -np.pi]) >>> grid = np.array([ … [-0.34, -0.02, 0.1, 0.34, 0.98], … [-0.4, -0.2, -0.1, 0.14, 0.68], … [-0.5, -0.12, 0.15, 0.64, 0.78], … [-0.1, 0, 0.06, 0.24, 0.8] … ]).T >>> fornberg = FornbergDerivative(lambda x: np.cos(x), x0) >>> np.allclose( … fornberg.differentiate(grid=grid, order=1), … -np.sin(x0), … rtol=1e-4, … atol=1e-4, … ) True

class derivkit.derivatives.fornberg.FornbergDerivative(function: Callable[[floating], floating], x0: float64 | ndarray[tuple[Any, ...], dtype[floating]])#

Bases: object

Supplies the Fornberg derivative.

The Fornberg derivative relies on the interpolation of function values by Lagrange polynomials. For more information see Bengt Fornberg, Calculation of Weights in Finite Difference Formulas, SIAM Review, vol. 40, No. 3, pp. 685–691, September 1998.

function#

the function to be differentiated. Must accept a single float and return a float, and must be vectorizable.

x0#

the evaluation points for the derivative. Must be a float or a structure castable to a Numpy array. If it castable to a Numpy array the function and its derivative will be vectorized over the array.

Initialises the class.

Parameters:
  • function – the function to be differentiated. Must accept a single float and return a float, and must be vectorizable.

  • x0 – the evaluation points for the derivative. Must be a float or a structure castable to a Numpy array. If it castable to a Numpy array the function and its derivative will be vectorized over the array.

differentiate(*, grid: ndarray[tuple[Any, ...], dtype[float64]], order: int = 1) ndarray[tuple[Any, ...], dtype[floating]]#

Constructs the derivative of a given order of a function at a point.

The derivative is constructed by recursively differentiating the Lagrange polynomials that approximate the function around the evaluation points.

Because the derivative of a given order is constructed from the derivatives of lower orders (this is part of the recursion) the method is capable of returning all derivatives up to the specified order. However, currently only the highest derivative is returned.

See section 3 of (Fornberg 1998) for more details.

Parameters:
  • grid

    an array of offsets relative to the evaluation points. These points specify the Lagrange interpolation of the function, and are used to calculate the derivatives. The following forms are supported:

    • If grid is a 1D array it is assumed that it contains linear offsets from the evaluation points. These are added uniformly to FornbergDerivative.x0.

    • If grid is an ND array it is assumed that it contains unique linear offsets for each evaluation point. In this case the first axis of the grid must correspond to the offsets while the remaining axes must be equal to the shape of FornbergDerivative.x0. In brief, in this case the grid must be of shape (n, *x0.shape) for some integer n.

      The advantage of this is that, in this case, the offsets can be tuned for each evaluation point, although each point must still be given the same number of offsets.

  • order – the order of the derivative. Must be a non-negative number. The case of order==0 corresponds to the Lagrange interpolation of the function.

Returns:

The derivative of FornbergDerivative.function evaluated at

FornbergDerivative.x0.

Raises:
  • ValueError – if order is smaller than 0.

  • RuntimeWarning – if grid contains duplicate offsets for a given point.