ó
ÚÆ÷Xc           @` sd  d  Z  d d l m Z m Z m 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 d& „  Z d' „  Z d( „  Z d) „  Z d* „  Z d+ e f d, „  ƒ  YZ d S(.   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   /tmp/pip-build-X4mzal/scipy/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   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyt   upcast_char6   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   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyt   upcast_scalar@   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   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyt   downcast_intp_indexG   s    !6c         C` s   t  j |  d |  j j d ƒ ƒS(   NR'   t   native(   R   t   asarrayR'   t   newbyteorder(   t   A(    (    s3   /tmp/pip-build-X4mzal/scipy/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   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyR   _   s    c         C` s3  t  j t  j ƒ j } t  j } | d k	 rE | | k rE t  j } qE n  t |  t  j ƒ rc |  f }  n  xÉ |  D]Á } t  j	 | ƒ } | j
 t  j k rj | r| j d k r¬ qj qt  j | j
 t  j ƒ r| j ƒ  } | j ƒ  } | t  j t  j ƒ j k r| t  j t  j ƒ j k rqj qqn  t  j } Pqj qj 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   int32R0   R   R   t   int64t
   isinstancet   ndarrayR9   R'   R.   t
   issubdtypet   integerR1   (   t   arraysR5   t   check_contentst   int32maxR'   R4   R6   (    (    s3   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyt   get_index_dtypew   s,    		c         C` sg   t  j |  t  j ƒ r t  j S|  j d k rG t  j |  t  j ƒ rG t  j St  j |  t  j ƒ rc t  j S|  S(   s    Mimic numpy's casting for np.sumt   u(   R   RH   t   float_t   kindR"   t   uintt   int_(   R'   (    (    s3   /tmp/pip-build-X4mzal/scipy/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   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyR   ·   s    c         C` sI   t  |  ƒ s t Sy t t |  ƒ |  k ƒ SWn t t f k
 rD t SXd S(   ss   Is x appropriate as an index into a sparse matrix? Returns True
    if it can be cast safely to a machine int.
    N(   R   t   FalseR   t   intR#   R3   (   RU   (    (    s3   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyR   ¼   s    c         C` sl   y |  \ } } Wn t  SXt | ƒ rd t | ƒ rd t j | ƒ d k rd t j | ƒ d k rd t Sn  t  Sd S(   s(   Is x a valid 2-tuple of dimensions?
    i    N(   RV   R   R   RT   t   True(   RU   t   Mt   N(    (    s3   /tmp/pip-build-X4mzal/scipy/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   /tmp/pip-build-X4mzal/scipy/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   R[   R\   R]   R   R   RG   RT   (   R%   (    (    s3   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyR
   Ý   s    "c         C` s   t  |  t j ƒ S(   N(   RF   R   RG   (   RU   (    (    s3   /tmp/pip-build-X4mzal/scipy/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   typeR\   R#   R   RH   R'   RI   t   formatt   __name__R3   (   t   axist	   axis_type(    (    s3   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyt   validateaxisç   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   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyt   _slicetoarange  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   baseRo   RF   R   RG   RT   R'   RP   t   nonzerot   _check_ellipsisR\   R]   t   sliceR   t
   IndexErrort   _check_boolean(   Rh   t   indexRo   t   rowt   col(    (    s3   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyt   _unpack_index	  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   EllipsisRt   R   RF   R\   t	   enumerateR]   R0   (   Rh   Rw   Ri   t   vt   first_ellipsist   tailt   ndt   nslice(    (    s3   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyRs   %  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.Rp   (	   Rq   R‚   Ru   RF   R   RG   R'   RP   t   _boolean_index_to_array(   Rh   Rx   Ry   R‚   (    (    s3   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyRv   K  s    $$c         C` s,   | j  d k r t d ƒ ‚ n  | j ƒ  d S(   Ni   s   invalid index shapei    (   RT   Ru   Rr   (   Rh   t   i(    (    s3   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyRƒ   Z  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(   Rv   RF   Rt   Rn   Rj   R   R   t
   atleast_1dRT   Ru   R   t   broadcast_arrays(   Rh   R„   Ri   t   i_slice(    (    s3   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyt   _index_to_arrays_  s8    ,)(	   Ra   t
   __module__t   __doc__Rn   Rz   Rs   Rv   Rƒ   Rˆ   (    (    (    s3   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyRe   þ   s   			&		(    (    RŠ   t
   __future__R    R   R   R?   t   numpyR   t   __all__R!   RU   t   typeDictR   R   R(   R+   R7   R<   R   R   RV   RM   R   R   R   R   R   R
   R	   Rd   t   objectRe   (    (    (    s3   /tmp/pip-build-X4mzal/scipy/scipy/sparse/sputils.pyt   <module>   s4    	"	
			5								