ó
Ê½÷Xc           @` sÍ   d  Z  d d l m Z m Z m Z d Z d d g Z d d l Z d d l	 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 d l m Z d e f d „  ƒ  YZ d „  Z d S(   s   Sparse DIAgonal formati    (   t   divisiont   print_functiont   absolute_imports   restructuredtext ent
   dia_matrixt   isspmatrix_diaNi   (   t
   isspmatrixt   _formatst   spmatrix(   t   _data_matrix(   t   isshapet   upcast_chart   getdtypet   get_index_dtypet   get_sum_dtypet   validateaxis(   t
   dia_matvecc           B` s=  e  Z d  Z d Z d d e d „ Z d „  Z d „  Z d „  Z	 d d „ Z
 e j
 j e
 _ e j	 j e	 _ d d d d „ Z e j j e _ d „  Z d	 „  Z d
 d „ Z e d „ Z e j j e _ d e d „ Z e j j e _ d „  Z e j j e _ e d „ Z e j j e _ e d „ Z e j j e _ e d „ Z RS(   s   Sparse matrix with DIAgonal storage

    This can be instantiated in several ways:
        dia_matrix(D)
            with a dense matrix

        dia_matrix(S)
            with another sparse matrix S (equivalent to S.todia())

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

        dia_matrix((data, offsets), shape=(M, N))
            where the ``data[k,:]`` stores the diagonal entries for
            diagonal ``offsets[k]`` (See example below)

    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
        DIA format data array of the matrix
    offsets
        DIA format offset array of the matrix

    Notes
    -----

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

    Examples
    --------

    >>> import numpy as np
    >>> from scipy.sparse import dia_matrix
    >>> dia_matrix((3, 4), dtype=np.int8).toarray()
    array([[0, 0, 0, 0],
           [0, 0, 0, 0],
           [0, 0, 0, 0]], dtype=int8)

    >>> data = np.array([[1, 2, 3, 4]]).repeat(3, axis=0)
    >>> offsets = np.array([0, -1, 2])
    >>> dia_matrix((data, offsets), shape=(4, 4)).toarray()
    array([[1, 0, 3, 0],
           [1, 2, 0, 4],
           [0, 2, 3, 0],
           [0, 0, 3, 4]])

    t   diac   
      C` sL  t  j |  ƒ t | ƒ rU | r. | j ƒ  } n  | j |  _ | j |  _ | j |  _ nt | ƒ rµ t | ƒ r‚ | r‚ | j ƒ  } n | j ƒ  } | j |  _ | j |  _ | j |  _ n¬t	 | t
 ƒ rßt | ƒ r-| |  _ t j d t | d t ƒƒ |  _ t d t |  j ƒ ƒ } t j d d | ƒ|  _ qay | \ } } Wn t d ƒ ‚ qaX| d  k rnt d ƒ ‚ n  t j t j | d d | d | ƒƒ |  _ t j t j | d d t d t | ƒ ƒ d | ƒƒ |  _ | |  _ n‚ y t j | ƒ } Wn t d	 |  j ƒ ‚ n Xd d
 l m }	 |	 | d | d | ƒj ƒ  } | j |  _ | j |  _ | j |  _ | d  k	 r…|  j j | ƒ |  _ n  |  j j d k r¦t d ƒ ‚ n  |  j j d k rÇt d ƒ ‚ n  |  j j d t |  j ƒ k rt d |  j j d t |  j ƒ f ƒ ‚ n  t t j |  j ƒ ƒ t |  j ƒ k rHt d ƒ ‚ n  d  S(   Ni    t   defaultt   maxvalt   dtypes,   unrecognized form for dia_matrix constructors   expected a shape argumentt   copyi   s+   unrecognized form for %s_matrix constructor(   t
   coo_matrixt   shapes   offsets array must have rank 1i   s   data array must have rank 2sB   number of diagonals (%d) does not match the number of offsets (%d)s&   offset array contains duplicate values(   i    i    (   R   t   __init__R   R   t   datat   offsetsR   R   t   todiat
   isinstancet   tupleR	   t   npt   zerosR   t   floatR   t   maxt
   ValueErrort   Nonet
   atleast_2dt   arrayt
   atleast_1dt   asarrayt   formatt   cooR   t   astypet   ndimt   lent   unique(
   t   selft   arg1R   R   R   t   At	   idx_dtypeR   R   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyR   N   sh    	!+&'c         C` sB   t  |  j ƒ  d } d |  j |  j j |  j |  j j d | f S(   Ni   sW   <%dx%d sparse matrix of type '%s'
	with %d stored elements (%d diagonals) in %s format>i    (   R   t	   getformatR   R   t   typet   nnzR   (   R-   R'   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyt   __repr__”   s    "c         C` su   |  j  \ } } t j |  j j  d ƒ } | |  j d d … d f } | d k } | | | k  M} | | | k  M} | S(   s~   Returns a mask of the same shape as self.data, where
        mask[i,j] is True when data[i,j] corresponds to a stored element.i   Ni    (   R   R   t   arangeR   R   R"   (   R-   t   num_rowst   num_colst   offset_indst   rowt   mask(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyt
   _data_mask›   s    c         C` s    |  j  ƒ  } t j |  j | ƒ S(   N(   R;   R   t   count_nonzeroR   (   R-   R:   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyR<   ¦   s    c         C` s‹   | d  k	 r t d ƒ ‚ n  |  j \ } } d } xN |  j D]C } | d k rf | t | | | ƒ 7} q: | t | | | ƒ 7} q: Wt | ƒ S(   Ns5   getnnz over an axis is not implemented for DIA formati    (   R"   t   NotImplementedErrorR   R   t   mint   int(   R-   t   axist   Mt   NR3   t   k(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyt   getnnzª   s    c      
   C` sð  t  | ƒ | d  k	 r/ | d k  r/ | d 7} n  t |  j ƒ } |  j \ } } d  } | d k rá |  j ƒ  } |  j | j d d ƒ }	 |	 j d | k r  |	 }
 n) t j	 | d |	 j ƒ}
 |	 |
 |	 j d *t j
 |
 d | ƒ} nÆ t j	 | d | ƒ} t j | d | ƒ} t | | t |  j ƒ |  j j d |  j |  j | | ƒ t j
 | ƒ } | d  k rt| j d | d | ƒ S| d  k	 rŒ| j } n  t j
 | j d | ƒ ƒ } | d  k	 rÔ| j | j k rÔt d ƒ ‚ n  | j d d d | d | ƒ S(	   Ni    i   R@   R   i   t   outs   dimensions do not match(    (   R   R"   R   R   R   R;   R   t   sumR   R   t   matrixt   onesR   R+   R   t   TR!   (   R-   R@   R   RE   t	   res_dtypeR6   R7   t   retR:   t   xt   rest   row_sumst   one(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyRF   º   s6    
	#c      	   C` s”   | } t  j |  j d d t |  j j | j j ƒ ƒ} |  j j d } |  j \ } } t | | t |  j	 ƒ | |  j	 |  j | j
 ƒ  | j
 ƒ  ƒ | S(   Ni    R   i   (   R   R   R   R
   R   t   charR   R   R+   R   t   ravel(   R-   t   otherRL   t   yt   LRA   RB   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyt   _mul_vectorå   s    :c         C` s8   t  j g  | j D]! } |  j | ƒ j d d ƒ ^ q ƒ S(   Niÿÿÿÿi   (   R   t   hstackRI   RU   t   reshape(   R-   RR   t   col(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyt   _mul_multimatrixó   s    i    c         C` s˜  |  j  \ } } | j d k r* t j } n t | ƒ } | d k  rg t | | | | ƒ } d } | } n& t | | | | ƒ } | } | | } | j d k r© | |  } n  | |  j k rÝ | |  j |  j | k | | … f <n· t j |  j |  j j	 j
 | ƒ ƒ |  _ t | |  j j  d ƒ }	 t j |  j j  d d |	 f d |  j j	 ƒ}
 |  j |
 d  d … d  |  j j  d … f <| |
 d | | … f <|
 |  _ d  S(   Ni    i   R   iÿÿÿÿ(   R   R*   R   t   infR+   R>   R   R   t   appendR   R2   R    R   (   R-   t   valuesRC   RA   RB   t   values_nt   nt	   min_indext	   max_indext   mR   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyt   _setdiagö   s*    	
%'/)c         C` s   | r |  j  ƒ  S|  Sd  S(   N(   R   (   R-   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyR     s    
c         C` s-  | d  k	 r t d ƒ ‚ n  |  j \ } } t |  j ƒ } |  j } t j t | ƒ d t j ƒd  d  … d  f } t j | d t j ƒ| | d  d  … d  f } t d | |  j	 j d ƒ }	 t j
 |  j	 t j |  j	 j d |	 f d |  j	 j ƒf ƒ }
 |
 | | f }
 t |
 | f d | | f d | ƒS(   Nso   Sparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.R   i    i   R   R   (   R"   R!   R   R    R   R   R5   R+   t   intcR   RV   R   R   R   (   R-   t   axesR   R6   R7   t   max_dimR   t   rt   ct
   pad_amountR   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyt	   transpose  s    
.0(c         C` sm   t  j |  j d k ƒ \ } t |  j ƒ } | j d k rR t  j | d |  j j ƒS|  j | d d  | … f S(   Ni    R   (	   R   t   whereR   R>   R   t   sizeR   R   R   (   R-   t   idxR^   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyt   diagonal6  s
    c         C` s  d d l  m } |  j d k r5 | |  j d |  j ƒS|  j \ } } |  j j \ } } t j | ƒ } | |  j d  d  … d  f } | d k }	 |	 | | k  M}	 |	 | | k  M}	 |	 |  j d k M}	 t
 d t |  j ƒ ƒ }
 t j | d d |
 ƒ} t j |	 j d d ƒ ƒ | d | d +| | | | d )| j |	 j j |
 d t ƒ} |  j j |	 j } | | | | f d |  j d |  j ƒS(	   Ni   (   t
   csc_matrixi    R   R   R@   R   R   (   t   cscRn   R3   R   R   R   R   R5   R   R"   R   R    R   t   cumsumRF   RI   R)   t   False(   R-   R   Rn   R6   R7   t   num_offsetst
   offset_lenR8   R9   R:   R0   t   indptrt   indicesR   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyt   tocsc?  s&    &c         C` s  |  j  \ } } |  j j  \ } } t j | ƒ } | |  j d  d  … d  f } | d k } | | | k  M} | | | k  M} | |  j d k M} | | } t j | | ƒ | j ƒ  }	 |  j | }
 d d l m	 } | |
 | |	 f f d |  j  d |  j
 ƒ} t | _ | S(   Ni    i   (   R   R   R   (   R   R   R   R5   R   R"   t   tileRQ   R(   R   R   t   Truet   has_canonical_format(   R-   R   R6   R7   Rr   Rs   R8   R9   R:   RX   R   R   R/   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyt   tocooY  s    
*	c         C` sH   | r( t  | |  j j ƒ  f d |  j ƒSt  | |  j f d |  j ƒSd S(   s‘   Returns a matrix with the same sparsity structure as self,
        but with different data.  By default the structure arrays are copied.
        R   N(   R   R   R   R   (   R-   R   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyt
   _with_datao  s    "N(   t   __name__t
   __module__t   __doc__R'   R"   Rq   R   R4   R;   R<   RD   R   RF   RU   RY   Rb   R   Ri   Rm   Rv   Rz   Rx   R{   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyR      s2   9F			)		 	c         C` s   t  |  t ƒ S(   N(   R   R   (   RL   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyR   y  s    (   R~   t
   __future__R    R   R   t   __docformat__t   __all__t   numpyR   t   baseR   R   R   R   R   t   sputilsR	   R
   R   R   R   R   t   _sparsetoolsR   R   R   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dia.pyt   <module>   s   .ÿ h