§
    ¢é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    ršt          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)Úgeeséÿÿÿÿc                 ó   — 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   €  € ó    )Úlworkéþÿÿÿc                 ó   — 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   € Ý˜1‘v”v ’}Ð$r&   Úoucc                 ó(   — t          | ¦  «        dk    S r4   r5   r"   s    r$   r*   zschur.<locals>.sfunctionª   s   € Ý˜1‘v”v ’|Ð#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Ñ!Ô!ˆˆå�Q‰ZŒZˆÝ	„}�R”X�rœzÑ*Ô*ð 0Ý�Q�bœh vÑ.Ô.Ð/Ñ/Ô/ˆÝ
ˆ2Œ8�}„}˜ÒÐ˜bœh qœk¨R¬X°a¬[Ò8Ð8ÝÐ1Ñ2Ô2Ð2à
Œ(Œ-€CØÐ!Ð!Ð! c°Ð&;Ð&;ØÕ#Ð#Ð#Ø—’˜3‘”ˆBˆBà—’˜3‘”ˆBð 
„w�!‚|€|Ý•r”v˜a r¤xÐ0Ñ0Ô0Ñ1Ô1‰ˆˆBØˆ<Ý”M "¨B¬HÐ5Ñ5Ô5Ý”M "¨B¬HÐ5Ñ5Ô5ð7ð 7õ ”M "¨B¬HÐ5Ñ5Ô5Ý”M "¨B¬HÐ5Ñ5Ô5°qð:ð :ð Ð5¥+¨b°!Ñ"4Ô"4€KÝ˜Y¨¨Ñ.Ô.�E€DØ€}˜ š˜à��n�n b°Ð3Ñ3Ô3ˆØ�r”
˜1”Ô"×)Ò)­"¬'Ñ2Ô2ˆà€|Øˆð	ð 	ð 	ð 	ð ˆÝ�D‰>Œ>ð 	NØˆIˆIØ�UŠ]ˆ]ð$ð $ð $ð $à�UŠ]ˆ]ð%ð %ð %ð %à�UŠ]ˆ]ð%ð %ð %ð %à�UŠ]ˆ]ð$ð $ð $ð $õ ð Mñ Nô Nð Nð ˆT�)˜R u¸+Øð!ñ !ô !€Fð �"Œ:€DØˆa‚x€xÝÐQ¨d¨UÐQÐQÐQÑRÔRÐRØ	�”˜!”˜q‘Ò	 Ð	 ÝÐNÑOÔOÐOØ	�”˜!”˜q‘Ò	 Ð	 ÝÐNÑOÔOÐOØ	�ŠˆÝÐKÑLÔLÐLà€|Ø�aŒy˜& œ*Ð$Ð$à�aŒy˜& œ* f¨Q¤iÐ/Ð/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ˆØŒGŒLˆÝ�4� QœÑ(Ô(ˆÝ˜	Õ#3°AÔ#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ÒÐØ%¨¯ª©¬¨Ñ3ˆKˆKà%¨¯ª°$©¬Ð(9Ñ9ˆKˆKÝ
ˆ;ÑÔ˜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Ñ-Ô-‰ˆˆ1ˆ1å•7˜Q ˜FÑ#Ô#‰ˆˆ1å˜Q ˜FÑ#Ô#ð Mð M‰ˆˆQØŒ6�QŠ;ˆ;˜!œ' !œ*¨¬°¬
Ò2Ð2ÝÐ9×@Ò@ÀÀcÄÑKÔKÑLÔLÐLð 3ð 	„wˆq„z�Q”W˜Q”ZÒÐØQ°q´wÐQÐQÈÌÐQÐQˆÝ˜Ñ!Ô!Ð!Ø	Œ�Œ
€AÝ�A�q�%   sÑ+Ô+Ñ,Ô,€AÝ�Q˜˜1ÑÔ�D€A€qå�1�Q‘3˜˜2ÑÔð ñ ˆÝˆq��A�a‘C�Œy‰>Œ>�C¥ Q q¨¡s¨A¨a©C x¤[Ñ!1Ô!1µC¸¸!¸Q¸$¼±L´LÑ!@ÑAÒAÑAÝ˜˜1˜Q™3˜q ™s˜7 A a¡C¨¨!© GÐ+Ô,Ñ-Ô-°°!°Q°$´Ñ7ˆBÝ�b˜”e˜Q˜q ! A¡#˜vœYÐ'Ñ(Ô(ˆAØ�1”˜‘	ˆAØ�!�Q�q‘S�&”	˜A‘ˆAÝ˜Ÿš™œ !�}¨ r¨1 gÐ.°aÐ8Ñ8Ô8ˆAà Ÿušu Q q¨¡s¨1¨Q©3 w°°!±°° }Ô%5Ñ6Ô6ˆAˆa�‰c�!�A‘#ˆg�q˜‘s�t�tˆmÑØ   ! A¡#  q¨¡s¨1¨Q©3 w Ô/×3Ò3°A·F²F±H´H´JÑ?Ô?ˆAˆdˆq�‰sˆd�A�a‘C˜˜!™�GˆmÑØ˜a˜a˜a  1¡ Q q¡S ˜jœM×-Ò-¨a¯fªf©h¬h¬jÑ9Ô9ˆAˆaˆaˆa��1‘�Q�q‘S�ˆj‰Màˆˆ!ˆQˆq‰Sˆ&‰	‰	Øˆaˆ4€Kr&   )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Ð
€à#�O�OÐ ð AEØðo0ð o0ð o0ð o0ðd €b„hˆu�o„oÔ€Ø€r„x�ÑÔÔ€à˜A A¨A°AØ˜A A¨Að/ð /€à ¨°¸ÀÐCÐCÐ Ø�Sˆz˜C ˜:Ð&€ð(ð (ð (ð
ð 
ð 
ðSð Sð Sð Sð Sð Sr&   