ó
î&]\c           @` 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 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   validateaxist   check_shape(   t
   dia_matvecc           B` sX  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
 d „ Z e j j e _ e d „ Z e j j e _ e d „ Z e j j e _ e d „ Z d „  Z e j j e _ 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` s~  t  j |  ƒ t | ƒ r[ | r. | j ƒ  } n  | j |  _ | j |  _ t | j ƒ |  _ n8t	 | ƒ rÁ t | ƒ rˆ | rˆ | j ƒ  } n | j
 ƒ  } | j |  _ | j |  _ t | j ƒ |  _ nÒt | t ƒ rt | ƒ r?t | ƒ |  _ t j d t | d t ƒƒ |  _ t d t |  j ƒ ƒ } t j d d | ƒ|  _ q“y | \ } } Wn t k
 rnt d ƒ ‚ q“X| d  k rŠt d ƒ ‚ n  t j t j | d d | d | ƒƒ |  _ t j t j | d d t d t | ƒ ƒ d | ƒƒ |  _ t | ƒ |  _ n’ y t j | ƒ } Wn$ t k
 r:t d	 |  j ƒ ‚ n Xd d
 l m }	 |	 | d | d | ƒj
 ƒ  } | j |  _ | j |  _ t | 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 rDt d |  j j d t  |  j ƒ f ƒ ‚ n  t  t j! |  j ƒ ƒ t  |  j ƒ k rz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   _shapeR   t   todiat
   isinstancet   tupleR	   t   npt   zerosR   t   floatR   t   maxt	   Exceptiont
   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/   lib/python2.7/site-packages/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   (   R0   R*   (    (    s/   lib/python2.7/site-packages/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%   (   R0   t   num_rowst   num_colst   offset_indst   rowt   mask(    (    s/   lib/python2.7/site-packages/scipy/sparse/dia.pyt
   _data_mask›   s    c         C` s    |  j  ƒ  } t j |  j | ƒ S(   N(   R>   R   t   count_nonzeroR   (   R0   R=   (    (    s/   lib/python2.7/site-packages/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(   R0   t   axist   Mt   NR6   t   k(    (    s/   lib/python2.7/site-packages/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   RC   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$   (   R0   RC   R   RH   t	   res_dtypeR9   R:   t   retR=   t   xt   rest   row_sumst   one(    (    s/   lib/python2.7/site-packages/scipy/sparse/dia.pyRI   º   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(   R0   t   otherRO   t   yt   LRD   RE   (    (    s/   lib/python2.7/site-packages/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   hstackRL   RX   t   reshape(   R0   RU   t   col(    (    s/   lib/python2.7/site-packages/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.   RA   R   R   t   appendR   R5   R"   R    (   R0   t   valuesRF   RD   RE   t   values_nt   nt	   min_indext	   max_indext   mR   (    (    s/   lib/python2.7/site-packages/scipy/sparse/dia.pyt   _setdiagö   s*    	
%'/)c         C` s   | r |  j  ƒ  S|  Sd  S(   N(   R   (   R0   R   (    (    s/   lib/python2.7/site-packages/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   R8   R.   t   intcR   RY   R    R   R   (   R0   t   axesR   R9   R:   t   max_dimR   t   rt   ct
   pad_amountR   (    (    s/   lib/python2.7/site-packages/scipy/sparse/dia.pyt	   transpose  s    
.0(c         C` s¼   |  j  \ } } | | k s( | | k r7 t d ƒ ‚ n  t j |  j | k ƒ \ } t d | ƒ t | | | ƒ } } | j d k r¡ t j | | d |  j	 j
 ƒS|  j	 | d | | … f S(   Ns   k exceeds matrix dimensionsi    R   (   R   R$   R   t   nonzeroR   R"   RA   t   sizeR    R   R   (   R0   RF   t   rowst   colst   idxt	   first_colt   last_col(    (    s/   lib/python2.7/site-packages/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   RC   R   R   (   t   cscRu   R6   R   R   R   R   R8   R   R%   R   R"   R    t   cumsumRI   RL   R,   t   False(   R0   R   Ru   R9   R:   t   num_offsetst
   offset_lenR;   R<   R=   R3   t   indptrt   indicesR   (    (    s/   lib/python2.7/site-packages/scipy/sparse/dia.pyt   tocscB  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   R8   R   R%   t   tileRT   R+   R   R   t   Truet   has_canonical_format(   R0   R   R9   R:   Ry   Rz   R;   R<   R=   R[   R   R   R2   (    (    s/   lib/python2.7/site-packages/scipy/sparse/dia.pyt   tocoo\  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   (   R0   R   R   (    (    s/   lib/python2.7/site-packages/scipy/sparse/dia.pyt
   _with_datar  s    "c         G` sÔ   t  | ƒ } | \ } } |  j d  d  … d  | … f |  _ | |  j d k rÇ t j |  j |  j d |  j j d k  ƒ rÇ |  j d  d  … d  f |  j d t j |  j j d ƒ k } d |  j | <n  | |  _ d  S(   Ni    i   (	   R   R   R   R   t   anyR   R%   R8   R   (   R0   R   RD   RE   R=   (    (    s/   lib/python2.7/site-packages/scipy/sparse/dia.pyt   resize{  s    "-!N(   t   __name__t
   __module__t   __doc__R*   R%   Rx   R   R7   R>   R?   RG   R   RI   RX   R\   Re   R   Rl   Rt   R}   R   R   R‚   R„   (    (    (    s/   lib/python2.7/site-packages/scipy/sparse/dia.pyR      s6   9F			)		 
		c         C` s   t  |  t ƒ S(   sÍ  Is x of dia_matrix type?

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

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

    Examples
    --------
    >>> from scipy.sparse import dia_matrix, isspmatrix_dia
    >>> isspmatrix_dia(dia_matrix([[5]]))
    True

    >>> from scipy.sparse import dia_matrix, csr_matrix, isspmatrix_dia
    >>> isspmatrix_dia(csr_matrix([[5]]))
    False
    (   R   R   (   RO   (    (    s/   lib/python2.7/site-packages/scipy/sparse/dia.pyR     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   R   t   _sparsetoolsR   R   R   (    (    (    s/   lib/python2.7/site-packages/scipy/sparse/dia.pyt   <module>   s   4ÿ |