Ë
    ü�DjÏq  ã                   óä   — 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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&y)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)ÚkronÚ
block_diag)Úsolve_sylvesterÚsolve_continuous_lyapunovÚsolve_discrete_lyapunovÚsolve_lyapunovÚsolve_continuous_areÚsolve_discrete_arec                 ó"  — t        | d¬«      \  }}t        |j                  «       j                  «       d¬«      \  }}t        j                  t        j                  |j                  «       j                  «       |«      |«      }t        d|||f«      \  }|€t        d«      ‚ ||||d¬«      \  }	}
}|
|	z  }	|dk  rt        d| fz  «      ‚t        j                  t        j                  ||	«      |j                  «       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

    Úreal©Úoutput)ÚtrsylzQLAPACK implementation does not contain a proper Sylvester equation solver (TRSYL)ÚC©Útranbr   z(Illegal value encountered in the %d term)r   ÚconjÚ	transposeÚnpÚdotr   ÚRuntimeErrorr   )ÚaÚbÚqÚrÚuÚsÚvÚfr   ÚyÚscaleÚinfos               úYC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\scipy/linalg/_solvers.pyr   r      s  € ôn �˜6Ô"�D€A€qô �—‘“×#Ñ#Ó%¨fÔ5�D€A€qô 	�‰Œr�v‰v�a—f‘f“h×(Ñ(Ó*¨AÓ.°Ó2€Aô ˜j¨1¨a°¨)Ó4�F€EØ€}Üð ?ó @ð 	@á˜1˜a ¨#Ô.�N€A€uˆdàˆa‰€Aàˆa‚xÜð (Ø,0¨5¨(ñ3ó 4ð 	4ô �6‰6”"—&‘&˜˜A“, §¡£× 2Ñ 2Ó 4Ó5Ð5ó    c                 ót  — t        j                  t        | d¬«      «      } t        j                  t        |d¬«      «      }t        }t	        | |f«      D ]Y  \  }}t        j
                  |«      rt        }t        j                  |j                  Ž rŒ>t        dj                  d|   «      «      ‚ | j                  |j                  k7  rt        d«      ‚t        | d¬«      \  }}|j                  «       j                  j                  |j                  |«      «      }t        d||f«      }|t        k(  rd	nd
}	 |||||	¬«      \  }
}}|dk  rt        d| › d�«      ‚|dk(  rt!        j"                  dt$        d¬«       |
|z  }
|j                  |
«      j                  |j                  «       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 {} should be square.Úaqú*Matrix a and q should have the same shape.r   r   r   ÚTr   r    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ÚequalÚshapeÚ
ValueErrorÚformatr   r"   r:   r%   r   ÚwarningsÚwarnÚRuntimeWarning)r'   r)   Úr_or_cÚindÚ_r*   r+   r.   r   Údtype_stringr/   r0   r1   s                r2   r   r   m   s”  € ôd 	�‰Ô(¨¸Ô>Ó?€AÜ
�‰Ô(¨¸Ô>Ó?€Aä€Fä˜Q ˜FÓ#ò N‰ˆˆQÜ�?‰?˜1ÔÜˆFä�x‰x˜Ÿ™Ò!ÜÐ:×AÑAÀ$ÀsÁ)ÓLÓMÐMðNð 	‡w�w�!—'‘'ÒÜÐEÓFÐFô �˜6Ô"�D€A€qð 	
�‰‹�
‰
�‰�q—u‘u˜Q“xÓ €Aô ˜W q¨! fÓ-€Eà ¤Eš/‘3¨s€LÙ˜1˜a ¨,Ô7�N€A€uˆdàˆa‚xÜð >Ø?C¸e¸Wð ELðLó Mð 	Mð 
�ŠÜ�‰ð Bô %°õ	4ð ˆ�J€Aà�5‰5�‹8�<‰<˜Ÿ™›Ÿ
™
Ó#Ð#r3   c                 óô   — t        | | j                  «       «      }t        j                  |j                  d   «      |z
  }t        ||j                  «       «      }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   r"   r$   ÚeyerC   r	   ÚflattenÚreshape)r'   r)   ÚlhsÚxs       r2   Ú_solve_discrete_lyapunov_directrS   Í   sX   € ô ˆq�!—&‘&“(Ó
€CÜ
�&‰&�—‘˜1‘Ó
 Ñ
$€CÜˆc�1—9‘9“;Ó€Aä�:‰:�a˜Ÿ™Ó!Ð!r3   c           	      ó”  — t        j                  | j                  d   «      }| j                  «       j	                  «       }t        ||z   «      }t        j                  ||z
  |«      }dt        j                  t        j                  t        | |z   «      |«      |«      z  }t        |j                  «       j	                  «       | «      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   r;   )r$   rN   rC   r"   r#   r   r%   r   )r'   r)   rN   ÚaHÚaHI_invr(   Úcs          r2   Ú!_solve_discrete_lyapunov_bilinearrX   Ü   s•   € ô �&‰&�—‘˜‘Ó
€CØ	
�‰‹×	Ñ	Ó	€BÜ�"�s‘(‹m€GÜ
�‰ˆr�C‰x˜Ó!€AØ	Œ"�&‰&”—‘œ˜A ™G› aÓ(¨'Ó
2Ñ2€AÜ˜!Ÿ&™&›(×,Ñ,Ó.°°Ó3Ð3r3   c                 ó  — t        j                  | «      } t        j                  |«      }|€| j                  d   dk\  rd}nd}|j                  «       }|dk(  rt	        | |«      }|S |dk(  rt        | |«      }|S t        d|z  «      ‚)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

    r   é
   ÚbilinearÚdirectzUnknown solver %s)r$   ÚasarrayrC   ÚlowerrS   rX   rD   )r'   r)   ÚmethodÚmethrR   s        r2   r   r   ë   s”   € ôL 	�
‰
�1‹€AÜ
�
‰
�1‹€AØ€~à�7‰7�1‰:˜ÒØ‰FàˆFà�<‰<‹>€DàˆxÒÜ+¨A¨qÓ1ˆð €Hð 
�Ò	Ü-¨a°Ó3ˆð €Hô Ð,¨vÑ5Ó6Ð6r3   c           
      óº
  — 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                  «       j                   ||d|z  …|d|z  …f<   |€dn| ||d|z  …d|z  d…f<   |€dn|j                  «       j                  |d|z  d…d|…f<   |j                  «       j                  |d|z  d…|d|z  …f<   ||d|z  d…d|z  d…f<   |
r=|�;t        ||j                  «       j                  t        j                  ||	¬«      «      }n7t        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                  «       j                  j%                  |dd…dd|z  …f   «      }|dd|z  …|d…f   j                  «       j                  j%                  |dd|z  …dd|z  …f   «      }|	t&        k(  rd	nd
}t)        ||dddd|¬«      \  }}}}}}|�Dt#        t        j*                  |j%                  |d|…d|…f   «      ||d…d|…f   f«      «      \  }}|d|…d|…f   }||d…d|…f   }t-        |«      \  }}}dt/        |«      z  t        j0                  d«      k  rt3        d«      ‚t5        |j                  «       j                  t5        |j                  «       j                  |j                  «       j                  d¬«      d¬«      j                  «       j                  j%                  |j                  «       j                  «      }|r|d|…df   |d| z  z  }|j                  «       j                  j%                  |«      }t7        |d«      }||j                  «       j                  z
  }t        j8                  t        j0                  d«      d|z  g«      }t7        |d«      |kD  rt3        d«      ‚||j                  «       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

    Úcarer;   ©ÚdtypeNç        r   r   ©ÚseparateÚpermuter   rA   ÚlhpTF©ÚsortÚoverwrite_aÚoverwrite_br6   r   ç      ð?ú!Failed to find a finite solution.©r^   ©Úunit_diagonalç     @�@çš™™™™™¹?zQThe associated Hamiltonian pencil has eigenvalues too close to the imaginary axis)Ú_are_validate_argsr$   Úemptyr"   r:   r   Ú
zeros_likerN   ÚabsÚfill_diagonalr   ÚallcloseÚ	ones_likeÚlog2ÚroundÚr_Ú
reciprocalr   r%   r>   r   Úvstackr   r   Úspacingr   r
   r   Úmax)r'   r(   r)   r*   Úer,   ÚbalancedÚmÚnrI   Úgen_areÚHÚJÚMrK   ÚscaÚelwisescaleÚout_strr+   Úu00Úu10ÚupÚulÚuurR   Úu_symÚn_u_symÚsym_thresholds                               r2   r   r   F  s8  € ôp /AØ56¸¸1¸aÀÀAÀvó/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—z‘z�k€A€aˆˆ!‰€eˆQˆq�‰sˆU€l�OØ˜9‘R¨1¨"€A€aˆˆ!‰€eˆQˆq‰S‰T€k�NØ˜	‘" q§v¡v£x§z¡z€A€aˆ�c�dˆBˆQˆB€h�KØ—V‘V“X—Z‘Z€A€aˆ�c�dˆAˆa�‰cˆE€k�NØ€A€aˆ�c�dˆAˆa‰C‰D€j�Má�1�=Ü�q˜!Ÿ&™&›(Ÿ*™*¤b§m¡m°A¸VÔ&DÓE‰ä”r—v‘v˜a ™c“{¤B§M¡M°!¸6Ô$BÓCˆâô �F‰F�1‹IœŸ™˜q›	Ñ!ˆÜ
×Ñ˜˜BÔÜ$ Q°¸AÔ>‰ˆ‰8ˆC�ä�{‰{˜3¤§¡¨SÓ 1Ô2ô —'‘'˜#“,ˆCä—‘˜#˜a  !¡˜* s¨2¨A wÑ.°Ñ1Ó2ˆAØ”r—u‘u˜Q   C¨¨!©¨ IÐ-Ñ.Ñ.ˆCàša ˜g™,¬¯©°sÓ);Ñ;ˆKØ�ÑˆAØ�ÑˆAô ˆa’�A�2‘3�‰i‹=�D€A€qØ	Š!ˆQ‰Rˆ%‰�‰‹×Ñ×Ñ˜a¢ 4 A a¡C 4 ™jÓ)€AØ	ˆ$ˆ1ˆQ‰3ˆ$�‘ˆ(‰×ÑÓ×Ñ× Ñ   4 A a¡C 4¨¨!¨A©#¨ :¡Ó/€Að ¤%š‰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���ˆF‰)€Cô �C“�J€BˆˆBØŒˆb‹�z”B—J‘J˜r“NÒ"ÜÐ=Ó>Ð>ô 	˜Ÿ™›Ÿ™Ü)¨"¯'©'«)¯+©+Ø*-¯(©(«*¯,©,Ø04ô6ð (,ô		÷
  ™4›6§!¡!§C¡C¨¯©«	¯©Ó$4ð ñ Ø	ˆS��!��T�‰]˜S  !˜WÑ$Ñ$ˆð �H‰H‹J�L‰L×Ñ˜SÓ!€EÜ�5˜!‹n€GØ�E—J‘J“L—N‘NÑ"€EÜ—F‘FœBŸJ™J uÓ-¨s°7©{Ð;Ó<€MäˆE�1ƒ~˜Ò%Üð <ó =ð 	=ð �—‘“—
‘
‰N˜AÑÐr3   c           
      ó˜
  — 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	                  «       j
                  ||d|z  …|d|z  …f<   |€dn| ||d|z  …d|z  d…f<   |€dn|j	                  «       j
                  |d|z  d…d|…f<   ||d|z  d…d|z  d…f<   t        j                  ||	¬«      }|€t        j                  |«      n||d|…d|…f<   | j	                  «       j
                  ||d|z  …|d|z  …f<   |j	                  «       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	                  «       j
                  j#                  |dd…dd|z  …f   «      }|dd…|d…f   j	                  «       j
                  j#                  |dd…dd|z  …f   «      }|	t$        k(  rd	nd
}t'        ||dddd|¬«      \  }}}}}}|�Dt!        t        j(                  |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	                  «       j
                  t3        |j	                  «       j
                  |j	                  «       j
                  d¬«      d¬«      j	                  «       j
                  j#                  |j	                  «       j
                  «      }|r|d|…df   |d| z  z  }|j	                  «       j
                  j#                  |«      }t5        |d«      }||j	                  «       j
                  z
  }t        j6                  t        j.                  d«      d|z  g«      }t5        |d«      |kD  rt1        d«      ‚||j	                  «       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

    Údarer;   rc   Nre   r   r   rf   r   rA   ÚiucTFrj   rn   ro   rp   rq   rs   rt   zMThe associated symplectic pencil has eigenvalues too close to the unit circle)ru   r$   ÚzerosrN   r"   r:   rw   rx   ry   r   rz   r{   r|   r}   r~   r   r   r%   r>   r   r€   r   r   r�   r   r
   r   r‚   )r'   r(   r)   r*   rƒ   r,   r„   r…   r†   rI   r‡   rˆ   r‰   rŠ   rK   r‹   rŒ   Úq_of_qrr�   r+   rŽ   r�   r�   r‘   r’   rR   r“   r”   r•   s                                r2   r   r     s/  € ôt /AØ56¸¸1¸aÀÀAÀvó/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‘f˜Q”i°!·&±&³(·*±*€A€aˆˆ!‰€eˆQˆq�‰sˆU€l�OØ˜9‘R¨1¨"€A€aˆˆ!‰€eˆQˆq‰S‰T€k�NØ˜	‘" q§v¡v£x§z¡z€A€aˆ�c�dˆBˆQˆB€h�KØ€A€aˆ�c�dˆAˆa‰C‰D€j�Mä
�‰�a˜vÔ&€AØ˜Y”—‘�q”	¨A€A€b€q€bˆ"ˆ1ˆ"€f�IØ—f‘f“h—j‘j€A€aˆˆ!‰€eˆQˆq�‰sˆU€l�OØ—f‘f“h—j‘j�[€A€aˆ�c�dˆAˆa�‰cˆE€k�Nâô �F‰F�1‹IœŸ™˜q›	Ñ!ˆÜ
×Ñ˜˜BÔÜ$ Q°¸AÔ>‰ˆ‰8ˆC�ä�{‰{˜3¤§¡¨SÓ 1Ô2ô —'‘'˜#“,ˆCä—‘˜#˜a  !¡˜* s¨2¨A wÑ.°Ñ1Ó2ˆAØ”r—u‘u˜Q   C¨¨!©¨ IÐ-Ñ.Ñ.ˆCàša ˜g™,¬¯©°sÓ);Ñ;ˆKØ�ÑˆAØ�ÑˆAô �A’a˜!˜™�f‘I“�J€GˆQØ’�1‘2�‰×ÑÓ×Ñ×#Ñ# A¢a¨¨!¨A©#¨ g¡JÓ/€AØ’�1‘2�‰×ÑÓ×Ñ×#Ñ# A¢a¨¨!¨A©#¨ g¡JÓ/€Að ¤%š‰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���ˆF‰)€Cô �C“�J€BˆˆBàŒˆb‹�z”B—J‘J˜r“NÒ"ÜÐ=Ó>Ð>ô 	˜Ÿ™›Ÿ™Ü)¨"¯'©'«)¯+©+Ø*-¯(©(«*¯,©,Ø04ô6ð (,ô		÷
  ™4›6§!¡!§C¡C¨¯©«	¯©Ó$4ð ñ Ø	ˆS��!��T�‰]˜S  !˜WÑ$Ñ$ˆð �H‰H‹J�L‰L×Ñ˜SÓ!€EÜ�5˜!‹n€GØ�E—J‘J“L—N‘NÑ"€EÜ—F‘FœBŸJ™J uÓ-¨s°7©{Ð;Ó<€MäˆE�1ƒ~˜Ò%Üð 9ó :ð 	:ð �—‘“—
‘
‰N˜AÑÐr3   c           
      ó*  — |j                  «       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 ]Y  \  }}	t        j
                  |	«      rt        }t        j                  |	j                  Ž rŒ>t        dj                  d|   «      «      ‚ |j                  \  }
}|
| j                  d   k7  rt        d«      ‚|
|j                  d   k7  rt        d	«      ‚||j                  d   k7  rt        d
«      ‚t        ||f«      D ]l  \  }}	t        |	|	j                  «       j                  z
  d«      t        j                  t        |	d«      «      dz  kD  sŒQt        dj                  d|   «      «      ‚ |dk(  rEt!        |d¬«      d   }|dk(  s%|t        j                  d«      t        |d«      z  k  rt        d«      ‚|duxs |du}|�r'|�Ät        j                  t	        |d¬«      «      }t        j                  |j                  Ž st        d«      ‚|
|j                  d   k7  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                  k7  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—   rb   z;Equation type unknown. Only 'care' and 'dare' is understoodTr5   r7   Úaqrr   z3Matrix a and b should have the same number of rows.r9   z3Matrix b and r should have the same number of cols.r   éd   z(Matrix {} should be symmetric/hermitian.r   rb   F)Ú
compute_uvéÿÿÿÿre   rn   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.)r^   rD   r$   r=   r   r@   rA   r>   r?   rB   rC   rE   r   r"   r:   r�   r   )r'   r(   r)   r*   rƒ   r,   Úeq_typerI   rJ   Úmatr…   r†   Úmin_svÚgeneralized_cases                 r2   ru   ru   ä  s*  € ðN ‡}�}ƒÐ.Ñ.Üð @ó Að 	Aô 	�‰Ô(¨¸Ô>Ó?€AÜ
�‰Ô(¨¸Ô>Ó?€AÜ
�‰Ô(¨¸Ô>Ó?€AÜ
�‰Ô(¨¸Ô>Ó?€Aô Ÿ™¨Ô*�W´€Fä˜q ! Q˜iÓ(ò O‰ˆˆSÜ�?‰?˜3ÔÜˆFä�x‰x˜Ÿ™Ò#ÜÐ:×AÑAÀ%ÈÁ*ÓMÓNÐNðOð �7‰7�D€A€qØˆA�G‰G�A‰J‚ÜÐNÓOÐOØˆA�G‰G�A‰J‚ÜÐEÓFÐFØˆA�G‰G�A‰J‚ÜÐNÓOÐOô ˜q !˜fÓ%ò 3‰ˆˆSÜ��c—h‘h“j—l‘lÑ" AÓ&¬¯©´D¸¸a³LÓ)AÀ#Ñ)EÓEÜð  ß &¡ t¨C¡yÓ 1ó3ð 3ð3ð �&ÒÜ�Q 5Ô)¨"Ñ-ˆØ�RŠ<˜6¤B§J¡J¨r£N´4¸¸1³:Ñ$=Ò=ÜÐ@ÓAÐAð  �}Ò5¨°¨ÐâØˆ=Ü—‘Ô0°ÀÔFÓGˆAÜ—8‘8˜QŸW™WÑ%Ü Ð!=Ó>Ð>Ø�A—G‘G˜A‘JŠÜ Ð!MÓNÐNô ˜ uÔ-¨bÑ1ˆFØ˜Š|˜v¬¯
©
°2«¼¸aÀ»Ñ(CÒCÜ Ð!DÓEÐEÜ�‰˜qÔ!Ü �Øˆ=Ü—‘Ô0°ÀÔFÓGˆAØ�w‰w˜!Ÿ'™'Ò!Ü Ð!MÓNÐNÜ�‰˜qÔ!Ü �àˆa��A�q˜!˜Q  6Ð+;Ð;Ð;r3   )N)NNT)rb   )'Ú__doc__rF   Únumpyr$   Únumpy.linalgr   r   r   r   r   Ú_basicr	   r
   r   Úlapackr   Ú_decomp_schurr   Ú
_decomp_lur   Ú
_decomp_qrr   Ú
_decomp_qzr   Ú_decompr   Ú_special_matricesr   r   Ú__all__r   r   r   rS   rX   r   r   r   ru   © r3   r2   ú<module>r±      sr   ðÙ %ó Û ß :Õ :ç ;Ñ ;Ý $Ý  Ý Ý Ý Ý 'ß /ò9€òL6ò^Y$ðz +€ò"ò4óXóvJóZNôbi<r3   