o
    ְi
                     @   s^   d dl Z d dlZd dlZd dlmZ dddZdd Zdd Zdd	d
ZdddZ	dd Z
dS )    N)internalc                 C   s   t || j}t| |S )a  Reverse the order of elements in an array along the given axis.

    Note that ``flip`` function has been introduced since NumPy v1.12.
    The contents of this document is the same as the original one.

    Args:
        a (~cupy.ndarray): Input array.
        axis (int or tuple of int or None): Axis or axes along which to flip
            over. The default, ``axis=None``, will flip over all of the axes of
            the input array. If axis is negative it counts from the last to the
            first axis. If axis is a tuple of ints, flipping is performed on
            all of the axes specified in the tuple.

    Returns:
        ~cupy.ndarray: Output array.

    .. seealso:: :func:`numpy.flip`

    )r   _normalize_axis_indicesndim_flip)aaxisaxes r	   P/home/ubuntu/.local/lib/python3.10/site-packages/cupy/_manipulation/rearrange.pyflip	   s   
r   c                 C   s(   | j dk r	td| dddddf S )aA  Flip array in the left/right direction.

    Flip the entries in each row in the left/right direction. Columns
    are preserved, but appear in a different order than before.

    Args:
        a (~cupy.ndarray): Input array.

    Returns:
        ~cupy.ndarray: Output array.

    .. seealso:: :func:`numpy.fliplr`

       Input must be >= 2-dNr   
ValueErrorr   r	   r	   r
   fliplr!   s   
r   c                 C   s    | j dk r	td| ddd S )a;  Flip array in the up/down direction.

    Flip the entries in each column in the up/down direction. Rows are
    preserved, but appear in a different order than before.

    Args:
        a (~cupy.ndarray): Input array.

    Returns:
        ~cupy.ndarray: Output array.

    .. seealso:: :func:`numpy.flipud`

       zInput must be >= 1-dNr   r   r   r	   r	   r
   flipud5   s   
r   c                    s<  |du rt   |d jS t|r|fn|}t fdd|D }t|tj	r| }t
t||j}t||f}t||f}g }t jD ]}dg j } j| ||< |t j| | qLt||D ]\}}||  |8  < ||   j| ;  < qot jD ]}t||  j||< q t| S t||}	|	jdkrtddd t jD }
|	D ]\}}|
|  |7  < qtdtdffg j }|
 D ](\}}| j| pd; }|rtd| t|dft| dtd|ff||< qt }tj| D ]}t| \}} | ||< q|S )	a@  Roll array elements along a given axis.

    Elements that roll beyond the last position are re-introduced at the first.

    Args:
        a (~cupy.ndarray): Array to be rolled.
        shift (int or tuple of int): The number of places by which elements are
            shifted. If a tuple, then `axis` must be a tuple of the same size,
            and each of the given axes is shifted by the corresponding number.
            If an int while `axis` is a tuple of ints, then the same value is
            used for all given axes.
        axis (int or tuple of int or None): The axis along which elements are
            shifted. By default, the array is flattened before shifting, after
            which the original shape is restored.

    Returns:
        ~cupy.ndarray: Output array.

    .. seealso:: :func:`numpy.roll`

    Nr   c                    s   g | ]	}t | jqS r	   )r   _normalize_axis_indexr   .0axr   r	   r
   
<listcomp>c   s    zroll.<locals>.<listcomp>r   z4'shift' and 'axis' should be scalars or 1D sequencesc                 S   s   i | ]}|d qS )r   r	   r   r	   r	   r
   
<dictcomp>   s    zroll.<locals>.<dictcomp>)rollravelreshapeshapenumpyisscalartuple
isinstancecupyndarraymaxlensizebroadcast_toranger   appendarangezip	broadcastndr   sliceitems
empty_like	itertoolsproduct)r   shiftr   r   n_axesindicesr   	ind_shapesbroadcastedshiftsshrollsoffsetresult	arr_index	res_indexr	   r   r
   r   I   sV   

r   r   r   r   c                 C   sL  | j }|dk rtdt|}t|dkrtd|d |d ks-t|d |d  |kr1td| |d   kr>|k rNn n| |d   krM|k sWn td| |f |d }|dkre| d	d	 S |dkrnt| |S ttd|}||d  ||d  ||d < ||d < |dkrt	t| |d f|S tt	| ||d fS )
a-  Rotate an array by 90 degrees in the plane specified by axes.

    Note that ``axes`` argument has been introduced since NumPy v1.12.
    The contents of this document is the same as the original one.

    Args:
        a (~cupy.ndarray): Array of two or more dimensions.
        k (int): Number of times the array is rotated by 90 degrees.
        axes: (tuple of ints): The array is rotated in the plane defined by
            the axes. Axes must be different.

    Returns:
        ~cupy.ndarray: Output array.

    .. seealso:: :func:`numpy.rot90`

    r   r   zlen(axes) must be 2r   r   zaxes must be differentzaxes must be >= %d and < %d   N)
r   r   r!   r&   absr   listr)   r#   	transpose)r   kr   a_ndimaxes_tr	   r	   r
   rot90   s(   (:
*rI   c                 C   s6   t d g| j }|D ]
}t d d d||< q
| t| S )Nr   )r/   r   r!   )r   r   indexerr   r	   r	   r
   r      s   r   )N)r   rA   )r2   r   r#   
cupy._corer   r   r   r   r   rI   r   r	   r	   r	   r
   <module>   s    


K.