
    
9jF                       S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKJr  S SK	J
r
   " S S5      r " S S\5      r\" 5       r  " S	 S
\5      r\" 5       r \4S jrS rSS jrSS jrSS jrSS jrSS jrSS jrSS jrg)    )annotationsN)	from_data)joinc                  4    \ rS rSrSrS rS	S jrS rS rSr	g)
AxisConcatenator   zTranslates slice objects to concatenation along an axis.

For detailed documentation on usage, see :func:`cupy.r_`.
This implementation is partially borrowed from NumPy's one.

c                    X2-
  nUS:  a  XES-   -  n[        [        U5      5      nUnUS U XeS  -   XgU -   nUR                  U5      $ )Nr      )listrange	transpose)	selfobjndimndmintrans1dk2defaxesk1axess	            Q/home/wildlama/miniconda3/lib/python3.13/site-packages/cupy/_indexing/generate.py_output_objAxisConcatenator._output_obj   s^    \Q;AvGuU|$s|gcl*rN}}T""    c                4    Xl         X@l        X l        X0l        g N)axisr   matrixr   )r   r   r   r   r   s        r   __init__AxisConcatenator.__init__"   s    	
r   c                   U R                   nU R                  n/ n/ n/ n[        U[        5      (       a  [        e[        U[
        5      (       d  U4n[        U5       GH
  u  px[        U[        5      (       a  [        e[        U[        5      (       a  US:w  a  [        S5      e[        e[        U5      [        R                  ;   a&  [        R                  " XS9n	UR                  U5        Oi[        R                  " USUS9n	US:  a<  [        R                  " USS9R                  n
US:w  a  X:  a  U R!                  XX25      n	UR                  U	5        UR                  U	5        GM     [        R"                  " / UQU Vs/ s H  oU   PM	     snQ76 nUb  U H  nXH   R%                  U5      XH'   M     [&        R(                  " [        U5      U R*                  S	9$ s  snf )
Nr   z+special directives must be the first entry.)r   F)copyr   r
   r"   )r   )r   r   
isinstancestrNotImplementedErrortuple	enumerateslice
ValueErrortypenumpy
ScalarTyper   arrayappendr   r   result_typeastyper   concatenater   )r   keyr   r   objsarraysscalarsiknewobjr   final_dtypes               r   __getitem__AxisConcatenator.__getitem__(   s   ,,

c3%%#u%%&CcNDA!U##))As##6$EG G))aE,,,"8q!"eD19$??159>>D"}!%!1!1&!Of%KK' #* ''LL72K7aq672KL"'..5  d$))<< 3Ls   G 
c                    g)Nr    )r   s    r   __len__AxisConcatenator.__len__O   s    r   )r   r   r   r   N)r   Fr
   r$   )
__name__
__module____qualname____firstlineno____doc__r   r   r<   r@   __static_attributes__r?   r   r   r   r      s    #%=Nr   r   c                  (   ^  \ rS rSrU 4S jrSrU =r$ )CClassS   c                $   > [         TU ]  SSSS9  g )Nr$      r   )r   r   superr   r   	__class__s    r   r   CClass.__init__U   s    1a0r   r?   rB   rC   rD   rE   r   rG   __classcell__rP   s   @r   rI   rI   S   s    1 1r   rI   c                  (   ^  \ rS rSrU 4S jrSrU =r$ )RClasst   c                "   > [         TU ]  5         g r   rM   rO   s    r   r   RClass.__init__v   s    r   r?   rR   rT   s   @r   rV   rV   t   s     r   rV   c                    [        U 5      n [        U 5      nSU-  n[        R                  " U4U -   US9n[	        U 5       H9  u  pV[        R
                  " XaS9R                  USU U4-   X5S-   S -   5      XE'   M;     U$ )a{  Returns an array representing the indices of a grid.

Computes an array where the subarrays contain index values 0,1,...
varying only along the corresponding axis.

Args:
    dimensions: The shape of the grid.
    dtype: Data type specifier. It is int by default.

Returns:
    ndarray:
    The array of grid indices,
    ``grid.shape = (len(dimensions),) + tuple(dimensions)``.

Examples
--------
>>> grid = cupy.indices((2, 3))
>>> grid.shape
(2, 2, 3)
>>> grid[0]        # row indices
array([[0, 0, 0],
       [1, 1, 1]])
>>> grid[1]        # column indices
array([[0, 1, 2],
       [0, 1, 2]])

.. seealso:: :func:`numpy.indices`

r
   dtypeNr
   )r(   lencupyemptyr)   arangereshape)
dimensionsr]   Nshaperesr8   dims          r   indicesrh      s    < z"JJA1HE
**aTJ&e
4CJ'S.66"1I1uv.
 ( Jr   c                 "   / n[        U 5      n[        U 5       H  u  p4[        R                  " U5      nUR                  S:w  a  [        S5      eUR                  S:X  a  UR                  [        R                  5      n[        R                  " UR                  [        R                  5      (       a  UR                  5       u  nUR                  SU-  UR                  4-   SX#-
  S-
  -  -   5      nUR!                  U5        M     [#        U5      $ )a  Construct an open mesh from multiple sequences.

This function takes N 1-D sequences and returns N outputs with N
dimensions each, such that the shape is 1 in all but one dimension
and the dimension with the non-unit shape value cycles through all
N dimensions.

Using `ix_` one can quickly construct index arrays that will index
the cross product. ``a[cupy.ix_([1,3],[2,5])]`` returns the array
``[[a[1,2] a[1,5]], [a[3,2] a[3,5]]]``.

Args:
    *args: 1-D sequences

Returns:
    tuple of ndarrays:
    N arrays with N dimensions each, with N the number of input sequences.
    Together these arrays form an open mesh.

Examples
--------
>>> a = cupy.arange(10).reshape(2, 5)
>>> a
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> ixgrid = cupy.ix_([0,1], [2,4])
>>> ixgrid
(array([[0],
       [1]]), array([[2, 4]]))

.. warning::

    This function may synchronize the device.

.. seealso:: :func:`numpy.ix_`

r
   z!Cross index must be 1 dimensionalr   r[   )r^   r)   r   asarrayr   r+   sizer2   r-   intpr_   
issubdtyper]   bool_nonzerorb   r0   r(   )argsoutndr9   news        r   ix_rt      s    N C	TBD/$88q=@AA88q=**UZZ(C??399djj11;;=DCkk$(chh[0426A:3FFG

3 " :r   c                <   [        U5      n[        U 5      U:w  a  [        SR                  U5      5      eU HE  n[        U[        R
                  5      (       a  M$  [        SR                  [        U5      5      5      e   [        U[        5      (       a  U4U-  n[        R                  " [        R                  U5      [        R                  " [        R                  5      R                   :  a  [        S5      eSnS/U-  nUc  SOUR#                  5       nUS:X  a&  [%        US-
  SS5       H  nXaUS-      -  nXgU'   M     O3US:X  a"  [%        SU5       H  nXaUS-
     -  nXgU'   M     O[        S	5      e[        R&                  " U 6 n [        R(                  " U S
   R*                  [        R                  S9n	[-        XX5       GHQ  u  pZp[        U[        R.                  5      (       d  [        S5      e[        R0                  " U[        R                  S5      (       dB  [        SR                  UR2                  [        R                  " 5       R2                  5      5      eUR5                  [        R                  SS9nUS:X  aA  [        R6                  " [        R8                  " X:  US
:  5      5      (       a  [        S5      eOGUS:X  a  [        R:                  " US
US-
  5      nO%US:X  a  X-  nO[        SR                  U5      5      eXU-  -  n	GMT     U	$ )ar  
Converts a tuple of index arrays into an array of flat indices, applying
boundary modes to the multi-index.

Args:
    multi_index (tuple of cupy.ndarray) : A tuple of integer arrays, one
        array for each dimension.
    dims (tuple of ints): The shape of array into which the indices from
        ``multi_index`` apply.
    mode ('raise', 'wrap' or 'clip'), optional: Specifies how out-of-bounds
        indices are handled.  Can specify either one mode or a tuple of
        modes, one mode per index:

        - *'raise'* -- raise an error
        - *'wrap'* -- wrap around (default)
        - *'clip'* -- clip to the range

        In 'clip' mode, a negative index which would normally wrap will
        clip to 0 instead.
    order ('C' or 'F'), optional: Determines whether the multi-index should
        be viewed as indexing in row-major (C-style) or column-major
        (Fortran-style) order.

Returns:
    raveled_indices (cupy.ndarray): An array of indices into the flattened
    version of an array of dimensions ``dims``.

.. warning::

    This function may synchronize the device when ``mode == 'raise'``.

Notes
-----
Note that the default `mode` (``'wrap'``) is different than in NumPy. This
is done to avoid potential device synchronization.

Examples
--------
>>> cupy.ravel_multi_index(cupy.asarray([[3,6,6],[4,5,1]]), (7,6))
array([22, 41, 37])
>>> cupy.ravel_multi_index(cupy.asarray([[3,6,6],[4,5,1]]), (7,6),
...                        order='F')
array([31, 41, 13])
>>> cupy.ravel_multi_index(cupy.asarray([[3,6,6],[4,5,1]]), (4,6),
...                        mode='clip')
array([22, 23, 19])
>>> cupy.ravel_multi_index(cupy.asarray([[3,6,6],[4,5,1]]), (4,4),
...                        mode=('clip', 'wrap'))
array([12, 13, 13])
>>> cupy.ravel_multi_index(cupy.asarray((3,1,4,1)), (6,7,8,9))
array(1621)

.. seealso:: :func:`numpy.ravel_multi_index`, :func:`unravel_index`
z5parameter multi_index must be a sequence of length {}z-{} object cannot be interpreted as an integerzQinvalid dims: array size defined by dims is larger than the maximum possible sizer
   CrL   r$   Forder not understoodr   r\   z+elements of multi_index must be cupy arrays	same_kindzgmulti_index entries could not be cast from dtype('{}') to dtype('{}') according to the rule 'same_kind'Fr#   raisez"invalid entry in coordinates arrayclipwrapzUnrecognized mode: {})r^   r+   formatr%   numbersIntegral	TypeErrorr,   r&   	functoolsreduceoperatormulr_   iinfoint64maxupperr   broadcast_arrayszerosre   zipndarraycan_castr]   r2   any
logical_orr{   )multi_indexdimsmodeorderr   dsravel_stridesr8   raveled_indicesstrideidx_modes                r   ravel_multi_indexr      s   p t9D
;4t& 	& !W--..?FFG   $x$d+djj.D.H.HH : ; 	; 	
AC$JM=CekkmE|taxR(AQKA ! ) 
#q$AQKA !   /00''5KjjQ!5!5TZZHO!$T+!L3#t||,,IJJ}}S$**k::DDJFIItzz|11E34 4 jj%j0Gxx#':;; !EFF <f_))CAE*Cf_'C4;;EBCCC<') "M* r   c                V   Uc  SOUR                  5       nUS:X  a  [        U5      nOUS:X  a  O[        S5      e[        R                  " U [        R
                  S5      (       dB  [        SR                  U R                  [        R
                  " 5       R                  5      5      eU S:  R                  5       (       a  [        S5      e/ nU H  nUR                  X-  5        X-  n M     U S:  R                  5       (       a  [        S5      eUS:X  a  [        U5      n[        U5      $ )a  Converts array of flat indices into a tuple of coordinate arrays.

Args:
    indices (cupy.ndarray): An integer array whose elements are indices
        into the flattened version of an array of dimensions :obj:`dims`.
    dims (tuple of ints): The shape of the array to use for unraveling
        indices.
    order ('C' or 'F'): Determines whether the indices should be viewed as
        indexing in row-major (C-style) or column-major (Fortran-style)
        order.

Returns:
    tuple of ndarrays:
    Each array in the tuple has the same shape as the indices array.

Examples
--------
>>> cupy.unravel_index(cupy.array([22, 41, 37]), (7, 6))
(array([3, 6, 6]), array([4, 5, 1]))
>>> cupy.unravel_index(cupy.array([31, 41, 13]), (7, 6), order='F')
(array([3, 6, 6]), array([4, 5, 1]))

.. warning::

    This function may synchronize the device.

.. seealso:: :func:`numpy.unravel_index`, :func:`ravel_multi_index`

rv   rw   rx   ry   zlIterator operand 0 dtype could not be cast from dtype('{}') to dtype('{}') according to the rule 'same_kind'r   zinvalid entry in index array)r   reversedr+   r_   r   r   r   r}   r]   r   r0   r(   )rh   r   r   unraveled_coordsrg   s        r   unravel_indexr   l  s   < =CekkmE|~	#/00==$**k::228&tzz|11334 	4 	!788..  	!788|#$45!""r   c                v    [         R                  " X 4[         R                  S9nU" X25      R                  5       $ )a  
Return the indices to access (n, n) arrays, given a masking function.

Assume `mask_func` is a function that, for a square array a of
size ``(n, n)`` with a possible offset argument `k`, when called
as ``mask_func(a, k)`` returns a new array with zeros in certain
locations (functions like :func:`~cupy.triu` or :func:`~cupy.tril` do
precisely this). Then this function returns the indices where the non-zero
values would be located.

Args:
    n (int): The returned indices will be valid to access arrays
        of shape (n, n).
    mask_func (callable): A function whose call signature is
        similar to that of :func:`~cupy.triu`, :func:`~tril`.  That is,
        ``mask_func(x, k)`` returns a boolean array, shaped like
        `x`.  `k` is an optional argument to the function.
    k (scalar): An optional argument which is passed through to
        `mask_func`. Functions like :func:`~cupy.triu`, :func:`~cupy.tril`
        take a second argument that is interpreted as an offset.

Returns:
    tuple of arrays: The `n` arrays of indices corresponding to
    the locations where ``mask_func(np.ones((n, n)), k)`` is
    True.

.. warning::

    This function may synchronize the device.

.. seealso:: :func:`numpy.mask_indices`
r\   )r_   onesint8ro   )n	mask_funcr9   as       r   mask_indicesr     s.    B 			1&		*AQ?""$$r   c                   ^ [         R                  " XU[        S9m[        U4S j[         R                  " TR
                  [        S9 5       5      $ )a#  Returns the indices of the lower triangular matrix.
Here, the first group of elements contains row coordinates
of all indices and the second group of elements
contains column coordinates.

Parameters
----------
n : int
    The row dimension of the arrays for which the returned
    indices will be valid.
k : int, optional
    Diagonal above which to zero elements. `k = 0`
    (the default) is the main diagonal, `k < 0` is
    below it and `k > 0` is above.
m : int, optional
    The column dimension of the arrays for which the
    returned arrays will be valid. By default, `m = n`.

Returns
-------
y : tuple of ndarrays
    The indices for the triangle. The returned tuple
    contains two arrays, each with the indices along
    one dimension of the array.

See Also
--------
numpy.tril_indices

r9   r]   c              3  l   >#    U  H)  n[         R                  " UTR                  5      T   v   M+     g 7fr   r_   broadcast_tore   .0indstri_s     r   	<genexpr>tril_indices.<locals>.<genexpr>  /      B@T ""44T:@   14r\   r_   triboolr(   rh   re   intr   r9   mr   s      @r   tril_indicesr     sE    @ 88AAT*D B!\\$**C@B B Br   c                    U R                   S:w  a  [        S5      e[        U R                  S   XR                  S   S9$ )a
  Returns the indices for the lower-triangle of arr.

Parameters
----------
arr : cupy.ndarray
      The indices are valid for square arrays
      whose dimensions are the same as arr.
k : int, optional
    Diagonal offset.

See Also
--------
numpy.tril_indices_from

rL   input array must be 2-dr$   r9   r   )r   r+   r   re   arrr9   s     r   tril_indices_fromr     s8    " xx1}233		"iim<<r   c                   ^ [         R                  " XUS-
  [        S9) m[        U4S j[         R                  " TR
                  [        S9 5       5      $ )a~  Returns the indices of the upper triangular matrix.
Here, the first group of elements contains row coordinates
of all indices and the second group of elements
contains column coordinates.

Parameters
----------
n : int
    The size of the arrays for which the returned indices will
    be valid.
k : int, optional
    Refers to the diagonal offset. By default, `k = 0` i.e.
    the main dialogal. The positive value of `k`
    denotes the diagonals above the main diagonal, while the negative
    value includes the diagonals below the main diagonal.
m : int, optional
    The column dimension of the arrays for which the
    returned arrays will be valid. By default, `m = n`.

Returns
-------
y : tuple of ndarrays
    The indices for the triangle. The returned tuple
    contains two arrays, each with the indices along
    one dimension of the array.

See Also
--------
numpy.triu_indices

r
   r   c              3  l   >#    U  H)  n[         R                  " UTR                  5      T   v   M+     g 7fr   r   r   s     r   r   triu_indices.<locals>.<genexpr>3  r   r   r\   r   r   s      @r   triu_indicesr     sL    B HHQQU$//D B!\\$**C@B B Br   c                    U R                   S:w  a  [        S5      e[        U R                  S   XR                  S   S9$ )a`  Returns indices for the upper-triangle of arr.

Parameters
----------
arr : cupy.ndarray
      The indices are valid for square arrays.
k : int, optional
    Diagonal offset (see 'triu_indices` for details).

Returns
-------
triu_indices_from : tuple of ndarrays
    Indices for the upper-triangle of `arr`.

See Also
--------
numpy.triu_indices_from

rL   r   r   r$   r   )r   r+   r   re   r   s     r   triu_indices_fromr   7  s8    * xx1}233		"iim<<r   )r|   rv   )rv   )r   )r   N)
__future__r   r   r~   r   r-   r_   cupy._creationr   cupy._manipulationr   r   rI   c_rV   r_r   rh   rt   r   r   r   r   r   r   r   r?   r   r   <module>r      s    "      $ #@ @F1 1 X4  X: " &R4nqh:#z"%V#BL=,$BN=r   