a
    a
d                     @   s   d Z dZddgZddlmZ ddlZddlmZm	Z	 dd	l
mZ dd
lmZmZmZ ddlmZmZmZmZmZmZmZ ddlmZ ddlmZmZmZmZmZmZm Z  G dd dee	Z!dd Z"dS )z)Compressed Block Sparse Row matrix formatzrestructuredtext en
bsr_matrixisspmatrix_bsr    )warnN   )_data_matrix_minmax_mixin)
_cs_matrix)
isspmatrix_formatsspmatrix)isshapegetdtypegetdata	to_nativeupcastget_index_dtypecheck_shape)_sparsetools)
bsr_matvecbsr_matvecscsr_matmat_maxnnz
bsr_matmatbsr_transposebsr_sort_indices	bsr_tocsrc                   @   sZ  e Zd ZdZdZd?ddZd@dd	Zd
d ZeedZ	dAddZ
ej
je
_dd ZdBddZejje_dd Zdd Zejdddd Zejdddd Zdd  Zd!d" Zd#d$ Zd%d& ZdCd'd(ZdDd)d*Zejje_dEd+d,Zejje_dFd-d.ZdGd/d0Zejje_dHd1d2Zejje_d3d4 Zd5d6 Zd7d8 Z d9d: Z!dId;d<Z"dJd=d>Z#dS )Kr   a  Block Sparse Row matrix

    This can be instantiated in several ways:
        bsr_matrix(D, [blocksize=(R,C)])
            where D is a dense matrix or 2-D ndarray.

        bsr_matrix(S, [blocksize=(R,C)])
            with another sparse matrix S (equivalent to S.tobsr())

        bsr_matrix((M, N), [blocksize=(R,C), dtype])
            to construct an empty matrix with shape (M, N)
            dtype is optional, defaulting to dtype='d'.

        bsr_matrix((data, ij), [blocksize=(R,C), shape=(M, N)])
            where ``data`` and ``ij`` satisfy ``a[ij[0, k], ij[1, k]] = data[k]``

        bsr_matrix((data, indices, indptr), [shape=(M, N)])
            is the standard BSR representation where the block column
            indices for row i are stored in ``indices[indptr[i]:indptr[i+1]]``
            and their corresponding block values are stored in
            ``data[ indptr[i]: indptr[i+1] ]``. If the shape parameter is not
            supplied, the matrix dimensions are inferred from the index arrays.

    Attributes
    ----------
    dtype : dtype
        Data type of the matrix
    shape : 2-tuple
        Shape of the matrix
    ndim : int
        Number of dimensions (this is always 2)
    nnz
        Number of stored values, including explicit zeros
    data
        Data array of the matrix
    indices
        BSR format index array
    indptr
        BSR format index pointer array
    blocksize
        Block size of the matrix
    has_sorted_indices
        Whether indices are sorted

    Notes
    -----
    Sparse matrices can be used in arithmetic operations: they support
    addition, subtraction, multiplication, division, and matrix power.

    **Summary of BSR format**

    The Block Compressed Row (BSR) format is very similar to the Compressed
    Sparse Row (CSR) format. BSR is appropriate for sparse matrices with dense
    sub matrices like the last example below.  Block matrices often arise in
    vector-valued finite element discretizations. In such cases, BSR is
    considerably more efficient than CSR and CSC for many sparse arithmetic
    operations.

    **Blocksize**

    The blocksize (R,C) must evenly divide the shape of the matrix (M,N).
    That is, R and C must satisfy the relationship ``M % R = 0`` and
    ``N % C = 0``.

    If no blocksize is specified, a simple heuristic is applied to determine
    an appropriate blocksize.

    Examples
    --------
    >>> from scipy.sparse import bsr_matrix
    >>> bsr_matrix((3, 4), dtype=np.int8).toarray()
    array([[0, 0, 0, 0],
           [0, 0, 0, 0],
           [0, 0, 0, 0]], dtype=int8)

    >>> row = np.array([0, 0, 1, 2, 2, 2])
    >>> col = np.array([0, 2, 2, 0, 1, 2])
    >>> data = np.array([1, 2, 3 ,4, 5, 6])
    >>> bsr_matrix((data, (row, col)), shape=(3, 3)).toarray()
    array([[1, 0, 2],
           [0, 0, 3],
           [4, 5, 6]])

    >>> indptr = np.array([0, 2, 3, 6])
    >>> indices = np.array([0, 2, 2, 0, 1, 2])
    >>> data = np.array([1, 2, 3, 4, 5, 6]).repeat(4).reshape(6, 2, 2)
    >>> bsr_matrix((data,indices,indptr), shape=(6, 6)).toarray()
    array([[1, 1, 0, 0, 2, 2],
           [1, 1, 0, 0, 2, 2],
           [0, 0, 0, 0, 3, 3],
           [0, 0, 0, 0, 3, 3],
           [4, 4, 5, 5, 6, 6],
           [4, 4, 5, 5, 6, 6]])

    ZbsrNFc              
   C   s  t |  t|rBt|r(|r(| }n|j|d}| | nt|trjt	|r t
|| _| j\}}|d u rzd}nt	|std| t|}td| t|td| _|\}}	|| dks||	 dkrtdtt|| ||	 ||	d}
tjd|
d	| _tj|| d
 |
d	| _qt|dkrZd
dlm} | ||||dj|d qt|dkr`|\}}}d
}|d urt|}|d urt|t|}t||f|dd}
tj|||
d| _tj|||
d| _t|||d| _| jjdkrtd| jjf |d urht	|s(td|f t|| jjd
d  krhtd|| jjd
d  f ntdnpzt|}W n6 ty } ztd| j |W Y d }~n
d }~0 0 d
dlm} |||d	j|d}| | |d urt
|| _n~| jd u rnz t| jd
 }| j d
 }W n0 tyL } ztd|W Y d }~n*d }~0 0 | j \}}	t
|| ||	 f| _| jd u r|d u rtdn
t
|| _|d ur| jj!|dd| _| j"dd d S )N	blocksize)r   r   zinvalid blocksize=%s)r   )defaultr   z#shape must be multiple of blocksizemaxvaldtyper      
coo_matrix)r!   shape   T)r   Zcheck_contents)copyr!   z,BSR data must be 3-dimensional, got shape=%szmismatching blocksize=%s vs %sz)unrecognized bsr_matrix constructor usagez+unrecognized form for %s_matrix constructorz!unable to infer matrix dimensionszneed to infer shapeFr'   )
full_check)#r   __init__r	   r   r'   tobsrZ	_set_self
isinstancetupler   r   Z_shaper%   
ValueErrornpzerosr   floatdatar   maxindicesindptrlencoor$   arrayr   ndimasarray	Exceptionformatr   astypecheck_format)selfZarg1r%   r!   r'   r   MNRC	idx_dtyper$   r2   r4   r5   r   e rF   `/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/scipy/sparse/bsr.pyr*   x   s    














 




zbsr_matrix.__init__Tc                 C   s  | j \}}| j\}}| jjjdkr4td| jjj  | jjjdkrTtd| jjj  t| j| jf}t	j
| j|d| _t	j
| j|d| _t| j| _| jjdks| jjdkrtd| jjdkrtdt| j|| d krtd	t| j|| d f | jd
 d
krtdt| jt| jkr2td| jd t| jkrPtd|   |r| jd
kr| j || krtd|| | j f | j d
k rtdt	| j d
k rtddS )zcheck whether the matrix format is valid

            *Parameters*:
                full_check:
                    True  - rigorous check, O(N) operations : default
                    False - basic check, O(1) operations

        iz'indptr array has non-integer dtype (%s)z(indices array has non-integer dtype (%s)r    r   z!indices, and indptr should be 1-Dr&   zdata should be 3-Dz&index pointer size (%d) should be (%d)r   z!index pointer should start with 0z*indices and data should have the same sizezQLast value of index pointer should be less than the size of index and data arraysz-column index values must be < %d (now max %d)z column index values must be >= 0z8index pointer values must form a non-decreasing sequenceN)r%   r   r5   r!   kindr   namer4   r   r/   r:   r   r2   r9   r.   r6   prunennzr3   mindiff)r?   r)   r@   rA   rB   rC   rD   rF   rF   rG   r>      sJ    	

zbsr_matrix.check_formatc                 C   s   | j jdd  S )Nr   )r2   r%   )r?   rF   rF   rG   _get_blocksize"  s    zbsr_matrix._get_blocksize)fgetc                 C   s0   |d urt d| j\}}t| jd | | S )Nz5getnnz over an axis is not implemented for BSR formatrI   )NotImplementedErrorr   intr5   )r?   axisrB   rC   rF   rF   rG   getnnz&  s    
zbsr_matrix.getnnzc                 C   s4   t |   d }d| j| jj| jf | j |f  S )Nr   z\<%dx%d sparse matrix of type '%s'
	with %d stored elements (blocksize = %dx%d) in %s format>)r
   Z	getformatr%   r!   typerM   r   )r?   r<   rF   rF   rG   __repr__/  s    zbsr_matrix.__repr__r   c                 C   s   | j \}}|| ks||kr.tjd| jjdS | j\}}tjt|t|d |t|d t	| jd}t
||| || ||| j| jt| j|	 |S Nr   r    )r%   r/   emptyr2   r!   r   r0   rN   r3   r   r   Zbsr_diagonalr5   r4   ravel)r?   krowscolsrB   rC   yrF   rF   rG   diagonal6  s    

 zbsr_matrix.diagonalc                 C   s   t d S NrR   )r?   keyrF   rF   rG   __getitem__H  s    zbsr_matrix.__getitem__c                 C   s   t d S r`   ra   )r?   rb   valrF   rF   rG   __setitem__K  s    zbsr_matrix.__setitem__zABSR matvec is deprecated in SciPy 0.19.0. Use * operator instead.)messagec                 C   s   | | S )zMultiply matrix by vector.rF   r?   otherrF   rF   rG   matvecR  s    zbsr_matrix.matveczABSR matmat is deprecated in SciPy 0.19.0. Use * operator instead.c                 C   s   | | S )z,Multiply this sparse matrix by other matrix.rF   rg   rF   rF   rG   matmatX  s    zbsr_matrix.matmatc                 C   s   | j dd|S NFr(   )tocoo
_add_denserg   rF   rF   rG   rm   ^  s    zbsr_matrix._add_densec              
   C   s`   | j \}}| j\}}tj| j d t| j|jd}t|| || ||| j| j| j	
 ||	 |S rX   )r%   r   r/   r0   r   r!   r   r5   r4   r2   rZ   )r?   rh   r@   rA   rB   rC   resultrF   rF   rG   _mul_vectora  s    

zbsr_matrix._mul_vectorc                 C   sr   | j \}}| j\}}|jd }tj||ft| j|jd}t|| || |||| j| j| j	
 |
 |
 
 |S )Nr   r    )r   r%   r/   r0   r   r!   r   r5   r4   r2   rZ   )r?   rh   rB   rC   r@   rA   Zn_vecsrn   rF   rF   rG   _mul_multivectorm  s    


zbsr_matrix._mul_multivectorc                 C   s  | j \}}|j \}}| j\}}t|r2|jd }nd}ddlm}	 |	|rf|dkrf|j||fdd}n|j||fd}t| j| j|j|jf}
t	|| || | j
|
| j
|
|j
|
|j
|
}t| j| j|j|jf|d}
tj| jj |
d}tj||
d}tj|| | t| j|jd}t||| || |||| j
|
| j
|
t| j|j
|
|j
|
t|j||| |d||}t|||f||f||fd	S )
Nr   )isspmatrix_csrF)r   r'   r   r   r    rI   )r%   r   )r%   r   r   csrrq   r+   r   r5   r4   r   r=   r/   rY   r   r!   r   rZ   r2   reshaper   )r?   rh   r@   ZK1ZK2rA   rB   nrC   rq   rD   bnnzr5   r4   r2   rF   rF   rG   _mul_sparse_matrixz  sP    








 





zbsr_matrix._mul_sparse_matrixc                 C   s2   |d| j fvr|  j|dS |r*|  S | S dS )a  Convert this matrix into Block Sparse Row Format.

        With copy=False, the data/indices may be shared between this
        matrix and the resultant bsr_matrix.

        If blocksize=(R, C) is provided, it will be used for determining
        block size of the bsr_matrix.
        Nr   )r   tocsrr+   r'   )r?   r   r'   rF   rF   rG   r+     s
    	zbsr_matrix.tobsrc                 C   s   | j \}}| j\}}| j}t| j| jft||d}tj|d |d}tj||d}	tj|t	| j
d}
t|| || ||| jj|dd| jj|dd| j||	|

 ddlm} ||
|	|f| j dS )Nr   r   r    Fr(   )
csr_matrixr%   )r%   r   rM   r   r5   r4   r3   r/   rY   r   r!   r   r=   r2   rr   rx   )r?   r'   r@   rA   rB   rC   rM   rD   r5   r4   r2   rx   rF   rF   rG   rw     s*    

	zbsr_matrix.tocsrc                 C   s   | j ddj|dS rk   )rw   tocsc)r?   r'   rF   rF   rG   rz     s    zbsr_matrix.tocscc                 C   s6  | j \}}| j\}}t| j}|jjttjjkr\|tj}t	||krXt
d|}|t||  |}||| d||}|tt|ddd|f7 }|d}|| j || d||}	|	tt||df7 }	|	d}	| jd}
|r|
 }
ddlm} ||
||	ff| j dS )zConvert this matrix to COOrdinate format.

        When copy=False the data array will be shared between
        this matrix and the resultant coo_matrix.
        zMatrix too big to convertrI   r   r#   ry   )r%   r   r/   rO   r5   r!   itemsizeZintpr=   anyr.   Zarangerepeatrs   Ztiler4   r2   r'   r7   r$   )r?   r'   r@   rA   rB   rC   Zindptr_diffZindptr_diff_limitedrowcolr2   r$   rF   rF   rG   rl     s(    

"

zbsr_matrix.tocooc                 C   s   | j ddj||dS )NFr(   )orderout)rl   toarray)r?   r   r   rF   rF   rG   r     s    zbsr_matrix.toarrayc                 C   s   |d urt d| j\}}| j\}}| j||  }| jdkrVt||f||f| j|dS tj|| d | jjd}tj|| j	jd}	tj|||f| j
jd}
t|| || ||| j| j	| j
 ||	|
 
 t|
|	|f||f|dS )NzoSparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.r   )r   r!   r'   r   r    )r%   r'   )r.   r   r%   rM   r   r!   r/   rY   r5   r4   r2   r   rZ   )r?   Zaxesr'   rB   rC   r@   rA   ZNBLKr5   r4   r2   rF   rF   rG   	transpose  s&    




zbsr_matrix.transposec                 C   s   | j s
dS | j\}}| j\}}| jdkd|| jdd}| d }| j| | jdt|< t	|| || | j
| j| |   dS )zRemove zero elements in-place.Nr   rI   r   )rT   )rM   r   r%   r2   rs   sumZnonzeror6   r   Zcsr_eliminate_zerosr5   r4   rL   )r?   rB   rC   r@   rA   maskZnonzero_blocksrF   rF   rG   eliminate_zeros&  s    

zbsr_matrix.eliminate_zerosc                 C   s   | j r
dS |   | j\}}| j\}}|| }d}d}t|D ]}|}	| j|d  }|	|k r| j|	 }
| j|	 }|	d7 }	|	|k r| j|	 |
kr|| j|	 7 }|	d7 }	qx|
| j|< || j|< |d7 }qT|| j|d < q>|   d| _ dS )zkEliminate duplicate matrix entries by adding them together

        The is an *in place* operation
        Nr   r   T)	Zhas_canonical_formatsort_indicesr   r%   ranger5   r4   r2   rL   )r?   rB   rC   r@   rA   Zn_rowrM   Zrow_endrH   ZjjjxrF   rF   rG   sum_duplicates:  s0    







zbsr_matrix.sum_duplicatesc              	   C   sN   | j r
dS | j\}}| j\}}t|| || ||| j| j| j  d| _ dS )z3Sort the indices of this matrix *in place*
        NT)Zhas_sorted_indicesr   r%   r   r5   r4   r2   rZ   )r?   rB   rC   r@   rA   rF   rF   rG   r   [  s    

&zbsr_matrix.sort_indicesc                 C   s   | j \}}| j\}}t| j|| d kr2td| jd }t| j|k rRtdt| j|k rhtd| jd| | _| jd| | _dS )z9 Remove empty space after all non-zero elements.
        r   z index pointer has invalid lengthrI   z"indices array has too few elementszdata array has too few elementsN)r   r%   r6   r5   r.   r4   r2   )r?   rB   rC   r@   rA   ru   rF   rF   rG   rL   h  s    


zbsr_matrix.prunec                 C   s  | j || jd}tt| j| | j }| j\}}t| jt|j }t| j| j	|j|j	f|d}	t
j| jj|	d}
t
j||	d}g d}||v rt
j|| | t
jd}n t
j|| | t| j|jd}|| jd | | jd | ||| j|	| j	|	| j|j|	|j	|	t
|j|
|| |
d }|d| }|d|| |  }||d	 k rn| }| }|d||}| j |||
f| jd
S )z5Apply the binary operation fn to two sparse matrices.r   r   r    )Z_ne_Z_lt_Z_gt_Z_le_Z_ge_r   r   rI   Nr"   ry   )	__class__r   getattrr   r<   r6   r2   r   r5   r4   r/   rY   r%   Zbool_r   r!   r=   rZ   r'   rs   )r?   rh   opZin_shapeZ	out_shapefnrB   rC   Zmax_bnnzrD   r5   r4   Zbool_opsr2   Zactual_bnnzrF   rF   rG   _binopt}  sD    

 




zbsr_matrix._binoptc                 C   sL   |r*| j || j | j f| j|jdS | j || j| jf| j|jdS dS )zReturns a matrix with the same sparsity structure as self,
        but with different data.  By default the structure arrays
        (i.e. .indptr and .indices) are copied.
        )r%   r!   N)r   r4   r'   r5   r%   r!   )r?   r2   r'   rF   rF   rG   
_with_data  s    zbsr_matrix._with_data)NNFN)T)N)r   )NF)F)F)T)NN)NF)NN)T)$__name__
__module____qualname____doc__r<   r*   r>   rP   propertyr   rU   r   rW   r_   rc   re   r/   Z	deprecateri   rj   rm   ro   rp   rv   r+   rw   rz   rl   r   r   r   r   r   rL   r   r   rF   rF   rF   rG   r      sJ   _
l
>








8





#



!
1c                 C   s
   t | tS )a  Is x of a bsr_matrix type?

    Parameters
    ----------
    x
        object to check for being a bsr matrix

    Returns
    -------
    bool
        True if x is a bsr matrix, False otherwise

    Examples
    --------
    >>> from scipy.sparse import bsr_matrix, isspmatrix_bsr
    >>> isspmatrix_bsr(bsr_matrix([[5]]))
    True

    >>> from scipy.sparse import bsr_matrix, csr_matrix, isspmatrix_bsr
    >>> isspmatrix_bsr(csr_matrix([[5]]))
    False
    )r,   r   )r   rF   rF   rG   r     s    )#r   Z__docformat____all__warningsr   Znumpyr/   r2   r   r   
compressedr   baser	   r
   r   Zsputilsr   r   r   r   r   r   r    r   r   r   r   r   r   r   r   r   r   rF   rF   rF   rG   <module>   s"   $$     1