ó
î&]\c           @` sˆ  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d d d d d	 d
 d d g	 Z	 d d d d d d d d d d d d d d d g Z
 g  e
 D] Z e j e ^ q¡ Z
 i  Z d „  Z d „  Z d „  Z d „  Z d  „  Z d d d! „ Z d/ d e d" „ Z d# „  Z d$ „  Z d% „  Z e d& „ Z d' „  Z d( „  Z d) „  Z d* „  Z d d+ „ Z d, „  Z  d- e! f d. „  ƒ  YZ" d S(0   s,    Utility functions for sparse matrix module
i    (   t   divisiont   print_functiont   absolute_importNt   upcastt   getdtypet   isscalarliket	   isintliket   isshapet
   issequencet   isdenset   ismatrixt   get_sum_dtypet   boolt   int8t   uint8t   shortt   ushortt   intct   uintct   longlongt	   ulonglongt   singlet   doublet
   longdoublet   csinglet   cdoublet   clongdoublec          G` s…   t  j t |  ƒ ƒ } | d k	 r% | St j |  g  ƒ } x4 t D], } t j | | ƒ r> | t  t |  ƒ <| Sq> Wt d |  f ƒ ‚ d S(   s›  Returns the nearest supported sparse dtype for the
    combination of one or more types.

    upcast(t0, t1, ..., tn) -> T  where T is a supported dtype

    Examples
    --------

    >>> upcast('int32')
    <type 'numpy.int32'>
    >>> upcast('bool')
    <type 'numpy.bool_'>
    >>> upcast('int32','float32')
    <type 'numpy.float64'>
    >>> upcast('bool',complex,float)
    <type 'numpy.complex128'>

    s%   no supported conversion for types: %rN(	   t   _upcast_memot   gett   hasht   Nonet   npt   find_common_typet   supported_dtypest   can_castt	   TypeError(   t   argst   tR   (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyR      s    c          G` sE   t  j |  ƒ } | d k	 r | St t t j |  ƒ Œ  } | t  |  <| S(   s9   Same as `upcast` but taking dtype.char as input (faster).N(   R   R   R   R   t   mapR   t   dtype(   R$   R%   (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyt   upcast_char7   s    
c         C` s   t  j d g d |  ƒ| j S(   s`   Determine data type for binary operation between an array of
    type `dtype` and a scalar.
    i    R'   (   R   t   arrayR'   (   R'   t   scalar(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyt   upcast_scalarA   s    c         C` s±   |  j  j t j  t j ƒ j k r­ |  j d k r@ |  j t j ƒ S|  j ƒ  } |  j ƒ  } | t j t j ƒ j k sŽ | t j t j ƒ j k  r t	 d ƒ ‚ n  |  j t j ƒ S|  S(   sž   
    Down-cast index array to np.intp dtype if it is of a larger dtype.

    Raise an error if the array contains a value that is too large for
    intp.
    i    sz   Cannot deal with arrays with indices larger than the machine maximum address size (e.g. 64-bit indices on 32-bit machine).(
   R'   t   itemsizeR   t   intpt   sizet   astypet   maxt   mint   iinfot
   ValueError(   t   arrt   maxvalt   minval(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyt   downcast_intp_indexH   s    !6c         C` s   t  j |  d |  j j d ƒ ƒS(   NR'   t   native(   R   t   asarrayR'   t   newbyteorder(   t   A(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyt	   to_native\   s    c         C` sŒ   |  d k rZ y | j } Wqˆ t k
 rV | d k	 rG t j | ƒ } qW t d ƒ ‚ qˆ Xn. t j |  ƒ } | t j k rˆ t j d ƒ n  | S(   sS  Function used to simplify argument processing.  If 'dtype' is not
    specified (is None), returns a.dtype; otherwise returns a np.dtype
    object created from the specified dtype argument.  If 'dtype' and 'a'
    are both None, construct a data type out of the 'default' parameter.
    Furthermore, 'dtype' must be in 'allowed' set.
    s   could not interpret data types0   object dtype is not supported by sparse matricesN(   R   R'   t   AttributeErrorR   R#   t   object_t   warningst   warn(   R'   t   at   defaultt   newdtype(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyR   `   s    c         C` s0  t  j t  j ƒ j } t  j t  j ƒ j } t  j } | d k	 rZ | | k rZ t  j } qZ n  t |  t  j	 ƒ rx |  f }  n  x± |  D]© } t  j
 | ƒ } t  j | j t  j ƒ s | r| j d k rÇ q qt  j | j t  j ƒ r| j ƒ  } | j ƒ  } | | k r| | k rq qqn  t  j } Pq q W| S(   s&  
    Based on input (integer) arrays `a`, determine a suitable index data
    type that can hold the data in the arrays.

    Parameters
    ----------
    arrays : tuple of array_like
        Input arrays whose types/contents to check
    maxval : float, optional
        Maximum value needed
    check_contents : bool, optional
        Whether to check the values in the arrays and not just their types.
        Default: False (check only the types)

    Returns
    -------
    dtype : dtype
        Suitable index data type (int32 or int64)

    i    N(   R   R2   t   int32R1   R0   R   R   t   int64t
   isinstancet   ndarrayR9   R"   R'   R.   t
   issubdtypet   integer(   t   arraysR5   t   check_contentst   int32mint   int32maxR'   R4   R6   (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyt   get_index_dtypex   s,    		c         C` sK   |  j  d k r+ t j |  t j ƒ r+ t j St j |  t j ƒ rG t j S|  S(   s    Mimic numpy's casting for np.sumt   u(   t   kindR   R"   t   uintt   int_(   R'   (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyR   ­   s
    $c         C` s(   t  j |  ƒ p' t |  ƒ o' |  j d k S(   s8   Is x either a scalar, an array scalar, or a 0-dim array?i    (   R   t   isscalarR	   t   ndim(   t   x(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyR   ¶   s    c         C` s™   t  j |  ƒ d k r t Sy t j |  ƒ Wnh t t f k
 r” y t t |  ƒ |  k ƒ } Wn t t f k
 rv t SX| r t	 j
 d t ƒ n  | SXt S(   ss   Is x appropriate as an index into a sparse matrix? Returns True
    if it can be cast safely to a machine int.
    i    s3   Inexact indices into sparse matrices are deprecated(   R   RT   t   Falset   operatort   indexR#   R3   R   t   intR?   R@   t   DeprecationWarningt   True(   RU   t	   loose_int(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyR   »   s    	
c         C` s˜   y |  \ } } Wn t  k
 r$ t SXt | ƒ r t | ƒ r t j | ƒ d k r t j | ƒ d k r | s† | d k r | d k r t Sq n  t Sd S(   sj   Is x a valid 2-tuple of dimensions?

    If nonneg, also checks that the dimensions are non-negative.
    i    N(   t	   ExceptionRV   R   R   RT   R[   (   RU   t   nonnegt   Mt   N(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyR   Ñ   s    *
c         C` sY   t  |  t t f ƒ r: t |  ƒ d k pX t j |  d ƒ pX t  |  t j ƒ oX |  j d k S(   Ni    i   (   RF   t   listt   tuplet   lenR   RS   RG   RT   (   R%   (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyR   ã   s    %c         C` sV   t  |  t t f ƒ r7 t |  ƒ d k r7 t |  d ƒ pU t  |  t j ƒ oU |  j d k S(   Ni    i   (   RF   Ra   Rb   Rc   R   R   RG   RT   (   R%   (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyR
   é   s    "c         C` s   t  |  t j ƒ S(   N(   RF   R   RG   (   RU   (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyR	   ï   s    c         C` s¡   |  d  k	 r t |  ƒ } | t k r3 t d ƒ ‚ n  t j t j | ƒ t j ƒ so t d j d | j	 ƒ ƒ ‚ n  d |  k o† d k n s t
 d ƒ ‚ q n  d  S(   Nsl   Tuples are not accepted for the 'axis' parameter. Please pass in one of the following: {-2, -1, 0, 1, None}.s#   axis must be an integer, not {name}t   nameiþÿÿÿi   s   axis out of range(   R   t   typeRb   R#   R   RH   R'   RI   t   formatt   __name__R3   (   t   axist	   axis_type(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyt   validateaxisó   s    c         C` så  t  |  ƒ d k r! t d ƒ ‚ n t  |  ƒ d k rŠ y t |  d ƒ } Wn' t k
 rp t j |  d ƒ f } q  Xt d „  | Dƒ ƒ } n t d „  |  Dƒ ƒ } | d k rÿ t  | ƒ d k rÍ t d ƒ ‚ qÀ| d d k  sí | d d k  rÀt d ƒ ‚ qÀnÁt j	 | d	 t
 ƒ} g  t | ƒ D] \ } } | d k  r!| ^ q!} t  | ƒ d k r–t j	 | d	 t
 ƒ} | | k rUt d
 j | | ƒ ƒ ‚ qUn¿ t  | ƒ d k rI| d }	 t j	 | d |	 !| |	 d ƒ }
 t | |
 ƒ \ } } | d k r&t d „  | Dƒ ƒ } t d
 j | | ƒ ƒ ‚ n  | d |	 !| f | |	 d } n t d ƒ ‚ t  | ƒ d k rÀt d „  | Dƒ ƒ } t  | ƒ d k r˜d } qÀt  | ƒ d k rÀd | d f } qÀn  t  | ƒ d k rát d ƒ ‚ n  | S(   s0   Imitate numpy.matrix handling of shape argumentsi    s8   function missing 1 required positional argument: 'shape'i   c         s` s   |  ] } t  j | ƒ Vq d  S(   N(   RW   RX   (   t   .0t   arg(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pys	   <genexpr>  s    c         s` s   |  ] } t  j | ƒ Vq d  S(   N(   RW   RX   (   Rk   Rl   (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pys	   <genexpr>  s    i   s,   shape must be a 2-tuple of positive integerss#   'shape' elements cannot be negativeR'   s-   cannot reshape array of size {} into shape {}c         s` s'   |  ] } | d  k  r d n | Vq d S(   i    t   newshapeN(    (   Rk   RU   (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pys	   <genexpr>/  s    s&   can only specify one unknown dimensionc         s` s!   |  ] } | d  k r | Vq d S(   i   N(    (   Rk   Rl   (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pys	   <genexpr>8  s    s   shape too large to be a matrixN(   i   i   (   Rc   R#   t   iterRW   RX   Rb   R   R3   R   t   prodRY   t	   enumerateRf   t   divmod(   R$   t   current_shapet
   shape_itert	   new_shapet   current_sizet   iRU   t   negative_indexest   new_sizet   skipt	   specifiedt   unspecifiedt	   remaindert	   err_shape(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyt   check_shape
  sP     1	
"	#	c         C` s[   |  j  d d ƒ } |  j  d t ƒ } |  rQ t d j d j |  j ƒ  ƒ ƒ ƒ ‚ n  | | f S(   s?  Unpack keyword arguments for reshape function.

    This is useful because keyword arguments after star arguments are not
    allowed in Python 2, but star keyword arguments are. This function unpacks
    'order' and 'copy' from the star keyword arguments (with defaults) and
    throws an error for any remaining.
    t   ordert   Ct   copys/   reshape() got unexpected keywords arguments: {}s   , (   t   popRV   R#   Rf   t   joint   keys(   t   kwargsR   R   (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyt   check_reshape_kwargsE  s    		t
   IndexMixinc           B` sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sT   
    This class simply exists to hold the methods necessary for fancy indexing.
    c         C` s+   | j  | ƒ \ } } } t j | | | ƒ S(   sT    Given a slice object, use numpy arange to change it to a 1D
        array.
        (   t   indicesR   t   arange(   t   selft   jt   shapet   startt   stopt   step(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyt   _slicetoarangeZ  s    c         C` s  d d l  m } t | | t j f ƒ rS | j d k rS | j j d k rS | j ƒ  S|  j	 | ƒ } t | t
 ƒ rÍ t | ƒ d k r’ | \ } } qà t | ƒ d k r¾ | d t d ƒ } } qà t d ƒ ‚ n | t d ƒ } } |  j | | ƒ \ } } | | f S(   s…    Parse index. Always return a tuple of the form (row, col).
        Where row/col is a integer, slice, or array of integers.
        i   (   t   spmatrixi   t   bi    s   invalid number of indicesN(   t   baseR‘   RF   R   RG   RT   R'   RP   t   nonzerot   _check_ellipsisRb   Rc   t   sliceR   t
   IndexErrort   _check_boolean(   RŠ   RX   R‘   t   rowt   col(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyt   _unpack_indexa  s    !
c         C` s’  | t  k r" t d ƒ t d ƒ f St | t ƒ rŽx6 t | ƒ D]" \ } } | t  k r> | } Pq> q> Wd } | d k	 rŽt | ƒ d k rž t d ƒ t d ƒ f St | ƒ d k r| d k rù | d t  k râ t d ƒ t d ƒ f St d ƒ | d f Sq| d t d ƒ f Sn  d } x2 | | d D]" } | t  k	 r%| | f } q%q%W| t | ƒ } t d d | ƒ } | |  t d ƒ f | | Sn  | S(   s6   Process indices with Ellipsis. Returns modified index.i   i   i    N(    (   t   EllipsisR–   R   RF   Rb   Rp   Rc   R0   (   RŠ   RX   R‹   t   vt   first_ellipsist   tailt   ndt   nslice(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyR•   }  s2     c         C` s­   d d l  m } | | ƒ s( | | ƒ r7 t d ƒ ‚ n  t | t j ƒ rm | j j d k rm |  j | ƒ } n  t | t j ƒ r£ | j j d k r£ |  j | ƒ } n  | | f S(   Ni   (   t
   isspmatrixso   Indexing with sparse matrices is not supported except boolean indexing where matrix and index are equal shapes.R’   (	   R“   R¢   R—   RF   R   RG   R'   RP   t   _boolean_index_to_array(   RŠ   R™   Rš   R¢   (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyR˜   £  s    $$c         C` s,   | j  d k r t d ƒ ‚ n  | j ƒ  d S(   Ni   s   invalid index shapei    (   RT   R—   R”   (   RŠ   Rv   (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyR£   ²  s    c         C` s  |  j  | | ƒ \ } } t | t ƒ } | rY |  j | |  j d ƒ d  d  … d  f } n t j | ƒ } t | t ƒ rà |  j | |  j d ƒ d  d  d  … f } | j d k rÈ | d  d  … d  f } qŽ| sŽt	 d ƒ ‚ qŽn® t
 | ƒ r[t j | ƒ } | j d k rŽt j | | ƒ \ } } | d  d  … d  f } | d  d  … d  f } | | f Sn3 t j | ƒ } | rŽ| j d k rŽt	 d ƒ ‚ n  t j | | ƒ \ } } | j d k rä| d  d  d  … f } | d  d  d  … f } n | j d k rt	 d ƒ ‚ n  | | f S(   Ni    i   s   index returns 3-dim structurei   s   Index dimension must be <= 2(   R˜   RF   R–   R   RŒ   R   R   t
   atleast_1dRT   R—   R   t   broadcast_arrays(   RŠ   Rv   R‹   t   i_slice(    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyt   _index_to_arrays·  s8    ,)(	   Rg   t
   __module__t   __doc__R   R›   R•   R˜   R£   R§   (    (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyR‡   V  s   			&		(    (#   R©   t
   __future__R    R   R   RW   R?   t   numpyR   t   __all__R!   RU   t   typeDictR   R   R(   R+   R7   R<   R   R   RV   RN   R   R   R   R   R   R
   R	   Rj   R~   R†   t   objectR‡   (    (    (    s3   lib/python2.7/site-packages/scipy/sparse/sputils.pyt   <module>   s:    	"	
			5								;	