ó
î&]\c           @` s¬  d  Z  d d l m Z m Z m Z d d l Z d d l m Z m Z m Z m	 Z	 d d l
 m 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 g Z d d d g Z d d e d e d „ Z e j e ƒ j Z e j e ƒ j Z i	 d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6Z i d d 6d d 6d d 6d d 6d d 6d d 6Z  d d g d d g g Z! d „  Z" d „  Z# e d „ Z$ d S(   s   Schur decomposition functions.i    (   t   divisiont   print_functiont   absolute_importN(   t   asarray_chkfinitet   singlet   asarrayt   array(   t   norm(   t   callablei   (   t   LinAlgErrort   _datacopied(   t   get_lapack_funcs(   t   eigvalst   schurt   rsf2csft   it   lt   dt   realc      	   C` së  | d# k r t  d ƒ ‚ n  | r0 t |  ƒ } n t |  ƒ } t | j ƒ d k sk | j d | j d k rz t  d	 ƒ ‚ n  | j j } | d$ k rÚ | d% k rÚ | t k rÂ | j d ƒ } d } qÚ | j d
 ƒ } d
 } n  | pì t	 | |  ƒ } t
 d& | f ƒ \ } | d" k s| d k rT| d „  | d d ƒ}	 |	 d d j j t j ƒ } n  | d" k rrd }
 d „  } n‡ d }
 t | ƒ r| } nl | d k r¥d „  } nT | d k r½d „  } n< | d k rÕd „  } n$ | d k ríd „  } n t  d ƒ ‚ | | | d | d | d |
 ƒ}	 |	 d } | d k  rIt  d j | ƒ ƒ ‚ ng | | j d d k rot d ƒ ‚ nA | | j d d k r•t d ƒ ‚ n | d k r°t d  ƒ ‚ n  |
 d k rÎ|	 d |	 d! f S|	 d |	 d! |	 d f Sd" S('   sÅ  
    Compute Schur decomposition of a matrix.

    The Schur decomposition is::

        A = Z T Z^H

    where Z is unitary and T is either upper-triangular, or for real
    Schur decomposition (output='real'), quasi-upper triangular.  In
    the quasi-triangular form, 2x2 blocks describing complex-valued
    eigenvalue pairs may extrude from the diagonal.

    Parameters
    ----------
    a : (M, M) array_like
        Matrix to decompose
    output : {'real', 'complex'}, optional
        Construct the real or complex Schur decomposition (for real matrices).
    lwork : int, optional
        Work array size. If None or -1, it is automatically computed.
    overwrite_a : bool, optional
        Whether to overwrite data in a (may improve performance).
    sort : {None, callable, 'lhp', 'rhp', 'iuc', 'ouc'}, optional
        Specifies whether the upper eigenvalues should be sorted.  A callable
        may be passed that, given a eigenvalue, returns a boolean denoting
        whether the eigenvalue should be sorted to the top-left (True).
        Alternatively, string parameters may be used::

            'lhp'   Left-hand plane (x.real < 0.0)
            'rhp'   Right-hand plane (x.real > 0.0)
            'iuc'   Inside the unit circle (x*x.conjugate() <= 1.0)
            'ouc'   Outside the unit circle (x*x.conjugate() > 1.0)

        Defaults to None (no sorting).
    check_finite : bool, optional
        Whether to check that the input matrix contains 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.

    Returns
    -------
    T : (M, M) ndarray
        Schur form of A. It is real-valued for the real Schur decomposition.
    Z : (M, M) ndarray
        An unitary Schur transformation matrix for A.
        It is real-valued for the real Schur decomposition.
    sdim : int
        If and only if sorting was requested, a third return value will
        contain the number of eigenvalues satisfying the sort condition.

    Raises
    ------
    LinAlgError
        Error raised under three conditions:

        1. The algorithm failed due to a failure of the QR algorithm to
           compute all eigenvalues
        2. If eigenvalue sorting was requested, the eigenvalues could not be
           reordered due to a failure to separate eigenvalues, usually because
           of poor conditioning
        3. If eigenvalue sorting was requested, roundoff errors caused the
           leading eigenvalues to no longer satisfy the sorting condition

    See also
    --------
    rsf2csf : Convert real Schur form to complex Schur form

    Examples
    --------
    >>> from scipy.linalg import schur, eigvals
    >>> A = np.array([[0, 2, 2], [0, 1, 2], [1, 0, 1]])
    >>> T, Z = schur(A)
    >>> T
    array([[ 2.65896708,  1.42440458, -1.92933439],
           [ 0.        , -0.32948354, -0.49063704],
           [ 0.        ,  1.31178921, -0.32948354]])
    >>> Z
    array([[0.72711591, -0.60156188, 0.33079564],
           [0.52839428, 0.79801892, 0.28976765],
           [0.43829436, 0.03590414, -0.89811411]])

    >>> T2, Z2 = schur(A, output='complex')
    >>> T2
    array([[ 2.65896708, -1.22839825+1.32378589j,  0.42590089+1.51937378j],
           [ 0.        , -0.32948354+0.80225456j, -0.59877807+0.56192146j],
           [ 0.        ,  0.                    , -0.32948354-0.80225456j]])
    >>> eigvals(T2)
    array([2.65896708, -0.32948354+0.80225456j, -0.32948354-0.80225456j])

    An arbitrary custom eig-sorting condition, having positive imaginary part, 
    which is satisfied by only one eigenvalue

    >>> T3, Z3, sdim = schur(A, output='complex', sort=lambda x: x.imag > 0)
    >>> sdim
    1

    R   t   complext   rt   cs%   argument must be 'real', or 'complex'i   i    i   s   expected square matrixt   Ft   Dt   geesiÿÿÿÿc         S` s   d  S(   N(   t   None(   t   x(    (    s8   lib/python2.7/site-packages/scipy/linalg/decomp_schur.pyt   <lambda>‹   s    t   lworkiþÿÿÿc         S` s   d  S(   N(   R   (   R   (    (    s8   lib/python2.7/site-packages/scipy/linalg/decomp_schur.pyR      s    t   lhpc         S` s   |  j  d k  S(   Ng        (   R   (   R   (    (    s8   lib/python2.7/site-packages/scipy/linalg/decomp_schur.pyR   –   s    t   rhpc         S` s   |  j  d k S(   Ng        (   R   (   R   (    (    s8   lib/python2.7/site-packages/scipy/linalg/decomp_schur.pyR   ˜   s    t   iucc         S` s   t  |  ƒ d k S(   Ng      ð?(   t   abs(   R   (    (    s8   lib/python2.7/site-packages/scipy/linalg/decomp_schur.pyR   š   s    t   oucc         S` s   t  |  ƒ d k S(   Ng      ð?(   R    (   R   (    (    s8   lib/python2.7/site-packages/scipy/linalg/decomp_schur.pyR   œ   s    sZ   'sort' parameter must either be 'None', or a callable, or one of ('lhp','rhp','iuc','ouc')t   overwrite_at   sort_ts0   illegal value in {}-th argument of internal geess2   Eigenvalues could not be separated for reordering.s2   Leading eigenvalues do not satisfy sort condition.s0   Schur form not found.  Possibly ill-conditioned.iýÿÿÿN(   R   R   R   R   (   R   R   (   R   R   (   R   (   t
   ValueErrorR   R   t   lent   shapet   dtypet   chart   _double_precisiont   astypeR
   R   R   R   t   numpyt   intR   t   formatR	   (   t   at   outputR   R"   t   sortt   check_finitet   a1t   typR   t   resultR#   t	   sfunctiont   info(    (    s8   lib/python2.7/site-packages/scipy/linalg/decomp_schur.pyR      sb    c/		 		
	t   bt   ht   Bt   fR   R   c          G` s[   d } d } x@ |  D]8 } | j  j } t | t | ƒ } t | t | ƒ } q Wt | | S(   Ni    (   R'   R(   t   maxt   _array_kindt   _array_precisiont   _array_type(   t   arrayst   kindt	   precisionR.   t   t(    (    s8   lib/python2.7/site-packages/scipy/linalg/decomp_schur.pyt   _commonType¾   s    c         G` sw   d } xL | D]D } | j  j |  k r; | | j ƒ  f } q | | j |  ƒ f } q Wt | ƒ d k ro | d S| Sd  S(   Ni   i    (    (   R'   R(   t   copyR*   R%   (   t   typeR?   t   cast_arraysR.   (    (    s8   lib/python2.7/site-packages/scipy/linalg/decomp_schur.pyt	   _castCopyÈ   s    c         C` so  | r$ t  t | |  f ƒ \ } }  n t  t | |  f ƒ \ } }  xe t | |  g ƒ D]Q \ } } | j d k s‡ | j d | j d k rR t d j d | ƒ ƒ ‚ qR qR W|  j d | j d k râ t d j | j |  j ƒ ƒ ‚ n  |  j d } t | |  t	 d g d ƒ ƒ } t
 | | |  ƒ \ } }  x=t | d d d	 ƒ D]%} t |  | | d f ƒ t t |  | d | d f ƒ t |  | | f ƒ k rMt |  | d | d … | d | d … f ƒ |  | | f } t | d |  | | d f g ƒ }	 | d |	 }
 |  | | d f |	 } t	 |
 j ƒ  | g | |
 g g d
 | ƒ} | j |  | d | d … | d d … f ƒ |  | d | d … | d d … f <|  d | d … | d | d … f j | j ƒ  j ƒ |  d | d … | d | d … f <| d d … | d | d … f j | j ƒ  j ƒ | d d … | d | d … f <n  d |  | | d f <q<W|  | f S(   s6  
    Convert real Schur form to complex Schur form.

    Convert a quasi-diagonal real-valued Schur form to the upper triangular
    complex-valued Schur form.

    Parameters
    ----------
    T : (M, M) array_like
        Real Schur form of the original array
    Z : (M, M) array_like
        Schur transformation matrix
    check_finite : bool, optional
        Whether to check that the input arrays 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.

    Returns
    -------
    T : (M, M) ndarray
        Complex Schur form of the original array
    Z : (M, M) ndarray
        Schur transformation matrix corresponding to the complex form

    See Also
    --------
    schur : Schur decomposition of an array

    Examples
    --------
    >>> from scipy.linalg import schur, rsf2csf
    >>> A = np.array([[0, 2, 2], [0, 1, 2], [1, 0, 1]])
    >>> T, Z = schur(A)
    >>> T
    array([[ 2.65896708,  1.42440458, -1.92933439],
           [ 0.        , -0.32948354, -0.49063704],
           [ 0.        ,  1.31178921, -0.32948354]])
    >>> Z
    array([[0.72711591, -0.60156188, 0.33079564],
           [0.52839428, 0.79801892, 0.28976765],
           [0.43829436, 0.03590414, -0.89811411]])
    >>> T2 , Z2 = rsf2csf(T, Z)
    >>> T2
    array([[2.65896708+0.j, -1.64592781+0.743164187j, -1.21516887+1.00660462j],
           [0.+0.j , -0.32948354+8.02254558e-01j, -0.82115218-2.77555756e-17j],
           [0.+0.j , 0.+0.j, -0.32948354-0.802254558j]])
    >>> Z2
    array([[0.72711591+0.j,  0.28220393-0.31385693j,  0.51319638-0.17258824j],
           [0.52839428+0.j,  0.24720268+0.41635578j, -0.68079517-0.15118243j],
           [0.43829436+0.j, -0.76618703+0.01873251j, -0.03063006+0.46857912j]])

    i   i    i   s   Input '{}' must be square.t   ZTs.   Input array shapes must match: Z: {} vs. T: {}g      @R   iÿÿÿÿR'   Ng        (   t   mapR   R   t	   enumeratet   ndimR&   R$   R-   RC   R   RG   t   rangeR    t   epsR   R   t   conjt   dott   T(   RP   t   ZR1   t   indt   Xt   NRB   t   mt   muR   R   t   st   G(    (    s8   lib/python2.7/site-packages/scipy/linalg/decomp_schur.pyR   Õ   s0    5) 	P@$+S\W(%   t   __doc__t
   __future__R    R   R   R+   R   R   R   R   t   numpy.linalgR   t   scipy._lib.sixR   t   miscR	   R
   t   lapackR   t   decompR   t   __all__R)   R   t   Falset   TrueR   t   finfot   floatRM   t   fepsR<   R=   R>   RC   RG   R   (    (    (    s8   lib/python2.7/site-packages/scipy/linalg/decomp_schur.pyt   <module>   s*   " &0	
	