Differentiation, Integration & Interpolation

Numgrids provides numerical operators for differentiation, integration, and interpolation on structured grids. Each operator is available both as a class that can be reused across multiple evaluations and as a convenience function for one-off computations.

class numgrids.Diff(grid: Grid, order: int, axis_index: int = 0, acc: int = 4)[source]

Partial derivative operator on a numerical grid.

Automatically selects the best differentiation strategy (finite differences, FFT spectral, Chebyshev spectral, or log-scale) based on the axis type at axis_index.

Examples

>>> from numgrids import *
>>> ax = create_axis(AxisType.CHEBYSHEV, 30, 0, 1)
>>> grid = Grid(ax)
>>> d = Diff(grid, 1)      # first derivative
>>> d(grid.coords ** 2)    # apply to x**2

Constructor for partial derivative operator.

Parameters:
  • grid (Grid) – The numerical grid on which to apply the partial derivative.

  • order (positive int) – The order of the derivative.

  • axis_index (int) – The axis index (which axis in the grid).

  • acc (int) – The accuracy order for finite-difference based methods. Higher values use wider stencils for better accuracy. Ignored by spectral methods (Chebyshev). Default is 4.

as_matrix() spmatrix[source]

Returns a matrix representation of the differential operator.

The data type is a scipy sparse matrix.

numgrids.diff(grid: Grid, f: ndarray[tuple[Any, ...], dtype[_ScalarT]], order: int = 1, axis_index: int = 0, acc: int = 4) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]

Differentiate a meshed function (with operator caching).

Creates a Diff operator on first call and caches it on the grid for subsequent calls with the same (order, axis_index, acc).

Parameters:
  • grid (Grid) – The grid on which f is defined.

  • f (NDArray) – Meshed function values with shape grid.shape.

  • order (int, optional) – Derivative order (default 1).

  • axis_index (int, optional) – Axis along which to differentiate (default 0).

  • acc (int, optional) – Accuracy order for finite-difference methods (default 4).

Returns:

The derivative array, same shape as f.

Return type:

NDArray

class numgrids.Integral(grid: Grid)[source]

The integration operator for integrating on a grid.

\[\int_V ... dV\]

Integration always runs over the whole grid.

Constructor

Parameters:

grid (Grid) – The grid on which to integrate.

numgrids.integrate(grid: Grid, f: ndarray[tuple[Any, ...], dtype[_ScalarT]]) float[source]

Integrate a meshed function over the entire grid domain.

The Integral operator is cached on the grid after the first call.

Parameters:
  • grid (Grid) – The grid on which f is defined.

  • f (NDArray) – Meshed function values.

Returns:

The definite integral over the grid domain.

Return type:

float

class numgrids.Interpolator(grid: Grid, f: ndarray[tuple[Any, ...], dtype[_ScalarT]], method: str = 'cubic')[source]

Interpolation of meshed function data on a grid.

Wraps scipy.interpolate.RegularGridInterpolator (multi-D) or scipy.interpolate.interp1d() (1-D) to provide a callable that evaluates the interpolant at arbitrary locations.

Create an interpolating function for the array data.

Call the Interpolator object like a normal function to interpolate.

Parameters:
  • grid (Grid) – The grid on which the array f is meshed.

  • f (numpy.ndarray) – The data to interpolate.

  • method (str) – Interpolation order. May be ‘linear’, ‘quadratic’ or ‘cubic’.

numgrids.interpolate(grid: Grid, f: ndarray[tuple[Any, ...], dtype[_ScalarT]], locations) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]

Interpolate a meshed function at arbitrary locations.

Parameters:
  • grid (Grid) – The grid on which f is defined.

  • f (NDArray) – Meshed function values.

  • locations (tuple, list[tuple], zip, or Grid) – Point(s) at which to interpolate.

Returns:

Interpolated value(s).

Return type:

NDArray