dpnp.interp

dpnp.interp(x, xp, fp, left=None, right=None, period=None)[source]

One-dimensional linear interpolation.

Returns the one-dimensional piecewise linear interpolant to a function with given discrete data points (xp, fp), evaluated at x.

For full documentation refer to numpy.interp.

Parameters:
  • x ({dpnp.ndarray, usm_ndarray}) -- Input 1-D array. The x-coordinates at which to evaluate the interpolated values.

  • xp ({dpnp.ndarray, usm_ndarray}) -- Input 1-D array. The x-coordinates of the data points, must be increasing if argument period is not specified. Otherwise, xp is internally sorted after normalizing the periodic boundaries with xp = xp % period.

  • fp ({dpnp.ndarray, usm_ndarray}) -- Input 1-D array. The y-coordinates of the data points, same length as xp.

  • left ({None, scalar, dpnp.ndarray, usm_ndarray}, optional) --

    Value to return for x < xp[0].

    Default: fp[0].

  • right ({None, scalar, dpnp.ndarray, usm_ndarray}, optional) --

    Value to return for x > xp[-1].

    Default: fp[-1].

  • period ({None, scalar, dpnp.ndarray, usm_ndarray}, optional) --

    A period for the x-coordinates. This parameter allows the proper interpolation of angular x-coordinates. Parameters left and right are ignored if period is specified.

    Default: None.

Returns:

y -- The interpolated values, same shape as x.

Return type:

{dpnp.ndarray, usm_ndarray}

Warning

The x-coordinate sequence is expected to be increasing, but this is not explicitly enforced. However, if the sequence xp is non-increasing, interpolation results are meaningless.

Note that, since NaN is unsortable, xp also cannot contain NaNs.

A simple check for xp being strictly increasing is:

import dpnp as np
np.all(np.diff(xp) > 0)

Examples

>>> import dpnp as np
>>> xp = np.array([1, 2, 3])
>>> fp = np.array([3 ,2 ,0])
>>> x = np.array([2.5])
>>> np.interp(x, xp, fp)
array([1.])
>>> x = np.array([0, 1, 1.5, 2.72, 3.14])
>>> np.interp(x, xp, fp)
array([3.  , 3.  , 2.5 , 0.56, 0.  ])
>>> x = np.array([3.14])
>>> UNDEF = -99.0
>>> np.interp(x, xp, fp, right=UNDEF)
array([-99.])

Interpolation with periodic x-coordinates:

>>> x = np.array([-180, -170, -185, 185, -10, -5, 0, 365])
>>> xp = np.array([190, -190, 350, -350])
>>> fp = np.array([5, 10, 3, 4])
>>> np.interp(x, xp, fp, period=360)
array([7.5 , 5.  , 8.75, 6.25, 3.  , 3.25, 3.5 , 3.75])

Complex interpolation:

>>> x = np.array([1.5, 4.0])
>>> xp = np.array([2,3,5])
>>> fp = np.array([1.0j, 0, 2+3j])
>>> np.interp(x, xp, fp)
array([0.+1.j , 1.+1.5j])