a
    a-                     @   s<  d Z ddlZddlZddlZddlZddlmZ g dZej	ej
ejejejejejejejejejejejejejejejgZi Zdd Zdd Zd	d
 Zdd Zdd Z d1ddZ!d2ddZ"d3ddZ#dd Z$dd Z%dd Z&d4ddZ'dd  Z(d!d" Z)d#d$ Z*d%d& Z+d5d'd(Z,d)d* Z-d+d, Z.d-d. Z/d6d/d0Z0dS )7z, Utility functions for sparse matrix module
    N)prod)
upcastgetdtypegetdataisscalarlike	isintlikeisshape
issequenceisdenseismatrixget_sum_dtypec                  G   sb   t t| }|dur|S t| g }tD ]$}t||r*|t t| < |  S q*td| f dS )a  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('bool')
    <type 'numpy.bool_'>
    >>> upcast('int32','float32')
    <type 'numpy.float64'>
    >>> upcast('bool',complex,float)
    <type 'numpy.complex128'>

    Nz%no supported conversion for types: %r)_upcast_memogethashnpZfind_common_typesupported_dtypescan_cast	TypeError)argstr    r   d/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/scipy/sparse/sputils.pyr      s    
r   c                  G   s2   t | }|dur|S tttj|  }|t | < |S )z9Same as `upcast` but taking dtype.char as input (faster).N)r   r   r   mapr   dtype)r   r   r   r   r   upcast_char6   s    
r   c                 C   s   t jdg| d| jS )z`Determine data type for binary operation between an array of
    type `dtype` and a scalar.
    r   r   )r   arrayr   )r   Zscalarr   r   r   upcast_scalar@   s    r   c                 C   sx   | j jt tjjkrt| jdkr,| tjS |  }|  }|ttjjks`|ttjjk rht	d| tjS | S )z
    Down-cast index array to np.intp dtype if it is of a larger dtype.

    Raise an error if the array contains a value that is too large for
    intp.
    r   zzCannot deal with arrays with indices larger than the machine maximum address size (e.g. 64-bit indices on 32-bit machine).)
r   itemsizer   ZintpsizeZastypemaxminiinfo
ValueError)arrmaxvalminvalr   r   r   downcast_intp_indexG   s    
$r'   c                 C   s   t j| | jddS )NZnativer   )r   asarrayr   Znewbyteorder)Ar   r   r   	to_native[   s    r*   c              
   C   sz   | du rXz
|j }W qv tyT } z*|dur6t |}n
td|W Y d}~qvd}~0 0 nt | }|tjkrvtd |S )aQ  Function used to simplify argument processing. If 'dtype' is not
    specified (is None), returns a.dtype; otherwise returns a np.dtype
    object created from the specified dtype argument. If 'dtype' and 'a'
    are both None, construct a data type out of the 'default' parameter.
    Furthermore, 'dtype' must be in 'allowed' set.
    Nzcould not interpret data typez0object dtype is not supported by sparse matrices)r   AttributeErrorr   r   Zobject_warningswarn)r   adefaultZnewdtypeer   r   r   r   _   s    
"


r   Fc                 C   s   t j| ||d}t|j |S )z
    This is a wrapper of `np.array(obj, dtype=dtype, copy=copy)`
    that will generate a warning if the result is an object array.
    )r   copy)r   r   r   r   )objr   r1   datar   r   r   r   w   s    
r   r   c                 C   s   t t jj}t t jj}t j}|dur8||kr8t j}t| t jrJ| f} | D ]l}t 	|}t 
|jt jsN|r|jdkr~qNn2t |jt jr| }| }||kr||krqNt j} qqN|S )a&  
    Based on input (integer) arrays `a`, determine a suitable index data
    type that can hold the data in the arrays.

    Parameters
    ----------
    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 : dtype
        Suitable index data type (int32 or int64)

    Nr   )r   r"   Zint32r!   r    intcZint64
isinstancendarrayr(   r   r   r   
issubdtypeinteger)Zarraysr%   Zcheck_contentsZint32minZint32maxr   r$   r&   r   r   r   get_index_dtype   s,    

r9   c                 C   s6   | j dkrt| tjrtjS t| tjr2tjS | S )z Mimic numpy's casting for np.sumu)kindr   r   uintint_r   r   r   r   r      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
   ndimxr   r   r   r      s    r   c                 C   s   t | dkrdS zt|  W nZ ttfyz   ztt| | k}W n ttfy`   Y Y dS 0 |rrt	dt
 | Y S 0 dS )zsIs x appropriate as an index into a sparse matrix? Returns True
    if it can be cast safely to a machine int.
    r   Fz3Inexact indices into sparse matrices are deprecatedT)r   r?   operatorindexr   r#   boolintr,   r-   DeprecationWarning)rA   Z	loose_intr   r   r   r      s    

r   c                 C   sn   z| \}}W n t y    Y dS 0 t|rft|rft|dkrft|dkrf|rb|dkrf|dkrfdS dS dS )zjIs x a valid 2-tuple of dimensions?

    If nonneg, also checks that the dimensions are non-negative.
    Fr   TN)	Exceptionr   r   r?   )rA   ZnonnegMNr   r   r   r      s    r   c                 C   s>   t | ttfr(t| dkp<t| d p<t | tjo<| jdkS )Nr      )r5   listtuplelenr   r>   r6   r?   r   r   r   r   r	      s
    r	   c                 C   s<   t | ttfr&t| dkr&t| d p:t | tjo:| jdkS )Nr      )r5   rK   rL   rM   r	   r   r6   r?   rN   r   r   r   r      s    

r   c                 C   s   t | tjS N)r5   r   r6   r@   r   r   r   r
      s    r
   c                 C   sh   | d urdt | }|tkr tdtt|tjsFtdj|jdd|   krZdksdn t	d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})namerJ   zaxis out of range)
typerL   r   r   r7   r   r8   format__name__r#   )ZaxisZ	axis_typer   r   r   validateaxis   s    rV   c                 C   s  t | dkrtdnft | dkrjzt| d }W n" tyT   t| d f}Y q|0 tdd |D }ntdd | D }|du rt |dkrtd	n |d dk s|d dk rtd
nt|}dd t|D }t |dkrt|}||krtd	||nt |dkr|d }t|d| ||d d  }t
||\}	}
|
dkrxtdd |D }td	|||d| |	f ||d d  }ntdt |dkrtd|S )z0Imitate numpy.matrix handling of shape argumentsr   z8function missing 1 required positional argument: 'shape'rJ   c                 s   s   | ]}t |V  qd S rP   rB   rC   .0argr   r   r   	<genexpr>       zcheck_shape.<locals>.<genexpr>c                 s   s   | ]}t |V  qd S rP   rW   rX   r   r   r   r[   "  r\   NrO   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   )rY   irA   r   r   r   
<listcomp>/  r\   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   ZnewshapeNr   )rY   rA   r   r   r   r[   :  r\   z&can only specify one unknown dimensionz$matrix shape must be two-dimensional)rM   r   iterrB   rC   rL   r#   r   	enumeraterT   divmod)r   Zcurrent_shapeZ
shape_iterZ	new_shapeZcurrent_sizeZnegative_indexesZnew_sizeskipZ	specifiedunspecified	remainderZ	err_shaper   r   r   check_shape  sH    



 
$re   c                 C   s<   |  dd}|  dd}| r4tdd|  ||fS )a?  Unpack keyword arguments for reshape function.

    This is useful because keyword arguments after star arguments are not
    allowed in Python 2, but star keyword arguments are. This function unpacks
    'order' and 'copy' from the star keyword arguments (with defaults) and
    throws an error for any remaining.
    orderCr1   Fz/reshape() got unexpected keywords arguments: {}z, )popr   rT   joinkeys)kwargsrf   r1   r   r   r   check_reshape_kwargsG  s    	rl   c                 C   s&   t tjddd}|duo$t| |S )zV
    Check whether object is pydata/sparse matrix, avoiding importing the module.
    sparseZSparseArrayN)getattrsysmodulesr   r5   )mZbase_clsr   r   r   is_pydata_spmatrixX  s    rr   c                  O   s   t j| i |t jS rP   )r   r   viewmatrix)r   rk   r   r   r   rt   g  s    rt   c                 C   s8   t | tjr"|d u s| j|kr"| S tj| |dtjS )Nr   )r5   r   rt   r   r(   rs   )r3   r   r   r   r   asmatrixk  s    ru   )NN)NF)r   NF)F)N)N)1__doc__ro   rB   r,   Znumpyr   Zscipy._lib._utilr   __all__Zbool_byteZubyteshortZushortr4   Zuintcr=   r<   ZlonglongZ	ulonglongsingledoubleZ
longdoubleZcsingleZcdoubleZclongdoubler   r   r   r   r   r'   r*   r   r   r9   r   r   r   r   r	   r   r
   rV   re   rl   rr   rt   ru   r   r   r   r   <module>   s@   "



5	

2