o
    X۷i2                     @  sH   d Z ddlmZ ddlZddlmZmZ ddlmZ G dd deZ	dS )z,
Convenience interface to N-D interpolation
    )annotationsN)NDInterpolatorBase_ndim_coords_from_arrays)KDTreec                   @  s"   e Zd ZdZdddZdd ZdS )	NearestNDInterpolatora  NearestNDInterpolator(x, y).

    Nearest-neighbor interpolator in N > 1 dimensions.

    Parameters
    ----------
    x : (npoints, ndims) 2-D ndarray of floats
        Data point coordinates.
    y : (npoints, ) 1-D ndarray of float or complex
        Data values.
    rescale : boolean, optional
        Rescale points to unit cube before performing interpolation.
        This is useful if some of the input dimensions have
        incommensurable units and differ by many orders of magnitude.
    tree_options : dict, optional
        Options passed to the underlying ``cKDTree``.

    See Also
    --------
    griddata :
        Interpolate unstructured D-D data.
    LinearNDInterpolator :
        Piecewise linear interpolator in N dimensions.
    CloughTocher2DInterpolator :
        Piecewise cubic, C1 smooth, curvature-minimizing interpolator in 2D.
    interpn : Interpolation on a regular grid or rectilinear grid.
    RegularGridInterpolator : Interpolator on a regular or rectilinear grid
                              in arbitrary dimensions (`interpn` wraps this
                              class).

    Notes
    -----
    Uses ``cupyx.scipy.spatial.KDTree``

    .. note:: For data on a regular grid use `interpn` instead.

    Examples
    --------
    We can interpolate values on a 2D plane:

    >>> from scipy.interpolate import NearestNDInterpolator
    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> rng = cupy.random.default_rng()
    >>> x = rng.random(10) - 0.5
    >>> y = rng.random(10) - 0.5
    >>> z = cupy.hypot(x, y)
    >>> X = cupy.linspace(min(x), max(x))
    >>> Y = cupy.linspace(min(y), max(y))
    >>> X, Y = cupy.meshgrid(X, Y)  # 2D grid for interpolation
    >>> interp = NearestNDInterpolator(list(zip(x, y)), z)
    >>> Z = interp(X, Y)
    >>> plt.pcolormesh(X, Y, Z, shading='auto')
    >>> plt.plot(x, y, "ok", label="input point")
    >>> plt.legend()
    >>> plt.colorbar()
    >>> plt.axis("equal")
    >>> plt.show()

    FNc                 C  sH   t j| |||ddd |d u rt }t| jfi || _t|| _d S )NF)rescaleneed_contiguousneed_values)	r   __init__dictr   pointstreecupyasarrayvalues)selfxyr   tree_options r   Y/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupyx/scipy/interpolate/_ndgriddata.pyr
   P   s   zNearestNDInterpolator.__init__c                 O  s0  t || jjd d}| |}| |}|d|jd }|j}|j}| jj|fi |\}}t	|}	| j
jdkrI|dd | j
jdd  }
n|dd }
t| j
jtjrdtj|
tj| j
jd}nt|
tj}| j
||	 df ||	< | j
jdkr|dd | j
jdd  }n|dd }||}|S )a  
        Evaluate interpolator at given points.

        Parameters
        ----------
        x1, x2, ... xn : array-like of float
            Points where to interpolate data at.
            x1, x2, ... xn can be array-like of float with broadcastable shape.
            or x1 can be array-like of float with shape ``(..., ndim)``
        **query_options
            This allows ``eps``, ``p`` and ``distance_upper_bound``
            being passed to the KDTree's query function to be explicitly set.
            See `cupyx.scipy.spatial.KDTree.query` for an overview of
            the different options.

            .. versionadded:: 1.12.0

           )ndimN)dtype.)r   r   shape_check_call_shape_scale_xreshaper   queryr   isfiniter   r   
issubdtyper   complexfloatingfullnan)r   argsquery_optionsxixi_flatoriginal_shapeflattened_shapedisti
valid_maskinterp_shapeinterp_values	new_shaper   r   r   __call__Y   s,   

	

zNearestNDInterpolator.__call__)FN)__name__
__module____qualname____doc__r
   r1   r   r   r   r   r      s    
=	r   )
r5   
__future__r   r   !cupyx.scipy.interpolate._interpndr   r   cupyx.scipy.spatialr   r   r   r   r   r   <module>   s    