Skip to content

interp

An implementation of numpy.interp for sparse arrays.

Thanks to the function dispatch of numpy, this enables interpolation on sparse arrays using the numpy universal function. This function effectively wraps np.interp by calling it on the array data and the fill value. See the numpy documentation for details on the parameters.

Parameters:

Name Type Description Default
x SparseArray

The x-coordinates at which to evaluate the interpolated values.

required
xp 1-D sequence or SparseArray

The x-coordinates of the data points.

required
fp 1-D sequence or SparseArray

The y-coordinates of the data points, same length as xp.

required
left float or complex

Value to return for x < xp[0], default is fp[0].

None
right float or complex

Value to return for x > xp[-1], default is fp[-1].

None
period None or float

A period for the x-coordinates.

None

Returns:

Name Type Description
out SparseArray

The interpolated values, same shape as x.

See Also

https://numpy.org/doc/stable/reference/generated/numpy.interp.html

Examples:

When interpolating a sparse array, its data and the fill value are interpolated. The returned array is pruned. Therefore, the fill value and the number of nonzero elements might change.

>>> import numpy as np
>>> xp = [1, 2, 3]
>>> fp = [3, 2, 0]
>>> y = np.interp(sparse.COO.from_numpy(np.array([0, 1, 1.5, 2.72, 3.14])), xp, fp)
>>> y.todense()
array([3.  , 3.  , 2.5 , 0.56, 0.  ])
>>> y.fill_value
np.float64(3.0)
>>> y.nnz
3
Source code in sparse/numba_backend/_common.py
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
def interp(x, xp, fp, left=None, right=None, period=None):
    """
    An implementation of ``numpy.interp`` for sparse arrays.

    Thanks to the function dispatch of numpy, this enables interpolation on sparse arrays
    using the numpy universal function. This function effectively wraps ``np.interp`` by
    calling it on the array data and the fill value. See the numpy documentation for
    details on the parameters.

    Parameters
    ----------
    x : SparseArray
        The x-coordinates at which to evaluate the interpolated values.
    xp : 1-D sequence or SparseArray
        The x-coordinates of the data points.
    fp : 1-D sequence or SparseArray
        The y-coordinates of the data points, same length as ``xp``.
    left : float or complex, optional
        Value to return for ``x < xp[0]``, default is ``fp[0]``.
    right : float or complex, optional
        Value to return for ``x > xp[-1]``, default is ``fp[-1]``.
    period : None or float, optional
        A period for the x-coordinates.

    Returns
    -------
    out : SparseArray
        The interpolated values, same shape as x.

    See Also
    --------
    https://numpy.org/doc/stable/reference/generated/numpy.interp.html

    Examples
    --------
    When interpolating a sparse array, its data and the fill value are interpolated. The
    returned array is pruned. Therefore, the fill value and the number of nonzero
    elements might change.

    >>> import numpy as np
    >>> xp = [1, 2, 3]
    >>> fp = [3, 2, 0]
    >>> y = np.interp(sparse.COO.from_numpy(np.array([0, 1, 1.5, 2.72, 3.14])), xp, fp)
    >>> y.todense()
    array([3.  , 3.  , 2.5 , 0.56, 0.  ])
    >>> y.fill_value
    np.float64(3.0)
    >>> y.nnz
    3
    """
    from ._compressed import GCXS
    from ._coo import COO
    from ._dok import DOK

    # Densify sparse interpolants
    if isinstance(xp, SparseArray):
        xp = xp.todense()
    if isinstance(fp, SparseArray):
        fp = fp.todense()

    def interp_func(xx):
        return np.interp(xx, xp, fp, left=left, right=right, period=period)

    # Shortcut for dense arrays
    if not isinstance(x, SparseArray):
        return interp_func(x)

    # Define output type
    out_kwargs = {}
    out_type = COO
    if isinstance(x, GCXS):
        out_type = GCXS
        out_kwargs["compressed_axes"] = x.compressed_axes
    elif isinstance(x, DOK):
        out_type = DOK

    # Perform interpolation on sparse object
    arr = as_coo(x)
    data = interp_func(arr.data)
    fill_value = interp_func(arr.fill_value)
    return COO(data=data, coords=arr.coords, shape=arr.shape, fill_value=fill_value, prune=True).asformat(
        out_type, **out_kwargs
    )