
    (ph/              
       :   S r SSKrSSKJrJrJrJr  SSKJr  SSK	J
r
Jr  SSKJr  SSKJr  S	S
/r/ SQr  SS jr\R&                  " \5      R*                  r\R&                  " \5      R*                  rSSSSSSSSSS.	rSSSSSSS.rSS/SS//rS rS rSS jrg)zSchur decomposition functions.    N)asarray_chkfinitesingleasarrayarray)norm   )LinAlgError_datacopied)get_lapack_funcs)eigvalsschurrsf2csf)ildc                 2   US;  a  [        S5      eU(       a  [        U 5      nO[        U 5      n[        R                  " UR
                  [        R                  5      (       a  [        U [        R
                  " S5      S9n[        UR                  5      S:w  d   UR                  S   UR                  S   :w  a  [        S5      eUR
                  R                  nUS	;   a3  US
;  a-  U[        ;   a  UR                  S5      nOUR                  S5      nUR                  S:X  a  [        [        R                  " SUR
                  S95      u  pUc<  [        R                  " XhR
                  S9[        R                  " XiR
                  S94$ [        R                  " XhR
                  S9[        R                  " XiR
                  S9S4$ U=(       d    [!        X`5      n[#        SU45      u  n
Ub  US:X  a8  U
" S USS9nUS   S   R$                  R                  [        R&                  5      nUc  SnS#S jnOPSn[)        U5      (       a  UnO;US:X  a  S#S jnO/US:X  a  S#S jnO#US:X  a  S#S jnOUS:X  a  S#S jnO[        S5      eU
" XX#US9nUS   nUS:  a  [        SU*  S35      eXR                  S   S-   :X  a  [+        S5      eXR                  S   S-   :X  a  [+        S 5      eUS:  a  [+        S!5      eUc
  US   US"   4$ US   US"   US   4$ )$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
    When the dtype of `a` is real, this specifies whether to compute
    the real or complex Schur decomposition.
    When the dtype of `a` is complex, this argument is ignored, and the
    complex Schur decomposition is computed.
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 an eigenvalue, returns a boolean denoting
    whether the eigenvalue should be sorted to the top-left (True).

    - If ``output='complex'`` OR the dtype of `a` is complex, the callable
      should have one argument: the eigenvalue expressed as a complex number.
    - If ``output='real'`` AND the dtype of `a` is real, the callable should have
      two arguments: the real and imaginary parts of the eigenvalue, respectively.

    Alternatively, string parameters may be used::

        'lhp'   Left-hand plane (real(eigenvalue) < 0.0)
        'rhp'   Right-hand plane (real(eigenvalue) >= 0.0)
        'iuc'   Inside the unit circle (abs(eigenvalue) <= 1.0)
        'ouc'   Outside the unit circle (abs(eigenvalue) > 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.
    Note that complex conjugate pairs for which the condition is true
    for either eigenvalue count as 2.

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])   # may vary

A custom eigenvalue-sorting condition that sorts by positive imaginary part
is satisfied by only one eigenvalue.

>>> _, _, sdim = schur(A, output='complex', sort=lambda x: x.imag > 1e-15)
>>> sdim
1

When ``output='real'`` and the array `a` is real, the `sort` callable must accept
the real and imaginary parts as separate arguments. Note that now the complex
eigenvalues ``-0.32948354+0.80225456j`` and ``-0.32948354-0.80225456j`` will be
treated as a complex conjugate pair, and according to the `sdim` documentation,
complex conjugate pairs for which the condition is True for *either* eigenvalue
increase `sdim` by *two*.

>>> _, _, sdim = schur(A, output='real', sort=lambda x, y: y > 1e-15)
>>> sdim
2

)realcomplexrcz%argument must be 'real', or 'complex'longdtype   r   r   zexpected square matrix)r   r   )FDr   r   )geesc                     g N )xs    M/var/www/html/venv/lib/python3.13/site-packages/scipy/linalg/_decomp_schur.py<lambda>schur.<locals>.<lambda>   s        )lworkc                     g r    r!   r"   ys     r#   	sfunctionschur.<locals>.sfunction   s    r&   lhpc                      U R                   S:  $ N        r   r*   s     r#   r,   r-      s    vv|#r&   rhpc                      U R                   S:  $ r0   r2   r*   s     r#   r,   r-      s    vv}$r&   iucc                 6    Uc  U OXS-  -   n[        U5      S:*  $ Ny              ?g      ?absr"   r+   zs      r#   r,   r-      s!    AbD1v}$r&   oucc                 6    Uc  U OXS-  -   n[        U5      S:  $ r7   r8   r:   s      r#   r,   r-      s!    AbD1v|#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.r    )
ValueErrorr   r   np
issubdtyper   integerlenshapechar_double_precisionastypesizer   eye
empty_liker
   r   r   int_callabler	   )aoutputr'   r>   sortcheck_finitea1typt0z0r   resultr?   r,   infos                  r#   r   r      s   t 22@AAq!QZ	}}RXXrzz**Qbhhv./
288}bhhqkRXXa[8122
((--C!!c&;##3B3B 
ww!|rvvarxx01<MM"HH5MM"HH57 7 MM"HH5MM"HH5q: : 5+b"4KY.ED}nb3r
1""))"''2|	 D>>IU]$U]%U]% U]$  M N N )u!F ":Dax,dUG3PQRR	!q	 NOO	!q	 NOO	KLL|ay&*$$ay&*fQi//r&   )	bhBr   r   fr   r   r   )r   r   r\   r   r   r   r\   r   r   r   c                      SnSnU  H?  nUR                   R                  n[        U[        U   5      n[        U[        U   5      nMA     [
        U   U   $ )Nr   )r   rG   max_array_kind_array_precision_array_type)arrayskind	precisionrO   ts        r#   _commonTyperf      sW    DIGGLL4Q(	#3A#67	  tY''r&   c                     SnU HF  nUR                   R                  U :X  a  X#R                  5       4-   nM2  X#R                  U 5      4-   nMH     [	        U5      S:X  a  US   $ U$ )Nr!   r   r   )r   rG   copyrI   rE   )typerb   cast_arraysrO   s       r#   	_castCopyrk      sc    K77<<4%3K%$(99K	 
 ;11~r&   c           	      <   U(       a  [        [        X45      u  pO[        [        X45      u  p[        X/5       HG  u  p4UR                  S:w  d"  UR
                  S   UR
                  S   :w  d  M7  [        SSU    S35      e   U R
                  S   UR
                  S   :w  a'  SUR
                   SU R
                   3n[        U5      eU R
                  S   n[        X[        S	/S
5      5      n[        XqU 5      u  p[        US-
  SS5       GH  n[        XUS-
  4   5      [        [        XS-
  US-
  4   5      [        XU4   5      -   -  :  Ga7  [        XS-
  US-   2US-
  US-   24   5      XU4   -
  n	[        U	S   XUS-
  4   /5      n
U	S   U
-  nXUS-
  4   U
-  n[        UR                  5       U/U* U//US9nUR!                  XS-
  US-   2US-
  S24   5      XS-
  US-   2US-
  S24'   U SUS-   2US-
  US-   24   R!                  UR                  5       R"                  5      U SUS-   2US-
  US-   24'   USS2US-
  US-   24   R!                  UR                  5       R"                  5      USS2US-
  US-   24'   SXUS-
  4'   GM     X4$ )a  
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 'ZTz' must be square.z"Input array shapes must match: Z: z vs. T: g      @r   r   r   Nr1   )mapr   r   	enumeratendimrF   rA   rf   r   rk   ranger9   epsr   r   conjdotT)ru   ZrR   indXmessageNre   mmur   r   sGs                 r#   r   r      s   l $qf-17QF#QF#66Q;!''!*
2wtCyk1BCDD $ 	wwqzQWWQZ6qwwixyQ!!	
AA%s+,AQ1DA1Q32qAaCy>CQsAaCx[!1CQ$L!@AAQ3qs7AaC!G+,-Q$7BbeQ!A#vY'(A1	AQqS&	AA!}r1g.a8A uuQs1Q3w!}%56Ac!A#gqstm !A#qs1Q3w/33AFFHJJ?AdqsdAaC!Gma1QqSjM--affhjj9Aa1QqSjMQqS&	  4Kr&   )r   NFNT)T)__doc__numpyrB   r   r   r   r   numpy.linalgr   _miscr	   r
   lapackr   _decompr   __all__rH   r   finfofloatrr   fepsr_   r`   ra   rf   rk   r   r!   r&   r#   <module>r      s    $  ; ;  , $ I
#  AEG0T 	hhuo	xxAAAAAAA/C SzC:&(
Sr&   