σ
Λ½χXc           @` s{  d  d l  m Z m Z m Z d d d d d d d d	 d
 d d d d d d d g Z d  d l m Z m Z m Z m	 Z	 m
 Z
 m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d  d l Z d  d l Z d d l m Z d d l m Z m Z d d l m Z d d l m  Z  d d l! m" Z" d d l# m$ Z$ m% Z% d d l& m' Z' m( Z( d d l) m* Z* e j+ e,  j- Z- e j+ e  j- Z. i d d 6d d 6d  d 6d d  6d  d! 6d d" 6Z/ d#   Z0 d d$  Z2 d%   Z3 e4 d&  Z5 d d'  Z6 e j7 d( d  d)    Z8 e j7 d( d  d* d+   Z9 d,   Z: d-   Z; d.   Z< d/   Z= d0   Z> d1   Z? e4 d2  Z@ e4 d3  ZA d S(4   i    (   t   divisiont   print_functiont   absolute_importt   expmt   expm2t   expm3t   cosmt   sinmt   tanmt   coshmt   sinhmt   tanhmt   logmt   funmt   signmt   sqrtmt   expm_frechett	   expm_condt   fractional_matrix_power(   t   Inft   dott   diagt   expt   productt   logical_nott   castt   ravelt	   transposet	   conjugatet   absolutet   amaxt   signt   isfinitet   sqrtt   singleNi   (   t   norm(   t   solvet   inv(   t   triu(   t   eig(   t   svd(   t   schurt   rsf2csf(   R   R   (   R   t   it   lt   ft   dt   Ft   Dc         C` sQ   t  j |   }  t |  j  d k s> |  j d |  j d k rM t d   n  |  S(   s  
    Wraps asarray with the extra requirement that the input be a square matrix.

    The motivation is that the matfuncs module has real functions that have
    been lifted to square matrix functions.

    Parameters
    ----------
    A : array_like
        A square matrix.

    Returns
    -------
    out : ndarray
        An ndarray copy or view or other representation of A.

    i   i    i   s    expected square array_like input(   t   npt   asarrayt   lent   shapet
   ValueError(   t   A(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyt   _asarray_square$   s    /c         C` s   t  j |   r t  j |  r | d k rW i t d d 6t d d 6t | j j } n  t  j	 | j
 d d | r | j } q n  | S(   s(  
    Return either B or the real part of B, depending on properties of A and B.

    The motivation is that B has been computed as a complicated function of A,
    and B may be perturbed by negligible imaginary components.
    If A is real and B is complex with small imaginary components,
    then return a real copy of B.  The assumption in that case would be that
    the imaginary components of B are numerical artifacts.

    Parameters
    ----------
    A : ndarray
        Input array whose type is to be checked as real vs. complex.
    B : ndarray
        Array to be returned, possibly without its imaginary part.
    tol : float
        Absolute tolerance.

    Returns
    -------
    out : real or complex array
        Either the input array B or only the real part of the input array B.

    g     @@i    g    .Ai   g        t   atolN(   R1   t	   isrealobjt   iscomplexobjt   Nonet   fepst   epst   _array_precisiont   dtypet   chart   allcloset   imagt   real(   R6   t   Bt   tol(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyt   _maybe_real<   s    -c         C` s.   t  |   }  d d l } | j j j |  |  S(   sΧ  
    Compute the fractional power of a matrix.

    Proceeds according to the discussion in section (6) of [1]_.

    Parameters
    ----------
    A : (N, N) array_like
        Matrix whose fractional power to evaluate.
    t : float
        Fractional power.

    Returns
    -------
    X : (N, N) array_like
        The fractional power of the matrix.

    References
    ----------
    .. [1] Nicholas J. Higham and Lijing lin (2011)
           "A Schur-Pade Algorithm for Fractional Powers of a Matrix."
           SIAM Journal on Matrix Analysis and Applications,
           32 (3). pp. 1056-1078. ISSN 0895-4798

    Examples
    --------
    >>> from scipy.linalg import fractional_matrix_power
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> b = fractional_matrix_power(a, 0.5)
    >>> b
    array([[ 0.75592895,  1.13389342],
           [ 0.37796447,  1.88982237]])
    >>> np.dot(b, b)      # Verify square root
    array([[ 1.,  3.],
           [ 1.,  4.]])

    i    N(   R7   t   scipy.linalg._matfuncs_inv_ssqt   linalgt   _matfuncs_inv_ssqt   _fractional_matrix_power(   R6   t   tt   scipy(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyR   b   s    (c         C` s­   t  |   }  d d l } | j j j |   } t |  |  } d t } t t |  |  d  t |  d  } | r t	 |  s | | k r t
 d |  n  | S| | f Sd S(   sϋ  
    Compute matrix logarithm.

    The matrix logarithm is the inverse of
    expm: expm(logm(`A`)) == `A`

    Parameters
    ----------
    A : (N, N) array_like
        Matrix whose logarithm to evaluate
    disp : bool, optional
        Print warning if error in the result is estimated large
        instead of returning estimated error. (Default: True)

    Returns
    -------
    logm : (N, N) ndarray
        Matrix logarithm of `A`
    errest : float
        (if disp == False)

        1-norm of the estimated error, ||err||_1 / ||A||_1

    References
    ----------
    .. [1] Awad H. Al-Mohy and Nicholas J. Higham (2012)
           "Improved Inverse Scaling and Squaring Algorithms
           for the Matrix Logarithm."
           SIAM Journal on Scientific Computing, 34 (4). C152-C169.
           ISSN 1095-7197

    .. [2] Nicholas J. Higham (2008)
           "Functions of Matrices: Theory and Computation"
           ISBN 978-0-898716-46-7

    .. [3] Nicholas J. Higham and Lijing lin (2011)
           "A Schur-Pade Algorithm for Fractional Powers of a Matrix."
           SIAM Journal on Matrix Analysis and Applications,
           32 (3). pp. 1056-1078. ISSN 0895-4798

    Examples
    --------
    >>> from scipy.linalg import logm, expm
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> b = logm(a)
    >>> b
    array([[-1.02571087,  2.05142174],
           [ 0.68380725,  1.02571087]])
    >>> expm(b)         # Verify expm(logm(a)) returns a
    array([[ 1.,  3.],
           [ 1.,  4.]])

    i    Niθ  i   s0   logm result may be inaccurate, approximate err =(   R7   RG   RH   RI   t   _logmRF   R=   R#   R   R    t   print(   R6   t   dispRL   R/   t   errtolt   errest(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyR      s    6
&c         C` sD   | d k	 r% d } t j | t  n  d d l } | j j j |   S(   s  
    Compute the matrix exponential using Pade approximation.

    Parameters
    ----------
    A : (N, N) array_like or sparse matrix
        Matrix to be exponentiated.

    Returns
    -------
    expm : (N, N) ndarray
        Matrix exponential of `A`.

    References
    ----------
    .. [1] Awad H. Al-Mohy and Nicholas J. Higham (2009)
           "A New Scaling and Squaring Algorithm for the Matrix Exponential."
           SIAM Journal on Matrix Analysis and Applications.
           31 (3). pp. 970-989. ISSN 1095-7162

    Examples
    --------
    >>> from scipy.linalg import expm, sinm, cosm

    Matrix version of the formula exp(0) = 1:

    >>> expm(np.zeros((2,2)))
    array([[ 1.,  0.],
           [ 0.,  1.]])

    Euler's identity (exp(i*theta) = cos(theta) + i*sin(theta))
    applied to a matrix:

    >>> a = np.array([[1.0, 2.0], [-1.0, 3.0]])
    >>> expm(1j*a)
    array([[ 0.42645930+1.89217551j, -2.13721484-0.97811252j],
           [ 1.06860742+0.48905626j, -1.71075555+0.91406299j]])
    >>> cosm(a) + 1j*sinm(a)
    array([[ 0.42645930+1.89217551j, -2.13721484-0.97811252j],
           [ 1.06860742+0.48905626j, -1.71075555+0.91406299j]])

    s2   argument q=... in scipy.linalg.expm is deprecated.i    N(   R;   t   warningst   warnt   DeprecationWarningt   scipy.sparse.linalgt   sparseRH   R   (   R6   t   qt   msgRL   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyR   Υ   s
    +t   new_namec         C` s«   t  |   }  |  j j } | d k r< |  j d  }  d } n  t |   \ } } t |  } t t | t t |    |  } | d k r | j	 j |  S| j |  Sd S(   sϋ   
    Compute the matrix exponential using eigenvalue decomposition.

    Parameters
    ----------
    A : (N, N) array_like
        Matrix to be exponentiated

    Returns
    -------
    expm2 : (N, N) ndarray
        Matrix exponential of `A`

    R-   R/   R.   R0   N(   R-   R/   R.   R0   (   R-   R.   (
   R7   R?   R@   t   astypeR'   R%   R   R   R   RC   (   R6   RK   t   st   vrt   vrit   r(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyR   	  s    	$i   c         C` sΏ   t  |   }  |  j d } |  j j } | d k rI |  j d  }  d } n  t j | d | } t j | d | } t | } x; t d |  D]* } | j	 |   | |  | (| | 7} q W| S(	   s-  
    Compute the matrix exponential using Taylor series.

    Parameters
    ----------
    A : (N, N) array_like
        Matrix to be exponentiated
    q : int
        Order of the Taylor series used is `q-1`

    Returns
    -------
    expm3 : (N, N) ndarray
        Matrix exponential of `A`

    i    R-   R/   R.   R0   R?   i   (   R-   R/   R.   R0   (
   R7   R4   R?   R@   RZ   R1   t   identityR   t   rangeR   (   R6   RW   t   nRK   t   eAt   trmt   castfunct   k(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyR   (  s    	
c         C` sP   t  |   }  t j |   r; d t d |   t d |   St d |   j Sd S(   s  
    Compute the matrix cosine.

    This routine uses expm to compute the matrix exponentials.

    Parameters
    ----------
    A : (N, N) array_like
        Input array

    Returns
    -------
    cosm : (N, N) ndarray
        Matrix cosine of A

    Examples
    --------
    >>> from scipy.linalg import expm, sinm, cosm

    Euler's identity (exp(i*theta) = cos(theta) + i*sin(theta))
    applied to a matrix:

    >>> a = np.array([[1.0, 2.0], [-1.0, 3.0]])
    >>> expm(1j*a)
    array([[ 0.42645930+1.89217551j, -2.13721484-0.97811252j],
           [ 1.06860742+0.48905626j, -1.71075555+0.91406299j]])
    >>> cosm(a) + 1j*sinm(a)
    array([[ 0.42645930+1.89217551j, -2.13721484-0.97811252j],
           [ 1.06860742+0.48905626j, -1.71075555+0.91406299j]])

    g      ΰ?y              π?y              πΏN(   R7   R1   R:   R   RC   (   R6   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyR   I  s      c         C` sP   t  |   }  t j |   r; d t d |   t d |   St d |   j Sd S(   s  
    Compute the matrix sine.

    This routine uses expm to compute the matrix exponentials.

    Parameters
    ----------
    A : (N, N) array_like
        Input array.

    Returns
    -------
    sinm : (N, N) ndarray
        Matrix sine of `A`

    Examples
    --------
    >>> from scipy.linalg import expm, sinm, cosm

    Euler's identity (exp(i*theta) = cos(theta) + i*sin(theta))
    applied to a matrix:

    >>> a = np.array([[1.0, 2.0], [-1.0, 3.0]])
    >>> expm(1j*a)
    array([[ 0.42645930+1.89217551j, -2.13721484-0.97811252j],
           [ 1.06860742+0.48905626j, -1.71075555+0.91406299j]])
    >>> cosm(a) + 1j*sinm(a)
    array([[ 0.42645930+1.89217551j, -2.13721484-0.97811252j],
           [ 1.06860742+0.48905626j, -1.71075555+0.91406299j]])

    y              ΰΏy              π?y              πΏN(   R7   R1   R:   R   RB   (   R6   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyR   p  s      c         C` s.   t  |   }  t |  t t |   t |     S(   sΎ  
    Compute the matrix tangent.

    This routine uses expm to compute the matrix exponentials.

    Parameters
    ----------
    A : (N, N) array_like
        Input array.

    Returns
    -------
    tanm : (N, N) ndarray
        Matrix tangent of `A`

    Examples
    --------
    >>> from scipy.linalg import tanm, sinm, cosm
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> t = tanm(a)
    >>> t
    array([[ -2.00876993,  -8.41880636],
           [ -2.80626879, -10.42757629]])

    Verify tanm(a) = sinm(a).dot(inv(cosm(a)))

    >>> s = sinm(a)
    >>> c = cosm(a)
    >>> s.dot(np.linalg.inv(c))
    array([[ -2.00876993,  -8.41880636],
           [ -2.80626879, -10.42757629]])

    (   R7   RF   R$   R   R   (   R6   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyR     s    "c         C` s.   t  |   }  t |  d t |   t |    S(   sπ  
    Compute the hyperbolic matrix cosine.

    This routine uses expm to compute the matrix exponentials.

    Parameters
    ----------
    A : (N, N) array_like
        Input array.

    Returns
    -------
    coshm : (N, N) ndarray
        Hyperbolic matrix cosine of `A`

    Examples
    --------
    >>> from scipy.linalg import tanhm, sinhm, coshm
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> c = coshm(a)
    >>> c
    array([[ 11.24592233,  38.76236492],
           [ 12.92078831,  50.00828725]])

    Verify tanhm(a) = sinhm(a).dot(inv(coshm(a)))

    >>> t = tanhm(a)
    >>> s = sinhm(a)
    >>> t - s.dot(np.linalg.inv(c))
    array([[  2.72004641e-15,   4.55191440e-15],
           [  0.00000000e+00,  -5.55111512e-16]])

    g      ΰ?(   R7   RF   R   (   R6   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyR	   ½  s    "c         C` s.   t  |   }  t |  d t |   t |    S(   sμ  
    Compute the hyperbolic matrix sine.

    This routine uses expm to compute the matrix exponentials.

    Parameters
    ----------
    A : (N, N) array_like
        Input array.

    Returns
    -------
    sinhm : (N, N) ndarray
        Hyperbolic matrix sine of `A`

    Examples
    --------
    >>> from scipy.linalg import tanhm, sinhm, coshm
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> s = sinhm(a)
    >>> s
    array([[ 10.57300653,  39.28826594],
           [ 13.09608865,  49.86127247]])

    Verify tanhm(a) = sinhm(a).dot(inv(coshm(a)))

    >>> t = tanhm(a)
    >>> c = coshm(a)
    >>> t - s.dot(np.linalg.inv(c))
    array([[  2.72004641e-15,   4.55191440e-15],
           [  0.00000000e+00,  -5.55111512e-16]])

    g      ΰ?(   R7   RF   R   (   R6   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyR
   γ  s    "c         C` s.   t  |   }  t |  t t |   t |     S(   sν  
    Compute the hyperbolic matrix tangent.

    This routine uses expm to compute the matrix exponentials.

    Parameters
    ----------
    A : (N, N) array_like
        Input array

    Returns
    -------
    tanhm : (N, N) ndarray
        Hyperbolic matrix tangent of `A`

    Examples
    --------
    >>> from scipy.linalg import tanhm, sinhm, coshm
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> t = tanhm(a)
    >>> t
    array([[ 0.3428582 ,  0.51987926],
           [ 0.17329309,  0.86273746]])

    Verify tanhm(a) = sinhm(a).dot(inv(coshm(a)))

    >>> s = sinhm(a)
    >>> c = coshm(a)
    >>> t - s.dot(np.linalg.inv(c))
    array([[  2.72004641e-15,   4.55191440e-15],
           [  0.00000000e+00,  -5.55111512e-16]])

    (   R7   RF   R$   R	   R
   (   R6   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyR   	  s    "c      	   C` sο  t  |   }  t |   \ } } t | |  \ } } | j \ } } t | t |    } | j | j j  } t | d  } xlt	 d |  D][} xRt	 d | | d  D]9}	 |	 | }
 | |	 d |
 d f | |
 d |
 d f | |	 d |	 d f } t
 |	 |
 d  } t | |	 d | f | | |
 d f  t | |	 d | f | | |
 d f  } | | } | |
 d |
 d f | |	 d |	 d f } | d k rΉ| | } n  | | |	 d |
 d f <t | t |   } q­ Wq Wt t | |  t t |    } t |  |  } i t d 6t d 6t | j j } | d k rX| } n  t d t | | | t t | d  d    } t t t t |    d d r·t } n  | rα| d | k rέt d |  n  | S| | f Sd S(	   s{  
    Evaluate a matrix function specified by a callable.

    Returns the value of matrix-valued function ``f`` at `A`. The
    function ``f`` is an extension of the scalar-valued function `func`
    to matrices.

    Parameters
    ----------
    A : (N, N) array_like
        Matrix at which to evaluate the function
    func : callable
        Callable object that evaluates a scalar function f.
        Must be vectorized (eg. using vectorize).
    disp : bool, optional
        Print warning if error in the result is estimated large
        instead of returning estimated error. (Default: True)

    Returns
    -------
    funm : (N, N) ndarray
        Value of the matrix function specified by func evaluated at `A`
    errest : float
        (if disp == False)

        1-norm of the estimated error, ||err||_1 / ||A||_1

    Examples
    --------
    >>> from scipy.linalg import funm
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> funm(a, lambda x: x*x)
    array([[  4.,  15.],
           [  5.,  19.]])
    >>> a.dot(a)
    array([[  4.,  15.],
           [  5.,  19.]])

    Notes
    -----
    This function implements the general algorithm based on Schur decomposition
    (Algorithm 9.1.1. in [1]_).

    If the input matrix is known to be diagonalizable, then relying on the
    eigendecomposition is likely to be faster. For example, if your matrix is
    Hermitian, you can do

    >>> from scipy.linalg import eigh
    >>> def funm_herm(a, func, check_finite=False):
    ...     w, v = eigh(a, check_finite=check_finite)
    ...     ## if you further know that your matrix is positive semidefinite,
    ...     ## you can optionally guard against precision errors by doing
    ...     # w = np.maximum(w, 0)
    ...     w = func(w)
    ...     return (v * w).dot(v.conj().T)

    References
    ----------
    .. [1] Gene H. Golub, Charles F. van Loan, Matrix Computations 4th ed.

    i    i   g        t   axisiθ  s0   funm result may be inaccurate, approximate err =N(   i    i    (   R7   R)   R*   R4   R   RZ   R?   R@   t   absR`   t   sliceR   t   minR   R   RF   R<   R=   R>   t   maxR#   R&   R   R   R   R    R   RN   (   R6   t   funcRO   t   Tt   ZRa   R/   t   mindent   pR+   t   jR[   t   kslt   valt   denRE   t   err(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyR   /  s@    >
DT
.$"	2$	c         C` s  t  |   }  d   } t |  | d d \ } } i d t d 6d t d 6t | j j } | | k  rj | St |  d d } t j	 |  } d | } |  | t j
 |  j d  }	 | }
 x t d  D]u } t |	  } d |	 | }	 d t |	 |	  |	 } t t | |  | d  } | | k  s1|
 | k r5Pn  | }
 qΖ W| rrt |  s^| | k rnt d	 |  n  |	 S|	 | f Sd
 S(   s'  
    Matrix sign function.

    Extension of the scalar sign(x) to matrices.

    Parameters
    ----------
    A : (N, N) array_like
        Matrix at which to evaluate the sign function
    disp : bool, optional
        Print warning if error in the result is estimated large
        instead of returning estimated error. (Default: True)

    Returns
    -------
    signm : (N, N) ndarray
        Value of the sign function at `A`
    errest : float
        (if disp == False)

        1-norm of the estimated error, ||err||_1 / ||A||_1

    Examples
    --------
    >>> from scipy.linalg import signm, eigvals
    >>> a = [[1,2,3], [1,2,1], [1,1,1]]
    >>> eigvals(a)
    array([ 4.12488542+0.j, -0.76155718+0.j,  0.63667176+0.j])
    >>> eigvals(signm(a))
    array([-1.+0.j,  1.+0.j,  1.+0.j])

    c         S` sf   t  j |   } | j j d k r8 d t t |   } n d t t |   } t t |  | k |  S(   NR-   g     @@(	   R1   RC   R?   R@   R<   R   R=   R   R   (   t   xt   rxt   c(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyt   rounded_signΊ  s
    RO   i    g     @@i   t
   compute_uvg      ΰ?id   s1   signm result may be inaccurate, approximate err =N(   R7   R   R<   R=   R>   R?   R@   R(   R1   R   R_   R4   R`   R%   R   R#   R    RN   (   R6   RO   Rx   t   resultRQ   RP   t   valst   max_svRw   t   S0t   prev_errestR+   t   iS0t   Pp(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyR     s0    !	*

(B   t
   __future__R    R   R   t   __all__t   numpyR   R   R   R   R   R   R   R   R   R   R   R   R   R    R!   R"   R1   RR   t   miscR#   t   basicR$   R%   t   special_matricesR&   t   decompR'   t
   decomp_svdR(   t   decomp_schurR)   R*   t   _expm_frechetR   R   t   _matfuncs_sqrtmR   t   finfot   floatR=   R<   R>   R7   R;   RF   R   t   TrueR   R   t	   deprecateR   R   R   R   R   R	   R
   R   R   R   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/matfuncs.pyt   <module>   sB   j0	&	-F4 	'	'	&	&	&	&h