o
    װi                     @   sx   U d dl Z d dlZd dlZd dlmZ dd dD Zi Zeed< dd Z	d	d
 Z
dddZdd Zdd ZdddZdS )    N	get_dtypec                 C   s   g | ]}t |qS  r   .0xr   r   O/home/ubuntu/.local/lib/python3.10/site-packages/cupyx/scipy/sparse/_sputils.py
<listcomp>   s    r	   )singledoublecsinglecdouble_upcast_memoc                 C   s   t | tjS N)
isinstancecupyndarrayr   r   r   r   isdense   s   r   c                 C   s   t | pt| o| jdkS )z8Is x either a scalar, an array scalar, or a 0-dim array?r   )r   isscalarr   ndimr   r   r   r   isscalarlike   s   r   r   Fc                 C   s   t t jj}t t jj}t j}|dur||krt j}t| t jr%| f} | D ]6}t |}t 	|j
t js]|rW|jdkr>q't |j
t jrW| }| }||krW||krWq't j} |S q'|S )a  Based on input (integer) arrays ``a``, determines a suitable index data
    type that can hold the data in the arrays.

    Args:
        arrays (tuple of array_like):
            Input arrays whose types/contents to check
        maxval (float, optional):
            Maximum value needed
        check_contents (bool, optional):
            Whether to check the values in the arrays and not just their types.
            Default: False (check only the types)

    Returns:
        dtype: Suitable index data type (int32 or int64)
    Nr   )r   iinfoint32minmaxint64r   r   asarraycan_castdtypesize
issubdtypeinteger)arraysmaxvalcheck_contentsint32minint32maxr   arrminvalr   r   r   get_index_dtype   s0   

r*   c                 C   st   | d ur6t | trtdt| }tt|tjs$tdj|j	dd|   kr1dks8t
d t
dd S d S )NzlTuples are not accepted for the 'axis' parameter. Please pass in one of the following: {-2, -1, 0, 1, None}.z#axis must be an integer, not {name})name   zaxis out of range)r   tuple	TypeErrortyper   r!   r   r"   format__name__
ValueError)axis	axis_typer   r   r   validateaxisF   s   
r6   c                  G   sT   t | }|dur|S tj|  }tD ]}t||r"|t | < |  S qtd| f )am  Returns the nearest supported sparse dtype for the
    combination of one or more types.

    upcast(t0, t1, ..., tn) -> T  where T is a supported dtype

    Examples:
        >>> upcast('int32')
        <type 'numpy.int32'>
        >>> upcast('int32','float32')
        <type 'numpy.float64'>
        >>> upcast('bool',float)
        <type 'numpy.complex128'>
    Nz%no supported conversion for types: %r)r   getnumpyresult_typesupported_dtypesr   r   r/   )argstupcastr   r   r   r=   ]   s   

r=   c                 C   s  t | dkr
tdt | dkr4zt| d }W n ty)   t| d f}Y nw tdd |D }n	tdd | D }|du r\t |dkrKtd	|d dk sW|d dk r[td
nst|}dd t	|D }t |dkrt|}||krtd
||nMt |dkr|d }t|d| ||d d  }t||\}	}
|
dkrtdd |D }td
|||d| |	f ||d d  }ntdt |dkrtd|S )zCheck validity of the shaper   z8function missing 1 required positional argument: 'shape'r-   c                 s       | ]}t |V  qd S r   operatorindexr   argr   r   r   	<genexpr>       zcheck_shape.<locals>.<genexpr>c                 s   r>   r   r?   rB   r   r   r   rD      rE   N   z,shape must be a 2-tuple of positive integersz#'shape' elements cannot be negativec                 S   s   g | ]
\}}|d k r|qS )r   r   )r   ir   r   r   r   r	      s    zcheck_shape.<locals>.<listcomp>z,cannot reshape array of size {} into shape{}c                 s   s     | ]}|d k r
dn|V  qdS )r   newshapeNr   r   r   r   r   rD      s    z&can only specify one unknown dimensionz$matrix shape must be two-dimensional)lenr/   iterr@   rA   r.   r3   r8   prod	enumerater1   divmod)r;   current_shape
shape_iter	new_shapecurrent_sizenegative_indexesnew_sizeskip	specifiedunspecified	remainder	err_shaper   r   r   check_shapez   sN   

"$rY   )r   NFr   )r   r@   r8   cupy._core._dtyper   r:   r   dict__annotations__r   r   r*   r6   r=   rY   r   r   r   r   <module>   s   
 
0