ó
Ê½÷Xc           @` sO  d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 m
 Z
 d  d l Z d  d l Z e j d d „ Z d d d „ Z d  d „ Z e d d d „ Z d	 „  Z d
 e f d „  ƒ  YZ d „  Z e e e e e d „ Z y/ e j e
 d d d d d g ƒ Z d „  Z Wn e k
 rJd „  Z n Xd S(   i    (   t   divisiont   print_functiont   absolute_importN(   t
   namedtuplec         C` s_   t  j |  d t ƒ| } | d k	 r7 | j | ƒ } n  t | t  j ƒ s[ t  j | ƒ } n  | S(   s"   Return an array of all value.
    t   dtypeN(   t   npt   onest   boolt   Nonet   astypet
   isinstancet   ndarrayt   asarray(   t   shapet   valuet   typecodet   out(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyt	   _valarray   s    c   	      ` s(  | d k r3 | d k r' t d ƒ ‚ qN t j } n | d k	 rN t d ƒ ‚ n  t j | Œ  } t ‡  f d †  | Dƒ ƒ } t j g  | D] } | j j ^ q† ƒ } t	 t j
 | d ƒ d | d | ƒ} t j | ˆ  | | Œ  ƒ | d k	 r$t ‡  f d †  | Dƒ ƒ } t j | ˆ  | | Œ  ƒ n  | S(	   s¼  
    np.where(cond, x, fillvalue) always evaluates x even where cond is False.
    This one only evaluates f(arr1[cond], arr2[cond], ...).
    For example,
    >>> a, b = np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])
    >>> def f(a, b):
        return a*b
    >>> _lazywhere(a > 2, (a, b), f, np.nan)
    array([ nan,  nan,  21.,  32.])

    Notice it assumes that all `arrays` are of the same shape, or can be
    broadcasted together.

    s%   One of (fillvalue, f2) must be given.s)   Only one of (fillvalue, f2) can be given.c         3` s!   |  ] } t  j ˆ  | ƒ Vq d  S(   N(   R   t   extract(   t   .0t   arr(   t   cond(    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pys	   <genexpr>3   s    i    R   R   c         3` s"   |  ] } t  j ˆ  | ƒ Vq d  S(   N(   R   R   (   R   R   (   R   (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pys	   <genexpr>8   s    N(   R   t
   ValueErrorR   t   nant   broadcast_arrayst   tuplet   mintypecodeR   t   charR   R   t   place(	   R   t   arrayst   ft	   fillvaluet   f2t   tempt   at   tcodeR   (    (   R   s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyt
   _lazywhere   s    (%c         ` sþ   t  j | Œ  } t  j g  | D] } | j j ^ q ƒ } t t  j | d ƒ d | d | ƒ} x› t t |  ƒ ƒ D]‡ } | | |  | } ‰  t  j	 ˆ  t
 k ƒ r¥ qo n  t  j ˆ  | d ƒ \ ‰  }	 t ‡  f d †  | Dƒ ƒ }
 t  j | ˆ  | |
 Œ  ƒ qo W| S(   s?  
    Mimic `np.select(condlist, choicelist)`.

    Notice it assumes that all `arrays` are of the same shape, or can be
    broadcasted together.

    All functions in `choicelist` must accept array arguments in the order
    given in `arrays` and must return an array of the same shape as broadcasted
    `arrays`.

    Examples
    --------
    >>> x = np.arange(6)
    >>> np.select([x <3, x > 3], [x**2, x**3], default=0)
    array([  0,   1,   4,   0,  64, 125])

    >>> _lazyselect([x < 3, x > 3], [lambda x: x**2, lambda x: x**3], (x,))
    array([   0.,    1.,    4.,   0.,   64.,  125.])

    >>> a = -np.ones_like(x)
    >>> _lazyselect([x < 3, x > 3],
    ...             [lambda x, a: x**2, lambda x, a: a * x**3],
    ...             (x, a), default=np.nan)
    array([   0.,    1.,    4.,   nan,  -64., -125.])

    i    R   R   c         3` s!   |  ] } t  j ˆ  | ƒ Vq d  S(   N(   R   R   (   R   R   (   R   (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pys	   <genexpr>a   s    (   R   R   R   R   R   R   R   t   ranget   lent   allt   FalseR   R   (   t   condlistt
   choicelistR   t   defaultR"   R#   R   t   indext   funct   _R!   (    (   R   s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyt   _lazyselect>   s    (%t   Cc         C` sî   t  j | ƒ } | d k r' | j } n  t |  d ƒ sB |  f }  n  t j t j |  ƒ | j	 } t  j
 | | d t  j ƒ } | j d d | } | d k r© | | } n  | | | | d !d  } t  j |  | | d | ƒ} | j d ƒ | S(   sô   Allocate a new ndarray with aligned memory.

    Primary use case for this currently is working around a f2py issue
    in Numpy 1.9.1, where dtype.alignment is such that np.zeros() does
    not necessarily create arrays aligned up to it.

    t   __len__i   t   datai    iÿÿÿÿt   orderN(   R   R   R   t	   alignmentt   hasattrt	   functoolst   reducet   operatort   mult   itemsizet   emptyt   uint8t   __array_interface__R   t   fill(   R   R   R3   t   alignt   sizet   buft   offsetR2   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyt   _aligned_zerosf   s    c         C` s6   |  j  d k	 r2 |  j |  j  j d k  r2 |  j ƒ  S|  S(   sÄ   Return an array equivalent to the input array. If the input
    array is a view of a much larger array, copy its contents to a
    newly allocated array. Otherwise, return the input unchaged.
    i   N(   t   baseR   R@   t   copy(   t   array(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyt   _prune_array€   s    (
t   DeprecatedImportc           B` s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s2  
    Deprecated import, with redirection + warning.

    Examples
    --------
    Suppose you previously had in some module::

        from foo import spam

    If this has to be deprecated, do::

        spam = DeprecatedImport("foo.spam", "baz")

    to redirect users to use "baz" module instead.

    c         C` s6   | |  _  | |  _ t |  j ƒ t j |  j |  _ d  S(   N(   t	   _old_namet	   _new_namet
   __import__t   syst   modulest   _mod(   t   selft   old_module_namet   new_module_name(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyt   __init__œ   s    		c         C` s   t  |  j ƒ S(   N(   t   dirRN   (   RO   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyt   __dir__¢   s    c         C` s0   t  j d |  j |  j f t ƒ t |  j | ƒ S(   Ns'   Module %s is deprecated, use %s instead(   t   warningst   warnRI   RJ   t   DeprecationWarningt   getattrRN   (   RO   t   name(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyt   __getattr__¥   s    	(   t   __name__t
   __module__t   __doc__RR   RT   RZ   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyRH   Š   s   		c         C` s€   |  d k s |  t j k r( t j j j St |  t j t j f ƒ rS t j j	 |  ƒ St |  t j j	 ƒ rl |  St
 d |  ƒ ‚ d S(   s6  Turn seed into a np.random.RandomState instance

    If seed is None (or np.random), return the RandomState singleton used
    by np.random.
    If seed is an int, return a new RandomState instance seeded with seed.
    If seed is already a RandomState instance, return it.
    Otherwise raise ValueError.
    s=   %r cannot be used to seed a numpy.random.RandomState instanceN(   R   R   t   randomt   mtrandt   _randR
   t   numberst   Integralt   integert   RandomStateR   (   t   seed(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyt   check_random_state­   s    	c   	      C` s÷   | s< d d l  } | j j |  ƒ r< d } t | ƒ ‚ q< n  | sf t j j |  ƒ rf t d ƒ ‚ qf n  | ru t j n t j } | |  ƒ }  | sº |  j	 t j	 d ƒ k rº t d ƒ ‚ qº n  | ró t j
 |  j	 t j ƒ só | |  d t j ƒ}  qó n  |  S(   sB  
    Helper function for scipy argument validation.

    Many scipy linear algebra functions do support arbitrary array-like
    input arguments.  Examples of commonly unsupported inputs include
    matrices containing inf/nan, sparse matrix representations, and
    matrices with complicated elements.

    Parameters
    ----------
    a : array_like
        The array-like input.
    check_finite : bool, optional
        Whether to check that the input matrices contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.
        Default: True
    sparse_ok : bool, optional
        True if scipy sparse matrices are allowed.
    objects_ok : bool, optional
        True if arrays with dype('O') are allowed.
    mask_ok : bool, optional
        True if masked arrays are allowed.
    as_inexact : bool, optional
        True to convert the input array to a np.inexact dtype.

    Returns
    -------
    ret : ndarray
        The converted validated array.

    i    Nsx   Sparse matrices are not supported by this function. Perhaps one of the scipy.sparse.linalg functions would work instead.s   masked arrays are not supportedt   Os   object arrays are not supportedR   (   t   scipy.sparset   sparset   issparseR   R   t   mat   isMaskedArrayt   asarray_chkfiniteR   R   t
   issubdtypet   inexactt   float_(	   R"   t   check_finitet	   sparse_okt
   objects_okt   mask_okt
   as_inexactt   scipyt   msgt   toarray(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyt   _asarray_validatedÀ   s"    #t   ArgSpect   argst   varargst   keywordst   defaultsc         C` sN  t  j |  ƒ } g  | j j ƒ  D]$ } | j t  j j k r | j ^ q } g  | j j ƒ  D]$ } | j t  j j k rY | j ^ qY } | r“ | d n d } g  | j j ƒ  D]$ } | j t  j j
 k r© | j ^ q© } | rã | d n d } g  | j j ƒ  D]6 } | j t  j j k rù | j | j k	 rù | j ^ qù p8d } t | | | | ƒ S(   sö  inspect.getargspec replacement using inspect.signature.

        inspect.getargspec is deprecated in python 3. This is a replacement
        based on the (new in python 3.3) `inspect.signature`.

        Parameters
        ----------
        func : callable
            A callable to inspect

        Returns
        -------
        argspec : ArgSpec(args, varargs, varkw, defaults)
            This is similar to the result of inspect.getargspec(func) under
            python 2.x.
            NOTE: if the first argument of `func` is self, it is *not*, I repeat
            *not* included in argspec.args.
            This is done for consistency between inspect.getargspec() under
            python 2.x, and inspect.signature() under python 3.x.
        i    N(   t   inspectt	   signaturet
   parameterst   valuest   kindt	   Parametert   POSITIONAL_OR_KEYWORDRY   t   VAR_POSITIONALR   t   VAR_KEYWORDR+   R;   Rz   (   R-   t   sigt   pR{   R|   t   varkwR~   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyt   getargspec_no_self  s    $$$!c         C` s9   t  j |  ƒ } | j d d k r5 | j j d ƒ n  | S(   sD  inspect.getargspec replacement for compatibility with python 3.x.

        inspect.getargspec is deprecated in python 3. This wraps it, and
        *removes* `self` from the argument list of `func`, if present.
        This is done for forward compatibility with python 3.

        Parameters
        ----------
        func : callable
            A callable to inspect

        Returns
        -------
        argspec : ArgSpec(args, varargs, varkw, defaults)
            This is similar to the result of inspect.getargspec(func) under
            python 2.x.
            NOTE: if the first argument of `func` is self, it is *not*, I repeat
            *not* included in argspec.args.
            This is done for consistency between inspect.getargspec() under
            python 2.x, and inspect.signature() under python 3.x.
        i    RO   (   R   t
   getargspecR{   t   pop(   R-   t   argspec(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyR‹   :  s    (    t
   __future__R    R   R   R6   R8   RL   RU   Ra   t   collectionsR   R   t   numpyR   R   R   R   R$   R/   t   floatRC   RG   t   objectRH   Rf   t   TrueR(   Ry   R€   Rz   R‹   t   AttributeError(    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/_lib/_util.pyt   <module>   s0   $(	
#		C+