§
    ´³Ùg€p  ã                   óâ   — d Z ddlZddlZddlmZmZmZmZm	Z	 ddl
mZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ g d¢Zd„ Zd„ ZeZd„ Z d„ Z!dd„Z"dd„Z#dd„Z$dd„Z%dS )zMatrix equation solver routinesé    N)ÚinvÚLinAlgErrorÚnormÚcondÚsvdé   )ÚsolveÚsolve_triangularÚmatrix_balance)Úget_lapack_funcs)Úschur)Úlu)Úqr)Úordqz)Ú_asarray_validated)Ú
block_diag)Úsolve_sylvesterÚsolve_continuous_lyapunovÚsolve_discrete_lyapunovÚsolve_lyapunovÚsolve_continuous_areÚsolve_discrete_arec                 ó|  — | j         dk    s|j         dk    rkt          j        t          j        t          j        t          j        dœ}t          d| ||f¬¦  «        \  }t          j        |j        ||j	                 ¬¦  «        S t          | d¬¦  «        \  }}t          |                     ¦   «                              ¦   «         d¬¦  «        \  }}t          j        t          j        |                     ¦   «                              ¦   «         |¦  «        |¦  «        }	t          d|||	f¦  «        \  }
|
€t          d	¦  «        ‚ |
|||	d
¬¦  «        \  }}}||z  }|dk     rt          d| fz  ¦  «        ‚t          j        t          j        ||¦  «        |                     ¦   «                              ¦   «         ¦  «        S )aŽ  
    Computes a solution (X) to the Sylvester equation :math:`AX + XB = Q`.

    Parameters
    ----------
    a : (M, M) array_like
        Leading matrix of the Sylvester equation
    b : (N, N) array_like
        Trailing matrix of the Sylvester equation
    q : (M, N) array_like
        Right-hand side

    Returns
    -------
    x : (M, N) ndarray
        The solution to the Sylvester equation.

    Raises
    ------
    LinAlgError
        If solution was not found

    Notes
    -----
    Computes a solution to the Sylvester matrix equation via the Bartels-
    Stewart algorithm. The A and B matrices first undergo Schur
    decompositions. The resulting matrices are used to construct an
    alternative Sylvester equation (``RY + YS^T = F``) where the R and S
    matrices are in quasi-triangular form (or, when R, S or F are complex,
    triangular form). The simplified equation is then solved using
    ``*TRSYL`` from LAPACK directly.

    .. versionadded:: 0.11.0

    Examples
    --------
    Given `a`, `b`, and `q` solve for `x`:

    >>> import numpy as np
    >>> from scipy import linalg
    >>> a = np.array([[-3, -2, 0], [-1, -1, 3], [3, -5, -1]])
    >>> b = np.array([[1]])
    >>> q = np.array([[1],[2],[3]])
    >>> x = linalg.solve_sylvester(a, b, q)
    >>> x
    array([[ 0.0625],
           [-0.5625],
           [ 0.6875]])
    >>> np.allclose(a.dot(x) + x.dot(b), q)
    True

    r   ©ÚsÚdÚcÚz©Útrsyl©Úarrays©ÚdtypeÚreal©ÚoutputNzQLAPACK implementation does not contain a proper Sylvester equation solver (TRSYL)ÚC©Útranbz(Illegal value encountered in the %d term)ÚsizeÚnpÚfloat32Úfloat64Ú	complex64Ú
complex128r   ÚemptyÚshapeÚtypecoder   ÚconjÚ	transposeÚdotÚRuntimeErrorr   )ÚaÚbÚqÚtdictÚfuncÚrÚur   ÚvÚfr    ÚyÚscaleÚinfos                 úK/granary/edev/quantum/lib/python3.11/site-packages/scipy/linalg/_solvers.pyr   r      s¤  € ðl 	„v�‚{€{�a”f ’k�kÝ”j¥r¤zÝ”l­¬ð8ð 8ˆå  °Q¸¸1°IÐ>Ñ>Ô>‰ˆÝŒx˜œ u¨T¬]Ô';Ð<Ñ<Ô<Ð<õ �˜6Ð"Ñ"Ô"�D€A€qõ �—’‘”×#Ò#Ñ%Ô%¨fÐ5Ñ5Ô5�D€A€qõ 	Œ�rŒv�a—f’f‘h”h×(Ò(Ñ*Ô*¨AÑ.Ô.°Ñ2Ô2€Aõ ˜j¨1¨a°¨)Ñ4Ô4�F€EØ€}Ýð ?ñ @ô @ð 	@à�U˜1˜a ¨#Ð.Ñ.Ô.�N€A€uˆdàˆa‰€Aàˆa‚x€xÝð (Ø,0¨5¨(ñ3ñ 4ô 4ð 	4õ Œ6•"”&˜˜A‘,”, §¢¡¤× 2Ò 2Ñ 4Ô 4Ñ5Ô5Ð5ó    c                 óŠ  — t          j        t          | d¬¦  «        ¦  «        } t          j        t          |d¬¦  «        ¦  «        }t          }t	          | |f¦  «        D ]L\  }}t          j        |¦  «        rt          }t          j        |j        Ž st          dd|         › d�¦  «        ‚ŒM| j        |j        k    rt          d¦  «        ‚| j
        dk    rjt           j        t           j        t           j        t           j        dœ}t          d	| |f¬
¦  «        \  }t          j        | j        ||j                 ¬¦  «        S t%          | d¬¦  «        \  }}|                     ¦   «         j                             |                     |¦  «        ¦  «        }	t          d||	f¦  «        }
|t          u rdnd} |
|||	|¬¦  «        \  }}}|dk     rt          d| › d�¦  «        ‚|dk    rt-          j        dt0          d¬¦  «         ||z  }|                     |¦  «                             |                     ¦   «         j        ¦  «        S )aÍ  
    Solves the continuous Lyapunov equation :math:`AX + XA^H = Q`.

    Uses the Bartels-Stewart algorithm to find :math:`X`.

    Parameters
    ----------
    a : array_like
        A square matrix

    q : array_like
        Right-hand side square matrix

    Returns
    -------
    x : ndarray
        Solution to the continuous Lyapunov equation

    See Also
    --------
    solve_discrete_lyapunov : computes the solution to the discrete-time
        Lyapunov equation
    solve_sylvester : computes the solution to the Sylvester equation

    Notes
    -----
    The continuous Lyapunov equation is a special form of the Sylvester
    equation, hence this solver relies on LAPACK routine ?TRSYL.

    .. versionadded:: 0.11.0

    Examples
    --------
    Given `a` and `q` solve for `x`:

    >>> import numpy as np
    >>> from scipy import linalg
    >>> a = np.array([[-3, -2, 0], [-1, -1, 0], [0, -5, -1]])
    >>> b = np.array([2, 4, -1])
    >>> q = np.eye(3)
    >>> x = linalg.solve_continuous_lyapunov(a, q)
    >>> x
    array([[ -0.75  ,   0.875 ,  -3.75  ],
           [  0.875 ,  -1.375 ,   5.3125],
           [ -3.75  ,   5.3125, -27.0625]])
    >>> np.allclose(a.dot(x) + x.dot(a.T), q)
    True
    T©Úcheck_finiteúMatrix Úaqú should be square.ú*Matrix a and q should have the same shape.r   r   r   r!   r#   r%   r&   r    ÚTr(   r)   zH?TRSYL exited with the internal error "illegal value in argument number z8.". See LAPACK documentation for the ?TRSYL error codes.r   z†Input "a" has an eigenvalue pair whose sum is very close to or exactly zero. The solution is obtained via perturbing the coefficients.é   )Ú
stacklevel)r,   Ú
atleast_2dr   ÚfloatÚ	enumerateÚiscomplexobjÚcomplexÚequalr2   Ú
ValueErrorr+   r-   r.   r/   r0   r   r1   r3   r   r4   rM   r6   ÚwarningsÚwarnÚRuntimeWarning)r8   r:   Úr_or_cÚindÚ_r;   r<   r=   r>   r@   r    Údtype_stringrA   rB   rC   s                  rD   r   r   s   sS  € õd 	ŒÕ(¨¸Ð>Ñ>Ô>Ñ?Ô?€AÝ
ŒÕ(¨¸Ð>Ñ>Ô>Ñ?Ô?€Aå€Få˜Q ˜FÑ#Ô#ð Fð F‰ˆˆQÝŒ?˜1ÑÔð 	ÝˆFåŒx˜œÐ!ð 	FÝÐD t¨C¤yÐDÐDÐDÑEÔEÐEð	Fð 	„w�!”'ÒÐÝÐEÑFÔFÐFð 	„v�‚{€{Ý”j¥r¤zÝ”l­¬ð8ð 8ˆå  °Q¸°FÐ;Ñ;Ô;‰ˆÝŒx˜œ u¨T¬]Ô';Ð<Ñ<Ô<Ð<õ �˜6Ð"Ñ"Ô"�D€A€qð 	
�Š‰ŒŒ
�Š�q—u’u˜Q‘x”xÑ Ô €Aõ ˜W q¨! fÑ-Ô-€Eà ¥E˜/˜/�3�3¨s€LØ�U˜1˜a ¨,Ð7Ñ7Ô7�N€A€uˆdàˆa‚x€xÝð LØ?C¸eðLð Lð Lñ Mô Mð 	Mð 
�ŠˆÝŒð Bõ %°ð	4ñ 	4ô 	4ð 	4ð ˆ�J€Aà�5Š5�‰8Œ8�<Š<˜Ÿš™œœ
Ñ#Ô#Ð#rE   c                 ó  — t          j        | |                      ¦   «         ¦  «        }t          j        |j        d         ¦  «        |z
  }t          ||                     ¦   «         ¦  «        }t          j        ||j        ¦  «        S )zÄ
    Solves the discrete Lyapunov equation directly.

    This function is called by the `solve_discrete_lyapunov` function with
    `method=direct`. It is not supposed to be called directly.
    r   )r,   Úkronr4   Úeyer2   r	   ÚflattenÚreshape)r8   r:   ÚlhsÚxs       rD   Ú_solve_discrete_lyapunov_directre   Ú   sb   € õ Œ'�!�Q—V’V‘X”XÑ
Ô
€CÝ
Œ&�”˜1”Ñ
Ô
 Ñ
$€CÝˆc�1—9’9‘;”;ÑÔ€AåŒ:�a˜œÑ!Ô!Ð!rE   c           	      óÀ  — t          j        | j        d         ¦  «        }|                      ¦   «                              ¦   «         }t          ||z   ¦  «        }t          j        ||z
  |¦  «        }dt          j        t          j        t          | |z   ¦  «        |¦  «        |¦  «        z  }t          |                     ¦   «                              ¦   «         | ¦  «        S )zÝ
    Solves the discrete Lyapunov equation using a bilinear transformation.

    This function is called by the `solve_discrete_lyapunov` function with
    `method=bilinear`. It is not supposed to be called directly.
    r   rN   )r,   r`   r2   r4   r5   r   r6   r   )r8   r:   r`   ÚaHÚaHI_invr9   r   s          rD   Ú!_solve_discrete_lyapunov_bilinearri   é   s¨   € õ Œ&�”˜”Ñ
Ô
€CØ	
�Š‰Œ×	Ò	Ñ	Ô	€BÝ�"�s‘(‰mŒm€GÝ
Œˆr�C‰x˜Ñ!Ô!€AØ	�"Œ&•”�˜A ™G™œ aÑ(Ô(¨'Ñ
2Ô
2Ñ2€AÝ˜!Ÿ&š&™(œ(×,Ò,Ñ.Ô.°°Ñ3Ô3Ð3rE   c                 ó.  — t          j        | ¦  «        } t          j        |¦  «        }|€| j        d         dk    rd}nd}|                     ¦   «         }|dk    rt	          | |¦  «        }n)|dk    rt          | |¦  «        }nt          d|› �¦  «        ‚|S )a	  
    Solves the discrete Lyapunov equation :math:`AXA^H - X + Q = 0`.

    Parameters
    ----------
    a, q : (M, M) array_like
        Square matrices corresponding to A and Q in the equation
        above respectively. Must have the same shape.

    method : {'direct', 'bilinear'}, optional
        Type of solver.

        If not given, chosen to be ``direct`` if ``M`` is less than 10 and
        ``bilinear`` otherwise.

    Returns
    -------
    x : ndarray
        Solution to the discrete Lyapunov equation

    See Also
    --------
    solve_continuous_lyapunov : computes the solution to the continuous-time
        Lyapunov equation

    Notes
    -----
    This section describes the available solvers that can be selected by the
    'method' parameter. The default method is *direct* if ``M`` is less than 10
    and ``bilinear`` otherwise.

    Method *direct* uses a direct analytical solution to the discrete Lyapunov
    equation. The algorithm is given in, for example, [1]_. However, it requires
    the linear solution of a system with dimension :math:`M^2` so that
    performance degrades rapidly for even moderately sized matrices.

    Method *bilinear* uses a bilinear transformation to convert the discrete
    Lyapunov equation to a continuous Lyapunov equation :math:`(BX+XB'=-C)`
    where :math:`B=(A-I)(A+I)^{-1}` and
    :math:`C=2(A' + I)^{-1} Q (A + I)^{-1}`. The continuous equation can be
    efficiently solved since it is a special case of a Sylvester equation.
    The transformation algorithm is from Popov (1964) as described in [2]_.

    .. versionadded:: 0.11.0

    References
    ----------
    .. [1] "Lyapunov equation", Wikipedia,
       https://en.wikipedia.org/wiki/Lyapunov_equation#Discrete_time
    .. [2] Gajic, Z., and M.T.J. Qureshi. 2008.
       Lyapunov Matrix Equation in System Stability and Control.
       Dover Books on Engineering Series. Dover Publications.

    Examples
    --------
    Given `a` and `q` solve for `x`:

    >>> import numpy as np
    >>> from scipy import linalg
    >>> a = np.array([[0.2, 0.5],[0.7, -0.9]])
    >>> q = np.eye(2)
    >>> x = linalg.solve_discrete_lyapunov(a, q)
    >>> x
    array([[ 0.70872893,  1.43518822],
           [ 1.43518822, -2.4266315 ]])
    >>> np.allclose(a.dot(x).dot(a.T)-x, -q)
    True

    Nr   é
   ÚbilinearÚdirectzUnknown solver )r,   Úasarrayr2   Úlowerre   ri   rV   )r8   r:   ÚmethodÚmethrd   s        rD   r   r   ø   s¢   € õL 	Œ
�1‰Œ€AÝ
Œ
�1‰Œ€AØ€~àŒ7�1Œ:˜ÒÐØˆFˆFàˆFà�<Š<‰>Œ>€DàˆxÒÐÝ+¨A¨qÑ1Ô1ˆˆØ	�Ò	Ð	Ý-¨a°Ñ3Ô3ˆˆåÐ3¨6Ð3Ð3Ñ4Ô4Ð4à€HrE   Tc           
      óŠ  — t          | |||||d¦  «        \
  } }}}}}}}}	}
t          j        d|z  |z   d|z  |z   f|	¬¦  «        }| |d|…d|…f<   d|d|…|d|z  …f<   ||d|…d|z  d…f<   | ||d|z  …d|…f<   |                      ¦   «         j         ||d|z  …|d|z  …f<   |€dn| ||d|z  …d|z  d…f<   |€dn|                     ¦   «         j        |d|z  d…d|…f<   |                     ¦   «         j        |d|z  d…|d|z  …f<   ||d|z  d…d|z  d…f<   |
r?|�=t          ||                     ¦   «         j        t          j        ||	¬¦  «        ¦  «        }n9t          t          j        d|z  ¦  «        t          j        ||	¬¦  «        ¦  «        }|�rt          j        |¦  «        t          j        |¦  «        z   }t          j	        |d¦  «         t          |dd¬¦  «        \  }\  }}t          j        |t          j        |¦  «        ¦  «        s�t          j        |¦  «        }t          j        ||d|z  …         |d|…         z
  dz  ¦  «        }dt          j        || |d|z  d…         f         z  }|dd…df         t          j        |¦  «        z  }||z  }||z  }t#          |dd…| d…f         ¦  «        \  }}|dd…|d…f                              ¦   «         j                             |dd…dd|z  …f         ¦  «        }|dd|z  …|d…f                              ¦   «         j                             |dd|z  …dd|z  …f         ¦  «        }|	t&          u rd	nd
}t)          ||dddd|¬¦  «        \  }}}}}}|�Qt#          t          j        |                     |d|…d|…f         ¦  «        ||d…d|…f         f¦  «        ¦  «        \  }}|d|…d|…f         }||d…d|…f         }t-          |¦  «        \  }}}dt/          |¦  «        z  t          j        d¦  «        k     rt3          d¦  «        ‚t5          |                     ¦   «         j        t5          |                     ¦   «         j        |                     ¦   «         j        d¬¦  «        d¬¦  «                             ¦   «         j                             |                     ¦   «         j        ¦  «        }|r||d|…df         |d|…         z  z  }|                     ¦   «         j                             |¦  «        }t7          |d¦  «        }||                     ¦   «         j        z
  }t          j        t          j        d¦  «        d|z  g¦  «        }t7          |d¦  «        |k    rt3          d¦  «        ‚||                     ¦   «         j        z   dz  S )a  
    Solves the continuous-time algebraic Riccati equation (CARE).

    The CARE is defined as

    .. math::

          X A + A^H X - X B R^{-1} B^H X + Q = 0

    The limitations for a solution to exist are :

        * All eigenvalues of :math:`A` on the right half plane, should be
          controllable.

        * The associated hamiltonian pencil (See Notes), should have
          eigenvalues sufficiently away from the imaginary axis.

    Moreover, if ``e`` or ``s`` is not precisely ``None``, then the
    generalized version of CARE

    .. math::

          E^HXA + A^HXE - (E^HXB + S) R^{-1} (B^HXE + S^H) + Q = 0

    is solved. When omitted, ``e`` is assumed to be the identity and ``s``
    is assumed to be the zero matrix with sizes compatible with ``a`` and
    ``b``, respectively.

    Parameters
    ----------
    a : (M, M) array_like
        Square matrix
    b : (M, N) array_like
        Input
    q : (M, M) array_like
        Input
    r : (N, N) array_like
        Nonsingular square matrix
    e : (M, M) array_like, optional
        Nonsingular square matrix
    s : (M, N) array_like, optional
        Input
    balanced : bool, optional
        The boolean that indicates whether a balancing step is performed
        on the data. The default is set to True.

    Returns
    -------
    x : (M, M) ndarray
        Solution to the continuous-time algebraic Riccati equation.

    Raises
    ------
    LinAlgError
        For cases where the stable subspace of the pencil could not be
        isolated. See Notes section and the references for details.

    See Also
    --------
    solve_discrete_are : Solves the discrete-time algebraic Riccati equation

    Notes
    -----
    The equation is solved by forming the extended hamiltonian matrix pencil,
    as described in [1]_, :math:`H - \lambda J` given by the block matrices ::

        [ A    0    B ]             [ E   0    0 ]
        [-Q  -A^H  -S ] - \lambda * [ 0  E^H   0 ]
        [ S^H B^H   R ]             [ 0   0    0 ]

    and using a QZ decomposition method.

    In this algorithm, the fail conditions are linked to the symmetry
    of the product :math:`U_2 U_1^{-1}` and condition number of
    :math:`U_1`. Here, :math:`U` is the 2m-by-m matrix that holds the
    eigenvectors spanning the stable subspace with 2-m rows and partitioned
    into two m-row matrices. See [1]_ and [2]_ for more details.

    In order to improve the QZ decomposition accuracy, the pencil goes
    through a balancing step where the sum of absolute values of
    :math:`H` and :math:`J` entries (after removing the diagonal entries of
    the sum) is balanced following the recipe given in [3]_.

    .. versionadded:: 0.11.0

    References
    ----------
    .. [1]  P. van Dooren , "A Generalized Eigenvalue Approach For Solving
       Riccati Equations.", SIAM Journal on Scientific and Statistical
       Computing, Vol.2(2), :doi:`10.1137/0902010`

    .. [2] A.J. Laub, "A Schur Method for Solving Algebraic Riccati
       Equations.", Massachusetts Institute of Technology. Laboratory for
       Information and Decision Systems. LIDS-R ; 859. Available online :
       http://hdl.handle.net/1721.1/1301

    .. [3] P. Benner, "Symplectic Balancing of Hamiltonian Matrices", 2001,
       SIAM J. Sci. Comput., 2001, Vol.22(5), :doi:`10.1137/S1064827500367993`

    Examples
    --------
    Given `a`, `b`, `q`, and `r` solve for `x`:

    >>> import numpy as np
    >>> from scipy import linalg
    >>> a = np.array([[4, 3], [-4.5, -3.5]])
    >>> b = np.array([[1], [-1]])
    >>> q = np.array([[9, 6], [6, 4.]])
    >>> r = 1
    >>> x = linalg.solve_continuous_are(a, b, q, r)
    >>> x
    array([[ 21.72792206,  14.48528137],
           [ 14.48528137,   9.65685425]])
    >>> np.allclose(a.T.dot(x) + x.dot(a)-x.dot(b).dot(b.T).dot(x), -q)
    True

    ÚcarerN   r#   Nç        r   r   ©ÚseparateÚpermuter%   rT   ÚlhpTF©ÚsortÚoverwrite_aÚoverwrite_brH   r'   ç      ð?ú!Failed to find a finite solution.©ro   ©Úunit_diagonalç     @�@çš™™™™™¹?zQThe associated Hamiltonian pencil has eigenvalues too close to the imaginary axis)Ú_are_validate_argsr,   r1   r4   rM   r   Ú
zeros_liker`   ÚabsÚfill_diagonalr   ÚallcloseÚ	ones_likeÚlog2ÚroundÚr_Ú
reciprocalr   r6   rQ   r   Úvstackr   r   Úspacingr   r
   r   Úmax)r8   r9   r:   r=   Úer   ÚbalancedÚmÚnrZ   Úgen_areÚHÚJÚMr\   ÚscaÚelwisescaleÚout_strr>   Úu00Úu10ÚupÚulÚuurd   Úu_symÚn_u_symÚsym_thresholds                               rD   r   r   S  sò  € õp /AØ56¸¸1¸aÀÀAÀvñ/Oô /OÑ+€A€qˆ!ˆQ��1�a˜˜F Gõ 	Œ�!�A‘#�a‘%˜˜1™˜Q™� vÐ.Ñ.Ô.€AØ€A€b€q€bˆ"ˆ1ˆ"€f�IØ€A€b€q€bˆ!ˆAˆa‰Cˆ%€i�LØ€A€b€q€bˆ!ˆA‰#ˆ$ˆ$€h�KØ�2€A€aˆˆ!‰€eˆRˆaˆR€i�LØ—v’v‘x”x”z�k€A€aˆˆ!‰€eˆQˆq�‰sˆU€l�OØ˜9�R�R¨1¨"€A€aˆˆ!‰€eˆQˆq‰SˆTˆT€k�NØ˜	�"�" q§v¢v¡x¤x¤z€A€aˆ�c€d€dˆBˆQˆB€h�KØ—V’V‘X”X”Z€A€aˆ�c€d€dˆAˆa�‰cˆE€k�NØ€A€aˆ�c€d€dˆAˆa‰CˆDˆD€j�Màð D�1�=Ý�q˜!Ÿ&š&™(œ(œ*¥b¤m°A¸VÐ&DÑ&DÔ&DÑEÔEˆˆå•r”v˜a ™c‘{”{¥B¤M°!¸6Ð$BÑ$BÔ$BÑCÔCˆàñ õ ŒF�1‰IŒI�œ˜q™	œ	Ñ!ˆÝ
Ô˜˜BÑÔÐÝ$ Q°¸AÐ>Ñ>Ô>‰ˆ‰8ˆC�åŒ{˜3¥¤¨SÑ 1Ô 1Ñ2Ô2ð 
	õ ”'˜#‘,”,ˆCå”˜#˜a  !¡˜eœ* s¨2¨A¨2¤wÑ.°Ñ1Ñ2Ô2ˆAØ•r”u˜Q   C¨¨!©¨¨¤IÐ-Ô.Ñ.ˆCà˜a˜a˜a ˜gœ,­¬°sÑ);Ô);Ñ;ˆKØ�ÑˆAØ�ÑˆAõ ˆa����A�2�3�3�Œi‰=Œ=�D€A€qØ	ˆ!ˆ!ˆ!ˆQˆRˆRˆ%Œ�Š‰ŒÔ×Ò˜a    4 A a¡C 4 œjÑ)Ô)€AØ	ˆ$ˆ1ˆQ‰3ˆ$���ˆ(Œ×ÒÑÔÔ× Ò   4 A a¡C 4¨¨!¨A©#¨ :¤Ñ/Ô/€Að ¥%˜˜ˆfˆf¨Y€Gå˜Q ¨¸4Ø)-¸EØ$+ð-ñ -ô -Ñ€A€qˆ!ˆQ��1ð
 	€}Ý•"”)˜QŸUšU 1 R a R¨¨!¨ V¤9Ñ-Ô-¨q°°°°R°a°R°¬yÐ9Ñ:Ô:Ñ;Ô;‰ˆˆ1Ø
ˆBˆQˆB���ˆFŒ)€CØ
ˆAˆBˆB���ˆFŒ)€Cõ �C‘”�J€BˆˆBØ�ˆb‰Œ�z•B”J˜r‘N”NÒ"Ð"ÝÐ=Ñ>Ô>Ð>õ 	˜Ÿš™œœÝ)¨"¯'ª'©)¬)¬+Ø*-¯(ª(©*¬*¬,Ø04ð6ñ 6ô 6ð (,ð		ñ 	ô 	÷
  š4™6œ6¤!§C¢C¨¯ª©	¬	¬Ñ$4Ô$4ð ð ð %Ø	ˆS��!��T�Œ]˜S  ! œWÑ$Ñ$ˆð �HŠH‰JŒJŒL×Ò˜SÑ!Ô!€EÝ�5˜!‰nŒn€GØ�E—J’J‘L”L”NÑ"€EÝ”F�BœJ uÑ-Ô-¨s°7©{Ð;Ñ<Ô<€MåˆE�1�~„~˜Ò%Ð%Ýð <ñ =ô =ð 	=ð �—’‘””
‰N˜AÑÐrE   c           
      óX  — t          | |||||d¦  «        \
  } }}}}}}}}	}
t          j        d|z  |z   d|z  |z   f|	¬¦  «        }| |d|…d|…f<   ||d|…d|z  d…f<   | ||d|z  …d|…f<   |€t          j        |¦  «        n|                     ¦   «         j        ||d|z  …|d|z  …f<   |€dn| ||d|z  …d|z  d…f<   |€dn|                     ¦   «         j        |d|z  d…d|…f<   ||d|z  d…d|z  d…f<   t          j        ||	¬¦  «        }|€t          j        |¦  «        n||d|…d|…f<   |                      ¦   «         j        ||d|z  …|d|z  …f<   |                     ¦   «         j         |d|z  d…|d|z  …f<   |�rt          j        |¦  «        t          j        |¦  «        z   }t          j        |d¦  «         t          |dd¬¦  «        \  }\  }}t          j
        |t          j        |¦  «        ¦  «        s�t          j        |¦  «        }t          j        ||d|z  …         |d|…         z
  dz  ¦  «        }dt          j        || |d|z  d…         f         z  }|dd…df         t          j        |¦  «        z  }||z  }||z  }t!          |dd…| d…f         ¦  «        \  }}|dd…|d…f                              ¦   «         j                             |dd…dd|z  …f         ¦  «        }|dd…|d…f                              ¦   «         j                             |dd…dd|z  …f         ¦  «        }|	t$          u rd	nd
}t'          ||dddd|¬¦  «        \  }}}}}}|�Qt!          t          j        |                     |d|…d|…f         ¦  «        ||d…d|…f         f¦  «        ¦  «        \  }}|d|…d|…f         }||d…d|…f         }t+          |¦  «        \  }}}dt-          |¦  «        z  t          j        d¦  «        k     rt1          d¦  «        ‚t3          |                     ¦   «         j        t3          |                     ¦   «         j        |                     ¦   «         j        d¬¦  «        d¬¦  «                             ¦   «         j                             |                     ¦   «         j        ¦  «        }|r||d|…df         |d|…         z  z  }|                     ¦   «         j                             |¦  «        }t5          |d¦  «        }||                     ¦   «         j        z
  }t          j        t          j        d¦  «        d|z  g¦  «        }t5          |d¦  «        |k    rt1          d¦  «        ‚||                     ¦   «         j        z   dz  S )al  
    Solves the discrete-time algebraic Riccati equation (DARE).

    The DARE is defined as

    .. math::

          A^HXA - X - (A^HXB) (R + B^HXB)^{-1} (B^HXA) + Q = 0

    The limitations for a solution to exist are :

        * All eigenvalues of :math:`A` outside the unit disc, should be
          controllable.

        * The associated symplectic pencil (See Notes), should have
          eigenvalues sufficiently away from the unit circle.

    Moreover, if ``e`` and ``s`` are not both precisely ``None``, then the
    generalized version of DARE

    .. math::

          A^HXA - E^HXE - (A^HXB+S) (R+B^HXB)^{-1} (B^HXA+S^H) + Q = 0

    is solved. When omitted, ``e`` is assumed to be the identity and ``s``
    is assumed to be the zero matrix.

    Parameters
    ----------
    a : (M, M) array_like
        Square matrix
    b : (M, N) array_like
        Input
    q : (M, M) array_like
        Input
    r : (N, N) array_like
        Square matrix
    e : (M, M) array_like, optional
        Nonsingular square matrix
    s : (M, N) array_like, optional
        Input
    balanced : bool
        The boolean that indicates whether a balancing step is performed
        on the data. The default is set to True.

    Returns
    -------
    x : (M, M) ndarray
        Solution to the discrete algebraic Riccati equation.

    Raises
    ------
    LinAlgError
        For cases where the stable subspace of the pencil could not be
        isolated. See Notes section and the references for details.

    See Also
    --------
    solve_continuous_are : Solves the continuous algebraic Riccati equation

    Notes
    -----
    The equation is solved by forming the extended symplectic matrix pencil,
    as described in [1]_, :math:`H - \lambda J` given by the block matrices ::

           [  A   0   B ]             [ E   0   B ]
           [ -Q  E^H -S ] - \lambda * [ 0  A^H  0 ]
           [ S^H  0   R ]             [ 0 -B^H  0 ]

    and using a QZ decomposition method.

    In this algorithm, the fail conditions are linked to the symmetry
    of the product :math:`U_2 U_1^{-1}` and condition number of
    :math:`U_1`. Here, :math:`U` is the 2m-by-m matrix that holds the
    eigenvectors spanning the stable subspace with 2-m rows and partitioned
    into two m-row matrices. See [1]_ and [2]_ for more details.

    In order to improve the QZ decomposition accuracy, the pencil goes
    through a balancing step where the sum of absolute values of
    :math:`H` and :math:`J` rows/cols (after removing the diagonal entries)
    is balanced following the recipe given in [3]_. If the data has small
    numerical noise, balancing may amplify their effects and some clean up
    is required.

    .. versionadded:: 0.11.0

    References
    ----------
    .. [1]  P. van Dooren , "A Generalized Eigenvalue Approach For Solving
       Riccati Equations.", SIAM Journal on Scientific and Statistical
       Computing, Vol.2(2), :doi:`10.1137/0902010`

    .. [2] A.J. Laub, "A Schur Method for Solving Algebraic Riccati
       Equations.", Massachusetts Institute of Technology. Laboratory for
       Information and Decision Systems. LIDS-R ; 859. Available online :
       http://hdl.handle.net/1721.1/1301

    .. [3] P. Benner, "Symplectic Balancing of Hamiltonian Matrices", 2001,
       SIAM J. Sci. Comput., 2001, Vol.22(5), :doi:`10.1137/S1064827500367993`

    Examples
    --------
    Given `a`, `b`, `q`, and `r` solve for `x`:

    >>> import numpy as np
    >>> from scipy import linalg as la
    >>> a = np.array([[0, 1], [0, -1]])
    >>> b = np.array([[1, 0], [2, 1]])
    >>> q = np.array([[-4, -4], [-4, 7]])
    >>> r = np.array([[9, 3], [3, 1]])
    >>> x = la.solve_discrete_are(a, b, q, r)
    >>> x
    array([[-4., -4.],
           [-4.,  7.]])
    >>> R = la.solve(r + b.T.dot(x).dot(b), b.T.dot(x).dot(a))
    >>> np.allclose(a.T.dot(x).dot(a) - x - a.T.dot(x).dot(b).dot(R), -q)
    True

    ÚdarerN   r#   Nrt   r   r   ru   r%   rT   ÚiucTFry   r}   r~   r   r€   r‚   rƒ   zMThe associated symplectic pencil has eigenvalues too close to the unit circle)r„   r,   Úzerosr`   r4   rM   r…   r†   r‡   r   rˆ   r‰   rŠ   r‹   rŒ   r�   r   r6   rQ   r   rŽ   r   r   r�   r   r
   r   r�   )r8   r9   r:   r=   r‘   r   r’   r“   r”   rZ   r•   r–   r—   r˜   r\   r™   rš   Úq_of_qrr›   r>   rœ   r�   rž   rŸ   r    rd   r¡   r¢   r£   s                                rD   r   r      sä  € õt /AØ56¸¸1¸aÀÀAÀvñ/Oô /OÑ+€A€qˆ!ˆQ��1�a˜˜F Gõ 	Œ�!�A‘#�a‘%˜˜1™˜Q™� vÐ.Ñ.Ô.€AØ€A€b€q€bˆ"ˆ1ˆ"€f�IØ€A€b€q€bˆ!ˆA‰#ˆ$ˆ$€h�KØ�2€A€aˆˆ!‰€eˆRˆaˆR€i�LØ#$ 9•b”f˜Q‘i”i�i°!·&²&±(´(´*€A€aˆˆ!‰€eˆQˆq�‰sˆU€l�OØ˜9�R�R¨1¨"€A€aˆˆ!‰€eˆQˆq‰SˆTˆT€k�NØ˜	�"�" q§v¢v¡x¤x¤z€A€aˆ�c€d€dˆBˆQˆB€h�KØ€A€aˆ�c€d€dˆAˆa‰CˆDˆD€j�Må
Œ�a˜vÐ&Ñ&Ô&€AØ˜Y•”�q‘	”	�	¨A€A€b€q€bˆ"ˆ1ˆ"€f�IØ—f’f‘h”h”j€A€aˆˆ!‰€eˆQˆq�‰sˆU€l�OØ—f’f‘h”h”j�[€A€aˆ�c€d€dˆAˆa�‰cˆE€k�Nàñ õ ŒF�1‰IŒI�œ˜q™	œ	Ñ!ˆÝ
Ô˜˜BÑÔÐÝ$ Q°¸AÐ>Ñ>Ô>‰ˆ‰8ˆC�åŒ{˜3¥¤¨SÑ 1Ô 1Ñ2Ô2ð 
	õ ”'˜#‘,”,ˆCå”˜#˜a  !¡˜eœ* s¨2¨A¨2¤wÑ.°Ñ1Ñ2Ô2ˆAØ•r”u˜Q   C¨¨!©¨¨¤IÐ-Ô.Ñ.ˆCà˜a˜a˜a ˜gœ,­¬°sÑ);Ô);Ñ;ˆKØ�ÑˆAØ�ÑˆAõ �A�a�a�a˜!˜˜˜�f”I‘”�J€GˆQØ����1�2�2�Œ×ÒÑÔÔ×#Ò# A a a a¨¨!¨A©#¨ g¤JÑ/Ô/€AØ����1�2�2�Œ×ÒÑÔÔ×#Ò# A a a a¨¨!¨A©#¨ g¤JÑ/Ô/€Að ¥%˜˜ˆfˆf¨Y€Gå˜Q ¨Ø)-Ø)-Ø*/Ø$+ð	-ñ -ô -Ñ€A€qˆ!ˆQ��1ð 	€}Ý•"”)˜QŸUšU 1 R a R¨¨!¨ V¤9Ñ-Ô-¨q°°°°R°a°R°¬yÐ9Ñ:Ô:Ñ;Ô;‰ˆˆ1Ø
ˆBˆQˆB���ˆFŒ)€CØ
ˆAˆBˆB���ˆFŒ)€Cõ �C‘”�J€BˆˆBà�ˆb‰Œ�z•B”J˜r‘N”NÒ"Ð"ÝÐ=Ñ>Ô>Ð>õ 	˜Ÿš™œœÝ)¨"¯'ª'©)¬)¬+Ø*-¯(ª(©*¬*¬,Ø04ð6ñ 6ô 6ð (,ð		ñ 	ô 	÷
  š4™6œ6¤!§C¢C¨¯ª©	¬	¬Ñ$4Ô$4ð ð ð %Ø	ˆS��!��T�Œ]˜S  ! œWÑ$Ñ$ˆð �HŠH‰JŒJŒL×Ò˜SÑ!Ô!€EÝ�5˜!‰nŒn€GØ�E—J’J‘L”L”NÑ"€EÝ”F�BœJ uÑ-Ô-¨s°7©{Ð;Ñ<Ô<€MåˆE�1�~„~˜Ò%Ð%Ýð 9ñ :ô :ð 	:ð �—’‘””
‰N˜AÑÐrE   rs   c           
      óŒ  — |                      ¦   «         dvrt          d¦  «        ‚t          j        t	          | d¬¦  «        ¦  «        } t          j        t	          |d¬¦  «        ¦  «        }t          j        t	          |d¬¦  «        ¦  «        }t          j        t	          |d¬¦  «        ¦  «        }t          j        |¦  «        rt          nt          }t          | ||f¦  «        D ]L\  }}	t          j        |	¦  «        rt          }t          j	        |	j
        Ž st          dd|         › d�¦  «        ‚ŒM|j
        \  }
}|
| j
        d         k    rt          d	¦  «        ‚|
|j
        d         k    rt          d
¦  «        ‚||j
        d         k    rt          d¦  «        ‚t          ||f¦  «        D ]o\  }}	t          |	|	                     ¦   «         j        z
  d¦  «        t          j        t          |	d¦  «        ¦  «        dz  k    rt          dd|         › d�¦  «        ‚Œp|dk    rUt          |d¬¦  «        d         }|dk    s)|t          j        d¦  «        t          |d¦  «        z  k     rt          d¦  «        ‚|dup|du}|�r6|�Õt          j        t	          |d¬¦  «        ¦  «        }t          j	        |j
        Ž st          d¦  «        ‚|
|j
        d         k    rt          d¦  «        ‚t          |d¬¦  «        d         }|dk    s)|t          j        d¦  «        t          |d¦  «        z  k     rt          d¦  «        ‚t          j        |¦  «        rt          }|�]t          j        t	          |d¬¦  «        ¦  «        }|j
        |j
        k    rt          d¦  «        ‚t          j        |¦  «        rt          }| ||||||
|||f
S )a‹  
    A helper function to validate the arguments supplied to the
    Riccati equation solvers. Any discrepancy found in the input
    matrices leads to a ``ValueError`` exception.

    Essentially, it performs:

        - a check whether the input is free of NaN and Infs
        - a pass for the data through ``numpy.atleast_2d()``
        - squareness check of the relevant arrays
        - shape consistency check of the arrays
        - singularity check of the relevant arrays
        - symmetricity check of the relevant matrices
        - a check whether the regular or the generalized version is asked.

    This function is used by ``solve_continuous_are`` and
    ``solve_discrete_are``.

    Parameters
    ----------
    a, b, q, r, e, s : array_like
        Input data
    eq_type : str
        Accepted arguments are 'care' and 'dare'.

    Returns
    -------
    a, b, q, r, e, s : ndarray
        Regularized input data
    m, n : int
        shape of the problem
    r_or_c : type
        Data type of the problem, returns float or complex
    gen_or_not : bool
        Type of the equation, True for generalized and False for regular ARE.

    )r¥   rs   z;Equation type unknown. Only 'care' and 'dare' is understoodTrG   rI   ÚaqrrK   r   z3Matrix a and b should have the same number of rows.rL   z3Matrix b and r should have the same number of cols.r   éd   r   z should be symmetric/hermitian.rs   F)Ú
compute_uvéÿÿÿÿrt   r}   z!Matrix r is numerically singular.NzMatrix e should be square.z*Matrix a and e should have the same shape.z!Matrix e is numerically singular.z*Matrix b and s should have the same shape.)ro   rV   r,   rP   r   rS   rT   rQ   rR   rU   r2   r   r4   rM   r�   r   )r8   r9   r:   r=   r‘   r   Úeq_typerZ   r[   Úmatr“   r”   Úmin_svÚgeneralized_cases                 rD   r„   r„   ñ  sÏ  € ðN ‡}‚}�„Ð.Ð.Ð.Ýð @ñ Aô Að 	Aõ 	ŒÕ(¨¸Ð>Ñ>Ô>Ñ?Ô?€AÝ
ŒÕ(¨¸Ð>Ñ>Ô>Ñ?Ô?€AÝ
ŒÕ(¨¸Ð>Ñ>Ô>Ñ?Ô?€AÝ
ŒÕ(¨¸Ð>Ñ>Ô>Ñ?Ô?€Aõ œ¨Ñ*Ô*Ð5�WˆWµ€Få˜q ! Q˜iÑ(Ô(ð Gð G‰ˆˆSÝŒ?˜3ÑÔð 	ÝˆFåŒx˜œÐ#ð 	GÝÐE u¨S¤zÐEÐEÐEÑFÔFÐFð	Gð Œ7�D€A€qØˆAŒG�AŒJ‚€ÝÐNÑOÔOÐOØˆAŒG�AŒJ‚€ÝÐEÑFÔFÐFØˆAŒG�AŒJ‚€ÝÐNÑOÔOÐOõ ˜q !˜fÑ%Ô%ð Sð S‰ˆˆSÝ��c—h’h‘j”j”lÑ" AÑ&Ô&­¬µD¸¸a±L´LÑ)AÔ)AÀ#Ñ)EÒEÐEÝÐQ t¨C¤yÐQÐQÐQÑRÔRÐRð Fð �&ÒÐÝ�Q 5Ð)Ñ)Ô)¨"Ô-ˆØ�RŠ<ˆ<˜6¥B¤J¨r¡N¤Nµ4¸¸1±:´:Ñ$=Ò=Ð=ÝÐ@ÑAÔAÐAð  �}Ð5¨°¨Ðàñ !Øˆ=Ý”Õ0°ÀÐFÑFÔFÑGÔGˆAÝ”8˜QœWÐ%ð ?Ý Ð!=Ñ>Ô>Ð>Ø�A”G˜A”JŠˆÝ Ð!MÑNÔNÐNõ ˜ uÐ-Ñ-Ô-¨bÔ1ˆFØ˜Š|ˆ|˜v­¬
°2©¬½¸aÀ¹¼Ñ(CÒCÐCÝ Ð!DÑEÔEÐEÝŒ˜qÑ!Ô!ð !Ý �Øˆ=Ý”Õ0°ÀÐFÑFÔFÑGÔGˆAØŒw˜!œ'Ò!Ð!Ý Ð!MÑNÔNÐNÝŒ˜qÑ!Ô!ð !Ý �àˆa��A�q˜!˜Q  6Ð+;Ð;Ð;rE   )N)NNT)rs   )&Ú__doc__rW   Únumpyr,   Únumpy.linalgr   r   r   r   r   Ú_basicr	   r
   r   Úlapackr   Ú_decomp_schurr   Ú
_decomp_lur   Ú
_decomp_qrr   Ú
_decomp_qzr   Ú_decompr   Ú_special_matricesr   Ú__all__r   r   r   re   ri   r   r   r   r„   © rE   rD   ú<module>r¿      s³  ðØ %Ð %ð €€€Ø Ð Ð Ð Ø :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ð :à ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø $Ð $Ð $Ð $Ð $Ð $Ø  Ð  Ð  Ð  Ð  Ð  Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø )Ð )Ð )Ð )Ð )Ð )ð9ð 9ð 9€ðR6ð R6ð R6ðj`$ð `$ð `$ðH +€ð"ð "ð "ð4ð 4ð 4ðXð Xð Xð XðvJð Jð Jð JðZNð Nð Nð Nðbh<ð h<ð h<ð h<ð h<ð h<rE   