
    `i$                       d Z ddlZddlZddlmZ ddlZddlmZ ddlm	Z	 ddl
mZmZ ddlmZmZ ddlmZmZmZmZmZmZmZ dd	lmZmZmZmZ  G d
 d          Z G d de          Z G d de          Z G d de          Z  G d de e          Z! G d de e          Z" G d de          Z# G d de#e          Z$ G d de#e          Z% G d de          Z& G d de&e          Z' G d  d!e&e          Z(d@d#Z)d$ Z*dAd%Z+dAd&Z,dBd(Z-dCd*Z. G d+ d,          Z/d- Z0d. Z1d/ Z2d0 Z3d1 Z4d2 Z5d3 Z6dDd7Z7dEd8Z8dAd9Z9dAd:Z:dFd<Z;dBd=Z<dGd?Z=dS )Hz]
ltisys -- a collection of classes and functions for modeling linear
time invariant systems.
    N)sqrt)linalg)make_interp_spline)expm
block_diag)_atleast_2d_or_noneabcd_normalize)	normalizetf2zpktf2sszpk2ssss2tfss2zpkzpk2tf)freqz	freqz_zpkfreqs	freqs_zpkc                        e Zd Z fdZ fdZed             Zed             Zed             Zed             Z	d Z
d Zd	 Z xZS )
LinearTimeInvariantc                 v    | t           u rt          d          t                                          |           S )z2Create a new object, don't allow direct instances.z\The LinearTimeInvariant class is not meant to be used directly, use `lti` or `dlti` instead.)r   NotImplementedErrorsuper__new__clssystemkwargs	__class__s      n/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupyx/scipy/signal/_ltisys.pyr   zLinearTimeInvariant.__new__   s>    %%%% '; < < < wws###    c                 r    t                                                       d| _        d| _        d| _        dS g
        Initialize the `lti` baseclass.

        The heavy lifting is done by the subclasses.
        N)r   __init__inputsoutputs_dt)selfr   s    r    r%   zLinearTimeInvariant.__init__    s4     	r!   c                     | j         S )zAReturn the sampling time of the system, `None` for `lti` systems.r(   r)   s    r    dtzLinearTimeInvariant.dt,        xr!   c                 &    | j         i S d| j         iS )Nr-   )r-   r,   s    r    _dt_dictzLinearTimeInvariant._dt_dict1   s    7?I$'?"r!   c                 4    |                                  j        S )zZeros of the system.)to_zpkzerosr,   s    r    r3   zLinearTimeInvariant.zeros8        {{}}""r!   c                 4    |                                  j        S )zPoles of the system.)r2   polesr,   s    r    r6   zLinearTimeInvariant.poles=   r4   r!   c                 X    t          | t                    r| S |                                 S )zConvert to `StateSpace` system, without copying.

        Returns
        -------
        sys: StateSpace
            The `StateSpace` system. If the class is already an instance of
            `StateSpace` then this instance is returned.
        )
isinstance
StateSpaceto_ssr,   s    r    _as_sszLinearTimeInvariant._as_ssB   s)     dJ'' 	 K::<<r!   c                 X    t          | t                    r| S |                                 S )a  Convert to `ZerosPolesGain` system, without copying.

        Returns
        -------
        sys: ZerosPolesGain
            The `ZerosPolesGain` system. If the class is already an instance of
            `ZerosPolesGain` then this instance is returned.
        )r8   ZerosPolesGainr2   r,   s    r    _as_zpkzLinearTimeInvariant._as_zpkP   s)     dN++ 	!K;;== r!   c                 X    t          | t                    r| S |                                 S )a  Convert to `TransferFunction` system, without copying.

        Returns
        -------
        sys: ZerosPolesGain
            The `TransferFunction` system. If the class is already an instance
            of `TransferFunction` then this instance is returned.
        )r8   TransferFunctionto_tfr,   s    r    _as_tfzLinearTimeInvariant._as_tf^   s*     d,-- 	 K::<<r!   )__name__
__module____qualname__r   r%   propertyr-   r0   r3   r6   r;   r>   rB   __classcell__r   s   @r    r   r      s        $ $ $ $ $
 
 
 
 
   X # # X# # # X# # # X#     ! ! !             r!   r   c                   \     e Zd ZdZ fdZ fdZddZddZddZdd	Z	ddZ
ddZ xZS )ltia  
    Continuous-time linear time invariant system base class.

    Parameters
    ----------
    *system : arguments
        The `lti` class can be instantiated with either 2, 3 or 4 arguments.
        The following gives the number of arguments and the corresponding
        continuous-time subclass that is created:

            * 2: `TransferFunction`:  (numerator, denominator)
            * 3: `ZerosPolesGain`: (zeros, poles, gain)
            * 4: `StateSpace`:  (A, B, C, D)

        Each argument can be an array or a sequence.

    See Also
    --------
    scipy.signal.lti
    ZerosPolesGain, StateSpace, TransferFunction, dlti

    Notes
    -----
    `lti` instances do not exist directly. Instead, `lti` creates an instance
    of one of its subclasses: `StateSpace`, `TransferFunction` or
    `ZerosPolesGain`.

    If (numerator, denominator) is passed in for ``*system``, coefficients for
    both the numerator and denominator should be specified in descending
    exponent order (e.g., ``s^2 + 3s + 5`` would be represented as ``[1, 3,
    5]``).

    Changing the value of properties that are not directly part of the current
    system representation (such as the `zeros` of a `StateSpace` system) is
    very inefficient and may lead to numerical inaccuracies. It is better to
    convert to the specific system representation first. For example, call
    ``sys = sys.to_zpk()`` before accessing/changing the zeros, poles or gain.
    c                 B   | t           u rut          |          }|dk    rt          j        t          g|R  S |dk    rt	          j        t          g|R  S |dk    rt          j        t
          g|R  S t          d          t                                          |           S )/Create an instance of the appropriate subclass.         zF`system` needs to be an instance of `lti` or have 2, 3 or 4 arguments.)rJ   lenTransferFunctionContinuousr   ZerosPolesGainContinuousStateSpaceContinuous
ValueErrorr   )r   r   Nr   s      r    r   zlti.__new__   s    #::FAAvv19.9179 9 9 9a/7,7/57 7 7 7a+34H =5;= = = = ! "@ A A A wws###r!   c                 4     t                      j        |  dS r#   )r   r%   )r)   r   r   s     r    r%   zlti.__init__   s     	&!!!!r!   Nc                 (    t          | |||          S )zm
        Return the impulse response of a continuous-time system.
        See `impulse` for details.
        X0TrU   )impulser)   rY   rZ   rU   s       r    r[   zlti.impulse   s    
 ta1----r!   c                 (    t          | |||          S )zg
        Return the step response of a continuous-time system.
        See `step` for details.
        rX   )stepr\   s       r    r^   zlti.step   s    
 DR1****r!   c                 (    t          | |||          S )zo
        Return the response of a continuous-time system to input `U`.
        See `lsim` for details.
        )rY   )lsim)r)   UrZ   rY   s       r    outputz
lti.output   s    
 D!Q2&&&&r!   d   c                 &    t          | ||          S )z
        Calculate Bode magnitude and phase data of a continuous-time system.

        Returns a 3-tuple containing arrays of frequencies [rad/s], magnitude
        [dB] and phase [deg]. See `bode` for details.
        wn)boder)   rf   rg   s      r    rh   zlti.bode   s     DA####r!   '  c                 &    t          | ||          S )z
        Calculate the frequency response of a continuous-time system.

        Returns a 2-tuple containing arrays of frequencies [rad/s] and
        complex magnitude.
        See `freqresp` for details.
        re   )freqrespri   s      r    rl   zlti.freqresp   s     Q''''r!   zohc                      t          d          )zReturn a discretized version of the current system.

        Parameters: See `cont2discrete` for details.

        Returns
        -------
        sys: instance of `dlti`
        z5to_discrete is not implemented for this system class.)r   r)   r-   methodalphas       r    to_discretezlti.to_discrete   s     " #2 3 3 	3r!   NNNNNrc   Nrj   rm   N)rC   rD   rE   __doc__r   r%   r[   r^   rb   rh   rl   rr   rG   rH   s   @r    rJ   rJ   m   s        % %L$ $ $ $ $&" " " " ". . . .+ + + +' ' ' '$ $ $ $( ( ( (
3 
3 
3 
3 
3 
3 
3 
3r!   rJ   c                        e Zd ZdZ fdZ fdZed             Zej        d             ZddZ	ddZ
dd	ZddZddZ xZS )dltia  
    Discrete-time linear time invariant system base class.

    Parameters
    ----------
    *system: arguments
        The `dlti` class can be instantiated with either 2, 3 or 4 arguments.
        The following gives the number of arguments and the corresponding
        discrete-time subclass that is created:

            * 2: `TransferFunction`:  (numerator, denominator)
            * 3: `ZerosPolesGain`: (zeros, poles, gain)
            * 4: `StateSpace`:  (A, B, C, D)

        Each argument can be an array or a sequence.
    dt: float, optional
        Sampling time [s] of the discrete-time systems. Defaults to ``True``
        (unspecified sampling time). Must be specified as a keyword argument,
        for example, ``dt=0.1``.

    See Also
    --------
    scipy.signal.dlti
    ZerosPolesGain, StateSpace, TransferFunction, lti

    Notes
    -----
    `dlti` instances do not exist directly. Instead, `dlti` creates an instance
    of one of its subclasses: `StateSpace`, `TransferFunction` or
    `ZerosPolesGain`.

    Changing the value of properties that are not directly part of the current
    system representation (such as the `zeros` of a `StateSpace` system) is
    very inefficient and may lead to numerical inaccuracies.  It is better to
    convert to the specific system representation first. For example, call
    ``sys = sys.to_zpk()`` before accessing/changing the zeros, poles or gain.

    If (numerator, denominator) is passed in for ``*system``, coefficients for
    both the numerator and denominator should be specified in descending
    exponent order (e.g., ``z^2 + 3z + 5`` would be represented as ``[1, 3,
    5]``).
    c                 T   | t           u r~t          |          }|dk    rt          j        t          g|R i |S |dk    rt	          j        t          g|R i |S |dk    rt          j        t
          g|R i |S t          d          t                                          |           S )rL   rM   rN   rO   zG`system` needs to be an instance of `dlti` or have 2, 3 or 4 arguments.)rz   rP   TransferFunctionDiscreter   ZerosPolesGainDiscreteStateSpaceDiscreterT   r   )r   r   r   rU   r   s       r    r   zdlti.__new__  s
   $;;FAAvv/7,A/5A A A9?A A Aa-56L I7=I I IAGI I Ia)12D <v < < <4:< < < ! "@ A A A wws###r!   c                 t    |                     dd          } t                      j        |i | || _        dS )r$   r-   TN)popr   r%   r-   )r)   r   r   r-   r   s       r    r%   zdlti.__init__"  s?     ZZd##&+F+++r!   c                     | j         S )z'Return the sampling time of the system.r+   r,   s    r    r-   zdlti.dt-  r.   r!   c                     || _         d S rt   r+   )r)   r-   s     r    r-   zdlti.dt2  s    r!   Nc                 (    t          | |||          S )zu
        Return the impulse response of the discrete-time `dlti` system.
        See `dimpulse` for details.
        x0trg   )dimpulser)   r   r   rg   s       r    r[   zdlti.impulse6  s    
 qA....r!   c                 (    t          | |||          S )zo
        Return the step response of the discrete-time `dlti` system.
        See `dstep` for details.
        r   )dstepr   s       r    r^   z	dlti.step=  s    
 TbA++++r!   c                 (    t          | |||          S )zp
        Return the response of the discrete-time system to input `u`.
        See `dlsim` for details.
        )r   )dlsim)r)   ur   r   s       r    rb   zdlti.outputD  s    
 T1aB''''r!   rc   c                 &    t          | ||          S )z
        Calculate Bode magnitude and phase data of a discrete-time system.

        Returns a 3-tuple containing arrays of frequencies [rad/s], magnitude
        [dB] and phase [deg]. See `dbode` for details.
        re   )dboderi   s      r    rh   z	dlti.bodeK  s     TQ!$$$$r!   rj   Fc                 (    t          | |||          S )z
        Calculate the frequency response of a discrete-time system.

        Returns a 2-tuple containing arrays of frequencies [rad/s] and
        complex magnitude.
        See `dfreqresp` for details.

        )rf   rg   whole)	dfreqresp)r)   rf   rg   r   s       r    rl   zdlti.freqrespT  s     au5555r!   rs   rt   ru   Nrj   F)rC   rD   rE   rx   r   r%   rF   r-   setterr[   r^   rb   rh   rl   rG   rH   s   @r    rz   rz      s        ) )T$ $ $ $ $&	 	 	 	 	   X Y  Y/ / / /, , , ,( ( ( (% % % %	6 	6 	6 	6 	6 	6 	6 	6r!   rz   c                        e Zd ZdZ fdZ fdZd Zed             Zej	        d             Zed             Z
e
j	        d             Z
d	 Zd
 Zd Zd Zed             Zed             Z xZS )r@   a  Linear Time Invariant system class in transfer function form.

    Represents the system as the continuous-time transfer function
    :math:`H(s)=\sum_{i=0}^N b[N-i] s^i / \sum_{j=0}^M a[M-j] s^j` or the
    discrete-time transfer function
    :math:`H(z)=\sum_{i=0}^N b[N-i] z^i / \sum_{j=0}^M a[M-j] z^j`, where
    :math:`b` are elements of the numerator `num`, :math:`a` are elements of
    the denominator `den`, and ``N == len(b) - 1``, ``M == len(a) - 1``.
    `TransferFunction` systems inherit additional
    functionality from the `lti`, respectively the `dlti` classes, depending on
    which system representation is used.

    Parameters
    ----------
    *system: arguments
        The `TransferFunction` class can be instantiated with 1 or 2
        arguments. The following gives the number of input arguments and their
        interpretation:

            * 1: `lti` or `dlti` system: (`StateSpace`, `TransferFunction` or
              `ZerosPolesGain`)
            * 2: array_like: (numerator, denominator)
    dt: float, optional
        Sampling time [s] of the discrete-time systems. Defaults to `None`
        (continuous-time). Must be specified as a keyword argument, for
        example, ``dt=0.1``.

    See Also
    --------
    scipy.signal.TransferFunction
    ZerosPolesGain, StateSpace, lti, dlti
    tf2ss, tf2zpk, tf2sos

    Notes
    -----
    Changing the value of properties that are not part of the
    `TransferFunction` system representation (such as the `A`, `B`, `C`, `D`
    state-space matrices) is very inefficient and may lead to numerical
    inaccuracies.  It is better to convert to the specific system
    representation first. For example, call ``sys = sys.to_ss()`` before
    accessing/changing the A, B, C, D system matrices.

    If (numerator, denominator) is passed in for ``*system``, coefficients
    for both the numerator and denominator should be specified in descending
    exponent order (e.g. ``s^2 + 3s + 5`` or ``z^2 + 3z + 5`` would be
    represented as ``[1, 3, 5]``)
    c                 z   t          |          dk    r5t          |d         t                    r|d                                         S | t          u rI|                    d          t          j        t          g|R i |S t          j        t          g|R i |S t                                          |           S )z8Handle object conversion if input is an instance of lti.   r   r-   )
rP   r8   r   rA   r@   getrQ   r   r|   r   r   s      r    r   zTransferFunction.__new__  s    v;;!
6!96I J J!9??$$$ """zz$'19.     
 07,      wws###r!   c                     t          |d         t                    rdS  t                      j        di | d| _        d| _        t          | \  | _        | _        dS )z&Initialize the state space LTI system.r   N )	r8   r   r   r%   _num_denr
   numdenr)   r   r   r   s      r    r%   zTransferFunction.__init__  se     fQi!455 	F 	""6"""		&/$(((r!   c                     d                     | j        j        t          | j                  t          | j                  t          | j                            S )z7Return representation of the system's transfer functionz{}(
{},
{},
dt: {}
))formatr   rC   reprr   r   r-   r,   s    r    __repr__zTransferFunction.__repr__  sC    )00N#NNNNMM	
 
 	
r!   c                     | j         S )z+Numerator of the `TransferFunction` system.)r   r,   s    r    r   zTransferFunction.num       yr!   c                     t          j        |          | _        t          | j        j                  dk    r| j        j        \  | _        | _        d S d| _        d| _        d S Nr   )cupy
atleast_1dr   rP   r   shaper'   r&   )r)   r   s     r    r   zTransferFunction.num  sT    OC((	 tx~""(,%DL$+++DLDKKKr!   c                     | j         S )z-Denominator of the `TransferFunction` system.)r   r,   s    r    r   zTransferFunction.den  r   r!   c                 8    t          j        |          | _        d S rt   )r   r   r   )r)   r   s     r    r   zTransferFunction.den  s    OC((			r!   c                 6    |j         | _         |j        | _        dS )z
        Copy the parameters of another `TransferFunction` object

        Parameters
        ----------
        system : `TransferFunction`
            The `StateSpace` system that is to be copied

        N)r   r   r)   r   s     r    _copyzTransferFunction._copy  s     ::r!   c                 *    t          j        |           S )z
        Return a copy of the current `TransferFunction` system.

        Returns
        -------
        sys : instance of `TransferFunction`
            The current system (copy)

        copydeepcopyr,   s    r    rA   zTransferFunction.to_tf       }T"""r!   c                 T    t          t          | j        | j                  i | j        S )z
        Convert system representation to `ZerosPolesGain`.

        Returns
        -------
        sys : instance of `ZerosPolesGain`
            Zeros, poles, gain representation of the current system

        )r=   r   r   r   r0   r,   s    r    r2   zTransferFunction.to_zpk  s0     vdh99 / $/ / 	/r!   c                 T    t          t          | j        | j                  i | j        S z
        Convert system representation to `StateSpace`.

        Returns
        -------
        sys : instance of `StateSpace`
            State space model of the current system

        )r9   r   r   r   r0   r,   s    r    r:   zTransferFunction.to_ss  s0     54844 + M+ + 	+r!   c                    t          |           t          |          z
  }|dk    r)t          j        t          j        |          |f          }n/|dk     r)t          j        t          j        |           | f          } | |fS )a  Change a transfer function from the variable `z` to `z**-1`.

        Parameters
        ----------
        num, den: 1d array_like
            Sequences representing the coefficients of the numerator and
            denominator polynomials, in order of descending degree of 'z'.
            That is, ``5z**2 + 3z + 2`` is presented as ``[5, 3, 2]``.

        Returns
        -------
        num, den: 1d array_like
            Sequences representing the coefficients of the numerator and
            denominator polynomials, in order of ascending degree of 'z**-1'.
            That is, ``5 + 3 z**-1 + 2 z**-2`` is presented as ``[5, 3, 2]``.
        r   rP   r   hstackr3   r   r   diffs      r    
_z_to_zinvzTransferFunction._z_to_zinv  su    $ 3xx#c(("!88+tz$//566CCAXX+tz4%00#677CCxr!   c                    t          |           t          |          z
  }|dk    r)t          j        |t          j        |          f          }n/|dk     r)t          j        | t          j        |           f          } | |fS )a  Change a transfer function from the variable `z` to `z**-1`.

        Parameters
        ----------
        num, den: 1d array_like
            Sequences representing the coefficients of the numerator and
            denominator polynomials, in order of ascending degree of 'z**-1'.
            That is, ``5 + 3 z**-1 + 2 z**-2`` is presented as ``[5, 3, 2]``.

        Returns
        -------
        num, den: 1d array_like
            Sequences representing the coefficients of the numerator and
            denominator polynomials, in order of descending degree of 'z'.
            That is, ``5z**2 + 3z + 2`` is presented as ``[5, 3, 2]``.
        r   r   r   s      r    
_zinv_to_zzTransferFunction._zinv_to_z!  su    $ 3xx#c(("!88+sDJt$4$4566CCAXX+sDJu$5$5677CCxr!   )rC   rD   rE   rx   r   r%   r   rF   r   r   r   r   rA   r2   r:   staticmethodr   r   rG   rH   s   @r    r@   r@   `  sM       . .^$ $ $ $ $*0 0 0 0 0
 
 
   X 	Z  Z   X 	Z) ) Z)  
# 
# 
#/ / /+ + +   \0   \    r!   r@   c                       e Zd ZdZddZdS )rQ   a`  
    Continuous-time Linear Time Invariant system in transfer function form.

    Represents the system as the transfer function
    :math:`H(s)=\sum_{i=0}^N b[N-i] s^i / \sum_{j=0}^M a[M-j] s^j`, where
    :math:`b` are elements of the numerator `num`, :math:`a` are elements of
    the denominator `den`, and ``N == len(b) - 1``, ``M == len(a) - 1``.
    Continuous-time `TransferFunction` systems inherit additional
    functionality from the `lti` class.

    Parameters
    ----------
    *system: arguments
        The `TransferFunction` class can be instantiated with 1 or 2
        arguments. The following gives the number of input arguments and their
        interpretation:

            * 1: `lti` system: (`StateSpace`, `TransferFunction` or
              `ZerosPolesGain`)
            * 2: array_like: (numerator, denominator)

    See Also
    --------
    scipy.signal.TransferFunction
    ZerosPolesGain, StateSpace, lti
    tf2ss, tf2zpk, tf2sos

    Notes
    -----
    Changing the value of properties that are not part of the
    `TransferFunction` system representation (such as the `A`, `B`, `C`, `D`
    state-space matrices) is very inefficient and may lead to numerical
    inaccuracies.  It is better to convert to the specific system
    representation first. For example, call ``sys = sys.to_ss()`` before
    accessing/changing the A, B, C, D system matrices.

    If (numerator, denominator) is passed in for ``*system``, coefficients
    for both the numerator and denominator should be specified in descending
    exponent order (e.g. ``s^2 + 3s + 5`` would be represented as
    ``[1, 3, 5]``)

    rm   Nc                 d    t          t          | j        | j        f|||          dd         d|iS )z
        Returns the discretized `TransferFunction` system.

        Parameters: See `cont2discrete` for details.

        Returns
        -------
        sys: instance of `dlti` and `StateSpace`
        rp   rq   Nr-   )r@   cont2discreter   r   ro   s       r    rr   z&TransferFunctionContinuous.to_discreteg  sU      $(/C/16<5:"< "< "< =@RC"A ' $&	' ' 	'r!   rw   rC   rD   rE   rx   rr   r   r!   r    rQ   rQ   ;  s3        ) )V' ' ' ' ' 'r!   rQ   c                       e Zd ZdZdS )r|   a  
    Discrete-time Linear Time Invariant system in transfer function form.

    Represents the system as the transfer function
    :math:`H(z)=\sum_{i=0}^N b[N-i] z^i / \sum_{j=0}^M a[M-j] z^j`, where
    :math:`b` are elements of the numerator `num`, :math:`a` are elements of
    the denominator `den`, and ``N == len(b) - 1``, ``M == len(a) - 1``.
    Discrete-time `TransferFunction` systems inherit additional functionality
    from the `dlti` class.

    Parameters
    ----------
    *system: arguments
        The `TransferFunction` class can be instantiated with 1 or 2
        arguments. The following gives the number of input arguments and their
        interpretation:

            * 1: `dlti` system: (`StateSpace`, `TransferFunction` or
              `ZerosPolesGain`)
            * 2: array_like: (numerator, denominator)
    dt: float, optional
        Sampling time [s] of the discrete-time systems. Defaults to `True`
        (unspecified sampling time). Must be specified as a keyword argument,
        for example, ``dt=0.1``.

    See Also
    --------
    scipy.signal.TransferFunctionDiscrete
    ZerosPolesGain, StateSpace, dlti
    tf2ss, tf2zpk, tf2sos

    Notes
    -----
    Changing the value of properties that are not part of the
    `TransferFunction` system representation (such as the `A`, `B`, `C`, `D`
    state-space matrices) is very inefficient and may lead to numerical
    inaccuracies.

    If (numerator, denominator) is passed in for ``*system``, coefficients
    for both the numerator and denominator should be specified in descending
    exponent order (e.g., ``z^2 + 3z + 5`` would be represented as
    ``[1, 3, 5]``).
    NrC   rD   rE   rx   r   r!   r    r|   r|   x  s        * *V 	Dr!   r|   c                        e Zd ZdZ fdZ fdZd Zed             Zej	        d             Zed             Z
e
j	        d             Z
ed	             Zej	        d
             Zd Zd Zd Zd Z xZS )r=   aP  
    Linear Time Invariant system class in zeros, poles, gain form.

    Represents the system as the continuous- or discrete-time transfer function
    :math:`H(s)=k \prod_i (s - z[i]) / \prod_j (s - p[j])`, where :math:`k` is
    the `gain`, :math:`z` are the `zeros` and :math:`p` are the `poles`.
    `ZerosPolesGain` systems inherit additional functionality from the `lti`,
    respectively the `dlti` classes, depending on which system representation
    is used.

    Parameters
    ----------
    *system : arguments
        The `ZerosPolesGain` class can be instantiated with 1 or 3
        arguments. The following gives the number of input arguments and their
        interpretation:

            * 1: `lti` or `dlti` system: (`StateSpace`, `TransferFunction` or
              `ZerosPolesGain`)
            * 3: array_like: (zeros, poles, gain)
    dt: float, optional
        Sampling time [s] of the discrete-time systems. Defaults to `None`
        (continuous-time). Must be specified as a keyword argument, for
        example, ``dt=0.1``.


    See Also
    --------
    scipy.signal.ZerosPolesGain
    TransferFunction, StateSpace, lti, dlti
    zpk2ss, zpk2tf, zpk2sos

    Notes
    -----
    Changing the value of properties that are not part of the
    `ZerosPolesGain` system representation (such as the `A`, `B`, `C`, `D`
    state-space matrices) is very inefficient and may lead to numerical
    inaccuracies.  It is better to convert to the specific system
    representation first. For example, call ``sys = sys.to_ss()`` before
    accessing/changing the A, B, C, D system matrices.
    c                 z   t          |          dk    r5t          |d         t                    r|d                                         S | t          u rI|                    d          t          j        t          g|R i |S t          j        t          g|R i |S t                                          |           S )z9Handle object conversion if input is an instance of `lti`r   r   r-   )
rP   r8   r   r2   r=   r   rR   r   r}   r   r   s      r    r   zZerosPolesGain.__new__  s    v;;!
6!96I J J!9##%%% .  zz$'/7,     
 .5*      wws###r!   c                     t          |d         t                    rdS  t                      j        di | d| _        d| _        d| _        |\  | _        | _        | _	        dS )z)Initialize the zeros, poles, gain system.r   Nr   )
r8   r   r   r%   _zeros_poles_gainr3   r6   gainr   s      r    r%   zZerosPolesGain.__init__  si     fQi!455 	F""6"""
,2)
DJ			r!   c           	          d                     | j        j        t          | j                  t          | j                  t          | j                  t          | j                            S )z5Return representation of the `ZerosPolesGain` system.z{}(
{},
{},
{},
dt: {}
))r   r   rC   r   r3   r6   r   r-   r,   s    r    r   zZerosPolesGain.__repr__  sR    .55N#OOMM
 
 	
r!   c                     | j         S )z%Zeros of the `ZerosPolesGain` system.)r   r,   s    r    r3   zZerosPolesGain.zeros       {r!   c                     t          j        |          | _        t          | j        j                  dk    r| j        j        \  | _        | _        d S d| _        d| _        d S r   )r   r   r   rP   r3   r   r'   r&   )r)   r3   s     r    r3   zZerosPolesGain.zeros  sV    oe,, tz  1$$(,
(8%DL$+++DLDKKKr!   c                     | j         S )z%Poles of the `ZerosPolesGain` system.)r   r,   s    r    r6   zZerosPolesGain.poles  r   r!   c                 8    t          j        |          | _        d S rt   )r   r   r   )r)   r6   s     r    r6   zZerosPolesGain.poles  s    oe,,r!   c                     | j         S )z$Gain of the `ZerosPolesGain` system.r   r,   s    r    r   zZerosPolesGain.gain  s     zr!   c                     || _         d S rt   r   )r)   r   s     r    r   zZerosPolesGain.gain  s    


r!   c                 N    |j         | _         |j        | _        |j        | _        dS )z
        Copy the parameters of another `ZerosPolesGain` system.

        Parameters
        ----------
        system : instance of `ZerosPolesGain`
            The zeros, poles gain system that is to be copied

        N)r6   r3   r   r   s     r    r   zZerosPolesGain._copy!  s#     \
\
K			r!   c                 `    t          t          | j        | j        | j                  i | j        S )z
        Convert system representation to `TransferFunction`.

        Returns
        -------
        sys : instance of `TransferFunction`
            Transfer function of the current system

        )r@   r   r3   r6   r   r0   r,   s    r    rA   zZerosPolesGain.to_tf/  s4      
DJ	!J!J 1"&-1 1 	1r!   c                 *    t          j        |           S )z
        Return a copy of the current 'ZerosPolesGain' system.

        Returns
        -------
        sys : instance of `ZerosPolesGain`
            The current system (copy)

        r   r,   s    r    r2   zZerosPolesGain.to_zpk<  r   r!   c                 `    t          t          | j        | j        | j                  i | j        S r   )r9   r   r3   r6   r   r0   r,   s    r    r:   zZerosPolesGain.to_ssH  s4     6$*dj$)DD + M+ + 	+r!   )rC   rD   rE   rx   r   r%   r   rF   r3   r   r6   r   r   rA   r2   r:   rG   rH   s   @r    r=   r=     sO       ( (R$ $ $ $ $,3 3 3 3 3
 
 
   X \  \   X \- - \-   X 
[  [     1 1 1
# 
# 
#+ + + + + + +r!   r=   c                       e Zd ZdZddZdS )rR   a>  
    Continuous-time Linear Time Invariant system in zeros, poles, gain form.

    Represents the system as the continuous time transfer function
    :math:`H(s)=k \prod_i (s - z[i]) / \prod_j (s - p[j])`, where :math:`k` is
    the `gain`, :math:`z` are the `zeros` and :math:`p` are the `poles`.
    Continuous-time `ZerosPolesGain` systems inherit additional functionality
    from the `lti` class.

    Parameters
    ----------
    *system : arguments
        The `ZerosPolesGain` class can be instantiated with 1 or 3
        arguments. The following gives the number of input arguments and their
        interpretation:

            * 1: `lti` system: (`StateSpace`, `TransferFunction` or
              `ZerosPolesGain`)
            * 3: array_like: (zeros, poles, gain)

    See Also
    --------
    TransferFunction, StateSpace, lti
    zpk2ss, zpk2tf, zpk2sos

    Notes
    -----
    Changing the value of properties that are not part of the
    `ZerosPolesGain` system representation (such as the `A`, `B`, `C`, `D`
    state-space matrices) is very inefficient and may lead to numerical
    inaccuracies.  It is better to convert to the specific system
    representation first. For example, call ``sys = sys.to_ss()`` before
    accessing/changing the A, B, C, D system matrices.

    Examples
    --------
    Construct the transfer function
    :math:`H(s)=\frac{5(s - 1)(s - 2)}{(s - 3)(s - 4)}`:

    >>> from scipy import signal

    >>> signal.ZerosPolesGain([1, 2], [3, 4], 5)
    ZerosPolesGainContinuous(
    array([1, 2]),
    array([3, 4]),
    5,
    dt: None
    )

    rm   Nc                 p    t          t          | j        | j        | j        f|||          dd         d|iS )z
        Returns the discretized `ZerosPolesGain` system.

        Parameters: See `cont2discrete` for details.

        Returns
        -------
        sys: instance of `dlti` and `ZerosPolesGain`
        r   Nr   r-   )r=   r   r3   r6   r   ro   s       r    rr   z$ZerosPolesGainContinuous.to_discrete  sX     DJ
DI>"(!&( ( ( ),-
   	r!   rw   r   r   r!   r    rR   rR   V  s3        1 1f     r!   rR   c                       e Zd ZdZdS )r}   a
  
    Discrete-time Linear Time Invariant system in zeros, poles, gain form.

    Represents the system as the discrete-time transfer function
    :math:`H(z)=k \prod_i (z - q[i]) / \prod_j (z - p[j])`, where :math:`k` is
    the `gain`, :math:`q` are the `zeros` and :math:`p` are the `poles`.
    Discrete-time `ZerosPolesGain` systems inherit additional functionality
    from the `dlti` class.

    Parameters
    ----------
    *system : arguments
        The `ZerosPolesGain` class can be instantiated with 1 or 3
        arguments. The following gives the number of input arguments and their
        interpretation:

            * 1: `dlti` system: (`StateSpace`, `TransferFunction` or
              `ZerosPolesGain`)
            * 3: array_like: (zeros, poles, gain)
    dt: float, optional
        Sampling time [s] of the discrete-time systems. Defaults to `True`
        (unspecified sampling time). Must be specified as a keyword argument,
        for example, ``dt=0.1``.

    See Also
    --------
    scipy.signal.ZerosPolesGainDiscrete
    TransferFunction, StateSpace, dlti
    zpk2ss, zpk2tf, zpk2sos

    Notes
    -----
    Changing the value of properties that are not part of the
    `ZerosPolesGain` system representation (such as the `A`, `B`, `C`, `D`
    state-space matrices) is very inefficient and may lead to numerical
    inaccuracies.  It is better to convert to the specific system
    representation first. For example, call ``sys = sys.to_ss()`` before
    accessing/changing the A, B, C, D system matrices.
    Nr   r   r!   r    r}   r}     s        & &N 	Dr!   r}   c                   `    e Zd ZdZdZdZ fdZ fdZd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zd Zd Zd Zd Z xZS )r9   a  
    Linear Time Invariant system in state-space form.

    Represents the system as the continuous-time, first order differential
    equation :math:`\dot{x} = A x + B u` or the discrete-time difference
    equation :math:`x[k+1] = A x[k] + B u[k]`. `StateSpace` systems
    inherit additional functionality from the `lti`, respectively the `dlti`
    classes, depending on which system representation is used.

    Parameters
    ----------
    *system: arguments
        The `StateSpace` class can be instantiated with 1 or 4 arguments.
        The following gives the number of input arguments and their
        interpretation:

            * 1: `lti` or `dlti` system: (`StateSpace`, `TransferFunction` or
              `ZerosPolesGain`)
            * 4: array_like: (A, B, C, D)
    dt: float, optional
        Sampling time [s] of the discrete-time systems. Defaults to `None`
        (continuous-time). Must be specified as a keyword argument, for
        example, ``dt=0.1``.

    See Also
    --------
    scipy.signal.StateSpace
    TransferFunction, ZerosPolesGain, lti, dlti
    ss2zpk, ss2tf, zpk2sos

    Notes
    -----
    Changing the value of properties that are not part of the
    `StateSpace` system representation (such as `zeros` or `poles`) is very
    inefficient and may lead to numerical inaccuracies.  It is better to
    convert to the specific system representation first. For example, call
    ``sys = sys.to_zpk()`` before accessing/changing the zeros, poles or gain.
    g      Y@Nc                 z   t          |          dk    r5t          |d         t                    r|d                                         S | t          u rI|                    d          t          j        t          g|R i |S t          j        t          g|R i |S t                                          |           S )z4Create new StateSpace object and settle inheritance.r   r   r-   )
rP   r8   r   r:   r9   r   rS   r   r~   r   r   s      r    r   zStateSpace.__new__  s     v;;!
6!96I J J!9??$$$ *zz$'+34H G5;G G G?EG G G *12D E39E E E=CE E E wws###r!   c                     t          |d         t                    rdS  t                      j        di | d| _        d| _        d| _        d| _        t          | \  | _	        | _
        | _        | _        dS )z+Initialize the state space lti/dlti system.r   Nr   )r8   r   r   r%   _A_B_C_Dr	   ABCDr   s      r    r%   zStateSpace.__init__  s{     fQi!455 	F 	""6""")7)@&r!   c           
          d                     | j        j        t          | j                  t          | j                  t          | j                  t          | j                  t          | j                            S )z1Return representation of the `StateSpace` system.z{}(
{},
{},
{},
{},
dt: {}
))	r   r   rC   r   r   r   r   r   r-   r,   s    r    r   zStateSpace.__repr__  sY    3::N#LLLLLLLLMM
 
 	
r!   c           	      ~    t          |t          t          j        t          t
          t          j        t          f          S rt   )r8   r9   r   ndarrayfloatcomplexnumberintr)   others     r    _check_binop_otherzStateSpace._check_binop_other   s,    %*dlE7"&+s"4 5 5 	5r!   c           	      ,   |                      |          st          S t          |t                    r1t	          |          t	          |           urt          S | j        |j        k    rt          d          | j        j        d         }|j        j        d         }t          j
        t          j        | j        | j        |j        z  f          t          j        t          j        ||f          |j        f          f          }t          j
        | j        |j        z  |j        f          }t          j        | j        | j        |j        z  f          }| j        |j        z  }n"| j        }| j        |z  }| j        }| j        |z  }t          j        |j        |j        |j        |j                  }t          t          j        ||          t          j        ||          t          j        ||          t          j        ||          fi | j        S )a]  
        Post-multiply another system or a scalar

        Handles multiplication of systems in the sense of a frequency domain
        multiplication. That means, given two systems E1(s) and E2(s), their
        multiplication, H(s) = E1(s) * E2(s), means that applying H(s) to U(s)
        is equivalent to first applying E2(s), and then E1(s).

        Notes
        -----
        For SISO systems the order of system application does not matter.
        However, for MIMO systems, where the two systems are matrices, the
        order above ensures standard Matrix multiplication rules apply.
        z,Cannot multiply systems with different `dt`.r   dtype)r   NotImplementedr8   r9   typer-   	TypeErrorr   r   r   vstackr   r   r   r3   r   result_typer   asarrayr0   )	r)   r   n1n2abcdcommon_dtypes	            r    __mul__zStateSpace.__mul__$  s    &&u-- 	"!!eZ(( !	E{{$t**,,%%w%("" NOOOaBq!B T[$&$&572B)CDD![$*b"X*>*>)HIIK L LATVeg-uw788ATVTVeg%5677A AA AAAA'!'17KK$,q===,q===,q===,q===+ + !M	+ + 	+r!   c           	         |                      |          rt          |t                    rt          S | j        }| j        }|| j        z  }|| j        z  }t          j	        |j
        |j
        |j
        |j
                  }t          t          j        ||          t          j        ||          t          j        ||          t          j        ||          fi | j        S )z4Pre-multiply a scalar or matrix (but not StateSpace)r   )r   r8   r9   r   r   r   r   r   r   r   r   r   r0   r)   r   r   r  r  r  r  s          r    __rmul__zStateSpace.__rmul__`  s    &&u-- 	"E:1N1N 	"!! FFDFNDFN'!'17KK$,q===,q===,q===,q===+ + !M	+ + 	+r!   c                 X    t          | j        | j        | j         | j         fi | j        S )z8Negate the system (equivalent to pre-multiplying by -1).)r9   r   r   r   r   r0   r,   s    r    __neg__zStateSpace.__neg__r  s,    $&$&46'DF7LLdmLLLr!   c           	      B   |                      |          st          S t          |t                    rt	          |          t	          |           ur=t          d                    t	          |           t	          |                              | j        |j        k    rt          d          t          | j	        |j	                  }t          j        | j        |j        f          }t          j        | j        |j        f          }| j        |j        z   }n{t          j        |          }| j        j        |j        k    r | j	        }| j        }| j        }| j        |z   }n2t%          d                    | j        j        |j                            t          j        |j        |j        |j        |j                  }t          t          j        ||          t          j        ||          t          j        ||          t          j        ||          fi | j        S )zM
        Adds two systems in the sense of frequency domain addition.
        zCannot add {} and {}z'Cannot add systems with different `dt`.z;Cannot add systems with incompatible dimensions ({} and {})r   )r   r   r8   r9   r   r   r   r-   r   r   r   r   r   r   r   r   
atleast_2dr   rT   r   r   r   r0   r  s          r    __add__zStateSpace.__add__v  s    &&u-- 	"!!eZ(( $	EE{{$t**,, 6 = =d4jj>B5kk!K !K L L L w%("" IJJJ 4657++ATVUW-..ATVUW-..A AAOE**Ev|u{** FFFFUN  ":"(&u{"C"CE E E '!'17KK$,q===,q===,q===,q===+ + !M	+ + 	+r!   c                 f    |                      |          st          S |                     |           S rt   r   r   r  r   s     r    __sub__zStateSpace.__sub__  s2    &&u-- 	"!!||UF###r!   c                 d    |                      |          st          S |                     |          S rt   r  r   s     r    __radd__zStateSpace.__radd__  s0    &&u-- 	"!!||E"""r!   c                 f    |                      |          st          S |                      |          S rt   r  r   s     r    __rsub__zStateSpace.__rsub__  s2    &&u-- 	"!!u%%%r!   c                     |                      |          rt          |t                    rt          S t          |t          j                  r|j        dk    rt          d          |                     d|z            S )z$
        Divide by a scalar
        r   z3Cannot divide StateSpace by non-scalar numpy arraysr   )	r   r8   r9   r   r   r   ndimrT   r  r   s     r    __truediv__zStateSpace.__truediv__  s    
 &&u-- 	"E:1N1N 	"!!eT\** 	GuzA~~EG G G ||AeG$$$r!   c                     | j         S )z(State matrix of the `StateSpace` system.)r   r,   s    r    r   zStateSpace.A       wr!   c                 .    t          |          | _        d S rt   )r   r   )r)   r   s     r    r   zStateSpace.A      %a((r!   c                     | j         S )z(Input matrix of the `StateSpace` system.)r   r,   s    r    r   zStateSpace.B  r  r!   c                 \    t          |          | _        | j        j        d         | _        d S )Nr   )r   r   r   r   r&   )r)   r   s     r    r   zStateSpace.B  s%    %a((fl2&r!   c                     | j         S )z)Output matrix of the `StateSpace` system.)r   r,   s    r    r   zStateSpace.C  r  r!   c                 \    t          |          | _        | j        j        d         | _        d S )Nr   )r   r   r   r   r'   )r)   r   s     r    r   zStateSpace.C  s$    %a((v|Ar!   c                     | j         S )z.Feedthrough matrix of the `StateSpace` system.)r   r,   s    r    r   zStateSpace.D  r  r!   c                 .    t          |          | _        d S rt   )r   r   )r)   r   s     r    r   zStateSpace.D  r  r!   c                 f    |j         | _         |j        | _        |j        | _        |j        | _        dS )z
        Copy the parameters of another `StateSpace` system.

        Parameters
        ----------
        system : instance of `StateSpace`
            The state-space system that is to be copied

        N)r   r   r   r   r   s     r    r   zStateSpace._copy  s,     r!   c                 h    t          t          | j        | j        | j        | j        fi |i | j        S )aC  
        Convert system representation to `TransferFunction`.

        Parameters
        ----------
        kwargs : dict, optional
            Additional keywords passed to `ss2zpk`

        Returns
        -------
        sys : instance of `TransferFunction`
            Transfer function of the current system

        )r@   r   r   r   r   r   r0   r)   r   s     r    rA   zStateSpace.to_tf   sM      tw$' "1 "1)/"1 "1 C48MC C 	Cr!   c                 h    t          t          | j        | j        | j        | j        fi |i | j        S )aO  
        Convert system representation to `ZerosPolesGain`.

        Parameters
        ----------
        kwargs : dict, optional
            Additional keywords passed to `ss2zpk`

        Returns
        -------
        sys : instance of `ZerosPolesGain`
            Zeros, poles, gain representation of the current system

        )r=   r   r   r   r   r   r0   r$  s     r    r2   zStateSpace.to_zpk  sM     vdgtw  0  0(. 0  0 B37=B B 	Br!   c                 *    t          j        |           S )z
        Return a copy of the current `StateSpace` system.

        Returns
        -------
        sys : instance of `StateSpace`
            The current system (copy)

        r   r,   s    r    r:   zStateSpace.to_ss$  r   r!   )rC   rD   rE   rx   __array_priority____array_ufunc__r   r%   r   r   r  r  r
  r  r  r  r  r  rF   r   r   r   r   r   r   rA   r2   r:   rG   rH   s   @r    r9   r9     s+       % %P O$ $ $ $ $$A A A A A 	
 	
 	
5 5 5:+ :+ :+x+ + +$M M M2+ 2+ 2+h$ $ $# # #& & &% % %   X X) ) X)   X X' ' X'   X X' ' X'   X X) ) X)  C C C$B B B$
# 
# 
# 
# 
# 
# 
#r!   r9   c                       e Zd ZdZddZdS )rS   a  
    Continuous-time Linear Time Invariant system in state-space form.

    Represents the system as the continuous-time, first order differential
    equation :math:`\dot{x} = A x + B u`.
    Continuous-time `StateSpace` systems inherit additional functionality
    from the `lti` class.

    Parameters
    ----------
    *system: arguments
        The `StateSpace` class can be instantiated with 1 or 3 arguments.
        The following gives the number of input arguments and their
        interpretation:

            * 1: `lti` system: (`StateSpace`, `TransferFunction` or
              `ZerosPolesGain`)
            * 4: array_like: (A, B, C, D)

    See Also
    --------
    scipy.signal.StateSpaceContinuous
    TransferFunction, ZerosPolesGain, lti
    ss2zpk, ss2tf, zpk2sos

    Notes
    -----
    Changing the value of properties that are not part of the
    `StateSpace` system representation (such as `zeros` or `poles`) is very
    inefficient and may lead to numerical inaccuracies.  It is better to
    convert to the specific system representation first. For example, call
    ``sys = sys.to_zpk()`` before accessing/changing the zeros, poles or gain.
    rm   Nc                 |    t          t          | j        | j        | j        | j        f|||          dd         d|iS )z
        Returns the discretized `StateSpace` system.

        Parameters: See `cont2discrete` for details.

        Returns
        -------
        sys: instance of `dlti` and `StateSpace`
        r   Nr   r-   )r9   r   r   r   r   r   ro   s       r    rr   z StateSpaceContinuous.to_discreteT  s[     =$&$&$&$&)I)+06/46 6 6 7:rc; !  	! ! 	!r!   rw   r   r   r!   r    rS   rS   1  s3           D! ! ! ! ! !r!   rS   c                       e Zd ZdZdS )r~   aM  
    Discrete-time Linear Time Invariant system in state-space form.

    Represents the system as the discrete-time difference equation
    :math:`x[k+1] = A x[k] + B u[k]`.
    `StateSpace` systems inherit additional functionality from the `dlti`
    class.

    Parameters
    ----------
    *system: arguments
        The `StateSpace` class can be instantiated with 1 or 3 arguments.
        The following gives the number of input arguments and their
        interpretation:

            * 1: `dlti` system: (`StateSpace`, `TransferFunction` or
              `ZerosPolesGain`)
            * 4: array_like: (A, B, C, D)
    dt: float, optional
        Sampling time [s] of the discrete-time systems. Defaults to `True`
        (unspecified sampling time). Must be specified as a keyword argument,
        for example, ``dt=0.1``.

    See Also
    --------
    scipy.signal.StateSpaceDiscrete
    TransferFunction, ZerosPolesGain, dlti
    ss2zpk, ss2tf, zpk2sos

    Notes
    -----
    Changing the value of properties that are not part of the
    `StateSpace` system representation (such as `zeros` or `poles`) is very
    inefficient and may lead to numerical inaccuracies.  It is better to
    convert to the specific system representation first. For example, call
    ``sys = sys.to_zpk()`` before accessing/changing the zeros, poles or gain.
    Nr   r   r!   r    r~   r~   e  s        $ $J 	Dr!   r~   Tc                 @   t          | t                    r|                                 }n?t          | t                    rt	          d          t          |                                  }t          j        |          }t          |j                  dk    rt          d          t          t
          j        |j        |j        |j        |j        f          \  }}}}	|j        d         }
|j        d         }|j        }|t          j        |
|j        j                  }t          j        ||
f|j        j                  }|d         dk    r||d<   n?|d         dk    r$|t)          |j        |d         z            z  |d<   nt          d          |du p6t          |t,          t.          f          o|dk    pt          j        |           }|dk    rft          j        ||j        z            }|s|t          j        ||	j        z            z  }|t          j        |          t          j        |          fS |d         |d         z
  }t          j        t          j        |          |          st          d          |rt)          |j        |z            }t9          d|          D ]}||dz
           |z  ||<   t          j        ||j        z            }|t          j        |          t          j        |          fS t          j        |          }|j        dk    r|dddf         }|j        d         |k    rt          d	          |j        d         |k    rt          d
          |st          j        t          j        ||z  ||z  g          t          j        ||
|z   f          g          }t)          |j                  }|d|
d|
f         }||
dd|
f         }t9          d|          D ]"}||dz
           |z  ||dz
           |z  z   ||<   #n%t          j        ||z  ||z  t          j        |
|f          g          t          j        t          j        ||
|z   f          t          j         |          g          t          j        ||
d|z  z   f          g}t          j        |          }t)          |j                  }|d|
d|
f         }||
|z   dd|
f         }||
|
|z   d|
f         |z
  }t9          d|          D ].}||dz
           |z  ||dz
           |z  z   ||         |z  z   ||<   /t          j        ||j        z            t          j        ||	j        z            z   }|t          j        |          t          j        |          fS )a  
    Simulate output of a continuous-time linear system.

    Parameters
    ----------
    system : an instance of the LTI class or a tuple describing the system.
        The following gives the number of elements in the tuple and
        the interpretation:

        * 1: (instance of `lti`)
        * 2: (num, den)
        * 3: (zeros, poles, gain)
        * 4: (A, B, C, D)

    U : array_like
        An input array describing the input at each time `T`
        (interpolation is assumed between given times).  If there are
        multiple inputs, then each column of the rank-2 array
        represents an input.  If U = 0 or None, a zero input is used.
    T : array_like
        The time steps at which the input is defined and at which the
        output is desired.  Must be nonnegative, increasing, and equally spaced
    X0 : array_like, optional
        The initial conditions on the state vector (zero by default).
    interp : bool, optional
        Whether to use linear (True, the default) or zero-order-hold (False)
        interpolation for the input array.

    Returns
    -------
    T : 1D ndarray
        Time values for the output.
    yout : 1D ndarray
        System response.
    xout : ndarray
        Time evolution of the state vector.

    Notes
    -----
    If (num, den) is passed in for ``system``, coefficients for both the
    numerator and denominator should be specified in descending exponent
    order (e.g. ``s^2 + 3s + 5`` would be represented as ``[1, 3, 5]``).

    See Also
    --------
    scipy.signal.lsim

    z3lsim can only be used with continuous-time systems.r   zT must be a rank-1 array.r   Nz Initial time must be nonnegative        z"Time steps are not equally spaced.z5U must have the same number of rows as elements in T.z(System does not define that many inputs.rM   )!r8   rJ   r;   rz   AttributeErrorr   r   rP   r   rT   mapr   r   r   r   r   sizer3   r   emptyr   rZ   r   r   anysqueezeallcloser   ranger  r   r   identity)r   ra   rZ   rY   interpsysr   r   r   r   n_statesn_inputsn_stepsxoutno_inputyoutr-   expAT_dtiMexpMTAdBdMlstBd1Bd0s                             r    r`   r`     s   b &# $mmoo	FD	!	! $ ( ) ) 	) 6l!!##A
17||q4555T\CE35#%#?@@JAq!QwqzHwqzHfG	zZ#%+..:w)35;77DtqyyQ	
1tAC!A$J'''Q;<<<T	  AU|,,8b HQKK  !|||D13J'' 	*DLQS)))D$,t$$dl4&8&888	
1!B=1r** ?=>>> 9 b>>q'"" 	+ 	+A1Q3i(*DGG|D13J''$,t$$dl4&8&888 	Av{{aaagJwqzW - . . 	. 	wqzXCDDD (I Ka"fa"f%566Xx(/B$CDDF G G QS		9H9ixi'(899ixi'(q'"" 	3 	3A1Q3i"nq1v{2DGG	3 QVQV!Z8(<==? @ @TZ8h3F(GHH!]8446 7 7
HhX&=>??	A KQS		9H9ixi'(HX%&&		12HX00)8);<sBq'"" 	I 	IAQqS	B1QqS6C<8AaD3JGDGG<qs
##dl1qs7&;&;;Ddl4  $,t"4"444r!   c                 0   ddl }|j                            |                                           }t	          j        |          }t	          j        t	          j        |j                            }|dk    rd}d|z  }t	          j	        dd|z  |          }|S )a	  Compute a reasonable set of time samples for the response time.

    This function is used by `impulse`, `impulse2`, `step` and `step2`
    to compute the response time when the `T` argument to the function
    is None.

    Parameters
    ----------
    A : array_like
        The system matrix, which is square.
    n : int
        The number of time samples to generate.

    Returns
    -------
    t : ndarray
        The 1-D array of length `n` of time samples at which the response
        is to be computed.

    r   Nr-        ?   )
numpyr   eigvalsr   r   r   minabsreallinspace)r   rg   npvalsrtcr   s          r    _default_response_timesrU  .  s    : 9QUUWW%%D<D$)$$%%ACxx	qBc1r61%%AHr!   c                    t          | t                    r|                                 }n?t          | t                    rt	          d          t          |                                  }|t          j        |j                  }nt          j        |j        |z             }|d}|t          |j	        |          }nt          j
        |          }t          |d||d          \  }}}||fS )az  Impulse response of continuous-time system.

    Parameters
    ----------
    system : an instance of the LTI class or a tuple of array_like
        describing the system.
        The following gives the number of elements in the tuple and
        the interpretation:

            * 1 (instance of `lti`)
            * 2 (num, den)
            * 3 (zeros, poles, gain)
            * 4 (A, B, C, D)

    X0 : array_like, optional
        Initial state-vector.  Defaults to zero.
    T : array_like, optional
        Time points.  Computed if not given.
    N : int, optional
        The number of time points to compute (if `T` is not given).

    Returns
    -------
    T : ndarray
        A 1-D array of time points.
    yout : ndarray
        A 1-D array containing the impulse response of the system (except for
        singularities at zero).

    Notes
    -----
    If (num, den) is passed in for ``system``, coefficients for both the
    numerator and denominator should be specified in descending exponent
    order (e.g. ``s^2 + 3s + 5`` would be represented as ``[1, 3, 5]``).

    See Also
    --------
    scipy.signal.impulse

    z6impulse can only be used with continuous-time systems.Nrc   r-  F)r7  )r8   rJ   r;   rz   r.  r   r3  r   rU  r   r   r`   )r   rY   rZ   rU   r8  X_hs           r    r[   r[   W  s    R &# $mmoo	FD	!	! $ ( ) ) 	) 6l!!##	zLL$$yy#CE1--LOO3Aq///GAq!a4Kr!   c                    t          | t                    r|                                 }n?t          | t                    rt	          d          t          |                                  }|d}|t          |j        |          }nt          j        |          }t          j	        |j
        |j        j                  }t          ||||d          }|d         |d         fS )a  Step response of continuous-time system.

    Parameters
    ----------
    system : an instance of the LTI class or a tuple of array_like
        describing the system.
        The following gives the number of elements in the tuple and
        the interpretation:

            * 1 (instance of `lti`)
            * 2 (num, den)
            * 3 (zeros, poles, gain)
            * 4 (A, B, C, D)

    X0 : array_like, optional
        Initial state-vector (default is zero).
    T : array_like, optional
        Time points (computed if not given).
    N : int, optional
        Number of time points to compute if `T` is not given.

    Returns
    -------
    T : 1D ndarray
        Output time points.
    yout : 1D ndarray
        Step response of system.


    Notes
    -----
    If (num, den) is passed in for ``system``, coefficients for both the
    numerator and denominator should be specified in descending exponent
    order (e.g. ``s^2 + 3s + 5`` would be represented as ``[1, 3, 5]``).

    See Also
    --------
    scipy.signal.step

    z3step can only be used with continuous-time systems.Nrc   F)rY   r7  r   r   )r8   rJ   r;   rz   r.  rU  r   r   r   onesr   r   r`   )r   rY   rZ   rU   r8  ra   rR  s          r    r^   r^     s    R &# $mmoo	FD	!	! $ ( ) ) 	) 6l!!##yy#CE1--LOO	!'35;''AQb///D7DGr!   rc   c                     t          | ||          \  }}dt          j        t          |                    z  }t          j        t          j        |j        |j                            dz  t          j        z  }|||fS )a(  
    Calculate Bode magnitude and phase data of a continuous-time system.

    Parameters
    ----------
    system : an instance of the LTI class or a tuple describing the system.
        The following gives the number of elements in the tuple and
        the interpretation:

            * 1 (instance of `lti`)
            * 2 (num, den)
            * 3 (zeros, poles, gain)
            * 4 (A, B, C, D)

    w : array_like, optional
        Array of frequencies (in rad/s). Magnitude and phase data is calculated
        for every value in this array. If not given a reasonable set will be
        calculated.
    n : int, optional
        Number of frequency points to compute if `w` is not given. The `n`
        frequencies are logarithmically spaced in an interval chosen to
        include the influence of the poles and zeros of the system.

    Returns
    -------
    w : 1D ndarray
        Frequency array [rad/s]
    mag : 1D ndarray
        Magnitude array [dB]
    phase : 1D ndarray
        Phase array [deg]

    See Also
    --------
    scipy.signal.bode

    Notes
    -----
    If (num, den) is passed in for ``system``, coefficients for both the
    numerator and denominator should be specified in descending exponent
    order (e.g. ``s^2 + 3s + 5`` would be represented as ``[1, 3, 5]``).

    re         4@g     f@)	rl   r   log10rN  unwraparctan2imagrO  pi)r   rf   rg   ymagphases         r    rh   rh     sn    X Fa1%%%DAq
CFF##
#CKQVQV4455=GEc5=r!   rj   c                 t   t          | t                    r4t          | t          t          f          r| }nT|                                 }n?t          | t
                    rt          d          t          |                                  }|j        dk    s|j        dk    rt          d          ||}n|}t          |t                    r2t          |j                                        |j        |          \  }}n:t          |t                    r%t          |j        |j        |j        |          \  }}||fS )a  Calculate the frequency response of a continuous-time system.

    Parameters
    ----------
    system : an instance of the `lti` class or a tuple describing the system.
        The following gives the number of elements in the tuple and
        the interpretation:

            * 1 (instance of `lti`)
            * 2 (num, den)
            * 3 (zeros, poles, gain)
            * 4 (A, B, C, D)

    w : array_like, optional
        Array of frequencies (in rad/s). Magnitude and phase data is
        calculated for every value in this array. If not given, a reasonable
        set will be calculated.
    n : int, optional
        Number of frequency points to compute if `w` is not given. The `n`
        frequencies are logarithmically spaced in an interval chosen to
        include the influence of the poles and zeros of the system.

    Returns
    -------
    w : 1D ndarray
        Frequency array [rad/s]
    H : 1D ndarray
        Array of complex magnitude values

    Notes
    -----
    If (num, den) is passed in for ``system``, coefficients for both the
    numerator and denominator should be specified in descending exponent
    order (e.g. ``s^2 + 3s + 5`` would be represented as ``[1, 3, 5]``).

    See Also
    --------
    scipy.signal.freqresp

    z7freqresp can only be used with continuous-time systems.r   z@freqresp() requires a SISO (single input, single output) system.N)worN)r8   rJ   r@   r=   r>   rz   r.  r&   r'   rT   r   r   ravelr   r   r3   r6   r   )r   rf   rg   r8  rg  rY  s         r    rl   rl     s@   R &# 	%f/@AA 	#CC..""CC	FD	!	! % ( ) ) 	) 6l""$$
zQ#+** + , , 	, 	}#'(( D SW]]__cgD99911	C	(	( DCIsxdCCC1a4Kr!   c                       e Zd Zd ZdS )Bunchc                 :    | j                             |           d S rt   )__dict__update)r)   kwdss     r    r%   zBunch.__init__R  s    T"""""r!   N)rC   rD   rE   r%   r   r!   r    rj  rj  Q  s#        # # # # #r!   rj  c                    |j         dk    rt          d          t          |          }| j         dk    rt          d          |j         dk    rt          d          | j        d         | j        d         k    rt          d          t	          |          | j        d         k    r,t          d| j        d         t	          |          fz            t	          |          | j        d         k     r,t          d	t	          |          | j        d         fz            t
          j                            |          }|D ](}t          ||k              |k    rt          d
          )t          }|dvrt          d          |dk    r7t          }t          t          j        |                    st          d          |dk     rt          d          |dk    rt          d          ||fS )z
    Check the poles come in complex conjugage pairs
    Check shapes of A, B and poles are compatible.
    Check the method chosen is compatible with provided poles
    Return update method to use and ordered poles

    r   zPoles must be a 1D array like.rM   zA must be a 2D array/matrix.zB must be a 2D array/matrixr   zA must be squarez2maximum number of poles is %d but you asked for %dz/number of poles is %d but you should provide %dzFat least one of the requested pole is repeated more than rank(B) times)KNV0YTz0The method keyword must be one of 'YT' or 'KNV0'rp  z'Complex poles are not supported by KNV0z#maxiter must be at least equal to 1zrtol can not be greater than 1)r  rT   _order_complex_polesr   rP   r   r   matrix_ranksum_YT_loop
_KNV0_loopallisreal)	r   r   r6   rp   rtolmaxiterrS  pupdate_loops	            r    _valid_inputsr}  V  s    zA~~9::: ''Evzz7888vzz6777wqzQWQZ+,,,
5zzAGAJM'!*c%jj12 3 3 	3
5zzAGAJJe**agaj12 3 3 	3""A 8 8qEz??Q 7 8 8 8  K^##KLLL 4;u%%&& 	HFGGG{{>??? axx9:::r!   c                    t          j        | t          j        |                              }g }t          j        | t          j        |           dk                        D ]A}t          j        |          | v r)|                    |t          j        |          f           Bt          j        ||f          }| j        d         t          |          k    rt          d          |S )z
    Check we have complex conjugates pairs and reorder P according to YT, ie
    real_poles, complex_i, conjugate complex_i, ....
    The lexicographic sort on the complex poles is added to help the user to
    compare sets of poles.
    r   z-Complex poles must come with their conjugates)
r   sortrx  ra  conjextendr   r   rP   rT   )r6   ordered_polesim_polesr{  s       r    rr  rr    s     IeDK$6$6788MHYuTYu--1233 / /9Q<<5  OOQ	!-...K 9::M{1~]++++HIIIr!   c                 J   t          j        ||d          }t           j                            |d          \  }}||         ||         j        z  }||dddf         z  }	t          j        |	d          s-|	t           j                            |	          z  }
|
|dd|f<   dS dS )z
    Algorithm "KNV0" Kautsky et Al. Robust pole
    assignment in linear state feedback, Int journal of Control
    1985, vol 41 p 1129->1155
    https://la.epfl.ch/files/content/sites/la/files/
        users/105941/public/KautskyNicholsDooren

    r   axiscompletemodeNr   r   )r   deleter   qrrZ   r4  norm)r   ker_poletransfer_matrixjr6   transfer_matrix_not_jQR
mat_ker_pjyjxjs              r    _KNV0r    s     !KCCC ;>>/j>AADAq!x{},J	a2h	B =Q #$+""2&&& "1# #r!   c           	         t          |          }t          |          }|ddddf         }|ddddf         }| |         j        ||j        z  ||j        z  z
  z  | |         z  }t          j                            |          \  }}	}
|j        dddddf         \  }}|
dddddf         \  }}t          j        |dd|df         |dd|df         f          }t          j        |	d         |	d                   s-| |         |z  }| |         |z  }t          j        ||f          }nt          j        t          j        | |         t          j        | |         j	                  f          t          j        t          j        | |         j	                  | |         f          f          }t          j        t          j        ||f          t          j        ||f          f          }||z  }||j        z  |z  }t          j        |d          st          d          |z  t          j                            |          z  }|d|dd|f         j	        d         df         |dd|f<   ||dd|f         j	        d         ddf         |dd|f<   dS |d|dd|f         j	        d         df         |dd|f<   ||dd|f         j	        d         ddf         |dd|f<   dS )zM
    Applies algorithm from YT section 6.1 page 19 related to real pairs
    Nr   rM   r   r   )r   rZ   r   r   svdr   r4  r   r3   r   r   r  )r  r  r  r@  r  r   vmumsmvmmu1mu2nu1nu2&transfer_matrix_j_mo_transfer_matrix_jker_pole_imo_mu1ker_pole_i_nu1ker_pole_mu_nuker_pole_ijmu_nu_matrixtransfer_matrix_ijs                         r    _YT_realr    sX    	AAAA 	
!!!R+A	!!!R+A
 
!!ac'AG+	,;A ##JBBtBQB4K HC"1"aaa+HC .2[1d
#1d
#:% .& .&* =A1&& 4#A;,!!s*&6%GHHkK!HQK$5668 9 9KHQK$566!!& ' '#
   {[#s$$dk3*&=&=>
 
 %|3)N,<<BC =+Q// 
"1gg(::"k../ABBC 2+_QQQT"(++Q.!
1 !3AAAqD!'*++Q.!
1 !/+_QQQT"(++Q.!
1 !/AAAqD!'*++Q.!
1r!   c                    t          d          |ddddf         z  }t          d          |ddddf         z  }|d|z  z   }| |         }|                                j        ||                                j        z  |                                |j        z  z
  z  |z  }	ddl}
|
j                            |	                                          \  }}t          j        |          t          j        |          }}t          j	        t          j
        |                    }|dd|d         df         }|dd|d         df         }|dd|df         d|dd|df         z  z   }t          j        t          j
        ||d                            t          j
        ||d                                      s||z  }nt          j        ||f          }||z  }t          j        ||                                j        z  |          }t          j        |d          sn|t          j                            |          z  }t          j        |dddf                   |dd|f<   t          j        |dddf                   |dd|f<   dS t          j        |dddf                   |dd|f<   t          j        |dddf                   |dd|f<   dS )zP
    Applies algorithm from YT section 6.2 page 20 related to complex pairs
    rM   Nr  r                 ?r   )r   r  rZ   rK  r   eigr   r   r   argsortrN  r4  r   dotr  rO  ra  )r  r  r  r@  r  uruir   r  r  rQ  e_vale_vec	e_val_idxr  r  r  ker_pole_mumu1_mu2_matrixtransfer_matrix_i_js                       r    _YT_complexr    s   
 
a1QQQD[>	!B	a1QQQD[>	!B
RU
A 1+K 	AFFHHJAC ?@;NA 9==))LE5<&&U(;(;5E TXe__--I
9R=$&
'C
9R=$&
'C 	1d
#
?111a:&&	' +
 =%	""677%	""6779 9 3!C'c3Z00!N2(K+2B2B2D2D2F$F#IK K =,a00 =2#{//0CDD E $	*=aaad*C D D1 $	*=aaad*C D D1 !%	+aaad*; < <1 $	+aaad*; < <1r!   c                 0   |t          j        |                   j        d         }|dz  }|dk    r+t          j        |          gt          j        d          gg}ng g g}t          j        |dz   t          |          dz   d          }	t          j        d||dz  z             }
|d                             d|
z             |d                             d|
z  dz              |d                             |	           |d                             |	dz              t          j        d|dz             }
|d                             d|
z  dz
             |d                             d|
z             |dk    rtt          j        |d                   rZ|d                             t          j        d                     |d                             t          j        d                     |d                             |	           |d                             |	dz              t          j        d||dz  z             }|D ]u}t          d|dz             D ]_}|d                             t          j        |                     |d                             t          j        ||z                        `v|dk    rtt          j        |d                   rZ|d                             t          j        d                     |d                             t          j        d                     |d                             |	           |d                             |	dz              t          j        d||dz  z             }|D ]}t          |dz   |dz             D ]o}||z   }||k    r||z   |z
  }|d                             t          j        |                     |d                             t          j        |                     p|dk    rtt          j        |d                   rZ|d                             t          j        d                     |d                             t          j        d                     |d                             |	           |d                             |	dz              t          d|dz             D ]_}|d                             t          j        |                     |d                             t          j        ||z                        `|dk    rtt          j        |d                   rZ|d                             t          j        d                     |d                             t          j        d                     |d                             |	           |d                             |	dz              t          j        |          j	        dz
  }d}d}||k     r|s~t          j
        t           j                            |                    }|D ]\  }}t          |          t          |          }}||k    rH|dk    s
J d            t          j        ||                   s
J d            t          || |||           rt          t          |j        d                             }|                    |           |                    |dz
             |dd|f         }t           j                            |d	          \  }}t          j        ||                   rIt          j        ||                   sJ d
t%          |          z               t'          | ||||           Yd
t%          |          z   }t          j        ||                    s
J |            t)          | ||||           t+          t          j        t           j                  j                  }t3          |t          j
        t           j                            |                    f          }t          j
        ||z
  |z            }||k     r||k    rd}|dz  }||k     r|~|||fS )z
    Algorithm "YT" Tits, Yang. Globally Convergent
    Algorithms for Robust Pole Assignment by State Feedback
    https://hdl.handle.net/1903/5598
    The poles P have to be sorted accordingly to section 6.2 page 20

    r   rM   r   Fzi!=0 for KNV call in YTzcalling KNV on a complex poleNr  r  z"mixing real and complex in YT_realT)r   rx  r   arrayarangerP   r  appendr5  rZ   rN  r   detr   r  listr   r  strr  r  r   finfofloat64epsmax)r  r  r6   r   rz  ry  nb_realhnbupdate_orderr_compr_pr_jr  r@  idx_1stopnb_trydet_transfer_matrixbidxtransfer_matrix_not_i_jr  rX  msg
sq_spacingdet_transfer_matrixcur_rtols                             r    ru  ru  N  s    DK&&'-a0G
Q,C {{G,,-
1?Bx[CJJqL!44F
+aWq[
)
)CO1S5!!!O1S57###O6"""O6!8$$$
+aQ

CO1S57###O1S5!!!
axxDKa))xQtz!}}---Qtz!}}---O6"""O6!8$$$
+aWq[
)
)C 4 4q#a% 	4 	4AO""4:a==111O""4:ac??3333	4 axxDKa))xQtz!}}---Qtz!}}---O6"""O6!8$$$
+aWq[
)
)C 6 6s1ugai(( 	6 	6AaCEw!GO""4:a==111O""4:e#4#45555	6 axxDKa))xQtz!}}---Qtz!}}---O6"""O6!8$$$1c!e__ 2 2Qtz!}}---Qtz!C%001111
axxDKa))xQtz!}}---Qtz!}}---O6"""O6!8$$$:l++-a/LDF
7

4
#x(H(HII  	D 	DDAqq663q66qAAvvAvvv8vvv{58,,MM.MMM,a?Au==== 5!6q!9::;;


!*9!!!S&*A' {~~&=J~OO1;uQx(( D;uQx00 2 2 3%'*5zz32 2 20Xq/1a@@@@>UKC Ka111663661!_aCCCC$*T\22677
!:"&(4;???+K+K"L"L#N O O8 !" ! ! d??2Z??D!Y 7

4
Z 6!!r!   c                 P   d}d}||k     r|st          j        t           j                            |                    }t	          |j        d                   D ]}	t          || ||	|           t          t          t          j        t           j	                  j
                            }
t          |
t          j        t           j                            |                    f          }t          j        ||z
  |z            }||k     r||
k    rd}|dz  }||k     r||||fS )zI
    Loop over all poles one by one and apply KNV method 0 algorithm
    Fr   Tr   )r   rN  r   r  r5  r   r  r   r  r  r  r  )r  r  r6   r   rz  ry  r  r  r  r  r  r  r  s                r    rv  rv    s0    DF
7

4
#x(H(HIIqwqz"" 	: 	:A!X59999$tz$,77;<<==
!:"&(4;???+K+K"L"L#N O O803GG/0 1 1d??2Z??D! 7

4
  6!!r!   rq  MbP?   c           
      X   t          | |||||          \  }}d}d}t          j                            |d          \  }	}
t          j                            |          }|	ddd|f         }|	dd|df         }|
d|ddf         }
|j        d         |k    r9t          j        | j                  }d}||j        d         k     r||         }t          j        |          |||f<   t          j        |           r]t          j	        |           |||dz   f<   t          j        |          ||dz   |dz   f<   t          j	        |          ||dz   |f<   |dz  }|dz  }||j        d         k     t          j        
                    ||| z
  d          d         }t          j        | j        d                   }t          j        }t          j        }n(g }d}t          |j        d                   D ]s}|rd}t          j        |j        | ||         t          j        |j        d                   z  z
            j        }t          j                            |d          \  }}|dd|j        d         df         }t          j        |d	          dddf         }|t          j                            |          z  }t          j        ||                    rTt          j        t          j        |          t          j	        |          g          }|                    ||g           d
}n|                    |           |dk    r|}]t          j        ||f          }u|dk    r; |||||||          \  }}}|s%|dk    rd| d| d}t+          j        |d           |                    t0                    }d}||j        d         dz
  k     rt          j        ||                    rS|dd|f                                         }|dd|dz   f         }|d|z  z
  |dd|f<   |d|z  z   |dd|dz   f<   |dz  }|dz  }||j        d         dz
  k     	 t          j                            |j        t          j        |          |j        z            j        }t          j                            |
|j        || z
  z            }n,# t          j        j        $ r} t;          d          | d} ~ ww xY w| }t          j        |          }t=                      }!||!_        | ||z  z
                                   }"ddl!}#|#j        "                    |"          d         }tG          t          j$        |                    }$|$|!_%        ||!_&        ||!_'        ||!_(        ||!_)        |!S )a  
    Compute K such that eigenvalues (A - dot(B, K))=poles.

    K is the gain matrix such as the plant described by the linear system
    ``AX+BU`` will have its closed-loop poles, i.e the eigenvalues ``A - B*K``,
    as close as possible to those asked for in poles.

    SISO, MISO and MIMO systems are supported.

    Parameters
    ----------
    A, B : ndarray
        State-space representation of linear system ``AX + BU``.
    poles : array_like
        Desired real poles and/or complex conjugates poles.
        Complex poles are only supported with ``method="YT"`` (default).
    method: {'YT', 'KNV0'}, optional
        Which method to choose to find the gain matrix K. One of:

            - 'YT': Yang Tits
            - 'KNV0': Kautsky, Nichols, Van Dooren update method 0

        See References and Notes for details on the algorithms.
    rtol: float, optional
        After each iteration the determinant of the eigenvectors of
        ``A - B*K`` is compared to its previous value, when the relative
        error between these two values becomes lower than `rtol` the algorithm
        stops.  Default is 1e-3.
    maxiter: int, optional
        Maximum number of iterations to compute the gain matrix.
        Default is 30.

    Returns
    -------
    full_state_feedback : Bunch object
        full_state_feedback is composed of:
            gain_matrix : 1-D ndarray
                The closed loop matrix K such as the eigenvalues of ``A-BK``
                are as close as possible to the requested poles.
            computed_poles : 1-D ndarray
                The poles corresponding to ``A-BK`` sorted as first the real
                poles in increasing order, then the complex congugates in
                lexicographic order.
            requested_poles : 1-D ndarray
                The poles the algorithm was asked to place sorted as above,
                they may differ from what was achieved.
            X : 2-D ndarray
                The transfer matrix such as ``X * diag(poles) = (A - B*K)*X``
                (see Notes)
            rtol : float
                The relative tolerance achieved on ``det(X)`` (see Notes).
                `rtol` will be NaN if it is possible to solve the system
                ``diag(poles) = (A - B*K)``, or 0 when the optimization
                algorithms can't do anything i.e when ``B.shape[1] == 1``.
            nb_iter : int
                The number of iterations performed before converging.
                `nb_iter` will be NaN if it is possible to solve the system
                ``diag(poles) = (A - B*K)``, or 0 when the optimization
                algorithms can't do anything i.e when ``B.shape[1] == 1``.

    Notes
    -----
    The Tits and Yang (YT), [2]_ paper is an update of the original Kautsky et
    al. (KNV) paper [1]_.  KNV relies on rank-1 updates to find the transfer
    matrix X such that ``X * diag(poles) = (A - B*K)*X``, whereas YT uses
    rank-2 updates. This yields on average more robust solutions (see [2]_
    pp 21-22), furthermore the YT algorithm supports complex poles whereas KNV
    does not in its original version.  Only update method 0 proposed by KNV has
    been implemented here, hence the name ``'KNV0'``.

    KNV extended to complex poles is used in Matlab's ``place`` function, YT is
    distributed under a non-free licence by Slicot under the name ``robpole``.
    It is unclear and undocumented how KNV0 has been extended to complex poles
    (Tits and Yang claim on page 14 of their paper that their method can not be
    used to extend KNV to complex poles), therefore only YT supports them in
    this implementation.

    As the solution to the problem of pole placement is not unique for MIMO
    systems, both methods start with a tentative transfer matrix which is
    altered in various way to increase its determinant.  Both methods have been
    proven to converge to a stable solution, however depending on the way the
    initial transfer matrix is chosen they will converge to different
    solutions and therefore there is absolutely no guarantee that using
    ``'KNV0'`` will yield results similar to Matlab's or any other
    implementation of these algorithms.

    Using the default method ``'YT'`` should be fine in most cases; ``'KNV0'``
    is only provided because it is needed by ``'YT'`` in some specific cases.
    Furthermore ``'YT'`` gives on average more robust results than ``'KNV0'``
    when ``abs(det(X))`` is used as a robustness indicator.

    [2]_ is available as a technical report on the following URL:
    https://hdl.handle.net/1903/5598

    See Also
    --------
    scipy.signal.place_poles

    References
    ----------
    .. [1] J. Kautsky, N.K. Nichols and P. van Dooren, "Robust pole assignment
           in linear state feedback", International Journal of Control, Vol. 41
           pp. 1129-1155, 1985.
    .. [2] A.L. Tits and Y. Yang, "Globally convergent algorithms for robust
           pole assignment by state feedback", IEEE Transactions on Automatic
           Control, Vol. 41, pp. 1432-1452, 1996.
    r   r  r  Nr   r   )rcondFr  TzSConvergence was not reached after maxiter iterations.
You asked for a tolerance of z	, we got .rM   )
stacklevelr  zfThe poles you've chosen can't be placed. Check the controllability matrix and try another set of poles)*r}  r   r   r  rs  r   r3   rO  rx  ra  lstsqeyenanr5  r  rZ   rt  r  r   r  r  warningswarnastyper   r   solvediagLinAlgErrorrT   rj  gain_matrixr   rK  r  rr  r   computed_polesrequested_polesrW  ry  nb_iter)%r   r   r6   rp   ry  rz  r|  r  r  r   zrankBu0u1
diag_polesr  r{  r  r  r  skip_conjugater  pole_space_jr  rX  
ker_pole_jtransfer_matrix_jr  err_msgrelimgr  efull_state_feedbacktemprQ  r  s%                                        r    place_polesr    s1   Z 'q!UFD'JJK HG ;>>!*>--DAqK##A&&E	
111fuf9B	
111eff9B	&5&!!!)A 	wqzU( Z((
EKN""c
A#'9Q<<JsCx A *.)A,,
3A:&+/9Q<<
3q5#a%<()-1
3q5#:&q1HC EKN"" k'':a<r'BB1E(171:..8(
   qwqz"" 4	: 4	:A !&8BD!E!HTXagaj5I5I,I*IJJLL ;>>,Z>@@DAq111l034445J$ !%! < < <QQQW E!2!%!1!12C!D!D"EE!H%%% ,$(K;L1M1M15;L1M1M1O %P %P!Z 8999 "&
+++Avv"3"&+$&78#: #: 199&1k(O27GT'K 'K#D(G 5D1HHO48O OCKO O O  g!4444
 *0099EKN1$$$E#J''' %aaaf-2244%aaaQh/ +.bf*3',/3J3q5)q1HC EKN1$$$	<!!/"3TY6 6(*6+ , ,,- +++Artqs|<<KK{& 	< 	< 	< 4 5 5:;<	< ,K)K((K''&1# K$$&&DIMM$"E(e)<)<==M)6&*/'+'")s    A/S S9$S44S9c                 l   t          | t                    rt          d          t          | t                    st          | dd         d| d         i} t          | t                    }|                                 } t          j        |          }|j        dk    rt          j	        |          j
        }|t          |          }|dz
  | j        z  }n4|d         }t          t          j        || j        z                      dz   }t          j        || j        j        d         f          }t          j        || j        j        d         f          }t          j        d||          }	|-t          j        | j        j        d         f          |dddf<   nt          j        |          |dddf<   ||}
n?t          |j                  dk    r|dddf         } t+          ||d	          |	          }
t-          d|dz
            D ]e}| j        ||ddf         z  | j        |
|ddf         z  z   ||dz   ddf<   | j        ||ddf         z  | j        |
|ddf         z  z   ||ddf<   f| j        ||dz
  ddf         z  | j        |
|dz
  ddf         z  z   ||dz
  ddf<   |r|	||fS |	|fS )
ab  
    Simulate output of a discrete-time linear system.

    Parameters
    ----------
    system : tuple of array_like or instance of `dlti`
        A tuple describing the system.
        The following gives the number of elements in the tuple and
        the interpretation:

            * 1: (instance of `dlti`)
            * 3: (num, den, dt)
            * 4: (zeros, poles, gain, dt)
            * 5: (A, B, C, D, dt)

    u : array_like
        An input array describing the input at each time `t` (interpolation is
        assumed between given times).  If there are multiple inputs, then each
        column of the rank-2 array represents an input.
    t : array_like, optional
        The time steps at which the input is defined.  If `t` is given, it
        must be the same length as `u`, and the final value in `t` determines
        the number of steps returned in the output.
    x0 : array_like, optional
        The initial conditions on the state vector (zero by default).

    Returns
    -------
    tout : ndarray
        Time values for the output, as a 1-D array.
    yout : ndarray
        System response, as a 1-D array.
    xout : ndarray, optional
        Time-evolution of the state-vector.  Only generated if the input is a
        `StateSpace` system.

    See Also
    --------
    scipy.signal.dlsim
    lsim, dstep, dimpulse, cont2discrete
    z7dlsim can only be used with discrete-time dlti systems.Nr   r-   r   r   r-  )r   )k)r8   rJ   r.  rz   r9   r;   r   r   r  r  rZ   rP   r-   r   floorr3   r   r   r   rP  r   r   r5  r   r   )r   r   r   r   is_ss_inputout_samplesstoptimer<  r>  toutu_dtr@  s               r    r   r   
  s   V &# 3 ( ) ) 	)%% 3vcrc{2vbz22 VZ00K]]__FAv{{OA y!ff!Ovy0R5$*X	%9::;;a? :{FHN1$5677D:{FHN1$5677D=hK888D 
zZ!2 455QT

\"%%QT
 	yqw<<1!!!T'
A,!!Q!,,,T22 1kAo&& C Cx$q!!!t*,vx$q!!!t*/DDQqS!!!VXQT
*VXQT
-BBQT

 %hk!mQQQ.>)??$hk!mQQQ.>)??@DQ	  T4Tzr!   c                 h   t          | t                    r|                                 } nPt          | t                    rt	          d          t          | dd         d| d         i                                } |d}|!t          j        d|| j        z  |d          }nt          j        |          }d}t          d| j
                  D ]b}t          j        |j        d         | j
        f          }d	|d|f<   t          | |||
          }|
|d         f}n||d         fz   }|d         }c||fS )a  
    Impulse response of discrete-time system.

    Parameters
    ----------
    system : tuple of array_like or instance of `dlti`
        A tuple describing the system.
        The following gives the number of elements in the tuple and
        the interpretation:

            * 1: (instance of `dlti`)
            * 3: (num, den, dt)
            * 4: (zeros, poles, gain, dt)
            * 5: (A, B, C, D, dt)

    x0 : array_like, optional
        Initial state-vector.  Defaults to zero.
    t : array_like, optional
        Time points.  Computed if not given.
    n : int, optional
        The number of time points to compute (if `t` is not given).

    Returns
    -------
    tout : ndarray
        Time values for the output, as a 1-D array.
    yout : tuple of ndarray
        Impulse response of system.  Each element of the tuple represents
        the output of the system based on an impulse in each input.

    See Also
    --------
    scipy.signal.dimpulse
    impulse, dstep, dlsim, cont2discrete
    z:dimpulse can only be used with discrete-time dlti systems.Nr   r-   rc   r   FendpointrI  r   r   r   )r8   rz   r;   rJ   r.  r   rP  r-   r   r5  r&   r3   r   r   	r   r   r   rg   r>  r@  r   
one_outputr  s	            r    r   r   t
  sU   J &$ <	FC	 	  < - . . 	. vcrc{2vbz2299;; 	y 	yM!Q]A>>>LOO D1fm$$  J
FM233!Q$61b111
<qM#DD:a=**D!}:r!   c                    t          | t                    r|                                 } nPt          | t                    rt	          d          t          | dd         d| d         i                                } |d}|!t          j        d|| j        z  |d          }nt          j        |          }d}t          d| j
                  D ]}t          j        |j        d         | j
        f          }t          j        |j        d         f          |dd|f<   t          | |||	          }|
|d
         f}n||d
         fz   }|d         }||fS )a  
    Step response of discrete-time system.

    Parameters
    ----------
    system : tuple of array_like
        A tuple describing the system.
        The following gives the number of elements in the tuple and
        the interpretation:

            * 1: (instance of `dlti`)
            * 3: (num, den, dt)
            * 4: (zeros, poles, gain, dt)
            * 5: (A, B, C, D, dt)

    x0 : array_like, optional
        Initial state-vector.  Defaults to zero.
    t : array_like, optional
        Time points.  Computed if not given.
    n : int, optional
        The number of time points to compute (if `t` is not given).

    Returns
    -------
    tout : ndarray
        Output time points, as a 1-D array.
    yout : tuple of ndarray
        Step response of system.  Each element of the tuple represents
        the output of the system based on a step response to each input.

    See Also
    --------
    scipy.signal.dlstep
    step, dimpulse, dlsim, cont2discrete
    z7dstep can only be used with discrete-time dlti systems.Nr   r-   rc   r   Fr  r  r   )r8   rz   r;   rJ   r.  r   rP  r-   r   r5  r&   r3   r   r[  r   r  s	            r    r   r   
  sk   J &$ <	FC	 	  < ( ) ) 	) vcrc{2vbz2299;; 	y 	yM!Q]A>>>LOO D1fm$$  J
FM233)QWQZM**!!!Q$61b111
<qM#DD:a=**D!}:r!   Fc                    t          | t                    s>t          | t                    rt          d          t          | dd         d| d         i} t          | t                    r|                                 } t          | t          t          f          st          d          | j	        dk    s| j
        dk    rt          d          ||}n|}t          | t                    rQt                              | j                                        | j                  \  }}t          ||||          \  }}n;t          | t                    r&t!          | j        | j        | j        ||          \  }}||fS )	a.  
    Calculate the frequency response of a discrete-time system.

    Parameters
    ----------
    system : an instance of the `dlti` class or a tuple describing the system.
        The following gives the number of elements in the tuple and
        the interpretation:

            * 1 (instance of `dlti`)
            * 2 (numerator, denominator, dt)
            * 3 (zeros, poles, gain, dt)
            * 4 (A, B, C, D, dt)

    w : array_like, optional
        Array of frequencies (in radians/sample). Magnitude and phase data is
        calculated for every value in this array. If not given a reasonable
        set will be calculated.
    n : int, optional
        Number of frequency points to compute if `w` is not given. The `n`
        frequencies are logarithmically spaced in an interval chosen to
        include the influence of the poles and zeros of the system.
    whole : bool, optional
        Normally, if 'w' is not given, frequencies are computed from 0 to the
        Nyquist frequency, pi radians/sample (upper-half of unit-circle). If
        `whole` is True, compute frequencies from 0 to 2*pi radians/sample.

    Returns
    -------
    w : 1D ndarray
        Frequency array [radians/sample]
    H : 1D ndarray
        Array of complex magnitude values

    See Also
    --------
    scipy.signal.dfeqresp

    Notes
    -----
    If (num, den) is passed in for ``system``, coefficients for both the
    numerator and denominator should be specified in descending exponent
    order (e.g. ``z^2 + 3z + 5`` would be represented as ``[1, 3, 5]``).
    z6dfreqresp can only be used with discrete-time systems.Nr   r-   zUnknown system typer   z?dfreqresp requires a SISO (single input, single output) system.)rg  r   )r8   rz   rJ   r.  r9   rB   r@   r=   rT   r&   r'   r   r   rh  r   r   r   r3   r6   r   )r   rf   rg   r   rg  r   r   rY  s           r    r   r     s   Z fd## 3fc"" 	;  ": ; ; ; vcrc{2vbz22&*%% !f/@AA 0.///}V^q00 + , , 	, 	}&*++ & $..vz/?/?/A/A6:NNSS#D66611	FN	+	+ &v|V[t$& & &1 a4Kr!   c                 >   t          | ||          \  }}t          | t                    r| j        }n| d         }dt	          j        t          |                    z  }t	          j        t	          j        t	          j	        |                              }||z  ||fS )aD  
    Calculate Bode magnitude and phase data of a discrete-time system.

    Parameters
    ----------
    system : an instance of the LTI class or a tuple describing the system.
        The following gives the number of elements in the tuple and
        the interpretation:

            * 1 (instance of `dlti`)
            * 2 (num, den, dt)
            * 3 (zeros, poles, gain, dt)
            * 4 (A, B, C, D, dt)

    w : array_like, optional
        Array of frequencies (in radians/sample). Magnitude and phase data is
        calculated for every value in this array. If not given a reasonable
        set will be calculated.
    n : int, optional
        Number of frequency points to compute if `w` is not given. The `n`
        frequencies are logarithmically spaced in an interval chosen to
        include the influence of the poles and zeros of the system.

    Returns
    -------
    w : 1D ndarray
        Frequency array [rad/time_unit]
    mag : 1D ndarray
        Magnitude array [dB]
    phase : 1D ndarray
        Phase array [deg]

    See Also
    --------
    scipy.signal.dbode

    Notes
    -----
    If (num, den) is passed in for ``system``, coefficients for both the
    numerator and denominator should be specified in descending exponent
    order (e.g. ``z^2 + 3z + 5`` would be represented as ``[1, 3, 5]``).
    re   r   r]  )
r   r8   rz   r-   r   r^  rN  rad2degr_  angle)r   rf   rg   rc  r-   rd  re  s          r    r   r   Y  s    V VqA&&&DAq&$ YBZ
CFF##
#CLTZ]]3344Er63r!   rm   c                 	   t          |           dk    r|                                 S t          |           dk    r[t          t          | d         | d                   |||          }t	          |d         |d         |d         |d                   |fz   S t          |           dk    rbt          t          | d         | d         | d                   |||          }t          |d         |d         |d         |d                   |fz   S t          |           dk    r| \  }}}}nt          d          |dk    r,|t          d
          |dk     s|dk    rt          d          |dk    rt          j	        |j
        d                   ||z  |z  z
  }	t          j	        |j
        d                   d|z
  |z  |z  z   }
t          j                            |	|
          }t          j                            |	||z            }t          j                            |	j        |j                  }|j        }||||z  z  z   }n|dk    s|dk    rt          | |dd          S |dk    s|dk    rt          | |dd          S |dk    rt          | |dd          S |dk    rt          j        ||f          }t          j        t          j        |j
        d         |j
        d         f          t          j        |j
        d         |j
        d         f          f          }t          j        ||f          }t#          ||z            }|d	|j
        d         d	d	f         }|d	d	d|j
        d         f         }|d	d	|j
        d         d	f         }|}|}nH|dk    r|j
        d         }|j
        d         }t%          t          j        ||g          |z  t          j	        |                    }t          j        ||d|z  z   f          }t          j        ||g          }t          j        |          }|d	|d|f         }|d	||||z   f         }|d	|||z   d	f         }|}||z
  ||z  z   }|}|||z  z   }na|dk    rIt          j        |d          st          d          t#          ||z            }||z  |z  }|}||z  |z  }nt          d|z            |||||fS )aM  
    Transform a continuous to a discrete state-space system.

    Parameters
    ----------
    system : a tuple describing the system or an instance of `lti`
        The following gives the number of elements in the tuple and
        the interpretation:

            * 1: (instance of `lti`)
            * 2: (num, den)
            * 3: (zeros, poles, gain)
            * 4: (A, B, C, D)

    dt : float
        The discretization time step.
    method : str, optional
        Which method to use:

            * gbt: generalized bilinear transformation
            * bilinear: Tustin's approximation ("gbt" with alpha=0.5)
            * euler: Euler (or forward differencing) method
              ("gbt" with alpha=0)
            * backward_diff: Backwards differencing ("gbt" with alpha=1.0)
            * zoh: zero-order hold (default)
            * foh: first-order hold (*versionadded: 1.3.0*)
            * impulse: equivalent impulse response (*versionadded: 1.3.0*)

    alpha : float within [0, 1], optional
        The generalized bilinear transformation weighting parameter, which
        should only be specified with method="gbt", and is ignored otherwise

    Returns
    -------
    sysd : tuple containing the discrete system
        Based on the input type, the output will be of the form

        * (num, den, dt)   for transfer function input
        * (zeros, poles, gain, dt)   for zeros-poles-gain input
        * (A, B, C, D, dt) for state-space system input

    Notes
    -----
    By default, the routine uses a Zero-Order Hold (zoh) method to perform
    the transformation. Alternatively, a generalized bilinear transformation
    may be used, which includes the common Tustin's bilinear approximation,
    an Euler's method technique, or a backwards differencing technique.

    See Also
    --------
    scipy.signal.cont2discrete


    r   rM   r   r   rN   rO   zKFirst argument must either be a tuple of 2 (tf), 3 (zpk), or 4 (ss) arrays.gbtNzUAlpha parameter must be specified for the generalized bilinear transform (gbt) methodzDAlpha parameter must be within the interval [0,1] for the gbt methodrI  bilineartusting      ?eulerforward_diffr-  backward_diffrm   fohr[   z;Impulse method is only applicableto strictly proper systemsz"Unknown transformation method '%s')rP   rr   r   r   r   r   r   rT   r   r  r   r   r  rZ   r   r3   r   r   r   r4  )r   r-   rp   rq   sysdr   r  r  r  imarhsadbdcdddem_upperem_loweremmsrg   r  ms11ms12ms13s                           r    r   r     sL   n 6{{a!!###
6{{aU6!9fQi88"V#(* * *T!Wd1gtAwQ88B5@@	V		VF1Ivay&)DDb$*%9 9 9d1gtAwQa99REAA	V		
1a 6 7 7 	7 = K L L LQYY%!)) 8 9 9 9 hqwqz""U2XaZ/hqwqz""cEk2%5a%77[sC(([sBqD)) [suac**TB	:		8!3!3VRSAAAA	7		f66VRSAAAA	?	"	"VRSAAAA	5;1v&& ;
AGAJ
+C D D $
AGAJ
+C D D F G G [(H-.."r']] QQQ1QWQZ< 171:;;	5GAJGAJ dk1a&11B6DD:q!a!e)n-- [(H-..[__ "1"ac'{"1"aAg+"1"a!eff*~D[4$;&T\	9		}Q"" 	; : ; ; ; !b&\\!Vb[URZ =FGGGr2r2r!   )NTrs   ru   rv   )rq  r  r  )NNr   rw   )>rx   r  r   mathr   r   cupyx.scipyr   cupyx.scipy.interpolater   cupyx.scipy.linalgr   r   "cupyx.scipy.signal._lti_conversionr   r	   *cupyx.scipy.signal._iir_filter_conversionsr
   r   r   r   r   r   r   !cupyx.scipy.signal._filter_designr   r   r   r   r   rJ   rz   r@   rQ   r|   r=   rR   r}   r9   rS   r~   r`   rU  r[   r^   rh   rl   rj  r}  rr  r  r  r  ru  rv  r  r   r   r   r   r   r   r   r!   r    <module>r(     sX                   6 6 6 6 6 6 / / / / / / / /) ) ) ) ) ) ) )= = = = = = = = = = = = = = = = = =( ( ( ( ( ( ( ( ( ( ( (S  S  S  S  S  S  S  S lt3 t3 t3 t3 t3
 t3 t3 t3ny6 y6 y6 y6 y6 y6 y6 y6xX X X X X* X X Xv:' :' :' :' :'!13 :' :' :'z,	 ,	 ,	 ,	 ,	/ ,	 ,	 ,	^l+ l+ l+ l+ l+( l+ l+ l+^C C C C C~s C C CL(	 (	 (	 (	 (	^T (	 (	 (	Vg# g# g# g# g#$ g# g# g#T1! 1! 1! 1! 1!:s 1! 1! 1!h&	 &	 &	 &	 &	T &	 &	 &	V[5 [5 [5 [5|& & &R< < < <~8 8 8 8v1 1 1 1hE E E EX# # # # # # # #
/ / /d  ("# "# "#`C
 C
 C
L9= 9= 9=xA" A" A"H" " "8] ] ] ]D	a a a aHG G G GTG G G GTN N N Nb5 5 5 5t^ ^ ^ ^ ^ ^r!   