a
    ˆù»jg5  ã                   @  sœ   d Z ddlmZ ddlmZ ddlZddlmZ er@ddl	mZ
 eejfZdd„ ZG d	d
„ d
ƒZddœdd„Zdd„ Zdd„ Zddd„Zdd„ Zdd„ ZdS )z0Indexing mixin for sparse array/matrix classes.
é    )Úannotations)ÚTYPE_CHECKINGNé   )Ú	isintlikec                 C  s0   t  | |¡\}}| jj|j_|jj|j_||fS )a   
    Same as np.broadcast_arrays(a, b) but old writeability rules.

    NumPy >= 1.17.0 transitions broadcast_arrays to return
    read-only arrays. Set writeability explicitly to avoid warnings.
    Retain the old writeability rules, as our Cython code assumes
    the old behavior.
    )ÚnpZbroadcast_arraysÚflagsZ	writeable)ÚaÚbÚxÚy© r   ú9E:\soccer\ml_env\Lib\site-packages\scipy/sparse/_index.pyÚ_broadcast_arrays   s    	r   c                   @  s°   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*S )+Ú
IndexMixinzS
    This class provides common dispatching and validation logic for indexing.
    c                 C  s"   ddl m} t| |ƒrtdƒ‚dS )zçWe do not currently support 1D sparse arrays.

        This function is called each time that a 1D array would
        result, raising an error instead.

        Once 1D sparse arrays are implemented, it should be removed.
        r   )ÚsparrayzcWe have not yet implemented 1D sparse slices; please index using explicit indices, e.g. `x[:, [0]]`N)Zscipy.sparser   Ú
isinstanceÚNotImplementedError)Úselfr   r   r   r   Ú_raise_on_1d_array_slice#   s
    
ÿz#IndexMixin._raise_on_1d_array_slicec                 C  s  |   |¡\}}t|tƒrŒt|tƒr.|  ||¡S t|tƒrL|  ¡  |  ||¡S |jdkrj|  ¡  |  ||¡S |jdkr€|  ||¡S t	dƒ‚�n:t|tƒ�r
t|tƒr¶|  ¡  |  
||¡S t|tƒrè|td ƒkrÜ||krÜ|  ¡ S |  ||¡S |jdk�r |  ||¡S t	dƒ‚n¼|jdk�rPt|tƒ�r6|  ¡  |  ||¡S t|tƒ�rÆ|  ||¡S nvt|tƒ�rh|  ||¡S t|tƒ�r~t	dƒ‚nH|jd dk�rÆ|jdk�sª|jd dk�rÆ|  |d d …df | ¡ ¡S t||ƒ\}}|j|jk�rêt	dƒ‚|jdk�r| jt |¡j| jd�S |  ||¡S )Nr   é   zindex results in >2 dimensionsr   ú'number of row and column indices differ©Údtype)Ú_validate_indicesr   Ú	INT_TYPESÚ_get_intXintÚslicer   Ú_get_intXsliceÚndimÚ_get_intXarrayÚ
IndexErrorÚ_get_sliceXintÚcopyÚ_get_sliceXsliceÚ_get_sliceXarrayÚ_get_arrayXintÚ_get_arrayXsliceÚshapeÚ_get_columnXarrayZravelr   ÚsizeÚ	__class__r   Z
atleast_2dr   Ú_get_arrayXarray)r   ÚkeyÚrowÚcolr   r   r   Ú__getitem__3   sT    








,zIndexMixin.__getitem__c           
      C  sb  |   |¡\}}t|tƒr\t|tƒr\tj|| jd�}|jdkrDtdƒ‚|  |||j	d ¡ d S t|t
ƒrŠtj| | jd ¡Ž d d …d f }n
t |¡}t|t
ƒrÜtj| | jd ¡Ž d d d …f }|jdkræ|d d …d f }n
t |¡}t||ƒ\}}|j|jk�r
tdƒ‚ddlm} ||ƒ�r |jdk�r<|d  }|d  }|jd dk�oX|jd dk}|jd dk�ov|jd dk}	|�s”|jd |jd k�r°|	�s¸|jd |jd k�s¸tdƒ‚|jd dk�sØ|jd dk�rÜd S |jdd	�}| ¡  |  |||¡ n^tj|| jd�}| ¡ j| ¡ jk�r4t ||j¡}|jdk�rDd S | |j¡}|  |||¡ d S )
Nr   r   z&Trying to assign a sequence to an itemr   r   ©Úissparsezshape mismatch in assignmentT)r"   )r   r   r   r   Úasarrayr   r)   Ú
ValueErrorÚ_set_intXintZflatr   ZarangeÚindicesr'   Z
atleast_1dr   r   r    Ú_baser1   ZtocooZsum_duplicatesÚ_set_arrayXarray_sparseZsqueezeZbroadcast_toZreshapeÚ_set_arrayXarray)
r   r,   r
   r-   r.   ÚiÚjr1   Zbroadcast_rowZbroadcast_colr   r   r   Ú__setitem__e   sV    

$

"


ÿÿ zIndexMixin.__setitem__c           
      C  sz  ddl m} t||tjfƒrT|jdkrT|jjdkrT|j| jkrFt	dƒ‚| 
¡ \}}nt|ƒ\}}| j\}}dddd	d
œdd„}t|ƒr¾t|ƒ}|| k s ||kr¬t	d| ƒ‚|dk rò||7 }n4t|ƒ }d urÜ|||dƒ}nt|tƒsò|  ||¡}t|ƒ�r:t|ƒ}|| k �s||k�r&t	d| ƒ‚|dk �rr||7 }n8t|ƒ }	d u�rZ||	|dƒ}nt|tƒ�sr|  ||¡}||fS )Nr   )Ú_spbaser   r	   z.boolean index shape does not match array shapeznpt.NDArray[np.bool_]ÚintÚstrznpt.NDArray[np.int_])ÚidxÚ	axis_sizeÚ	axis_nameÚreturnc                 S  s2   t | ƒ|kr*td|› dt | ƒ› d|› �ƒ‚t| ƒS )Nzboolean z index has incorrect length: z instead of )Úlenr    Ú_boolean_index_to_array)r?   r@   rA   r   r   r   Ú_validate_bool_idx¥   s    ÿÿz8IndexMixin._validate_indices.<locals>._validate_bool_idxzrow index (%d) out of ranger   r-   zcolumn index (%d) out of rangeÚcolumn)r6   r<   r   r   Zndarrayr   r   Úkindr'   r    ÚnonzeroÚ_unpack_indexr   r=   Ú_compatible_boolean_indexr   Ú
_asindices)
r   r,   r<   r-   r.   ÚMÚNrE   Zbool_rowZbool_colr   r   r   r   ™   sB    ÿ
ÿ





zIndexMixin._validate_indicesc              
   C  sÖ   zt  |¡}W n4 tttfyB } ztdƒ|‚W Y d}~n
d}~0 0 |jdvrVtdƒ‚|jdkrd|S | ¡ }||kr€td| ƒ‚| 	¡ }|dk rÒ|| k r¦td| ƒ‚||u s¶|j
js¾| ¡ }||dk   |7  < |S )z“Convert `idx` to a valid index for an axis with a given length.

        Subclasses that need special validation can override this method.
        zinvalid indexN)r   r   zIndex dimension must be 1 or 2r   úindex (%d) out of range)r   r2   r3   Ú	TypeErrorÚMemoryErrorr    r   r)   ÚmaxÚminr   Zowndatar"   )r   r?   Úlengthr
   ÚeZmax_indxZmin_indxr   r   r   rK   É   s&     


zIndexMixin._asindicesc                 C  sP   | j \}}t|ƒ}|| k s$||kr0td| ƒ‚|dk r@||7 }|  |tdƒ¡S )zGReturn a copy of row i of the matrix, as a (1 x n) row vector.
        rN   r   N)r'   r=   r    r   r   ©r   r9   rL   rM   r   r   r   Ú_getrowç   s    
zIndexMixin._getrowc                 C  sP   | j \}}t|ƒ}|| k s$||kr0td| ƒ‚|dk r@||7 }|  tdƒ|¡S )zMReturn a copy of column i of the matrix, as a (m x 1) column vector.
        rN   r   N)r'   r=   r    r!   r   rU   r   r   r   Ú_getcolò   s    
zIndexMixin._getcolc                 C  s
   t ƒ ‚d S ©N©r   ©r   r-   r.   r   r   r   r   ý   s    zIndexMixin._get_intXintc                 C  s
   t ƒ ‚d S rX   rY   rZ   r   r   r   r      s    zIndexMixin._get_intXarrayc                 C  s
   t ƒ ‚d S rX   rY   rZ   r   r   r   r     s    zIndexMixin._get_intXslicec                 C  s
   t ƒ ‚d S rX   rY   rZ   r   r   r   r!     s    zIndexMixin._get_sliceXintc                 C  s
   t ƒ ‚d S rX   rY   rZ   r   r   r   r#   	  s    zIndexMixin._get_sliceXslicec                 C  s
   t ƒ ‚d S rX   rY   rZ   r   r   r   r$     s    zIndexMixin._get_sliceXarrayc                 C  s
   t ƒ ‚d S rX   rY   rZ   r   r   r   r%     s    zIndexMixin._get_arrayXintc                 C  s
   t ƒ ‚d S rX   rY   rZ   r   r   r   r&     s    zIndexMixin._get_arrayXslicec                 C  s
   t ƒ ‚d S rX   rY   rZ   r   r   r   r(     s    zIndexMixin._get_columnXarrayc                 C  s
   t ƒ ‚d S rX   rY   rZ   r   r   r   r+     s    zIndexMixin._get_arrayXarrayc                 C  s
   t ƒ ‚d S rX   rY   ©r   r-   r.   r
   r   r   r   r4     s    zIndexMixin._set_intXintc                 C  s
   t ƒ ‚d S rX   rY   r[   r   r   r   r8     s    zIndexMixin._set_arrayXarrayc                 C  s4   t j| ¡ | jd�}t||ƒ\}}|  |||¡ d S )Nr   )r   r2   Ztoarrayr   r   r8   )r   r-   r.   r
   Ú_r   r   r   r7   !  s    z"IndexMixin._set_arrayXarray_sparseN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r/   r;   r   rK   rV   rW   r   r   r   r!   r#   r$   r%   r&   r(   r+   r4   r8   r7   r   r   r   r   r      s*   240r   zctuple[int | slice | npt.NDArray[np.bool_ | np.int_], int | slice | npt.NDArray[np.bool_ | np.int_]])rB   c                 C  sÆ   t | ƒ} t| tƒrRt| ƒdkr(| \}}qšt| ƒdkrH| d tdƒ }}qštdƒ‚nHt| ƒ}|du rr| tdƒ }}n(|jdk rˆ|tdƒfS |jdkrš| ¡ S ddl	m
} ||ƒs¶||ƒr¾tdƒ‚||fS )z“ Parse index. Always return a tuple of the form (row, col).
    Valid type for row/col is integer, slice, array of bool, or array of integers.
    r   r   r   Nzinvalid number of indicesr0   zoIndexing with sparse matrices is not supported except boolean indexing where matrix and index are equal shapes.)Ú_check_ellipsisr   ÚtuplerC   r   r    rJ   r   rH   r6   r1   )Úindexr-   r.   r?   r1   r   r   r   rI   (  s(    




ÿrI   c                 C  s–   | t u rtdƒtdƒfS t| tƒs&| S dd„ t| ƒD ƒ}|s@| S t|ƒdkrTtdƒ‚|\}tddt| ƒ ƒ}| d|… tdƒf|  | |d d…  S )z6Process indices with Ellipsis. Returns modified index.Nc                 S  s   g | ]\}}|t u r|‘qS r   )ÚEllipsis)Ú.0r9   Úvr   r   r   Ú
<listcomp>W  ó    z#_check_ellipsis.<locals>.<listcomp>r   z0an index can only have a single ellipsis ('...')r   é   )rd   r   r   rb   Ú	enumeraterC   r    rQ   )rc   Zellipsis_indicesr9   Z
num_slicesr   r   r   ra   N  s    
ra   c                 C  s   t  | ¡} | jjdkr| S dS )z8Returns a compatible array if elements are boolean.
    r	   N)r   Z
asanyarrayr   rG   ©r?   r   r   r   Ú_maybe_bool_ndarrayc  s    
rl   r   c                 C  sP   |dk rdS zt t| ƒdƒ}W n ty2   Y dS 0 t|tƒrBdS t||d ƒS )zQReturns True if first element of the incompatible
    array type is boolean.
    r   NT)ÚnextÚiterrO   r   ÚboolÚ_first_element_bool)r?   Zmax_dimÚfirstr   r   r   rp   l  s    
rp   c                 C  s   t | dƒst| ƒrt| ƒS dS )ztReturns a boolean index array that can be converted to
    integer array. Returns None if no such array exists.
    r   N)Úhasattrrp   rl   rk   r   r   r   rJ   {  s    rJ   c                 C  s    | j dkrtdƒ‚t | ¡d S )Nr   zinvalid index shaper   )r   r    r   Úwhererk   r   r   r   rD   …  s    
rD   )r   )r`   Ú
__future__r   Útypingr   Únumpyr   Z_sputilsr   Znumpy.typingZnptr=   Úintegerr   r   r   rI   ra   rl   rp   rJ   rD   r   r   r   r   Ú<module>   s"   
  &	

