ó
Ë½÷Xc           @` sa  d  Z  d d l m Z m Z m 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 d d d d d g Z  d „  Z! d „  Z" d „  Z# d „  Z$ d d „ Z& d d e' d „ Z( d d e' d „ Z) d d „ Z* d S(   s   Matrix equation solver routinesi    (   t   divisiont   print_functiont   absolute_importN(   t   invt   LinAlgErrort   normt   condt   svdi   (   t   solvet   solve_triangulart   matrix_balance(   t   get_lapack_funcs(   t   schur(   t   lu(   t   qr(   t   ordqz(   t   _asarray_validated(   t   kront
   block_diagt   solve_sylvestert   solve_lyapunovt   solve_discrete_lyapunovt   solve_continuous_aret   solve_discrete_arec         C` s  t  |  d d ƒ\ } } t  | j ƒ  j ƒ  d d ƒ\ } } t j t j | j ƒ  j ƒ  | ƒ | ƒ } t d	 | | | f ƒ \ } | d
 k rœ t d ƒ ‚ n  | | | | d d ƒ\ }	 }
 } |
 |	 }	 | d k  rê t d | f ƒ ‚ n  t j t j | |	 ƒ | j ƒ  j ƒ  ƒ S(   sâ  
    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

    t   outputt   realt   trsylsQ   LAPACK implementation does not contain a proper Sylvester equation solver (TRSYL)t   tranbt   Ci    s(   Illegal value encountered in the %d term(   s   trsylN(	   R   t   conjt	   transposet   npt   dotR   t   Nonet   RuntimeErrorR   (   t   at   bt   qt   rt   ut   st   vt   fR   t   yt   scalet   info(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/_solvers.pyR      s    &$*!
c         C` s   t  |  |  j ƒ  j ƒ  | ƒ S(   s£  
    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 : array_like
        Solution to the continuous Lyapunov equation

    See Also
    --------
    solve_sylvester : computes the solution to the Sylvester equation

    Notes
    -----
    Because the continuous Lyapunov equation is just a special form of the
    Sylvester equation, this solver relies entirely on solve_sylvester for a
    solution.

    .. versionadded:: 0.11.0

    (   R   R   R   (   R#   R%   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/_solvers.pyR   [   s    !c         C` sW   t  |  |  j ƒ  ƒ } t j | j d ƒ | } t | | j ƒ  ƒ } t j | | j ƒ S(   sÄ   
    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.
    i    (   R   R   R   t   eyet   shapeR   t   flattent   reshape(   R#   R%   t   lhst   x(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/_solvers.pyt   _solve_discrete_lyapunov_direct   s    c         C` s”   t  j |  j d ƒ } |  j ƒ  j ƒ  } t | | ƒ } t  j | | | ƒ } d t  j t  j t |  | ƒ | ƒ | ƒ } t | j ƒ  j ƒ  | ƒ S(   sÝ   
    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.
    i    i   (   R   R.   R/   R   R   R   R    R   (   R#   R%   R.   t   aHt   aHI_invR$   t   c(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/_solvers.pyt!   _solve_discrete_lyapunov_bilinearŽ   s    ,c         C` s«   t  j |  ƒ }  t  j | ƒ } | d k rO |  j d d k rF d } qO d } n  | j ƒ  } | d k ry t |  | ƒ } n. | d k r— t |  | ƒ } n t d | ƒ ‚ | S(   sÉ  
    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_lyapunov : computes the solution to the continuous 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] Hamilton, James D. Time Series Analysis, Princeton: Princeton
       University Press, 1994.  265.  Print.
       http://www.scribd.com/doc/20577138/Hamilton-1994-Time-Series-Analysis
    .. [2] Gajic, Z., and M.T.J. Qureshi. 2008.
       Lyapunov Matrix Equation in System Stability and Control.
       Dover Books on Engineering Series. Dover Publications.

    i    i
   t   bilineart   directs   Unknown solver %sN(   R   t   asarrayR!   R/   t   lowerR4   R8   t
   ValueError(   R#   R%   t   methodt   methR3   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/_solvers.pyR      s    7		c         C` sV  t  |  | | | | | d ƒ \
 }  } } } } } } } }	 }
 t j d | | d | | f d |	 ƒ} |  | d | … d | … f <d | d | … | d | … f <| | d | … d | d … f <| | | d | … d | … f <|  j ƒ  j | | d | … | d | … f <| d k r$d n | | | d | … d | d … f <| d k r[d n | j ƒ  j | d | d … d | … f <| j ƒ  j | d | d … | d | … f <| | d | d … d | d … f <|
 r| d k	 rt | | j ƒ  j t j | d |	 ƒƒ } n+ t t j d | ƒ t j | d |	 ƒƒ } | rHt j	 | ƒ t j	 | ƒ } d | t j
 | ƒ <t | d d d d	 ƒ\ } \ } } t j | t j | ƒ ƒ sHt j | ƒ } t j | | d | !| |  d ƒ } d t j | | | d | f } | d d … d f t j | ƒ } | | 9} | | 9} qHn  t | d d … | d … f ƒ \ } } | d d … | d … f j ƒ  j j | d d … d d | … f ƒ } | d d | … | d … f j ƒ  j j | d d | … d d | … f ƒ } |	 t k rd
 n d } t | | d d d t d t d t d | ƒ\ } } } } } } | d k	 rÅt t j | j | d | … d | … f ƒ | | d … d | … f f ƒ ƒ \ } } n  | d | … d | … f } | | d … d | … f } t | ƒ \ } } } d t | ƒ t j d ƒ k  r@t d ƒ ‚ n  t | j ƒ  j t | j ƒ  j | j ƒ  j d t ƒd t ƒj ƒ  j j | j ƒ  j ƒ } | rÅ| | d | … d f | |  9} n  | j ƒ  j j | ƒ } t | d ƒ } | | j ƒ  j } t j t j d ƒ | g ƒ } t | d ƒ | k rAt d ƒ ‚ n  | | j ƒ  j d S(   s  
    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 2m 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

    t   carei   t   dtypeNg        t   separatei   t   permutei    R   t   complext   sortt   lhpt   overwrite_at   overwrite_bt   check_finiteR   g      ð?s!   Failed to find a finite solution.R<   t   unit_diagonalg     @@sQ   The associated Hamiltonian pencil has eigenvalues too close to the imaginary axis(    t   _are_validate_argsR   t   emptyR   t   TR!   R   t
   zeros_likeR.   t   abst   diag_indices_fromR
   t   allcloset	   ones_liket   log2t   roundt   r_t
   reciprocalR   R    t   floatR   t   Truet   Falset   vstackR   R   t   spacingR   R	   R   t   max(   R#   R$   R%   R&   t   eR(   t   balancedt   mt   nt   r_or_ct   gen_aret   Ht   Jt   Mt   _t   scat   elwisescalet   out_strR'   t   u00t   u10t   upt   ult   uuR3   t   u_symt   n_u_symt   sym_threshold(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/_solvers.pyR   é   sn    g9+  !.7;-$-+$&##
)HPY	%c         C` sX  t  |  | | | | | d ƒ \
 }  } } } } } } } }	 }
 t j d | | d | | f d |	 ƒ} |  | d | … d | … f <| | d | … d | d … f <| | | d | … d | … f <| d k rß t j | ƒ n | j ƒ  j | | d | … | d | … f <| d k rd n | | | d | … d | d … f <| d k rUd n | j ƒ  j | d | d … d | … f <| | d | d … d | d … f <t j | d |	 ƒ} | d k rÒt j | ƒ n | | d | … d | … f <|  j ƒ  j | | d | … | d | … f <| j ƒ  j | d | d … | d | … f <| rRt j | ƒ t j | ƒ } d | t j	 | ƒ <t
 | d d d d	 ƒ\ } \ } } t j | t j | ƒ ƒ sRt j | ƒ } t j | | d | !| |  d ƒ } d t j | | | d | f } | d d … d f t j | ƒ } | | 9} | | 9} qRn  t | d d … | d … f ƒ \ } } | d d … | d … f j ƒ  j j | d d … d d | … f ƒ } | d d … | d … f j ƒ  j j | d d … d d | … f ƒ } |	 t k rd
 n d } t | | d d d t d t d t d | ƒ\ } } } } } } | d k	 rÇt t j | j | d | … d | … f ƒ | | d … d | … f f ƒ ƒ \ } } n  | d | … d | … f } | | d … d | … f } t | ƒ \ } } } d t | ƒ t j d ƒ k  rBt d ƒ ‚ n  t | j ƒ  j t | j ƒ  j | j ƒ  j d t ƒd t ƒj ƒ  j j | j ƒ  j ƒ } | rÇ| | d | … d f | |  9} n  | j ƒ  j j | ƒ } t | d ƒ } | | j ƒ  j } t j t j d ƒ | g ƒ } t | d ƒ | k rCt d ƒ ‚ n  | | j ƒ  j d S(   s@  
    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 2m 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

    t   darei   RA   Ng        RB   i   RC   i    R   RD   RE   t   iucRG   RH   RI   R   g      ð?s!   Failed to find a finite solution.R<   RJ   g     @@sL   The associated symplectic pencil has eigenvaluestoo close to the unit circle(   RK   R   t   zerosR!   R.   R   RM   RN   RO   RP   R
   RQ   RR   RS   RT   RU   RV   R   R    RW   R   RX   RY   RZ   R   R   R[   R   R	   R   R\   (   R#   R$   R%   R&   R]   R(   R^   R_   R`   Ra   Rb   Rc   Rd   Re   Rf   Rg   Rh   t   q_of_qrRi   R'   Rj   Rk   Rl   Rm   Rn   R3   Ro   Rp   Rq   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/_solvers.pyR   ¤  sp    h9+ !H7;$7-.$&##
)HHY	%R@   c         C` sà  | j  ƒ  d k r! t d ƒ ‚ n  t j t |  d t ƒƒ }  t j t | d t ƒƒ } t j t | d t ƒƒ } t j t | d t ƒƒ } t j | ƒ r¢ t n t } xi t	 |  | | f ƒ D]R \ } }	 t j |	 ƒ râ t } n  t j
 |	 j Œ  s¾ t d j d | ƒ ƒ ‚ q¾ q¾ W| j \ }
 } |
 |  j d k rEt d ƒ ‚ n  |
 | j d k rgt d	 ƒ ‚ n  | | j d k r‰t d
 ƒ ‚ n  xt t	 | | f ƒ D]` \ } }	 t |	 |	 j ƒ  j d ƒ t j t |	 d ƒ ƒ d k rœt d j d | ƒ ƒ ‚ qœqœW| d k rbt | d t ƒd } | d k sP| t j d ƒ t | d ƒ k  rbt d ƒ ‚ qbn  | d k	 pw| d k	 } | r¾| d k	 rXt j t | d t ƒƒ } t j
 | j Œ  sÈt d ƒ ‚ n  |
 | j d k rêt d ƒ ‚ n  t | d t ƒd } | d k s.| t j d ƒ t | d ƒ k  r=t d ƒ ‚ n  t j | ƒ rXt } qXn  | d k	 r¾t j t | d t ƒƒ } | j | j k r t d ƒ ‚ n  t j | ƒ r»t } q»q¾n  |  | | | | | |
 | | | f
 S(   s  
    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.

    Rr   R@   s;   Equation type unknown. Only 'care' and 'dare' is understoodRI   s   Matrix {} should be square.t   aqri    s3   Matrix a and b should have the same number of rows.s*   Matrix a and q should have the same shape.s3   Matrix b and r should have the same number of cols.i   id   s(   Matrix {} should be symmetric/hermitian.R   t
   compute_uviÿÿÿÿg        g      ð?s!   Matrix r is numerically singular.s   Matrix e should be square.s*   Matrix a and e should have the same shape.s!   Matrix e is numerically singular.s*   Matrix b and s should have the same shape.(   s   dares   careN(   R<   R=   R   t
   atleast_2dR   RX   t   iscomplexobjRD   RW   t	   enumeratet   equalR/   t   formatR   R   RM   R[   R   RY   R!   (   R#   R$   R%   R&   R]   R(   t   eq_typeRa   t   indt   matR_   R`   t   min_svt   generalized_case(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/_solvers.pyRK   b  s^    '"	 8	..(+   t   __doc__t
   __future__R    R   R   t   numpyR   t   numpy.linalgR   R   R   R   R   t   basicR   R	   R
   t   lapackR   t   decomp_schurR   t	   decomp_luR   t	   decomp_qrR   t
   _decomp_qzR   t   decompR   t   special_matricesR   R   t   __all__R   R   R4   R8   R!   R   RX   R   R   RK   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/_solvers.pyt   <module>   s*   
(		>	$		L»¾