o
    W۷i)                     @  s~   d dl mZ d dlZd dlZd dlmZ dddZdddZedd	d
dZdd e	fddZ
dddZdddZdddZdS )    )annotationsN)_corec                 C  s   t | tjr
| j}nt| }|dkrt| } |dkr!t| } |dkrA| jt| }tj||f| j	d}| |
|dd< |S |dkrMt| 
|S td)a  Returns a diagonal or a diagonal array.

    Args:
        v (array-like): Array or array-like object.
        k (int): Index of diagonals. Zero indicates the main diagonal, a
            positive value an upper diagonal, and a negative value a lower
            diagonal.

    Returns:
        cupy.ndarray: If ``v`` indicates a 1-D array, then it returns a 2-D
        array with the specified diagonal filled by ``v``. If ``v`` indicates a
        2-D array, then it returns the specified diagonal of ``v``. In latter
        case, if ``v`` is a :class:`cupy.ndarray` object, then its view is
        returned.

    .. seealso:: :func:`numpy.diag`

          dtypeNzInput must be 1- or 2-d.)
isinstancecupyndarrayndimnumpyarraysizeabszerosr   diagonal
ValueError)vkr   r   ret r   K/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupy/_creation/matrix.pydiag	   s   


r   c                 C  s$   t | r
t | } t|  |S )aE  Creates a diagonal array from the flattened input.

    Args:
        v (array-like): Array or array-like object.
        k (int): Index of diagonals. See :func:`cupy.diag` for detail.

    Returns:
        cupy.ndarray: A 2-D diagonal array with the diagonal copied from ``v``.

    .. seealso:: :func:`numpy.diagflat`

    )r   isscalarasarrayr	   r   ravel)r   r   r   r   r   diagflat1   s   

r   zint32 m, int32 kzT outzK
    int row = i / m;
    int col = i % m;
    out = (col <= row + k);
    cupy_tric                 C  s*   |du r| }t j| |f|d}t|||S )a  Creates an array with ones at and below the given diagonal.

    Args:
        N (int): Number of rows.
        M (int): Number of columns. ``M == N`` by default.
        k (int): The sub-diagonal at and below which the array is filled. Zero
            is the main diagonal, a positive value is above it, and a negative
            value is below.
        dtype: Data type specifier.

    Returns:
        cupy.ndarray: An array with ones at and below the given diagonal.

    .. seealso:: :func:`numpy.tri`

    Nr   )r	   empty_tri_kernel)NMr   r   outr   r   r   triP   s   r#   c                 C  s:   t | } t| jdd |td}t || | jdS )a}  Returns a lower triangle of an array.

    Args:
        m (array-like): Array or array-like object.
        k (int): The diagonal above which to zero elements. Zero is the main
            diagonal, a positive value is above it, and a negative value is
            below.

    Returns:
        cupy.ndarray: A lower triangle of an array.

    .. seealso:: :func:`numpy.tril`

    Nr   r   r   r	   r   r#   shapeboolwherer   typemr   maskr   r   r   trilh   s   
r.   c                 C  s>   t | } t| jdd |d td}t || jd| S )a  Returns an upper triangle of an array.

    Args:
        m (array-like): Array or array-like object.
        k (int): The diagonal below which to zero elements. Zero is the main
            diagonal, a positive value is above it, and a negative value is
            below.

    Returns:
        cupy.ndarray: An upper triangle of an array.

    .. seealso:: :func:`numpy.triu`

    r$   Nr   r%   r   r&   r+   r   r   r   triu}   s   
r/   Fc                 C  s   t | } | jdkrtd|du rt| }t jt| |ft| jt	d}|s3|dddddf n|}t j
| ddt ||d |S )a  Returns a Vandermonde matrix.

    Args:
        x (array-like): 1-D array or array-like object.
        N (int, optional): Number of columns in the output.
            ``N = len(x)`` by default.
        increasing (bool, optional): Order of the powers of the columns.
            If True, the powers increase from right to left,
            if False (the default) they are reversed.

    Returns:
        cupy.ndarray: A Vandermonde matrix.

    .. seealso:: :func:`numpy.vander`

    r   z.x must be a one-dimensional array or sequence.Nr   )r"   )r	   r   r   r   lenr   r   promote_typesr   intpowerreshapearange)xr    
increasingr   tmpr   r   r   vander   s   

 r:   )r   )NF)
__future__r   r   r	   r   r   r   ElementwiseKernelr   floatr#   r.   r/   r:   r   r   r   r   <module>   s     

(

