@@ -3250,3 +3250,88 @@ def diff(x, axis=-1, n=1, prepend=None, append=None):
32503250 for _ in range (n ):
32513251 result = result [(slice (None ),) * axis + (slice (1 , None ),)] - result [(slice (None ),) * axis + (slice (None , - 1 ),)]
32523252 return result
3253+
3254+
3255+ def interp (x , xp , fp , left = None , right = None , period = None ):
3256+ """
3257+ An implementation of ``numpy.interp`` for sparse arrays.
3258+
3259+ Thanks to the function dispatch of numpy, this enables interpolation on sparse arrays
3260+ using the numpy universal function. This function effectively wraps ``np.interp`` by
3261+ calling it on the array data and the fill value. See the numpy documentation for
3262+ details on the parameters.
3263+
3264+ Parameters
3265+ ----------
3266+ x : SparseArray
3267+ The x-coordinates at which to evaluate the interpolated values.
3268+ xp : 1-D sequence or SparseArray
3269+ The x-coordinates of the data points.
3270+ fp : 1-D sequence or SparseArray
3271+ The y-coordinates of the data points, same length as ``xp``.
3272+ left : float or complex, optional
3273+ Value to return for ``x < xp[0]``, default is ``fp[0]``.
3274+ right : float or complex, optional
3275+ Value to return for ``x > xp[-1]``, default is ``fp[-1]``.
3276+ period : None or float, optional
3277+ A period for the x-coordinates.
3278+
3279+ Returns
3280+ -------
3281+ out : SparseArray
3282+ The interpolated values, same shape as x.
3283+
3284+ See Also
3285+ --------
3286+ https://numpy.org/doc/stable/reference/generated/numpy.interp.html
3287+
3288+ Examples
3289+ --------
3290+ When interpolating a sparse array, its data and the fill value are interpolated. The
3291+ returned array is pruned. Therefore, the fill value and the number of nonzero
3292+ elements might change.
3293+
3294+ >>> import numpy as np
3295+ >>> xp = [1, 2, 3]
3296+ >>> fp = [3, 2, 0]
3297+ >>> y = np.interp(sparse.COO.from_numpy(np.array([0, 1, 1.5, 2.72, 3.14])), xp, fp)
3298+ >>> y.todense()
3299+ array([3. , 3. , 2.5 , 0.56, 0. ])
3300+ >>> y.fill_value
3301+ np.float64(3.0)
3302+ >>> y.nnz
3303+ 3
3304+ """
3305+ from ._compressed import GCXS
3306+ from ._coo import COO
3307+ from ._dok import DOK
3308+
3309+ # Densify sparse interpolants
3310+ if isinstance (xp , SparseArray ):
3311+ xp = xp .todense ()
3312+ if isinstance (fp , SparseArray ):
3313+ fp = fp .todense ()
3314+
3315+ def interp_func (xx ):
3316+ return np .interp (xx , xp , fp , left = left , right = right , period = period )
3317+
3318+ # Shortcut for dense arrays
3319+ if not isinstance (x , SparseArray ):
3320+ return interp_func (x )
3321+
3322+ # Define output type
3323+ out_kwargs = {}
3324+ out_type = COO
3325+ if isinstance (x , GCXS ):
3326+ out_type = GCXS
3327+ out_kwargs ["compressed_axes" ] = x .compressed_axes
3328+ elif isinstance (x , DOK ):
3329+ out_type = DOK
3330+
3331+ # Perform interpolation on sparse object
3332+ arr = as_coo (x )
3333+ data = interp_func (arr .data )
3334+ fill_value = interp_func (arr .fill_value )
3335+ return COO (data = data , coords = arr .coords , shape = arr .shape , fill_value = fill_value , prune = True ).asformat (
3336+ out_type , ** out_kwargs
3337+ )
0 commit comments