
    
9j>              	      |   S SK Jr  S SKJr  S SKrS SKrS SKrS SKJr  S#S jrS#S jr	\R                  " SSS	S
5      rS r\R                  " SSSSSSS5      rS$S jr\R                   R#                  5       S 5       r  S%SS.S jjr  S&S jr " S S\5      r " S S\5      r " S S\5      rS rS  rS! rS" rg)'    )annotations)
NamedTupleN)_corec                &   Uc}  U R                  5       n [        U[        R                  5      (       a#  UR                  [        R
                  :X  a  X)    $ [        R                  " U R                  [        S9nSX1'   X   $ [        U[        R                  5      (       a4  UR                  [        R
                  :X  a  [        R                  " U) XS9$ [        R                  " U R                  U   [        S9nSX1'   [        R                  " X0US9$ )a  
Delete values from an array along the specified axis.

Args:
    arr (cupy.ndarray):
        Values are deleted from a copy of this array.
    indices (slice, int or array of ints):
        These indices correspond to values that will be deleted from the
        copy of `arr`.
        Boolean indices are treated as a mask of elements to remove.
    axis (int or None):
        The axis along which `indices` correspond to values that will be
        deleted. If `axis` is not given, `arr` will be flattened.

Returns:
    cupy.ndarray:
        A copy of `arr` with values specified by `indices` deleted along
        `axis`.

.. warning:: This function may synchronize the device.

.. seealso:: :func:`numpy.delete`.
dtypeFaxis)ravel
isinstancecupyndarrayr   bool_onessizeboolcompressshape)arrindicesr
   masks       W/home/wildlama/miniconda3/lib/python3.13/site-packages/cupy/_manipulation/add_remove.pydeleter      s    2 |iikgt||,,$**1Lx= yy.y gt||,,$**1L=='3::yy45}}TT22    c                   [         R                  " U 5      n [         R                  " U5      nUcC  [        R                  " U R	                  5       UR	                  5       4S5      R	                  5       $ [        R                  " X4U5      $ )aO  
Append values to the end of an array.

Args:
    arr (array_like):
        Values are appended to a copy of this array.
    values (array_like):
        These values are appended to a copy of ``arr``.  It must be of the
        correct shape (the same shape as ``arr``, excluding ``axis``).  If
        ``axis`` is not specified, ``values`` can be any shape and will be
        flattened before use.
    axis (int or None):
        The axis along which ``values`` are appended.  If ``axis`` is not
        given, both ``arr`` and ``values`` are flattened before use.

Returns:
    cupy.ndarray:
        A copy of ``arr`` with ``values`` appended to ``axis``.  Note that
        ``append`` does not occur in-place: a new array is allocated and
        filled.  If ``axis`` is None, ``out`` is a flattened array.

.. seealso:: :func:`numpy.append`
r   )r   asarrayr   concatenate_methodr   )r   valuesr
   s      r   appendr   =   sj    2 ,,s
C\\&!F|''YY[&,,.)1..3eg	6##SM488r   zraw T x, int64 sizezT yzy = x[i % size]cupy_resizec                \   [         R                  " U 5      (       a  [        R                  " X5      $ [        R                  " U 5      n U R
                  S:X  a  [        R                  " XR                  S9$ [        R                  " XR                  5      n[        X R
                  U5        U$ )a  Return a new array with the specified shape.

If the new array is larger than the original array, then the new
array is filled with repeated copies of ``a``.  Note that this behavior
is different from a.resize(new_shape) which fills with zeros instead
of repeated copies of ``a``.

Args:
    a (array_like): Array to be resized.
    new_shape (int or tuple of int): Shape of resized array.

Returns:
    cupy.ndarray:
        The new array is formed from the data in the old array, repeated
        if necessary to fill out the required number of elements.  The
        data are repeated in the order that they are stored in memory.

.. seealso:: :func:`numpy.resize`
r   r   )
numpyisscalarr   fullr   r   zerosr   empty_resize_kernel)a	new_shapeouts      r   resizer+   e   sr    ( ~~ayy&&QAvv{zz)7733
**Y
(C1ffc"Jr   zT data, int64 lenzint64 yzdata == T(0) ? len : _jz	min(a, b)zy = alenfirst_nonzeroc                l   U R                   S:X  a  U $ U R                   S:  a  [        S5      eSnU R                  nUR                  5       nSU;   a#  [	        X R                  5      R                  5       nSU;   a7  U R                  [	        U SSS2   U R                  5      R                  5       -
  nXU $ )a  Trim the leading and/or trailing zeros from a 1-D array or sequence.

Returns the trimmed array

Args:
    filt(cupy.ndarray): Input array
    trim(str, optional):
        'fb' default option trims the array from both sides.
        'f' option trim zeros from front.
        'b' option trim zeros from back.

Returns:
    cupy.ndarray: trimmed input

.. seealso:: :func:`numpy.trim_zeros`

r      z'Multi-dimensional trim is not supportedFBN)ndimNotImplementedErrorr   upper_first_nonzero_krnlitem)filttrimstartends       r   
trim_zerosr<      s    $ yyA~yy1}!"KLLE
))C::<D
d{#D))499;
d{ii-d4R4j$))DIIKKc?r   c                    [         R                  " [         R                  " U5      5      n[         R                  " X5      U S S & g N)r   logical_notisnanlogical_and)r   x0mask1s      r   _unique_update_mask_equal_nanrD      s.    TZZ^,Et+DGr   T)	equal_nanc          	       ^^ Uc  [        XUUXPR                  S9nU$ [        R                  " XS5      n U R                  n[        R                  " SUS   [        R
                  S9nU R                  US   [        R                  " USS 5      5      n [        R                  " U 5      n [        R                  " U R                  [        R                  5      n	[        R                  " U 5      mU mU	(       a  U R                  [        R
                  5      mUU4S jn
[        R                  " US   [        R
                  S9nUR!                  5       S4/nU/ :w  a  UR#                  S5      u  pU/ :X  a  M!  US   n/ n/ n[%        S['        U5      5       H>  nU
" UU   U5      (       a  UR)                  UU   5        M*  UR)                  UU   5        M@     U['        U5      -   nUR)                  UU45        UR)                  UUS-   45        XU'   U/ :w  a  M  X   n U R*                  S:  aV  [        R                  " U R                  [        R,                  S9nSUSS& U SS U SS :g  USS& [        R.                  " USS	9nO5[        R0                  " U R                  S   [        R,                  S9nS
USS& U U   n U R                  " UR3                  5       R5                  5       /USS Q76 n [        R                  " U SU5      n U 4nU(       a  XkU   4-  nU(       aQ  [        R6                  " U5      S-
  n[        R                  " UR                  [        R
                  S9nUUU'   UU4-  nU(       aj  [        R8                  " U5      S   n[        R                  " UR*                  S-   4UR                  5      nUUSS& UR*                  US'   XhSS USS -
  4-  n['        U5      S:X  a  US   nU$ )a}	  Find the unique elements of an array.

Returns the sorted unique elements of an array. There are three optional
outputs in addition to the unique elements:

* the indices of the input array that give the unique values
* the indices of the unique array that reconstruct the input array
* the number of times each unique value comes up in the input array

Args:
    ar(array_like): Input array. This will be flattened if it is not
        already 1-D.
    return_index(bool, optional): If True, also return the indices of `ar`
        (along the specified axis, if provided, or in the flattened array)
        that result in the unique array.
    return_inverse(bool, optional): If True, also return the indices of the
        unique array (for the specified axis, if provided) that can be used
        to reconstruct `ar`.
    return_counts(bool, optional): If True, also return the number of times
        each unique item appears in `ar`.
    axis(int or None, optional): The axis to operate on. If None, ar will
        be flattened. If an integer, the subarrays indexed by the given
        axis will be flattened and treated as the elements of a 1-D array
        with the dimension of the given axis, see the notes for more
        details. The default is None.
    equal_nan(bool, optional): If True, collapse multiple NaN values in the
        return array into one.

Returns:
    cupy.ndarray or tuple:
        If there are no optional outputs, it returns the
        :class:`cupy.ndarray` of the sorted unique values. Otherwise, it
        returns the tuple which contains the sorted unique values and
        following.

        * The indices of the first occurrences of the unique values in the
          original array. Only provided if `return_index` is True.
        * The indices to reconstruct the original array from the
          unique array. Only provided if `return_inverse` is True.
        * The number of times each of the unique values comes up in the
          original array. Only provided if `return_counts` is True.

Notes:
   When an axis is specified the subarrays indexed by the axis are sorted.
   This is done by making the specified axis the first dimension of the
   array (move the axis to the first dimension to keep the order of the
   other axes) and then flattening the subarrays in C order.

.. warning::

    This function may synchronize the device.

.. seealso:: :func:`numpy.unique`
N)return_indexreturn_inversereturn_countsrE   inverse_shaper   r   r/   c                   > TU    TU   p2[         R                  " X#-
  S5      nUR                  S   S:  a-  US   nT(       a  [         R                  " U5      (       a  gUS:  $ g)Nfr   TF)r   r<   r   r@   )idx1idx2leftrightcompdiffar_cmp
is_complexs         r   compare_axis_elems"unique.<locals>.compare_axis_elems  s]    TlF4Let|S1::a=17Ddjj..!8Or   Tr2   r	   F)
_unique_1dr   r   moveaxisarangeintpreshapemathprodascontiguousarray
issubdtyper   unsignedintegeriscomplexobjastyper&   tolistpopranger,   r   r   r   anyr   sumr7   cumsumnonzero)arrG   rH   rI   r
   rE   ret
orig_shapeidxis_unsignedrU   sorted_indicesqueuecurrentoffmid_elemrO   rP   ielem_posr   imaskinv_idxri   rS   rT   s                           @@r   uniquerx      sx   p |(6'4#,HHF 
	r	#B J
++aAdii
8C	JqM499Z^#<	=B				#B//"((D,@,@AK""2&JF499% ZZ
1TYY?NjjlAE
2+yy|b=1:q#g,'A!'!*h77GAJ'WQZ(	 ( T?dC[!eX\*+#+x % 2+( 
	B	ww{zz"(($**5Raab6RW$QRxx1% yy"((1+djj9QR 
DB	DHHJOO%	7
12	7B	r1d	#B
#Cd#$$D!A%**TZZtyy9"'wx,,t$Q'jj',,*,gmm<CR))B12wSb!""
3x1}!fJr   c                p   [         R                  " U 5      R                  5       n U(       d  U(       a  U R                  5       nX   nOU R	                  5         U n[         R
                  " UR                  [         R                  S9nSUS S& USS  US S :g  USS & U(       a  [        USS  US S 5        Xx   n	U(       d  U(       d	  U(       d  U	$ U	4n	U(       a	  U	WU   4-  n	U(       a^  [         R                  " U5      S-
  n
[         R
                  " UR                  [         R                  S9nXW'   XR                  U5      4-  n	U(       ai  [         R                  " U5      S   n[         R
                  " UR                  S-   4UR                  5      nXS S& UR                  US'   XSS  US S -
  4-  n	U	$ )Nr   Tr/   r2   r   )r   r   flattenargsortsortr&   r   r   rD   rh   rZ   r[   ri   r   r   )rj   rG   rH   rI   rE   rJ   permauxr   rk   rv   rw   ri   rm   s                 r   rW   rW   K  sy   	b		!	!	#B~zz|h
	::ciitzz2DD!H12w#cr("DH%d12hCR9
)C}

$CtDz{D!A%**TZZtyy9}-..,,t$Q'jj',,*,gmm<CR))B12wSb!""Jr   c                  >    \ rS rSr% S\S'   S\S'   S\S'   S\S'   Srg)	UniqueAllResultir  cupy.ndarrayr   r   inverse_indicescounts N__name__
__module____qualname____firstlineno____annotations____static_attributes__r   r   r   r   r   r  s    !!r   r   c                  *    \ rS rSr% S\S'   S\S'   Srg)UniqueCountsResultiy  r   r   r   r   Nr   r   r   r   r   r   y  s    r   r   c                  *    \ rS rSr% S\S'   S\S'   Srg)UniqueInverseResulti~  r   r   r   r   Nr   r   r   r   r   r   ~  s    !!r   r   c                ,    [        U SSSSS9n[        U6 $ )a^  
Find the unique elements of an array, and counts, inverse and indices.

This function is an Array API compatible alternative to:

>>> x = cupy.array([1, 1, 2])
>>> np.unique(x, return_index=True, return_inverse=True,
...           return_counts=True, equal_nan=False)
(array([1, 2]), array([0, 2]), array([0, 0, 1]), array([2, 1]))

Parameters
----------
x : ndarray
    Input array. It will be flattened if it is not already 1-D.

Returns
-------
out : namedtuple
    The result containing:

    * values - The unique elements of an input array.
    * indices - The first occurring indices for each unique element.
    * inverse_indices - The indices from the set of unique elements
      that reconstruct `x`.
    * counts - The corresponding counts for each unique element.

See Also
--------
unique : Find the unique elements of an array.
numpy.unique_all

TFrG   rH   rI   rE   )rx   r   xresults     r   
unique_allr     s+    B 	F F##r   c                ,    [        U SSSSS9n[        U6 $ )aX  
Find the unique elements and counts of an input array `x`.

This function is an Array API compatible alternative to:

>>> x = cupy.array([1, 1, 2])
>>> cupy.unique(x, return_counts=True, equal_nan=False)
(array([1, 2]), array([2, 1]))

Parameters
----------
x : ndarray
    Input array. It will be flattened if it is not already 1-D.

Returns
-------
out : namedtuple
    The result containing:

    * values - The unique elements of an input array.
    * counts - The corresponding counts for each unique element.

See Also
--------
unique : Find the unique elements of an array.
np.unique_counts

FTr   )rx   r   r   s     r   unique_countsr     s*    : 	F v&&r   c                ,    [        U SSSSS9n[        U6 $ )a  
Find the unique elements of `x` and indices to reconstruct `x`.

This function is Array API compatible alternative to:

>>> x = cupy.array([1, 1, 2])
>>> cupy.unique(x, return_inverse=True, equal_nan=False)
(array([1, 2]), array([0, 0, 1]))

Parameters
----------
x : ndarray
    Input array. It will be flattened if it is not already 1-D.

Returns
-------
out : namedtuple
    The result containing:

    * values - The unique elements of an input array.
    * inverse_indices - The indices from the set of unique elements
      that reconstruct `x`.

See Also
--------
unique : Find the unique elements of an array.
numpy.unique_inverse

FTr   )rx   r   r   s     r   unique_inverser     s*    < 	F ''r   c                    [        U SSSSS9$ )a  
Returns the unique elements of an input array `x`.

This function is Array API compatible alternative to:

>>> x = cupy.array([1, 1, 2])
>>> cupy.unique(x, equal_nan=False)
array([1, 2])

Parameters
----------
x : ndarray
    Input array. It will be flattened if it is not already 1-D.

Returns
-------
out : ndarray
    The unique elements of an input array.

See Also
--------
unique : Find the unique elements of an array.
numpy.unique_values

Fr   )rx   )r   s    r   unique_valuesr     s     4 	 r   r>   )fb)FFFN)FFFTN)
__future__r   typingr   r"   r   r\   r   r   r   ElementwiseKernelr'   r+   ReductionKernelr6   r<   fusionfuserD   rx   rW   r   r   r   r   r   r   r   r   r   r   <module>r      s    "     +3b9B ((5< ++	 @ , ,
 38%)T8<Tn 7<BF"Nj  
"* "
($V$'N%(P r   