bundles / scipy latest / scipy / interpolate / _rgi / interpn
function
scipy.interpolate._rgi:interpn
source: build-install/usr/lib/python3.14/site-packages/scipy/interpolate/_rgi.py:650
Signature
def interpn(points, values, xi, method = linear, bounds_error = True, fill_value = nan)Summary
Multidimensional interpolation on regular or rectilinear grids.
Extended Summary
Strictly speaking, not all regular grids are supported - this function works on rectilinear grids, that is, a rectangular grid with even or uneven spacing.
Parameters
points: tuple of ndarray of float, with shapes (m1, ), ..., (mn, )The points defining the regular grid in n dimensions. The points in each dimension (i.e. every elements of the points tuple) must be strictly ascending or descending.
values: array_like, shape (m1, ..., mn, ...)The data on the regular grid in n dimensions. Complex data is accepted.
xi: ndarray of shape (..., ndim)The coordinates to sample the gridded data at
method: str, optionalThe method of interpolation to perform. Supported are "linear", "nearest", "slinear", "cubic", "quintic", "pchip", and "splinef2d". "splinef2d" is only supported for 2-dimensional data.
bounds_error: bool, optionalIf True, when interpolated values are requested outside of the domain of the input data, a ValueError is raised. If False, then
fill_valueis used.fill_value: number, optionalIf provided, the value to use for points outside of the interpolation domain. If None, values outside the domain are extrapolated. Extrapolation is not supported by method "splinef2d".
Returns
values_x: ndarray, shape xi.shape[:-1] + values.shape[ndim:]Interpolated values at
xi. See notes for behaviour whenxi.ndim == 1.
Notes
In the case that xi.ndim == 1 a new axis is inserted into the 0 position of the returned array, values_x, so its shape is instead (1,) + values.shape[ndim:].
If the input data is such that input dimensions have incommensurate units and differ by many orders of magnitude, the interpolant may have numerical artifacts. Consider rescaling the data before interpolation.
Examples
Evaluate a simple example function on the points of a regular 3-D grid:import numpy as np from scipy.interpolate import interpn def value_func_3d(x, y, z): return 2 * x + 3 * y - z x = np.linspace(0, 4, 5) y = np.linspace(0, 5, 6) z = np.linspace(0, 6, 7) points = (x, y, z) values = value_func_3d(*np.meshgrid(*points, indexing='ij'))✓
point = np.array([2.21, 3.12, 1.15]) print(interpn(points, values, point))✓
[12.63]Compare with value at point by function
value_func_3d(*point)
✗np.float64(12.629999999999999)Since the function is linear, the interpolation is exact using linear method. If ``fill_value=None``, then values outside of the domain are extrapolated.
x = np.linspace(0, 10, 11) y = np.linspace(0, 15, 16) def value_func_2d(x, y): return 5 * x - 3 * y points = (x, y) values = value_func_2d(*np.meshgrid(*points, indexing='ij')) xi = np.array([[25, 36]]) interpn(points, values, xi, method='linear', bounds_error=False, fill_value=None)✓
array([17.])If a ``fill_value`` is specified, any points outside the domain will be set to ``fill_value``.
interpn(points, values, xi, method='linear', bounds_error=False, fill_value=42)
✓array([42.])When one axis is a single grid point, interpolation is only done at that coordinate. Any other value along that axis is out of bounds, and set to ``fill_value`` (if there is one). Evaluate interpolation function at a point outside of the single-point axis
x = np.array([0.0]) # x axis has length 1 y = np.linspace(0, 10, 11) points = (x, y) values = value_func_2d(*np.meshgrid(*points, indexing='ij')) xi = np.array([[1.0, 0.5]]) interpn(points, values, xi, method='linear', bounds_error=False, fill_value = 42)✓
array([42.])Evaluate interpolation function at a point on the single-point axis
xi = np.array([[0.0, 0.5]])
✓interpn(points, values, xi, method='linear', bounds_error=False, fill_value = 42)✗
array([-1.5])
See also
- LinearNDInterpolator
Piecewise linear interpolant on unstructured data in N dimensions
- NearestNDInterpolator
Nearest neighbor interpolation on unstructured data in N dimensions
- RectBivariateSpline
Bivariate spline approximation over a rectangular mesh
- RegularGridInterpolator
interpolation on a regular or rectilinear grid in arbitrary dimensions (
interpnwraps this class).- scipy.ndimage.map_coordinates
interpolation on grids with equal spacing (suitable for e.g., N-D image resampling)
Aliases
scipy.interpolate.interpn