ó
Ê½÷Xc           @` s  d  Z  d d l m Z m Z m 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 d d l m Z d d l m Z m Z m Z m Z m  Z  m! Z! d e e f d „  ƒ  YZ" d „  Z# d S(   s)   Compressed Block Sparse Row matrix formati    (   t   divisiont   print_functiont   absolute_imports   restructuredtext ent
   bsr_matrixt   isspmatrix_bsr(   t   warnNi   (   t   _data_matrixt   _minmax_mixin(   t
   _cs_matrix(   t
   isspmatrixt   _formatst   spmatrix(   t   isshapet   getdtypet	   to_nativet   upcastt   get_index_dtype(   t   _sparsetools(   t
   bsr_matvect   bsr_matvecst   csr_matmat_pass1t   bsr_matmat_pass2t   bsr_transposet   bsr_sort_indicesc           B` s‹  e  Z d  Z d Z d d e d d „ Z e d „ Z d „  Z	 e
 d e	 ƒ Z d d „ Z e j j e _ d „  Z d „  Z d	 „  Z d
 „  Z e j d d ƒ d „  ƒ Z e j d d ƒ d „  ƒ Z d „  Z d „  Z d „  Z d e d „ Z e d „ Z e j j e _ e d „ Z e j j e _ e d „ Z d e d „ Z e j j e _ d „  Z d „  Z d „  Z  d „  Z! d d d „ Z" e d „ Z# RS(   sÆ  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 nonzero elements
    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]])

    t   bsrc         C` s<  t  j |  ƒ t | ƒ r\ t | ƒ r: | r: | j ƒ  } n | j d | ƒ } |  j | ƒ nât | t ƒ rÓt	 | ƒ r| |  _
 |  j
 \ } } | d  k r¤ d } n+ t	 | ƒ sÃ t d | ƒ ‚ n  t | ƒ } t j d | t | d t ƒƒ |  _ | \ } }	 | | d k s | |	 d k r/t d ƒ ‚ n  t d t | | | |	 | |	 ƒ ƒ }
 t j d d |
 ƒ|  _ t j | | d d |
 ƒ|  _ q>t | ƒ d	 k rÚd d
 l m } |  j | | d | ƒj d | ƒ ƒ q>t | ƒ d k rÄ| \ } } } d } | d  k	 rt | ƒ } n  | d  k	 r@t | t | ƒ ƒ } n  t | | f d | d t ƒ}
 t j | d | d |
 ƒ|  _ t j | d | d |
 ƒ|  _ t j | d | d t | | ƒ ƒ|  _ q>t d ƒ ‚ nk y t j | ƒ } Wn t d |  j ƒ ‚ n Xd d
 l m } | | d | ƒj d | ƒ } |  j | ƒ | d  k	 rV| |  _
 nx |  j
 d  k rÎy* t |  j ƒ d } |  j j ƒ  d } Wn t d ƒ ‚ qÎX|  j \ } }	 | | | |	 f |  _
 n  |  j
 d  k r| d  k røt d ƒ ‚ q| |  _
 n  | d  k	 r(|  j j | ƒ |  _ n  |  j d t  ƒ d  S(   Nt	   blocksizei   s   invalid blocksize=%si    t   defaults#   shape must be multiple of blocksizet   maxvalt   dtypei   (   t
   coo_matrixi   t   check_contentst   copys)   unrecognized bsr_matrix constructor usages+   unrecognized form for %s_matrix constructors!   unable to infer matrix dimensionss   need to infer shapet
   full_check(   i   i   (   i    (!   R   t   __init__R	   R   R   t   tobsrt	   _set_selft
   isinstancet   tupleR   t   shapet   Nonet
   ValueErrort   npt   zerosR   t   floatt   dataR   t   maxt   indicest   indptrt   lent   cooR   t   Truet   arrayt   asarrayt   formatR   t   astypet   check_formatt   False(   t   selft   arg1R&   R   R   R   t   Mt   Nt   Rt   Ct	   idx_dtypeR   R,   R.   R/   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyR!   x   s|    		% &#(*c         C` sª  |  j  \ } } |  j \ } } |  j j j d k rM t d |  j j j ƒ n  |  j j j d k r| t d |  j j j ƒ n  t |  j |  j f ƒ } t	 j
 |  j d | ƒ|  _ t	 j
 |  j d | ƒ|  _ t |  j ƒ |  _ |  j j d k s |  j j d k rt d ƒ ‚ n  |  j j d k r0t d ƒ ‚ n  t |  j ƒ | | d k rwt d	 t |  j ƒ | | d f ƒ ‚ n  |  j d
 d
 k r™t d ƒ ‚ n  t |  j ƒ t |  j ƒ k rÆt d ƒ ‚ n  |  j d t |  j ƒ k rñt d ƒ ‚ n  |  j ƒ  | r¦|  j d
 k r¦|  j j ƒ  | | k rOt d | | |  j j ƒ  f ƒ ‚ n  |  j j ƒ  d
 k  rst d ƒ ‚ n  t	 j |  j ƒ j ƒ  d
 k  r£t d ƒ ‚ q£q¦n  d S(   sè   check whether the matrix format is valid

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

        t   is'   indptr array has non-integer dtype (%s)s(   indices array has non-integer dtype (%s)R   i   s!   indices, and indptr should be 1-Di   s   data should be 3-Ds&   index pointer size (%d) should be (%d)i    s!   index pointer should start with 0s*   indices and data should have the same sizeiÿÿÿÿsQ   Last value of index pointer should be less than the size of index and data arrayss-   column index values must be < %d (now max %d)s    column index values must be >= 0s8   index pointer values must form a non-decreasing sequenceN(   R&   R   R/   R   t   kindR   t   nameR.   R   R)   R4   R   R,   t   ndimR(   R0   t   prunet   nnzR-   t   mint   diff(   R9   R    R;   R<   R=   R>   R?   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyR7   Ö   sD    	$$
&c         C` s   |  j  j d S(   Ni   (   R,   R&   (   R9   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   _get_blocksize  s    t   fgetc         C` sC   | d  k	 r t d ƒ ‚ n  |  j \ } } t |  j d | | ƒ S(   Ns5   getnnz over an axis is not implemented for BSR formatiÿÿÿÿ(   R'   t   NotImplementedErrorR   t   intR/   (   R9   t   axisR=   R>   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   getnnz  s    c         C` s@   t  |  j ƒ  d } d |  j |  j j |  j f |  j | f S(   Ni   s\   <%dx%d sparse matrix of type '%s'
	with %d stored elements (blocksize = %dx%d) in %s format>(   R
   t	   getformatR&   R   t   typeRE   R   (   R9   R5   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   __repr__!  s     c      	   C` s…   |  j  \ } } |  j \ } } t j t | | ƒ d t |  j ƒ ƒ} t j | | | | | | |  j	 |  j
 t j |  j ƒ | ƒ | S(   s0   Returns the main diagonal of the matrix
        R   (   R&   R   R)   t   emptyRF   R   R   R   t   bsr_diagonalR/   R.   t   ravelR,   (   R9   R;   R<   R=   R>   t   y(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   diagonal(  s    'c         C` s
   t  ‚ d  S(   N(   RJ   (   R9   t   key(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   __getitem__7  s    c         C` s
   t  ‚ d  S(   N(   RJ   (   R9   RV   t   val(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   __setitem__:  s    t   messagesA   BSR matvec is deprecated in scipy 0.19.0. Use * operator instead.c         C` s   |  | S(   s   Multiply matrix by vector.(    (   R9   t   other(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   matvecA  s    sA   BSR matmat is deprecated in scipy 0.19.0. Use * operator instead.c         C` s   |  | S(   s,   Multiply this sparse matrix by other matrix.(    (   R9   R[   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   matmatG  s    c      
   C` s†   |  j  \ } } |  j \ } } t j |  j  d d t |  j | j ƒ ƒ} t | | | | | | |  j |  j |  j	 j
 ƒ  | | ƒ	 | S(   Ni    R   (   R&   R   R)   R*   R   R   R   R/   R.   R,   RS   (   R9   R[   R;   R<   R=   R>   t   result(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   _mul_vectorM  s    +
c         C` s¡   |  j  \ } } |  j \ } } | j d } t j | | f d t |  j | j ƒ ƒ} t | | | | | | | |  j |  j |  j	 j
 ƒ  | j
 ƒ  | j
 ƒ  ƒ
 | S(   Ni   R   (   R   R&   R)   R*   R   R   R   R/   R.   R,   RS   (   R9   R[   R=   R>   R;   R<   t   n_vecsR^   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   _mul_multivectorY  s    *c         C` sš  |  j  \ } } | j  \ } } |  j \ } } t | ƒ rI | j d } n d } d d l m }	 |	 | ƒ r˜ | d k r˜ | j d | | f d t ƒ } n | j d | | f ƒ } t |  j |  j	 | j | j	 f d | | | | ƒ}
 t
 j |  j j  d |
 ƒ} t | | | | |  j j |
 ƒ |  j	 j |
 ƒ | j j |
 ƒ | j	 j |
 ƒ | ƒ | d } t |  j |  j	 | j | j	 f d | ƒ}
 | j |
 ƒ } t
 j | d |
 ƒ} t
 j | | | d t |  j | j ƒ ƒ} t | | | | | | | |  j j |
 ƒ |  j	 j |
 ƒ t
 j |  j ƒ | j j |
 ƒ | j	 j |
 ƒ t
 j | j ƒ | | | ƒ | j d | | ƒ } t | | | f d | | f d | | f ƒS(	   Ni   (   t   isspmatrix_csrR   R   R   R   iÿÿÿÿR&   (   R&   R   R   t   csrRb   R"   R8   R   R/   R.   R)   RQ   R   R6   R   R   R   RS   R,   t   reshapeR   (   R9   R[   R;   t   K1t   K2R<   R=   t   nR>   Rb   R?   R/   t   bnnzR.   R,   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   _mul_sparse_matrixf  sN    !
	,c         C` sC   | d |  j g k r+ |  j ƒ  j d | ƒ S| r; |  j ƒ  S|  Sd S(   s  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.
        R   N(   R'   R   t   tocsrR"   R   (   R9   R   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyR"   £  s
    	
c         C` s   |  j  d t ƒ j d | ƒ S(   NR   (   t   tocooR8   Rj   (   R9   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyRj   ³  s    c         C` s   |  j  d t ƒ j d | ƒ S(   NR   (   Rk   R8   t   tocsc(   R9   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyRl   ¹  s    c         C` sÅ  |  j  \ } } |  j \ } } t j |  j ƒ } | j j t j t j ƒ j k r | j t j ƒ } t j	 | | k ƒ r‡ t
 d ƒ ‚ n  | } n  | t j | | ƒ j | ƒ } | j | | ƒ j d | | ƒ } | t j t j | ƒ j d d ƒ d | f ƒ 7} | j d ƒ } | |  j j | | ƒ j d | | ƒ }	 |	 t j t j | ƒ | d f ƒ 7}	 |	 j d ƒ }	 |  j j d ƒ }
 | r–|
 j ƒ  }
 n  d d l m } | |
 | |	 f f d |  j  ƒS(   s£   Convert this matrix to COOrdinate format.

        When copy=False the data array will be shared between
        this matrix and the resultant coo_matrix.
        s   Matrix too big to convertiÿÿÿÿi   (   R   R&   (   R&   R   R)   RG   R/   R   t   itemsizet   intpR6   t   anyR(   t   aranget   repeatRd   t   tileR.   R,   R   R1   R   (   R9   R   R;   R<   R=   R>   t   indptr_difft   indptr_diff_limitedt   rowt   colR,   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyRk   ¾  s(    !	 "1)%c         C` sM  | d  k	 r t d ƒ ‚ n  |  j \ } } |  j \ } } |  j | | } |  j d k r„ t | | f d | | f d |  j d | ƒSt j | | d d |  j	 j ƒ} t j | d |  j
 j ƒ}	 t j | | | f d |  j j ƒ}
 t | | | | | | |  j	 |  j
 |  j j ƒ  | |	 |
 j ƒ  ƒ
 t |
 |	 | f d | | f d | ƒS(   Nso   Sparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.i    R   R   R   i   R&   (   R'   R(   R   R&   RE   R   R   R)   RQ   R/   R.   R,   R   RS   (   R9   t   axesR   R=   R>   R;   R<   t   NBLKR/   R.   R,   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt	   transposeá  s     #$c         C` s¾   |  j  \ } } |  j \ } } |  j d k j d | | ƒ j d d ƒ } | j ƒ  d } t | ƒ d k ro d S|  j | |  j t | ƒ *t j | | | | |  j	 |  j
 | ƒ |  j ƒ  d S(   s   Remove zero elements in-place.i    iÿÿÿÿRL   i   N(   R   R&   R,   Rd   t   sumt   nonzeroR0   R   t   csr_eliminate_zerosR/   R.   RD   (   R9   R=   R>   R;   R<   t   maskt   nonzero_blocks(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   eliminate_zeros   s    +c         C` s=  |  j  r d S|  j ƒ  |  j \ } } |  j \ } } | | } d } d } xØ t | ƒ D]Ê } | }	 |  j | d } x™ |	 | k  r|  j |	 }
 |  j |	 } |	 d 7}	 x> |	 | k  rè |  j |	 |
 k rè | |  j |	 7} |	 d 7}	 q« W|
 |  j | <| |  j | <| d 7} qx W| |  j | d <qX W|  j ƒ  t	 |  _  d S(   sk   Eliminate duplicate matrix entries by adding them together

        The is an *in place* operation
        Ni    i   (
   t   has_canonical_formatt   sort_indicesR   R&   t   rangeR/   R.   R,   RD   R2   (   R9   R=   R>   R;   R<   t   n_rowRE   t   row_endR@   t   jjt   jt   x(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   sum_duplicates  s0    	


"
c         C` sk   |  j  r d S|  j \ } } |  j \ } } t | | | | | | |  j |  j |  j j ƒ  ƒ t |  _  d S(   s3   Sort the indices of this matrix *in place*
        N(	   t   has_sorted_indicesR   R&   R   R/   R.   R,   RS   R2   (   R9   R=   R>   R;   R<   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyR   4  s    	3c         C` sÃ   |  j  \ } } |  j \ } } t |  j ƒ | | d k rJ t d ƒ ‚ n  |  j d } t |  j ƒ | k  r{ t d ƒ ‚ n  t |  j ƒ | k  rŸ t d ƒ ‚ n  |  j |  |  _ |  j |  |  _ d S(   s9    Remove empty space after all non-zero elements.
        i   s    index pointer has invalid lengthiÿÿÿÿs"   indices array has too few elementss   data array has too few elementsN(   R   R&   R0   R/   R(   R.   R,   (   R9   R=   R>   R;   R<   Rh   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyRD   A  s    c         C` s2  |  j  | d |  j ƒ} t t |  j | |  j ƒ } |  j \ } } t |  j ƒ t | j ƒ } t |  j |  j	 | j | j	 f d | ƒ}	 t
 j |  j j d |	 ƒ}
 t
 j | d |	 ƒ} d d d d d g } | | k rþ t
 j | | | d t
 j ƒ} n, t
 j | | | d t |  j | j ƒ ƒ} | |  j d	 | |  j d
 | | | |  j j |	 ƒ |  j	 j |	 ƒ |  j | j j |	 ƒ | j	 j |	 ƒ t
 j | j ƒ |
 | | ƒ |
 d } | |  } | | | |  } | | d k  rþ| j ƒ  } | j ƒ  } n  | j d | | ƒ } |  j  | | |
 f d |  j ƒS(   s5   Apply the binary operation fn to two sparse matrices.R   R   R   t   _ne_t   _lt_t   _gt_t   _le_t   _ge_i    i   iÿÿÿÿi   R&   (   t	   __class__R   t   getattrR   R5   R0   R,   R   R/   R.   R)   RQ   R&   t   bool_R   R   R6   RS   R   Rd   (   R9   R[   t   opt   in_shapet	   out_shapet   fnR=   R>   t   max_bnnzR?   R/   R.   t   bool_opsR,   t   actual_bnnz(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   _binoptV  s>    	#,%

c         C` sr   | r@ |  j  | |  j j ƒ  |  j j ƒ  f d |  j d | j ƒS|  j  | |  j |  j f d |  j d | j ƒSd S(   sµ   Returns 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   R.   R   R/   R&   R   (   R9   R,   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt
   _with_data‡  s
    'N($   t   __name__t
   __module__t   __doc__R5   R'   R8   R!   R2   R7   RH   t   propertyR   RM   R   RP   RU   RW   RY   R)   t	   deprecateR\   R]   R_   Ra   Ri   R"   Rj   Rl   Rk   Ry   R   Rˆ   R   RD   R™   Rš   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyR      s>   _^>								=#		!		1c         C` s   t  |  t ƒ S(   N(   R$   R   (   R‡   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyR   ›  s    ($   R   t
   __future__R    R   R   t   __docformat__t   __all__t   warningsR   t   numpyR)   R,   R   R   t
   compressedR   t   baseR	   R
   R   t   sputilsR   R   R   R   R   t    R   R   R   R   R   R   R   R   R   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/bsr.pyt   <module>   s   (.ÿ ÿ ‡