
    Ug)              
          d 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
mZ ddlmZ ddlmZ d	d
gZg dZ	 	 ddZ ej        e          j        Z ej        e          j        Zdddddddddd	ZdddddddZddgddggZd Zd ZddZdS )zSchur decomposition functions.    N)asarray_chkfinitesingleasarrayarray)norm   )LinAlgError_datacopied)get_lapack_funcs)eigvalsschurrsf2csf)ildrealFTc                 B   |dvrt          d          |rt          |           }nt          |           }t          j        |j        t          j                  r#t          | t          j        d                    }t          |j                  dk    s|j        d         |j        d         k    rt          d          |j        j	        }|d	v r8|d
vr4|t          v r|                    d          }n|                    d          }|j        dk    rt          t          j        d|j                            \  }}	|6t          j        ||j                  t          j        ||	j                  fS t          j        ||j                  t          j        ||	j                  dfS |pt!          ||           }t#          d|f          \  }
||dk    r? |
d |d          }|d         d         j                            t          j                  }|d}d }nKd}t)          |          r|}n7|dk    rd }n-|dk    rd }n#|dk    rd }n|dk    rd }nt          d           |
|||||          }|d         }|dk     rt          d|  d          ||j        d         dz   k    rt+          d           ||j        d         dz   k    rt+          d!          |dk    rt+          d"          ||d         |d#         fS |d         |d#         |d         fS )$a  
    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
    --------
    >>> import numpy as np
    >>> 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], # may vary
           [ 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   complexrcz%argument must be 'real', or 'complex'longdtype   r   r   zexpected square matrix)r   r   )FDr   r   N)geesc                     d S N xs    Y/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/scipy/linalg/_decomp_schur.py<lambda>zschur.<locals>.<lambda>   s         )lworkc                     d S r    r!   r"   s    r$   	sfunctionzschur.<locals>.sfunction   s    4r&   lhpc                     | j         dk     S N        r   r"   s    r$   r*   zschur.<locals>.sfunction   s    v|#r&   rhpc                     | j         dk    S r-   r/   r"   s    r$   r*   zschur.<locals>.sfunction   s    v}$r&   iucc                 (    t          |           dk    S Ng      ?absr"   s    r$   r*   zschur.<locals>.sfunction   s    1vv}$r&   oucc                 (    t          |           dk    S r4   r5   r"   s    r$   r*   zschur.<locals>.sfunction   s    1vv|#r&   zZ'sort' parameter must either be 'None', or a callable, or one of ('lhp','rhp','iuc','ouc'))r'   overwrite_asort_tzillegal value in z-th argument of internal geesz2Eigenvalues could not be separated for reordering.z2Leading eigenvalues do not satisfy sort condition.z/Schur form not found. Possibly ill-conditioned.)
ValueErrorr   r   np
issubdtyper   integerlenshapechar_double_precisionastypesizer   eye
empty_liker
   r   r   int_callabler	   )aoutputr'   r9   sortcheck_finitea1typt0z0r   resultr:   r*   infos                  r$   r   r      s   H 222@AAA q!!QZZ	}RXrz** 0Qbhv..///
28}}bhqkRXa[881222
(-C!!!c&;&;###3BB3B 
w!||rvarx00011B<M"BH555M"BH5557 7 M"BH555M"BH555q: : 5+b!"4"4KY..ED}nnb333r
1"))"'22|	 	 	 	 D>> 	NIIU]]$ $ $ $U]]% % % %U]]% % % %U]]$ $ $ $  M N N N T)Ru+! ! !F ":DaxxQdUQQQRRR	!q	 	 NOOO	!q	 	 NOOO	KLLL|ay&*$$ay&*fQi//r&   )	bhBr   r   fr   r   r   )r   r   rW   r   r   r   rW   r   r   r   c                      d}d}| D ]D}|j         j        }t          |t          |                   }t          |t          |                   }Et
          |         |         S )Nr   )r   rB   max_array_kind_array_precision_array_type)arrayskind	precisionrJ   ts        r$   _commonTypera      s^    DI 8 8GL4Q((	#3A#677		tY''r&   c                     d}|D ]D}|j         j        | k    r||                                fz   }+||                    |           fz   }Et	          |          dk    r|d         S |S )Nr!   r   r   )r   rB   copyrD   r@   )typer]   cast_arraysrJ   s       r$   	_castCopyrf      sw    K : :7<4%3KK%$(99KK
;11~r&   c           	      $   |rt          t          || f          \  }} nt          t          || f          \  }} t          || g          D ]T\  }}|j        dk    s|j        d         |j        d         k    r(t          d                    d|                             U| j        d         |j        d         k    r!d|j         d| j         }t          |          | j        d         }t          || t          dgd	                    }t          |||           \  }} t          |dz
  dd
          D ]}t          | ||dz
  f                   t          t          | |dz
  |dz
  f                   t          | ||f                   z   z  k    rut          | |dz
  |dz   |dz
  |dz   f                   | ||f         z
  }	t          |	d         | ||dz
  f         g          }
|	d         |
z  }| ||dz
  f         |
z  }t          |                                |g| |gg|          }|                    | |dz
  |dz   |dz
  df                   | |dz
  |dz   |dz
  df<   | d|dz   |dz
  |dz   f                             |                                j                  | d|dz   |dz
  |dz   f<   |dd|dz
  |dz   f                             |                                j                  |dd|dz
  |dz   f<   d| ||dz
  f<   | |fS )aQ  
    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
    --------
    >>> import numpy as np
    >>> 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]])

    r   r   r   zInput '{}' must be square.ZTz"Input array shapes must match: Z: z vs. T: g      @r   r   r   Nr.   )mapr   r   	enumeratendimrA   r<   formatra   r   rf   ranger6   epsr   r   conjdotT)rq   ZrM   indXmessageNr`   mmur   r   sGs                 r$   r   r      sC   l  $$q!f--117QF##1QF## M MQ6Q;;!'!*
229@@cKKLLL 3 	wqzQWQZQqwQQQQ!!!	
AAq%s++,,AQ1DAq1Q32  qAaCy>>CQqsAaCx[!1!1C!Q$LL!@AAA1Q3qs7AaC!G+,--!Q$7BbeQq!A#vY'((A1	A!QqS&	AA!}r1g.a888A uuQqs1Q3w!}%566Aac!A#gqsttm !A#qs1Q3w/33AFFHHJ??AdqsdAaC!Gmaaa1QqSjM--affhhj99Aaaa1QqSjM!QqS&		a4Kr&   )r   NFNT)T)__doc__numpyr=   r   r   r   r   numpy.linalgr   _miscr	   r
   lapackr   _decompr   __all__rC   r   finfofloatrn   fepsrZ   r[   r\   ra   rf   r   r!   r&   r$   <module>r      st   $ $     ; ; ; ; ; ; ; ; ; ; ; ;       , + + + + + + + $ $ $ $ $ $      I
#OO  AEo0 o0 o0 o0d bhuoorxAAAAAAA/ /CC SzC:&( ( (
 
 
S S S S S Sr&   