ó
Ë½÷Xc           @` s¾   d  Z  d d l m Z m Z m Z d g Z d d l 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
 e j j f d „  ƒ  YZ d d „ Z e d d „ Z d S(   sz   
Matrix square root for general matrices and for upper triangular matrices.

This module exists to avoid cyclic imports.

i    (   t   divisiont   print_functiont   absolute_importt   sqrtmN(   t   _asarray_validatedi   (   t   norm(   t   ztrsylt   dtrsyl(   t   schurt   rsf2csft
   SqrtmErrorc           B` s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s;   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/_matfuncs_sqrtm.pyR
      s   i@   c      	   C` sž  t  j |  ƒ } t  j |  ƒ o0 t  j | ƒ d k } | sK | j t ƒ } n  t  j t  j | ƒ ƒ } |  j \ } } t | | d ƒ } t	 | | ƒ \ } } | d }	 | | }
 |
 | | |	 | k rÕ t
 d ƒ ‚ n  g  } d } x^ |
 | f | |	 f f D]D \ } } x5 t | ƒ D]' } | j | | | f ƒ | | 7} qWqú Wxø | D]ð \ } } xá t | | ƒ D]Ð } xÇ t | d | d d ƒ D]« } d } | | d k rÜ| | | d | … f j | | d | … | f ƒ } n  | | | f | | | f } | st d ƒ ‚ n  |  | | f | | | | | f <q†WqeWqIWxZt | ƒ D]L} | | \ } } x3t | d d d ƒ D]} | | \ } } |  | | … | | … f } | | d k rû| | | | … | | … f j | | | … | | … f ƒ } n  | | | … | | … f } | | | … | | … f } | rWt | | | ƒ \ } } } n t | | | ƒ \ } } } | | | | | … | | … f <qwWqJW| S(   sÜ  
    Matrix square root of an upper triangular matrix.

    This is a helper function for `sqrtm` and `logm`.

    Parameters
    ----------
    T : (N, N) array_like upper triangular
        Matrix whose square root to evaluate
    blocksize : int, optional
        If the blocksize is not degenerate with respect to the
        size of the input array, then use a blocked algorithm. (Default: 64)

    Returns
    -------
    sqrtm : (N, N) ndarray
        Value of the sqrt function at `T`

    References
    ----------
    .. [1] Edvin Deadman, Nicholas J. Higham, Rui Ralha (2013)
           "Blocked Schur Algorithms for Computing the Matrix Square Root,
           Lecture Notes in Computer Science, 7782. pp. 171-182.

    i    i   s   internal inconsistencyiÿÿÿÿs%   failed to find the matrix square root(   t   npt   diagt	   isrealobjt   mint   astypet   complext   sqrtt   shapet   maxt   divmodt	   Exceptiont   ranget   appendt   dotR
   R   R   (   t   Tt	   blocksizet   T_diagt   keep_it_realt   Rt   nt   nblockst   bsmallt   nlarget   blarget   nsmallt   start_stop_pairst   startt   countt   sizet   it   stopt   jt   st   denomt   jstartt   jstopt   istartt   istopt   St   Riit   Rjjt   xt   scalet   info(    (    s;   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/_matfuncs_sqrtm.pyt   _sqrtm_triu   sV    $

%!:.+(c         C` sõ  t  |  d t d t ƒ}  t |  j ƒ d k r< t d ƒ ‚ n  | d k  rW t d ƒ ‚ n  t j |  ƒ } | r´ t |  ƒ \ } } t j | t j	 | ƒ ƒ sÌ t
 | | ƒ \ } } qÌ n t |  d d ƒ\ } } t } y@ t | d	 | ƒ} t j | ƒ j } | j | ƒ j | ƒ }	 Wn6 t k
 rJt } t j |  ƒ }	 |	 j t j ƒ n X| r™t j t j | ƒ d
 k ƒ }
 |
 r‚t d ƒ n | r•t d ƒ n  |	 Sy1 t |	 j |	 ƒ |  d ƒ d t |  d ƒ } Wn t k
 ræt j } n X|	 | f Sd S(   s¤  
    Matrix square root.

    Parameters
    ----------
    A : (N, N) array_like
        Matrix whose square root to evaluate
    disp : bool, optional
        Print warning if error in the result is estimated large
        instead of returning estimated error. (Default: True)
    blocksize : integer, optional
        If the blocksize is not degenerate with respect to the
        size of the input array, then use a blocked algorithm. (Default: 64)

    Returns
    -------
    sqrtm : (N, N) ndarray
        Value of the sqrt function at `A`

    errest : float
        (if disp == False)

        Frobenius norm of the estimated error, ||err||_F / ||A||_F

    References
    ----------
    .. [1] Edvin Deadman, Nicholas J. Higham, Rui Ralha (2013)
           "Blocked Schur Algorithms for Computing the Matrix Square Root,
           Lecture Notes in Computer Science, 7782. pp. 171-182.

    Examples
    --------
    >>> from scipy.linalg import sqrtm
    >>> a = np.array([[1.0, 3.0], [1.0, 4.0]])
    >>> r = sqrtm(a)
    >>> r
    array([[ 0.75592895,  1.13389342],
           [ 0.37796447,  1.88982237]])
    >>> r.dot(r)
    array([[ 1.,  3.],
           [ 1.,  4.]])

    t   check_finitet
   as_inexacti   s$   Non-matrix input to matrix function.i   s#   The blocksize should be at least 1.t   outputR   R   i    s2   Matrix is singular and may not have a square root.s   Failed to find a square root.t   froN(   R   t   Truet   lenR   t
   ValueErrorR   R   R   t   array_equalt   triuR	   t   FalseR9   t	   conjugateR   R   R
   t
   empty_liket   fillt   nant   anyR   t   printR   t   inf(   t   At   dispR   R   R   t   Zt   failflagR   t   ZHt   Xt   nzeigt   arg2(    (    s;   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/_matfuncs_sqrtm.pyR   s   s@    ,1(   t   __doc__t
   __future__R    R   R   t   __all__t   numpyR   t   scipy._lib._utilR   t   miscR   t   lapackR   R   t   decomp_schurR   R	   t   linalgt   LinAlgErrorR
   R9   R>   R   (    (    (    s;   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/_matfuncs_sqrtm.pyt   <module>   s   	Y