
    (ph?                         S SK r S SKrS SKJr  SSKJrJrJr  SSKJ	r	  SS/r
/ SQrS	 rS
 rS rS rS r  SS jr  SS jr  SS jrg)    N)asarray_chkfinite   )LinAlgError_datacopiedLinAlgWarning)get_lapack_funcsqzordqz)ildc                     [        U 5      (       a  U nU$ U S:X  a  [        nU$ U S:X  a  [        nU$ U S:X  a  [        nU$ U S:X  a  [        nU$ [        S5      e)NlhprhpiucouczLsort parameter must be None, a callable, or one of ('lhp','rhp','iuc','ouc'))callable_lhp_rhp_iuc_ouc
ValueError)sort	sfunctions     J/var/www/html/venv/lib/python3.13/site-packages/scipy/linalg/_decomp_qz.py_select_functionr      s    ~~	  
	  
	  
	  
	
   < = 	=    c                     [         R                  " U [        S9nUS:g  nSX#) '   [         R                  " X   X   -  5      S:  X#'   U$ Ndtyper   Fg        np
empty_likeboolrealxyoutnonzeros       r   r   r   !   G    
--
&CAvGCMGGAJqz12S8CLJr   c                     [         R                  " U [        S9nUS:g  nSX#) '   [         R                  " X   X   -  5      S:  X#'   U$ r   r"   r'   s       r   r   r   *   r,   r   c                 z    [         R                  " U [        S9nUS:g  nSX#) '   [        X   X   -  5      S:  X#'   U$ )Nr    r   F      ?r#   r$   r%   absr'   s       r   r   r   3   sC    
--
&CAvGCM
1:-.4CLJr   c                     [         R                  " U [        S9nU S:H  nUS:H  nSX#U-  '   SX#) U-  '   [        X)    X)    -  5      S:  X$) '   U$ )Nr    r   FTr/   r0   )r(   r)   r*   xzeroyzeros        r   r   r   <   sc    
--
&C!VE!VECCqy6*+c1CKJr   c           
         Ub  [        S5      eUS;  a  [        S5      eU(       a  [        U 5      n[        U5      n	O,[        R                  " U 5      n[        R                  " U5      n	UR                  u  pU	R                  u  pXs=:X  a  Us=:X  a  U:X  d  O  [        S5      eUR
                  R                  nUS;   a7  US;  a1  U[        ;   a  UR                  S5      nSnOUR                  S5      nSnU	R
                  R                  nUS;   a7  US;  a1  U[        ;   a  U	R                  S5      n	SnOU	R                  S5      n	SnU=(       d    [        X5      nU=(       d    [        X5      n[        S	X45      u  nUb  US
:X  a.  U" S XS
S9nUS   S   R                  R                  [        5      nS nU" UXX5USS9nUS
   nUS:  a  [        SU*  S35      eUS:  a(  UU::  a"  [        R                  " SUS-
   S3[        SS9  O<UUS-   :X  a  [!        S5      eUUS-   :X  a  [!        S5      eUUS-   :X  a  [!        S5      eUUR"                  4$ )NzcThe 'sort' input of qz() has to be None and will be removed in a future release. Use ordqz instead.)r&   complexrcz%argument must be 'real', or 'complex'z)Array dimensions must be square and agree)r6   r8   )FDr:   r9   )ggesc                     g N r(   s    r   <lambda>_qz.<locals>.<lambda>t   s    r   )lworkr   c                     g r>   r?   r@   s    r   r   _qz.<locals>.sfunctionw   s    r   )rC   overwrite_aoverwrite_bsort_tIllegal value in argument z of ggesztThe QZ iteration failed. (a,b) are not in Schur form, but ALPHAR(j), ALPHAI(j), and BETA(j) should be correct for J=r   z,...,N   )
stacklevelz(Something other than QZ iteration failed   zAfter reordering, roundoff changed values of some complex eigenvalues so that leading eigenvalues in the Generalized Schur form no longer satisfy sort=True. This could also be due to scaling.z#Reordering failed in <s,d,c,z>tgsen)r   r   r#   asarrayshaper!   char_double_precisionastyper   r   r&   intwarningswarnr   r   typecode)ABoutputrC   r   rG   rH   check_finitea1b1a_ma_nb_mb_ntypatypbr;   resultr   infos                       r   _qzre   F   sh    K L 	L 22@AAq!q!ZZ]ZZ]xxHCxxHC$#$$DEE88==D!!d*&<$$3BD3BD88==D!!d*&<$$3BD3BD5+b"4K5+b"4KY1ED}nbB7r
1""))#.)R5)!5F ":Dax5teWHEFF	dck ''+Avhf67D!"	$ 
QDEE	Q J K 	K 
Q?@@4==  r   c                 D    [        XX#UXVUS9u  pUS   US   US   US   4$ )a0  
QZ decomposition for generalized eigenvalues of a pair of matrices.

The QZ, or generalized Schur, decomposition for a pair of n-by-n
matrices (A,B) is::

    (A,B) = (Q @ AA @ Z*, Q @ BB @ Z*)

where AA, BB is in generalized Schur form if BB is upper-triangular
with non-negative diagonal and AA is upper-triangular, or for real QZ
decomposition (``output='real'``) block upper triangular with 1x1
and 2x2 blocks. In this case, the 1x1 blocks correspond to real
generalized eigenvalues and 2x2 blocks are 'standardized' by making
the corresponding elements of BB have the form::

    [ a 0 ]
    [ 0 b ]

and the pair of corresponding 2x2 blocks in AA and BB will have a complex
conjugate pair of generalized eigenvalues. If (``output='complex'``) or
A and B are complex matrices, Z' denotes the conjugate-transpose of Z.
Q and Z are unitary matrices.

Parameters
----------
A : (N, N) array_like
    2-D array to decompose
B : (N, N) array_like
    2-D array to decompose
output : {'real', 'complex'}, optional
    Construct the real or complex QZ decomposition for real matrices.
    Default is 'real'.
lwork : int, optional
    Work array size. If None or -1, it is automatically computed.
sort : {None, callable, 'lhp', 'rhp', 'iuc', 'ouc'}, optional
    NOTE: THIS INPUT IS DISABLED FOR NOW. Use ordqz instead.

    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). For
    real matrix pairs, the sort function takes three real arguments
    (alphar, alphai, beta). The eigenvalue
    ``x = (alphar + alphai*1j)/beta``. For complex matrix pairs or
    output='complex', the sort function takes two complex arguments
    (alpha, beta). The eigenvalue ``x = (alpha/beta)``.  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).
overwrite_a : bool, optional
    Whether to overwrite data in a (may improve performance)
overwrite_b : bool, optional
    Whether to overwrite data in b (may improve performance)
check_finite : bool, optional
    If true checks the elements of `A` and `B` are finite numbers. If
    false does no checking and passes matrix through to
    underlying algorithm.

Returns
-------
AA : (N, N) ndarray
    Generalized Schur form of A.
BB : (N, N) ndarray
    Generalized Schur form of B.
Q : (N, N) ndarray
    The left Schur vectors.
Z : (N, N) ndarray
    The right Schur vectors.

See Also
--------
ordqz

Notes
-----
Q is transposed versus the equivalent function in Matlab.

.. versionadded:: 0.11.0

Examples
--------
>>> import numpy as np
>>> from scipy.linalg import qz

>>> A = np.array([[1, 2, -1], [5, 5, 5], [2, 4, -8]])
>>> B = np.array([[1, 1, -3], [3, 1, -1], [5, 6, -2]])

Compute the decomposition.  The QZ decomposition is not unique, so
depending on the underlying library that is used, there may be
differences in the signs of coefficients in the following output.

>>> AA, BB, Q, Z = qz(A, B)
>>> AA
array([[-1.36949157, -4.05459025,  7.44389431],
       [ 0.        ,  7.65653432,  5.13476017],
       [ 0.        , -0.65978437,  2.4186015 ]])  # may vary
>>> BB
array([[ 1.71890633, -1.64723705, -0.72696385],
       [ 0.        ,  8.6965692 , -0.        ],
       [ 0.        ,  0.        ,  2.27446233]])  # may vary
>>> Q
array([[-0.37048362,  0.1903278 ,  0.90912992],
       [-0.90073232,  0.16534124, -0.40167593],
       [ 0.22676676,  0.96769706, -0.11017818]])  # may vary
>>> Z
array([[-0.67660785,  0.63528924, -0.37230283],
       [ 0.70243299,  0.70853819, -0.06753907],
       [ 0.22088393, -0.30721526, -0.92565062]])  # may vary

Verify the QZ decomposition.  With real output, we only need the
transpose of ``Z`` in the following expressions.

>>> Q @ AA @ Z.T  # Should be A
array([[ 1.,  2., -1.],
       [ 5.,  5.,  5.],
       [ 2.,  4., -8.]])
>>> Q @ BB @ Z.T  # Should be B
array([[ 1.,  1., -3.],
       [ 3.,  1., -1.],
       [ 5.,  6., -2.]])

Repeat the decomposition, but with ``output='complex'``.

>>> AA, BB, Q, Z = qz(A, B, output='complex')

For conciseness in the output, we use ``np.set_printoptions()`` to set
the output precision of NumPy arrays to 3 and display tiny values as 0.

>>> np.set_printoptions(precision=3, suppress=True)
>>> AA
array([[-1.369+0.j   ,  2.248+4.237j,  4.861-5.022j],
       [ 0.   +0.j   ,  7.037+2.922j,  0.794+4.932j],
       [ 0.   +0.j   ,  0.   +0.j   ,  2.655-1.103j]])  # may vary
>>> BB
array([[ 1.719+0.j   , -1.115+1.j   , -0.763-0.646j],
       [ 0.   +0.j   ,  7.24 +0.j   , -3.144+3.322j],
       [ 0.   +0.j   ,  0.   +0.j   ,  2.732+0.j   ]])  # may vary
>>> Q
array([[ 0.326+0.175j, -0.273-0.029j, -0.886-0.052j],
       [ 0.794+0.426j, -0.093+0.134j,  0.402-0.02j ],
       [-0.2  -0.107j, -0.816+0.482j,  0.151-0.167j]])  # may vary
>>> Z
array([[ 0.596+0.32j , -0.31 +0.414j,  0.393-0.347j],
       [-0.619-0.332j, -0.479+0.314j,  0.154-0.393j],
       [-0.195-0.104j,  0.576+0.27j ,  0.715+0.187j]])  # may vary

With complex arrays, we must use ``Z.conj().T`` in the following
expressions to verify the decomposition.

>>> Q @ AA @ Z.conj().T  # Should be A
array([[ 1.-0.j,  2.-0.j, -1.-0.j],
       [ 5.+0.j,  5.+0.j,  5.-0.j],
       [ 2.+0.j,  4.+0.j, -8.+0.j]])
>>> Q @ BB @ Z.conj().T  # Should be B
array([[ 1.+0.j,  1.+0.j, -3.+0.j],
       [ 3.-0.j,  1.-0.j, -1.+0.j],
       [ 5.+0.j,  6.+0.j, -2.+0.j]])

)rY   rC   r   rG   rH   rZ   r   r   )re   )
rW   rX   rY   rC   r   rG   rH   rZ   rc   _s
             r   r	   r	      s>    R A4 +!-/IF !9fQiVBZ77r   c                 X   [        XUSUUUS9u  Gtpxpp  pUS:X  a'  U
S   U
S   [        R                  " S5      -  -   U
S   pOUS:X  a  U
S   U
S   S-  -   U
S   pOU
u  p[        U5      nU" X5      n[	        S	Xx45      nUS
;   a  SUR
                  S   -  S-   OSnU" UXxXSUSS9Gtnnn
nn      n	nUS:X  a'  U
S   U
S   [        R                  " S5      -  -   U
S   pOUS:X  a  U
S   U
S   S-  -   U
S   pOU
u  pUS:  a  [        SU*  S35      eUS:X  a  [        S5      eUUXUU4$ )a  QZ decomposition for a pair of matrices with reordering.

Parameters
----------
A : (N, N) array_like
    2-D array to decompose
B : (N, N) array_like
    2-D array to decompose
sort : {callable, 'lhp', 'rhp', 'iuc', 'ouc'}, optional
    Specifies whether the upper eigenvalues should be sorted. A
    callable may be passed that, given an ordered pair ``(alpha,
    beta)`` representing the eigenvalue ``x = (alpha/beta)``,
    returns a boolean denoting whether the eigenvalue should be
    sorted to the top-left (True). For the real matrix pairs
    ``beta`` is real while ``alpha`` can be complex, and for
    complex matrix pairs both ``alpha`` and ``beta`` can be
    complex. The callable must be able to accept a NumPy
    array. 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)

    With the predefined sorting functions, an infinite eigenvalue
    (i.e., ``alpha != 0`` and ``beta = 0``) is considered to lie in
    neither the left-hand nor the right-hand plane, but it is
    considered to lie outside the unit circle. For the eigenvalue
    ``(alpha, beta) = (0, 0)``, the predefined sorting functions
    all return `False`.
output : str {'real','complex'}, optional
    Construct the real or complex QZ decomposition for real matrices.
    Default is 'real'.
overwrite_a : bool, optional
    If True, the contents of A are overwritten.
overwrite_b : bool, optional
    If True, the contents of B are overwritten.
check_finite : bool, optional
    If true checks the elements of `A` and `B` are finite numbers. If
    false does no checking and passes matrix through to
    underlying algorithm.

Returns
-------
AA : (N, N) ndarray
    Generalized Schur form of A.
BB : (N, N) ndarray
    Generalized Schur form of B.
alpha : (N,) ndarray
    alpha = alphar + alphai * 1j. See notes.
beta : (N,) ndarray
    See notes.
Q : (N, N) ndarray
    The left Schur vectors.
Z : (N, N) ndarray
    The right Schur vectors.

See Also
--------
qz

Notes
-----
On exit, ``(ALPHAR(j) + ALPHAI(j)*i)/BETA(j), j=1,...,N``, will be the
generalized eigenvalues.  ``ALPHAR(j) + ALPHAI(j)*i`` and
``BETA(j),j=1,...,N`` are the diagonals of the complex Schur form (S,T)
that would result if the 2-by-2 diagonal blocks of the real generalized
Schur form of (A,B) were further reduced to triangular form using complex
unitary transformations. If ALPHAI(j) is zero, then the jth eigenvalue is
real; if positive, then the ``j``\ th and ``(j+1)``\ st eigenvalues are a
complex conjugate pair, with ``ALPHAI(j+1)`` negative.

.. versionadded:: 0.17.0

Examples
--------
>>> import numpy as np
>>> from scipy.linalg import ordqz
>>> A = np.array([[2, 5, 8, 7], [5, 2, 2, 8], [7, 5, 6, 6], [5, 4, 4, 8]])
>>> B = np.array([[0, 6, 0, 0], [5, 0, 2, 1], [5, 2, 6, 6], [4, 7, 7, 7]])
>>> AA, BB, alpha, beta, Q, Z = ordqz(A, B, sort='lhp')

Since we have sorted for left half plane eigenvalues, negatives come first

>>> (alpha/beta).real < 0
array([ True,  True, False, False], dtype=bool)

N)rY   r   rG   rH   rZ   sr   r   y              ?rM   r   tgsensd      )ijobrC   liworkrJ   z	 of tgsenzReordering of (A, B) failed because the transformed matrix pair (A, B) would be too far from generalized Schur form; the problem is very ill-conditioned. (A, B) may have been partially reordered.)re   r#   	complex64r   r   rO   r   )rW   rX   r   rY   rG   rH   rZ   AABBri   abQZtypalphabetar   selectrl   rC   AAABBBQQZZrd   s                            r   r
   r
   @  s   t ),Ad8C8C9E)G% RQQ1a
 czebeBLL$444bet	ebeCi'At &Iu#FWrh/E"%+AbhhqkMB1E.3FBA9::?/K+Cr2r1aAt
 czebeBLL$444bet	ebeCi'Atax5teWIFGG	 & ' 	' U"b((r   )r&   NNFFT)r   r&   FFT)rT   numpyr#   r   _miscr   r   r   lapackr   __all__rQ   r   r   r   r   r   re   r	   r
   r?   r   r   <module>r      sq      # : : $ /# & AF(,H!V @E'+l8^ 8=*.A)r   