ó
Ê½÷Xc           @` sB  d  Z  d d l m Z m Z m Z d Z d d g Z d d l Z d d l Z d d l	 Z
 d d l m Z m Z m Z m Z m Z d d	 l m Z m Z d d
 l m Z m Z m Z m Z m Z m Z m Z m Z m Z y d d l m Z  Wn e! k
 rd „  Z  n Xd e e e" f d „  ƒ  YZ# d „  Z$ d „  Z% d „  Z& d S(   s   Dictionary Of Keys based matrixi    (   t   divisiont   print_functiont   absolute_imports   restructuredtext ent
   dok_matrixt   isspmatrix_dokN(   t   zipt   xranget	   iteritemst   iterkeyst
   itervaluesi   (   t   spmatrixt
   isspmatrix(	   t   isdenset   getdtypet   isshapet	   isintliket   isscalarliket   upcastt   upcast_scalart
   IndexMixint   get_index_dtype(   t   isSequenceTypec         C` s+   t  |  d ƒ p* t  |  d ƒ p* t  |  d ƒ S(   Nt   __len__t   __next__t   next(   t   hasattr(   t   x(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   _is_sequence   s    c           B` s…  e  Z d  Z d Z d d e d „ Z d d „ Z d „  Z e	 j j e _ e	 j j e _ d „  Z
 d d „ Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d e d „ Z e	 j j e _ d „  Z d „  Z e	 j j e _ d „  Z d „  Z e d „ Z e	 j j e _ e d „ Z e	 j j e _ e d „ Z e	 j j e _ d „  Z  RS(   sð  
    Dictionary Of Keys based sparse matrix.

    This is an efficient structure for constructing sparse
    matrices incrementally.

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

        dok_matrix(S)
            with a sparse matrix, S

        dok_matrix((M,N), [dtype])
            create the matrix with initial shape (M,N)
            dtype is optional, defaulting to dtype='d'

    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

    Notes
    -----

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

    Allows for efficient O(1) access of individual elements.
    Duplicates are not allowed.
    Can be efficiently converted to a coo_matrix once constructed.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import dok_matrix
    >>> S = dok_matrix((5, 5), dtype=np.float32)
    >>> for i in range(5):
    ...     for j in range(5):
    ...         S[i, j] = i + j    # Update element

    t   dokc   	      C` s…  t  j |  ƒ t j |  ƒ t | d t ƒ|  _ t | t ƒ rh t | ƒ rh | \ } } | | f |  _	 nt
 | ƒ rç t | ƒ r• | r• | j ƒ  } n | j ƒ  } | d  k	 r¿ | j | ƒ } n  |  j | ƒ | j	 |  _	 | j |  _ nš y t j | ƒ } Wn t d ƒ ‚ n Xt | j	 ƒ d k r4t d ƒ ‚ n  d d l m } | | d | ƒj ƒ  } |  j | ƒ | j	 |  _	 | j |  _ d  S(   Nt   defaults   invalid input formati   s'   expected rank <=2 dense array or matrixi   (   t
   coo_matrixt   dtype(   t   dictt   __init__R
   R   t   floatR   t
   isinstancet   tupleR   t   shapeR   R   t   copyt   todokt   Nonet   astypet   updatet   npt   asarrayt	   TypeErrort   lent   cooR   (	   t   selft   arg1R%   R   R&   t   Mt   NR   t   d(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyR!   O   s4    c         C` s(   | d  k	 r t d ƒ ‚ n  t j |  ƒ S(   Ns5   getnnz over an axis is not implemented for DOK format(   R(   t   NotImplementedErrorR    R   (   R0   t   axis(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   getnnzr   s    c         C` s   t  d „  t |  ƒ Dƒ ƒ S(   Nc         s` s   |  ] } | d  k Vq d S(   i    N(    (   t   .0R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pys	   <genexpr>y   s    (   t   sumR	   (   R0   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   count_nonzerox   s    c         C` s   t  j |  ƒ S(   N(   R    R   (   R0   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyR   ~   s    g        c         C` s·   y. | \ } } t  | ƒ r' t  | ƒ s- t ‚ Wn& t t t f k
 rV t d ƒ ‚ n X| d k  s• | |  j d k s• | d k  s• | |  j d k r¤ t d ƒ ‚ n  t j |  | | ƒ S(   st   This overrides the dict.get method, providing type checking
        but otherwise equivalent functionality.
        s    index must be a pair of integersi    i   s   index out of bounds(   R   t   AssertionErrorR-   t
   ValueErrort
   IndexErrorR%   R    t   get(   R0   t   keyR   t   it   j(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyR>      s    ">c         C` sV  |  j  j d ƒ } |  j | ƒ \ } } t | ƒ } t | ƒ } | r| rt | ƒ } t | ƒ } | d k  rƒ | |  j d 7} n  | d k  s¢ | |  j d k r± t d ƒ ‚ n  | d k  rÑ | |  j d 7} n  | d k  sð | |  j d k rÿ t d ƒ ‚ n  t j |  | | f | ƒ S| s-t	 | t
 ƒ r | sBt	 | t
 ƒ r | r[t
 | | d ƒ n | } | rzt
 | | d ƒ n | } | j |  j d ƒ }	 | j |  j d ƒ }
 t |	 Œ  } t |
 Œ  } t | ƒ t | ƒ f } t | ƒ } t |  ƒ d | k  r | d k r |  j |	 |
 | ƒ Sn  |  j | | ƒ \ } } | j d k r]t | j d |  j  ƒS| j ƒ  } | |  j d k  s–| j ƒ  |  j d k rÊt d | j ƒ  |  j d |  j d d f ƒ ‚ n  | d k  r| j ƒ  } | | d k  c |  j d 7<n  | j ƒ  } | |  j d k  s;| j ƒ  |  j d k rot d | j ƒ  |  j d |  j d d f ƒ ‚ n  | d k  r§| j ƒ  } | | d k  c |  j d 7<n  t | j d |  j  ƒ} x t | j d ƒ D]{ } xr t | j d ƒ D]] } t j |  | | | f | | | f f | ƒ } | d k rít j | | | f | ƒ qíqíWqÓW| S(   s½   If key=(i,j) is a pair of integers, return the corresponding
        element.  If either i or j is a slice or sequence, return a new sparse
        matrix with just these elements.
        i    s   index out of boundsi   i   R   s"   index (%d) out of range -%d to %d)(   R   t   typet   _unpack_indexR   t   intR%   R=   R    R>   R#   t   slicet   indicesR   R.   t   _prodt   _getitem_rangest   _index_to_arrayst   sizeR   t   mint   maxR&   t   __setitem__(   R0   t   indext   zeroR@   RA   t	   i_intliket	   j_intliket   i_slicet   j_slicet	   i_indicest	   j_indicest   i_seqt   j_seqt   newshapet   newsizet   min_it   min_jt   newdokt   at   bt   v(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   __getitem__Ž   sh    "-. -. /$c         C` s7  t  t | ƒ \ } } } t  t | ƒ \ } } }	 t | d |  j ƒ}
 xë |  j ƒ  D]Ý \ } } t | ƒ } t | ƒ } t | | | ƒ \ } } | d k  sR | | d k sR | d k r½ qR n  t | | |	 ƒ \ } } | d k  sR | | d k sR | d k rqR n  t j |
 | | f t j |  | | f ƒ ƒ qR W|
 S(   NR   i    i   (	   t   mapRD   R   R   t   keyst   divmodR    RM   R`   (   R0   RT   RU   R%   t   i_startt   i_stopt   i_stridet   j_startt   j_stopt   j_strideR\   t   iit   jjR]   t   raR^   t   rb(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyRH   Ú   s    ((c         C` s  t  | t ƒ rñ t | ƒ d k rñ | \ } } t | ƒ rñ t | ƒ rñ d | k oc |  j d k  n rñ d | k o† |  j d k  n rñ t j | d |  j ƒ} | j d k rî | d k rî t	 j
 |  t | ƒ t | ƒ f | d ƒ d  Sqñ n  |  j | ƒ \ } } |  j | | ƒ \ } } t | ƒ r9| j ƒ  } n  t j | d |  j ƒ} t j | | ƒ \ } } | j | j k rŠt d ƒ ‚ n  t j | ƒ d k r£d  S| j ƒ  } | |  j d k  sÜ| j ƒ  |  j d k rt d | j ƒ  |  j d |  j d d f ƒ ‚ n  | d k  rH| j ƒ  } | | d k  c |  j d 7<n  | j ƒ  } | |  j d k  s| j ƒ  |  j d k rµt d | j ƒ  |  j d |  j d d f ƒ ‚ n  | d k  rí| j ƒ  } | | d k  c |  j d 7<n  t	 j |  t t | j | j ƒ | j ƒ ƒ d | k r}| d k }	 xJ t | |	 j | |	 j ƒ D]( }
 t	 j |  |
 ƒ d k rN|  |
 =qNqNWn  d  S(   Ni   i    i   R   s   shape mismatch in assignments"   index (%d) out of range -%d to %d)(    (   R#   R$   R.   R   R%   R+   R,   R   t   ndimR    RM   RD   RC   RI   R   t   toarrayt   broadcast_arraysR<   RJ   RK   RL   R=   R&   R*   t   izipt   flatR`   (   R0   RN   R   R@   RA   R_   t   _RZ   R[   t   zeroesR?   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyRM   ð   sL    !;#)
-. -. +$c      
   C` s¨  t  | ƒ r® t |  j | ƒ } t |  j d | ƒ} |  j \ } } x_t | ƒ D]X } xO t | ƒ D]A } |  j | | f d ƒ | } | d k rb | | | | f <qb qb WqO Wnö t | t ƒ r\| j |  j k rÞ t d ƒ ‚ n  t	 |  j | j ƒ } t |  j d | ƒ} | j
 |  ƒ t j d d ƒ 0 x( | j ƒ  D] }	 | |	 c | |	 7<q5WWd  QXnH t | ƒ r|  j ƒ  }
 |
 | } n# t | ƒ r |  j ƒ  | } n t S| S(   NR   i    s   matrix dimensions are not equalt   overt   ignore(   R   R   R   R   R%   R   R>   R#   R<   R   R*   R+   t   errstateRb   R   t   tocscR   t   todenset   NotImplemented(   R0   t   othert	   res_dtypet   newR2   R3   R@   RA   t   aijR?   t   csc(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   __add__$  s2    !c   
      C` sh  t  | ƒ rŸ t |  j d |  j ƒ} |  j \ } } x.t | ƒ D]X } xO t | ƒ D]A } |  j | | f d ƒ | } | d k rS | | | | f <qS qS Wq@ WnÅ t | t ƒ r| j |  j k rÏ t d ƒ ‚ n  t |  j d |  j ƒ} | j |  ƒ xm | D] } | | c | | 7<qû WnH t	 | ƒ rA|  j
 ƒ  }	 |	 | } n# t | ƒ r`| |  j ƒ  } n t S| S(   NR   i    s   matrix dimensions are not equal(   R   R   R%   R   R   R>   R#   R<   R*   R   Rx   R   Ry   Rz   (
   R0   R{   R}   R2   R3   R@   RA   R~   R?   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   __radd__E  s,    c         C` sc   |  j  j d k r! t d ƒ ‚ n  t |  j d |  j  ƒ} x# |  j ƒ  D] } |  | | | <qF W| S(   NR^   s1   negating a sparse boolean matrix is not supportedR   (   R   t   kindR5   R   R%   Rb   (   R0   R}   R?   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   __neg__`  s    c         C` sV   t  |  j | ƒ } t |  j d | ƒ} x( t |  ƒ D] \ } } | | | | <q4 W| S(   NR   (   R   R   R   R%   R   (   R0   R{   R|   R}   R?   t   val(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   _mul_scalarj  s
    c         C` sj   t  j |  j d d t |  j | j ƒ ƒ} x8 t |  ƒ D]* \ \ } } } | | c | | | 7<q8 W| S(   Ni    R   (   R+   t   zerosR%   R   R   R   (   R0   R{   t   resultR@   RA   R_   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   _mul_vectorr  s    +c   	   	   C` s   |  j  \ } } | j  d } t j | | f d t |  j | j ƒ ƒ} xP t |  ƒ D]B \ \ } } } | | d  d  … f c | | | d  d  … f 7<qS W| S(   Ni   R   (   R%   R+   R†   R   R   R   (	   R0   R{   R2   R3   t   n_vecsR‡   R@   RA   R_   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   _mul_multivectory  s    *4c         C` sC   t  | ƒ r; x( t |  ƒ D] \ } } | | |  | <q W|  St Sd  S(   N(   R   R   Rz   (   R0   R{   R?   R„   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   __imul__‚  s
    c         C` st   t  | ƒ rb t |  j | ƒ } t |  j d | ƒ} x( t |  ƒ D] \ } } | | | | <q@ W| S|  j ƒ  | Sd  S(   NR   (   R   R   R   R   R%   R   t   tocsr(   R0   R{   R|   R}   R?   R„   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   __truediv__Œ  s    c         C` sC   t  | ƒ r; x( t |  ƒ D] \ } } | | |  | <q W|  St Sd  S(   N(   R   R   Rz   (   R0   R{   R?   R„   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   __itruediv__˜  s
    c         C` s„   | d  k	 r t d ƒ ‚ n  |  j \ } } t | | f d |  j d | ƒ} x2 t |  ƒ D]$ \ } } | | | d | d f <qX W| S(   Nso   Sparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.R   R&   i   i    (   R(   R<   R%   R   R   R   (   R0   t   axesR&   R2   R3   R}   R?   t   value(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt	   transpose¥  s    !c         C` sl   |  j  \ } } t | | f d |  j ƒ} x; t |  ƒ D]- \ } } t j | ƒ | | d | d f <q7 W| S(   s(    Return the conjugate transpose
        R   i   i    (   R%   R   R   R   R+   t   conj(   R0   R2   R3   R}   R?   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt
   conjtranspµ  s
    %c         C` s)   t  |  j d |  j ƒ} | j |  ƒ | S(   NR   (   R   R%   R   R*   (   R0   R}   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyR&   ¾  s    c         C` sa   |  j  d |  j d f d |  j ƒ} x5 t |  j d ƒ D]  } |  | | f | d | f <q9 W| S(   sO   Returns a copy of row i of the matrix as a (1 x n)
        DOK matrix.
        i   R   i    (   t	   __class__R%   R   t   range(   R0   R@   t   outRA   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   getrowÅ  s    %c         C` sa   |  j  |  j d d f d |  j ƒ} x5 t |  j d ƒ D]  } |  | | f | | d f <q9 W| S(   sR   Returns a copy of column j of the matrix as a (m x 1)
        DOK matrix.
        i    i   R   (   R”   R%   R   R•   (   R0   RA   R–   R@   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   getcolÎ  s    %c         C` s  d d l  m } |  j d k r5 | |  j d |  j ƒSt d t |  j ƒ ƒ } t j t	 |  ƒ d |  j d |  j ƒ} t j d „  t
 |  ƒ Dƒ d | d |  j ƒ} t j d „  t
 |  ƒ Dƒ d | d |  j ƒ} | | | | f f d	 |  j d |  j ƒ} t | _ | S(
   Ni   (   R   i    R   t   maxvalt   countc         s` s   |  ] \ } } | Vq d  S(   N(    (   R8   R@   Rs   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pys	   <genexpr>Þ  s    c         s` s   |  ] \ } } | Vq d  S(   N(    (   R8   Rs   RA   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pys	   <genexpr>ß  s    R%   (   R/   R   t   nnzR%   R   R   RL   R+   t   fromiterR	   R   t   Truet   has_canonical_format(   R0   R&   R   t	   idx_dtypet   datat   It   Jt   A(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   tocoo×  s    '..*	c         C` s   | r |  j  ƒ  S|  Sd  S(   N(   R&   (   R0   R&   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyR'   æ  s    
c         C` s   |  j  d t ƒ j d | ƒ S(   NR&   (   R¤   t   FalseRx   (   R0   R&   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyRx   î  s    c         C` s©   t  | ƒ s t d ƒ ‚ n  | \ } } |  j \ } } | | k  sN | | k  rœ xK t |  j ƒ  ƒ D]4 \ } } | | k s… | | k ra |  | | f =qa qa Wn  | |  _ d S(   s    Resize the matrix in-place to dimensions given by 'shape'.

        Any non-zero elements that lie outside the new shape are removed.
        s1   dimensions must be a 2-tuple of positive integersN(   R   R-   R%   t   _listRb   t   _shape(   R0   R%   t   newMt   newNR2   R3   R@   RA   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   resizeó  s    N(!   t   __name__t
   __module__t   __doc__t   formatR(   R¥   R!   R7   R:   R
   R   R>   R`   RH   RM   R€   R   Rƒ   R…   Rˆ   RŠ   R‹   R   RŽ   R‘   R“   R&   R—   R˜   R¤   R'   Rx   Rª   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyR      sF   0#			L		4	!		
					
									c         C` s"   t  |  t ƒ s t |  ƒ }  n  |  S(   s   Force x to a list.(   R#   t   list(   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyR¦     s    c         C` s   t  |  t ƒ S(   N(   R#   R   (   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyR     s    c         C` s)   t  |  ƒ d k r d St j t j |  ƒ S(   sF   Product of a list of numbers; ~40x faster vs np.prod for Python tuplesi    i   (   R.   t	   functoolst   reducet   operatort   mul(   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyRG     s    ('   R­   t
   __future__R    R   R   t   __docformat__t   __all__R°   R²   t   numpyR+   t   scipy._lib.sixR   Rq   R   R   R   R	   t   baseR
   R   t   sputilsR   R   R   R   R   R   R   R   R   R   R   t   ImportErrorR    R   R¦   R   RG   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/dok.pyt   <module>   s$   (@ÿ ê		