
    `iL                     &   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ dZej        ZddZ	ddZ
ddZdd	Zdd
ZddZddZddZ	 	 ddZej                            d          	 	 dd            Zej                            d          	 	 dd            Zej                            d          	 	 dd            Zej                            d          	 	 dd            Zej                            d          	 	 dd            ZdS )     N)_spline_prefilter_core)_spline_kernel_weights)_utilzS
// workaround for HIP: line begins with #include
#include <cupy/math_constants.h>
c           
          g }|                     d           |dk    rd| nd}t          |           D ]!}|                     d| d| d| d           "|S )	a
  Extract target coordinate from coords array (for map_coordinates).

    Notes
    -----
    Assumes the following variables have been initialized on the device::

        coords (ndarray): array of shape (ncoords, ndim) containing the target
            coordinates.
        c_j: variables to hold the target coordinates

    computes::

        c_j = coords[i + j * ncoords];

    ncoords is determined by the size of the output array, y.
    y will be indexed by the CIndexer, _ind.
    Thus ncoords = _ind.size();

    z ptrdiff_t ncoords = _ind.size();r    + (W) 	
    W c_z = coords[i + z * ncoords];)appendrangendimnprepadopsprejs        w/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/scipy/ndimage/_interp_kernels.py_get_coord_mapr      s    ( CJJ1222 '!
7


C4[[ 2 2

 1	
1 11 1(+1 1 1 	2 	2 	2 	2J    c                     g }|dk    rd| nd}t          |           D ]'}|                    d| d| d| d| d| d	           (|S )
a  Compute target coordinate based on a shift followed by a zoom.

    This version zooms from the center of the edge pixels.

    Notes
    -----
    Assumes the following variables have been initialized on the device::

        in_coord[ndim]: array containing the source coordinate
        zoom[ndim]: array containing the zoom for each axis
        shift[ndim]: array containing the zoom for each axis

    computes::

        c_j = zoom[j] * (in_coord[j] - shift[j])

    r   r   r   r	    = zoom[] * ((W)in_coord[
] - shift[z])r
   r   r   r   s        r   _get_coord_zoom_and_shiftr   /   s    $ C '!
7


C4[[ D D

 C	
C CC C()C C56C C:=C C C 	D 	D 	D 	DJr   c                     g }|dk    rd| nd}t          |           D ]$}|                    d| d| d| d| d	           %|S )	a  Compute target coordinate based on a shift followed by a zoom.

    This version zooms from the outer edges of the grid pixels.

    Notes
    -----
    Assumes the following variables have been initialized on the device::

        in_coord[ndim]: array containing the source coordinate
        zoom[ndim]: array containing the zoom for each axis
        shift[ndim]: array containing the zoom for each axis

    computes::

        c_j = zoom[j] * (in_coord[j] - shift[j] + 0.5) - 0.5

    r   r   r   r	   r   r   z] - shift[j] + 0.5) - 0.5r
   r   r   s        r   _get_coord_zoom_and_shift_gridr   I   s    $ C '!
7


C4[[ N N

 M	
M MM M()M MDGM M M 	N 	N 	N 	NJr   c                     g }|dk    rd| nd}t          |           D ]$}|                    d| d| d| d| d	           %|S )	a|  Compute target coordinate based on a zoom.

    This version zooms from the center of the edge pixels.

    Notes
    -----
    Assumes the following variables have been initialized on the device::

        in_coord[ndim]: array containing the source coordinate
        zoom[ndim]: array containing the zoom for each axis

    computes::

        c_j = zoom[j] * in_coord[j]

    r   r   r   r	   r   ] * (W)in_coord[]r
   r   r   s        r   _get_coord_zoomr!   c   s    " C '!
7


C4[[ 5 5

 4	
4 44 4'(4 4+.4 4 4 	5 	5 	5 	5Jr   c                     g }|dk    rd| nd}t          |           D ]$}|                    d| d| d| d| d	           %|S )	a  Compute target coordinate based on a zoom (grid_mode=True version).

    This version zooms from the outer edges of the grid pixels.

    Notes
    -----
    Assumes the following variables have been initialized on the device::

        in_coord[ndim]: array containing the source coordinate
        zoom[ndim]: array containing the zoom for each axis

    computes::

        c_j = zoom[j] * (in_coord[j] + 0.5) - 0.5

    r   r   r   r	   r   r   z] + 0.5) - 0.5r
   r   r   s        r   _get_coord_zoom_gridr#   |   s    " C '!
7


C4[[ C C

 B	
B BB B()B B9<B B B 	C 	C 	C 	CJr   c                     g }|dk    rd| nd}t          |           D ]$}|                    d| d| d| d| d	           %|S )	aC  Compute target coordinate based on a shift.

    Notes
    -----
    Assumes the following variables have been initialized on the device::

        in_coord[ndim]: array containing the source coordinate
        shift[ndim]: array containing the zoom for each axis

    computes::

        c_j = in_coord[j] - shift[j]

    r   r   r   r	   z = (W)in_coord[r   r    r
   r   r   s        r   _get_coord_shiftr%      s     C '!
7


C4[[ 6 6

 5	
5 55 5()5 5,/5 5 5 	6 	6 	6 	6Jr   c                 <   g }|dk    rd| nd}| dz   }t          |           D ]w}|                    d| d           t          |           D ]'}|                    d| d||z  |z    d	| d
           (|                    d| d||z  | z    d| d           x|S )a{  Compute target coordinate based on a homogeneous transformation matrix.

    The homogeneous matrix has shape (ndim, ndim + 1). It corresponds to
    affine matrix where the last row of the affine is assumed to be:
    ``[0] * ndim + [1]``.

    Notes
    -----
    Assumes the following variables have been initialized on the device::

        mat(array): array containing the (ndim, ndim + 1) transform matrix.
        in_coords(array): coordinates of the input

    For example, in 2D:

        c_0 = mat[0] * in_coords[0] + mat[1] * in_coords[1] + aff[2];
        c_1 = mat[3] * in_coords[0] + mat[4] * in_coords[1] + aff[5];

    r   r   r      z
            W c_z
 = (W)0.0;z
            c_z += mat[r   ];r    r
   r   )r   r   r   r   ncolr   ks          r   _get_coord_affiner+      s?   ( C '!
7


C!8D4[[ 6 6

 !! ! ! 	" 	" 	"t 	A 	AAJJ @@ @(Q,@ @89@ @ @ A A A A

 55 5(T/5 5,/5 5 5 	6 	6 	6 	6Jr   unsigned intc                     t          |           }d| d| d| dg}t          |dz
  dd          D ]$}|                    d| |          d	| d
           %|                    d           d                    |          S )z
    declare a multi-index array in_coord and unravel the 1D index, i into it.
    This code assumes that the array is a C-ordered array.
    z	
        z
 in_coord[z];
        z s, t, idx = i;r'   r   z
        s = z(;
        t = idx / s;
        in_coord[z!] = idx - t * s;
        idx = t;z
        in_coord[0] = idx;
)lenr   r   join)shapeuint_tr   coder   s        r   _unravel_loop_indexr5      s    
 u::D#	# ## #	# # # $D 4!8Q##   1X     	 	 	 	
 	KK    99T??r   r   Fc                  |   g }|rdnd}|                     | d           |rd}d}nd}d}t          |          D ]!}|                     d| d	| d
| d           "|                     d| d|dz
   d           t          |dz
  dd          D ]'}|                     d| d|dz
   d| d| d	           (|
s#|                     t          ||                     | | ||	          z   }|t          j        u rd}n/|t          j        k    rd}n|t          j         k    rd}nd| d| }|dk    rHd                    d t          |          D                       }|                     d| d| d           |dk    r|dk    r|                     d            t          |          D ]}|dk    r|                     d!| d           n"|                     d"| d#| d$| d%| d&	           |dk    re|dk    rd'}d(}nd)| }d*}|                     t          j        ||d+| ||                     |dk    r|                     d"| d#| d$| d,           |                     d-| d.| d/| d0| d	           d1                    d2 t          |          D                       }|d3k    rPd                    d4 t          |          D                       }|                     d5| d6| d7| d8| d9	           n|                     d:| d8| d           n|dk    rt          |          D ]}|                     d-| d#| d$| d%| d;| d<| d/| d=| d>| d?| d@| dA           |dk    r|                     dB| dC| dD           n(|                     d"| dE| d/| dF| dG| dH| d           |dk    r|dk    rdI}d(}ndJ| }d*}|                     t          j        ||d+| ||                     |dk    rdKndL| }|                     t          j        ||d+| ||                     |dk    r(|                     dM| dE| d$| dN| dG| d$| dO           |                     dP                    g dQ| dR| dS| dT| dU| dV| d.| dW| dX| dY| dZ| d[| d\| d0| d]| d^| d_| d[| d`| d0| da                     Đn|dk    r|d3k    rd}n|dbk    rdb}nt          j
        |          }|                     dc| dd           t          |          D ]}|                     de| df|dz    d           |                     t          |                             ||g                     |dhv rJ|                     di| d           d'}|                     t          j        ||d+| |d(                     ndj| }|dz  rdk}ndl}|                     |                    |||dmz  n                     |                     | do| df|dz    d           t          |dz             D ]T}dp| df| dq}|                     d"| dr| d           |                     t          j        ||d+| |                     U|                     ds| dt| d.| du| dv| dw| dx| dy| dz| d{| d|| d}| d{| d~| dt           |dk    rd                    d t          |          D                       }d1                    d t          |          D                       }|d3k    s|dk    rd|dk    r^d                    d t          |          D                       }|                     d5| d| d| d| d| d| d8| d| d| d           n%|                     d-| d| d8| d| d| d           |                     d|z             |dk    r|                     d           |r|                     d           n|                     d           d                    |          }|                    dd          }d                    ||||d                    d |D                                 }|dk    r|dz  }||fS )aQ  
    Args:
        coord_func (function): generates code to do the coordinate
            transformation. See for example, `_get_coord_shift`.
        ndim (int): The number of dimensions.
        large_int (bool): If true use Py_ssize_t instead of int for indexing.
        yshape (tuple): Shape of the output array.
        mode (str): Signal extension mode to use at the array boundaries
        cval (float): constant value used when `mode == 'constant'`.
        name (str): base name for the interpolation kernel
        integer_output (bool): boolean indicating whether the output has an
            integer type.
        nprepad (int): integer indicating the amount of prepadding at the
            boundaries.

    Returns:
        operation (str): code body for the ElementwiseKernel
        name (str): name for the ElementwiseKernel
    doubleYz out = 0.0;size_t	ptrdiff_tr,   intzconst z xsize_z = x.shape()[r(   z sx_r'   z = 1;r   r.   z = sx_z	 * xsize_r
   z(Y)CUDART_NANz(Y)CUDART_INFz(Y)(-CUDART_INF)()constantz || c           	      &    g | ]}d | d| d| dS )z(c_z < 0) || (c_z	 > xsize_z - 1) .0r   s     r   
<listcomp>z+_generate_interp_custom.<locals>.<listcomp>  s4    LLL16166!66a666LLLr   z
        if (z)
        {
            out = z";
        }
        else
        {wrapzdouble dcoord;z
                dcoord = c_z
                z cf_z = (z)floor((double)c_z + 0.5);dcoordTcf_Fxsize_z)floor(dcoord + 0.5);z
            z ic_z = cf_z * sx_z + c                     g | ]}d | S ic_r@   rA   s     r   rC   z+_generate_interp_custom.<locals>.<listcomp>E       @ @ @qq @ @ @r   zgrid-constantc                     g | ]}d | d	S z(ic_z < 0)r@   rA   s     r   rC   z+_generate_interp_custom.<locals>.<listcomp>G        F F FQ F F Fr   z
            if (z) {
                out = z.;
            } else {
                out = (z)x[z];
            }z
            out = (z);
            z cc_z + 1;
            z n_z = (c_z == cf_z)) ? 1 : 2;  // points needed
            z$
                double dcoordf = c_z%;
                double dcoordc = c_z + 1;z cf_bounded_z;
                z cc_bounded_z = cc_dcoordfcf_bounded_dcoordccc_bounded_z
                    z&)floor(dcoordf);;
                    z*)floor(dcoordf + 1);;
                    r   z
            for (int s_z = 0; s_z < n_z; s_z.++)
                {
                    W w_z;
                    z;
                    if (s_z7 == 0)
                    {
                        w_z	 = (W)cc_z - c_z;
                        ic_z = cf_bounded_zM;
                    } else
                    {
                        w_z = c_z	 - (W)cf_z = cc_bounded_z;
                    }nearestz#
            W wx, wy;
            z start;z
            W weights_[)r   order)rD   zdouble dcoord = c_z
(double)c_zA
                start = ({int_t})floor({coord_var}) - {order_2};zG
                start = ({int_t})floor({coord_var} + 0.5) - {order_2};   )int_t	coord_varorder_2z ci_ci_r    z = start + z
            W w_z;
            z;
            for (int k_z = 0; k_z <= z; k_z,++)
                {
                    w_z = weights_z[k_z];
                    ic_z = ci_z] * sx_z * c                     g | ]}d | S )w_r@   rA   s     r   rC   z+_generate_interp_custom.<locals>.<listcomp>  s    <<<1h1hh<<<r   c                     g | ]}d | S rI   r@   rA   s     r   rC   z+_generate_interp_custom.<locals>.<listcomp>  rK   r   c                     g | ]}d | d	S rM   r@   rA   s     r   rC   z+_generate_interp_custom.<locals>.<listcomp>  rN   r   z) {
                out += z * (z)(z();
            } else {
                z val = (z!];
                out += val * (z);
            }z];
            out += val * (z);}zy = (Y)rint((double)out);zy = (Y)out;r/   -_z5cupyx_scipy_ndimage_interpolate_{}_order{}_{}_{}d_y{}c                     g | ]}| S r@   r@   rA   s     r   rC   z+_generate_interp_custom.<locals>.<listcomp>  s    .F.F.F!!v.F.F.Fr   _i64)r   r   r5   numpynaninfr1   r    _generate_boundary_condition_opsr   _get_spline_modespline_weights_inlineformatreplace)
coord_funcr   	large_intyshapemodecvalrU   nameinteger_outputr   omit_in_coordr   internal_dtyper3   rW   r   _condixvarfloat_ix
_coord_idxspline_moderX   op_strr*   _weight	operationmode_strs                              r   _generate_interp_customr~      s8   . C!/8XXSNJJ.---...  4[[ A A

?E??!??!???@@@@JJ333D1H3334444!8Q## G G

EFEEAEEQEEEEEFFFF 8

&vv66777 

4))
)Cuy				%)		!*>**D**zLLdLLLN N

      	 	 	 zz6>>JJ'(((t 	2 	2Av~~

 ## # # $ $ $ $ 

 II II I#(I I;<I I I J J J z!!6>>$E#HH%!IIE$H

:e\a\\5(D DE E E 6>>JJ  B B  B B  B#( B  B  B C C C JJ 11 11 1!"1 1*+1 1 1 2 2 2 2ZZ @ @E$KK @ @ @AA
?""KK F F%++ F F FGGEJJ    '	  ,6	       JJ 8"8 8'18 8 8 9 9 9 9 
!t 6	 6	AJJ   $ 78   !"      !"  +,      v~~

 /$%/ /$%/ / / 0 0 0 0
 

 44 4$%4 4-.4 44 4$%4 4-.4 4 4 5 5 5 z!!6>>%E#HH-!--E$H

:e\a\\5(D DE E E &*V^^		9Jq9J9J

:e\a\\5(D DE E E 6>>JJ () /4  () /4     JJ         #$   +,   23        	    !"	   
         ()    01        ./    78        $%    01        ./    78         S6	n 
?""$KKY#KK0A$GGK 	

    	 	 	 t -	 -	AJJ ++ + 19+ + + , , ,JJ,U3::Qe:LLMMM x

4444555$	

:i!udD DE E E E -,,	qy JDJJJ9eqj      JJE::q::519:::;;;519%% B B&a!

 ** *#$* * * + + +

:#ULQLL%A AB B B B
 JJ       $%  +0  67 
  
 &' 
 ,-    "#  ()  23       qyy**<<d<<<==ZZ @ @E$KK @ @ @AA
?""uqyyTZ5G5GKK F F%++ F F FGGEJJ   "0 4;   	  *8	  =G	 
  . 
 29       JJ ;; ;%3; ;8B; ;); ;-4; ; ; < < < 	

3:z

3 "

.////

=!!!		#I||C%%HBIIeXtSXX.F.Fv.F.F.F%G%G D d?r   T)for_each_device        r'   c                     d}d}	t          t          | |||||d||d          \  }
}t          j        ||	|
|t                    S )Nzraw X x, raw W coordsY ymapT)rl   r   rm   rn   ro   rp   rU   rq   rr   r   rs   preamble)r~   r   cupyElementwiseKernelmath_constants_preambler   rm   rn   ro   rp   rU   rr   r   	in_params
out_paramsr|   rq   s               r   _get_map_kernelr     sr     (IJ-!%  OIt !)ZD+BD D D Dr   c                     d}d}	t          t          | |||||d||
  
        \  }
}t          j        ||	|
|t                    S )Nzraw X x, raw W shiftr   shift
rl   r   rm   rn   ro   rp   rU   rq   rr   r   r   )r~   r%   r   r   r   r   s               r   _get_shift_kernelr     so     'IJ-#%  OIt !)ZD+BD D D Dr   c	                     d}	d}
|rt           }nt          }t          || ||||||rdnd||
  
        \  }}t          j        |	|
||t
                    S )Nz raw X x, raw W shift, raw W zoomr   zoom_shift_grid
zoom_shiftr   r   )r   r   r~   r   r   r   )r   rm   rn   ro   rp   rU   rr   	grid_moder   r   r   zoom_shift_funcr|   rq   s                 r   _get_zoom_shift_kernelr     s     3IJ 483-""+=%  OIt !)ZD+BD D D Dr   c	                     d}	d}
t          |rt          nt          | ||||||rdnd||
  
        \  }}t          j        |	|
||t
                    S )Nzraw X x, raw W zoomr   	zoom_gridzoomr   r   )r~   r#   r!   r   r   r   )r   rm   rn   ro   rp   rU   rr   r   r   r   r   r|   rq   s                r   _get_zoom_kernelr   /  s     &IJ-+4I''/%1[[6%  OIt !)ZD+BD D D Dr   c                     d}d}	t          t          | |||||d||
  
        \  }
}t          j        ||	|
|t                    S )Nzraw X x, raw W matr   affiner   r   )r~   r+   r   r   r   r   s               r   _get_affine_kernelr   D  so     %IJ-$%  OIt !)ZD+BD D D Dr   )r   )r,   )r   Fr   F)r   r'   Fr   )r   r'   FFr   )rd   r   cupy._core.internalcupyx.scipy.ndimager   r   r   r   ri   r   r   r   r!   r#   r%   r+   r5   r~   memoizer   r   r   r   r   r@   r   r   <module>r      sr         6 6 6 6 6 6 6 6 6 6 6 6 % % % % % % 
 /D    :   4   4   2   2   .   D   * KL*/F F F FR D))CD23D D D *)D* D))EF45D D D *)D( D))JKJKD D D *)D0 D))DEDED D D *)D( D))FG56D D D *)D D Dr   