a
    BCCf                    @   sN  d Z ddlZddlmZ ddlmZ ddl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mZmZmZ ddlZddlZdd	lmZmZmZmZmZm Z m!Z!m"Z"m#Z# ddl$Z$g d
Z%G dd dZ&G dd de&Z'G dd de&Z(G dd de&Z)G dd de)e'Z*G dd de)e(Z+G dd de&Z,G dd de,e'Z-G dd de,e(Z.G dd de&Z/G dd  d e/e'Z0G d!d" d"e/e(Z1dRd$d%Z2d&d' Z3dSd(d)Z4dTd*d+Z5dUd-d.Z6dVd0d1Z7G d2d3 d3Z8d4d5 Z9d6d7 Z:d8d9 Z;d:d; Z<d<d= Z=d>d? Z>d@dA Z?dWdEdFZ@dXdGdHZAdYdIdJZBdZdKdLZCd[dNdOZDd\dPdQZEdS )]z]
ltisys -- a collection of classes and functions for modeling linear
time invariant systems.
    N)qr)linalg)make_interp_spline   )tf2zpkzpk2tf	normalizefreqsfreqz	freqs_zpk	freqz_zpk)tf2ssabcd_normalizess2tfzpk2ssss2zpkcont2discrete_atleast_2d_or_none)	real
atleast_1dsqueezeasarrayzerosdot	transposeoneslinspace)ltidltiTransferFunctionZerosPolesGain
StateSpacelsimimpulsestepbodefreqrespplace_polesdlsimdstepdimpulse	dfreqrespdbodec                       sp   e Zd Z fddZ fddZedd Zedd Zed	d
 Zedd Z	dd Z
dd Zdd Z  ZS )LinearTimeInvariantc                    s   | 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__ P/var/www/html/django/DPS/env/lib/python3.9/site-packages/scipy/signal/_ltisys.pyr0   0   s    zLinearTimeInvariant.__new__c                    s    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selfr5   r7   r8   r;   8   s    
zLinearTimeInvariant.__init__c                 C   s   | j S )zAReturn the sampling time of the system, `None` for `lti` systems.r>   r?   r7   r7   r8   dtD   s    zLinearTimeInvariant.dtc                 C   s   | j d u ri S d| j iS d S )NrB   )rB   r?   r7   r7   r8   _dt_dictI   s    
zLinearTimeInvariant._dt_dictc                 C   s
   |   jS )zZeros of the system.)to_zpkr   r?   r7   r7   r8   r   P   s    zLinearTimeInvariant.zerosc                 C   s
   |   jS )zPoles of the system.)rD   polesr?   r7   r7   r8   rE   U   s    zLinearTimeInvariant.polesc                 C   s   t | tr| S |  S d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.
        N)
isinstancer!   to_ssr?   r7   r7   r8   _as_ssZ   s    	
zLinearTimeInvariant._as_ssc                 C   s   t | tr| S |  S d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.
        N)rF   r    rD   r?   r7   r7   r8   _as_zpkh   s    	
zLinearTimeInvariant._as_zpkc                 C   s   t | tr| S |  S d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.
        N)rF   r   to_tfr?   r7   r7   r8   _as_tfv   s    	
zLinearTimeInvariant._as_tf)__name__
__module____qualname__r0   r;   propertyrB   rC   r   rE   rH   rI   rK   __classcell__r7   r7   r5   r8   r-   /   s   



r-   c                       sh   e Zd ZdZ fddZ fddZdddZdd	d
ZdddZdddZ	dddZ
dddZ  ZS )r   a   
    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
    --------
    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.

    Examples
    --------
    >>> from scipy import signal

    >>> signal.lti(1, 2, 3, 4)
    StateSpaceContinuous(
    array([[1]]),
    array([[2]]),
    array([[3]]),
    array([[4]]),
    dt: None
    )

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

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

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

    >>> signal.lti([3, 4], [1, 2])
    TransferFunctionContinuous(
    array([3., 4.]),
    array([1., 2.]),
    dt: None
    )

    c                    sr   | t u rft|}|dkr*tjtg|R  S |dkrDt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.)r   lenTransferFunctionContinuousr0   ZerosPolesGainContinuousStateSpaceContinuous
ValueErrorr/   )r2   r3   Nr5   r7   r8   r0      s(    zlti.__new__c                    s   t  j|  dS r9   )r/   r;   r@   r3   r5   r7   r8   r;      s    zlti.__init__Nc                 C   s   t | |||dS )zm
        Return the impulse response of a continuous-time system.
        See `impulse` for details.
        X0TrZ   )r#   r@   r]   r^   rZ   r7   r7   r8   r#      s    zlti.impulsec                 C   s   t | |||dS )zg
        Return the step response of a continuous-time system.
        See `step` for details.
        r\   )r$   r_   r7   r7   r8   r$      s    zlti.stepc                 C   s   t | |||dS )zo
        Return the response of a continuous-time system to input `U`.
        See `lsim` for details.
        )r]   )r"   )r@   Ur^   r]   r7   r7   r8   output   s    z
lti.outputd   c                 C   s   t | ||dS )ai  
        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.

        Examples
        --------
        >>> from scipy import signal
        >>> import matplotlib.pyplot as plt

        >>> sys = signal.TransferFunction([1], [1, 1])
        >>> w, mag, phase = sys.bode()

        >>> plt.figure()
        >>> plt.semilogx(w, mag)    # Bode magnitude plot
        >>> plt.figure()
        >>> plt.semilogx(w, phase)  # Bode phase plot
        >>> plt.show()

        wn)r%   r@   rd   re   r7   r7   r8   r%      s    zlti.bode'  c                 C   s   t | ||d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.
        rc   )r&   rf   r7   r7   r8   r&     s    zlti.freqrespzohc                 C   s   t ddS )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.N)r.   r@   rB   methodalphar7   r7   r8   to_discrete  s    	zlti.to_discrete)NNN)NNN)N)Nrb   )Nrg   )rh   N)rL   rM   rN   __doc__r0   r;   r#   r$   ra   r%   r&   rl   rP   r7   r7   r5   r8   r      s   G





r   c                       sx   e Zd ZdZ fddZ fddZedd Zejdd Zdd
dZ	dddZ
dddZdddZdddZ  ZS )r   a
  
    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
    --------
    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]``).

    .. versionadded:: 0.18.0

    Examples
    --------
    >>> from scipy import signal

    >>> signal.dlti(1, 2, 3, 4)
    StateSpaceDiscrete(
    array([[1]]),
    array([[2]]),
    array([[3]]),
    array([[4]]),
    dt: True
    )

    >>> signal.dlti(1, 2, 3, 4, dt=0.1)
    StateSpaceDiscrete(
    array([[1]]),
    array([[2]]),
    array([[3]]),
    array([[4]]),
    dt: 0.1
    )

    Construct the transfer function
    :math:`H(z) = \frac{5(z - 1)(z - 2)}{(z - 3)(z - 4)}` with a sampling time
    of 0.1 seconds:

    >>> signal.dlti([1, 2], [3, 4], 5, dt=0.1)
    ZerosPolesGainDiscrete(
    array([1, 2]),
    array([3, 4]),
    5,
    dt: 0.1
    )

    Construct the transfer function :math:`H(z) = \frac{3z + 4}{1z + 2}` with
    a sampling time of 0.1 seconds:

    >>> signal.dlti([3, 4], [1, 2], dt=0.1)
    TransferFunctionDiscrete(
    array([3., 4.]),
    array([1., 2.]),
    dt: 0.1
    )

    c                    s   | t u rxt|}|dkr0tjtg|R i |S |dkrPtjtg|R i |S |dkrptjtg|R i |S tdt | S )rQ   rR   rS   rT   zG`system` needs to be an instance of `dlti` or have 2, 3 or 4 arguments.)r   rU   TransferFunctionDiscreter0   ZerosPolesGainDiscreteStateSpaceDiscreterY   r/   )r2   r3   r4   rZ   r5   r7   r8   r0     s,    zdlti.__new__c                    s(   | dd}t j|i | || _dS )r:   rB   TN)popr/   r;   rB   )r@   r3   r4   rB   r5   r7   r8   r;     s    zdlti.__init__c                 C   s   | j S )z'Return the sampling time of the system.rA   r?   r7   r7   r8   rB     s    zdlti.dtc                 C   s
   || _ d S NrA   )r@   rB   r7   r7   r8   rB     s    Nc                 C   s   t | |||dS )zu
        Return the impulse response of the discrete-time `dlti` system.
        See `dimpulse` for details.
        x0tre   )r*   r@   rt   ru   re   r7   r7   r8   r#     s    zdlti.impulsec                 C   s   t | |||dS )zo
        Return the step response of the discrete-time `dlti` system.
        See `dstep` for details.
        rs   )r)   rv   r7   r7   r8   r$     s    z	dlti.stepc                 C   s   t | |||dS )zp
        Return the response of the discrete-time system to input `u`.
        See `dlsim` for details.
        )rt   )r(   )r@   uru   rt   r7   r7   r8   ra     s    zdlti.outputrb   c                 C   s   t | ||dS )a  
        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.

        Examples
        --------
        >>> from scipy import signal
        >>> import matplotlib.pyplot as plt

        Construct the transfer function :math:`H(z) = \frac{1}{z^2 + 2z + 3}`
        with sampling time 0.5s:

        >>> sys = signal.TransferFunction([1], [1, 2, 3], dt=0.5)

        Equivalent: signal.dbode(sys)

        >>> w, mag, phase = sys.bode()

        >>> plt.figure()
        >>> plt.semilogx(w, mag)    # Bode magnitude plot
        >>> plt.figure()
        >>> plt.semilogx(w, phase)  # Bode phase plot
        >>> plt.show()

        rc   )r,   rf   r7   r7   r8   r%     s    z	dlti.boderg   Fc                 C   s   t | |||d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.

        )rd   re   whole)r+   )r@   rd   re   rx   r7   r7   r8   r&     s    	zdlti.freqresp)NNN)NNN)N)Nrb   )Nrg   F)rL   rM   rN   rm   r0   r;   rO   rB   setterr#   r$   ra   r%   r&   rP   r7   r7   r5   r8   r   ,  s   X





r   c                       s   e Zd ZdZ fddZ fddZdd Zedd	 Zej	d
d	 Zedd Z
e
j	dd Z
dd Zdd Zdd Zdd Zedd Zedd Z  ZS )r   aJ
  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
    --------
    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]``)

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

    >>> from scipy import signal

    >>> num = [1, 3, 3]
    >>> den = [1, 2, 1]

    >>> signal.TransferFunction(num, den)
    TransferFunctionContinuous(
    array([1., 3., 3.]),
    array([1., 2., 1.]),
    dt: None
    )

    Construct the transfer function
    :math:`H(z) = \frac{z^2 + 3z + 3}{z^2 + 2z + 1}` with a sampling time of
    0.1 seconds:

    >>> signal.TransferFunction(num, den, dt=0.1)
    TransferFunctionDiscrete(
    array([1., 3., 3.]),
    array([1., 2., 1.]),
    dt: 0.1
    )

    c                    sx   t |dkr&t|d tr&|d  S | tu rl|ddu rTt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   rB   N)
rU   rF   r-   rJ   r   getrV   r0   rn   r/   r1   r5   r7   r8   r0   7  s&    zTransferFunction.__new__c                    sD   t |d trdS t jf i | d| _d| _t| \| _| _dS )z&Initialize the state space LTI system.r   N)	rF   r-   r/   r;   _num_denr   numdenr@   r3   r4   r5   r7   r8   r;   L  s    zTransferFunction.__init__c                 C   s&   d | jjt| jt| jt| jS )z7Return representation of the system's transfer functionz{}(
{},
{},
dt: {}
))formatr6   rL   reprr}   r~   rB   r?   r7   r7   r8   __repr__Z  s    zTransferFunction.__repr__c                 C   s   | j S )z+Numerator of the `TransferFunction` system.)r{   r?   r7   r7   r8   r}   c  s    zTransferFunction.numc                 C   s<   t || _t| jjdkr,| jj\| _| _nd| _d| _d S Nr   )r   r{   rU   r}   shaper=   r<   )r@   r}   r7   r7   r8   r}   h  s
    
c                 C   s   | j S )z-Denominator of the `TransferFunction` system.)r|   r?   r7   r7   r8   r~   s  s    zTransferFunction.denc                 C   s   t || _d S rr   )r   r|   )r@   r~   r7   r7   r8   r~   x  s    c                 C   s   |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[   r7   r7   r8   _copy|  s    
zTransferFunction._copyc                 C   s
   t | S )z
        Return a copy of the current `TransferFunction` system.

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

        copydeepcopyr?   r7   r7   r8   rJ     s    
zTransferFunction.to_tfc                 C   s   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~   rC   r?   r7   r7   r8   rD     s    
zTransferFunction.to_zpkc                 C   s   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

        )r!   r   r}   r~   rC   r?   r7   r7   r8   rG     s    
zTransferFunction.to_ssc                 C   sT   t | t | }|dkr.tt||f}n|dk rLtt| | 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   rU   nphstackr   r}   r~   diffr7   r7   r8   
_z_to_zinv  s    zTransferFunction._z_to_zinvc                 C   sT   t | t | }|dkr.t|t|f}n|dk rLt| t| 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   r7   r7   r8   
_zinv_to_z  s    zTransferFunction._zinv_to_z)rL   rM   rN   rm   r0   r;   r   rO   r}   ry   r~   r   rJ   rD   rG   staticmethodr   r   rP   r7   r7   r5   r8   r     s(   K	





r   c                   @   s   e Zd ZdZdddZdS )rV   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
    --------
    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]``)

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

    >>> from scipy import signal

    >>> num = [1, 3, 3]
    >>> den = [1, 2, 1]

    >>> signal.TransferFunction(num, den)
    TransferFunctionContinuous(
    array([ 1.,  3.,  3.]),
    array([ 1.,  2.,  1.]),
    dt: None
    )

    rh   Nc                 C   s*   t t| j| jf|||ddd d|iS )z
        Returns the discretized `TransferFunction` system.

        Parameters: See `cont2discrete` for details.

        Returns
        -------
        sys: instance of `dlti` and `StateSpace`
        rj   rk   NrB   )r   r   r}   r~   ri   r7   r7   r8   rl     s    
z&TransferFunctionContinuous.to_discrete)rh   NrL   rM   rN   rm   rl   r7   r7   r7   r8   rV     s   ;rV   c                   @   s   e Zd ZdZdS )rn   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
    --------
    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]``).

    Examples
    --------
    Construct the transfer function
    :math:`H(z) = \frac{z^2 + 3z + 3}{z^2 + 2z + 1}` with a sampling time of
    0.5 seconds:

    >>> from scipy import signal

    >>> num = [1, 3, 3]
    >>> den = [1, 2, 1]

    >>> signal.TransferFunction(num, den, dt=0.5)
    TransferFunctionDiscrete(
    array([ 1.,  3.,  3.]),
    array([ 1.,  2.,  1.]),
    dt: 0.5
    )

    NrL   rM   rN   rm   r7   r7   r7   r8   rn   /  s   =rn   c                       s   e Zd ZdZ fddZ fddZdd Zedd	 Zej	d
d	 Zedd Z
e
j	dd Z
edd Zej	dd Zdd Zdd Zdd Zdd Z  ZS )r    az  
    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
    --------
    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.

    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
    )

    Construct the transfer function
    :math:`H(z) = \frac{5(z - 1)(z - 2)}{(z - 3)(z - 4)}` with a sampling time
    of 0.1 seconds:

    >>> signal.ZerosPolesGain([1, 2], [3, 4], 5, dt=0.1)
    ZerosPolesGainDiscrete(
    array([1, 2]),
    array([3, 4]),
    5,
    dt: 0.1
    )

    c                    sx   t |dkr&t|d tr&|d  S | tu rl|ddu rTt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   rB   N)
rU   rF   r-   rD   r    rz   rW   r0   ro   r/   r1   r5   r7   r8   r0     s&    zZerosPolesGain.__new__c                    sJ   t |d trdS t jf i | d| _d| _d| _|\| _| _| _	dS )z)Initialize the zeros, poles, gain system.r   N)
rF   r-   r/   r;   _zeros_poles_gainr   rE   gainr   r5   r7   r8   r;     s    zZerosPolesGain.__init__c                 C   s.   d | jjt| jt| jt| jt| jS )z5Return representation of the `ZerosPolesGain` system.z{}(
{},
{},
{},
dt: {}
))r   r6   rL   r   r   rE   r   rB   r?   r7   r7   r8   r     s    zZerosPolesGain.__repr__c                 C   s   | j S )z%Zeros of the `ZerosPolesGain` system.)r   r?   r7   r7   r8   r     s    zZerosPolesGain.zerosc                 C   s<   t || _t| jjdkr,| jj\| _| _nd| _d| _d S r   )r   r   rU   r   r   r=   r<   )r@   r   r7   r7   r8   r     s
    
c                 C   s   | j S )z%Poles of the `ZerosPolesGain` system.)r   r?   r7   r7   r8   rE     s    zZerosPolesGain.polesc                 C   s   t || _d S rr   )r   r   )r@   rE   r7   r7   r8   rE     s    c                 C   s   | j S )z$Gain of the `ZerosPolesGain` system.r   r?   r7   r7   r8   r     s    zZerosPolesGain.gainc                 C   s
   || _ d S rr   r   )r@   r   r7   r7   r8   r     s    c                 C   s   |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)rE   r   r   r[   r7   r7   r8   r     s    
zZerosPolesGain._copyc                 C   s   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   r   rE   r   rC   r?   r7   r7   r8   rJ     s    
zZerosPolesGain.to_tfc                 C   s
   t | S )z
        Return a copy of the current 'ZerosPolesGain' system.

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

        r   r?   r7   r7   r8   rD      s    
zZerosPolesGain.to_zpkc                 C   s   t t| j| j| ji | jS r   )r!   r   r   rE   r   rC   r?   r7   r7   r8   rG   ,  s    
zZerosPolesGain.to_ss)rL   rM   rN   rm   r0   r;   r   rO   r   ry   rE   r   r   rJ   rD   rG   rP   r7   r7   r5   r8   r    p  s(   D







r    c                   @   s   e Zd ZdZdddZdS )rW   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
    )

    rh   Nc                 C   s.   t t| j| j| jf|||d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   rB   )r    r   r   rE   r   ri   r7   r7   r8   rl   n  s    
z$ZerosPolesGainContinuous.to_discrete)rh   Nr   r7   r7   r7   r8   rW   :  s   3rW   c                   @   s   e Zd ZdZdS )ro   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
    --------
    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.

    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
    )

    Construct the transfer function
    :math:`H(z) = \frac{5(z - 1)(z - 2)}{(z - 3)(z - 4)}` with a sampling time
    of 0.1 seconds:

    >>> signal.ZerosPolesGain([1, 2], [3, 4], 5, dt=0.1)
    ZerosPolesGainDiscrete(
    array([1, 2]),
    array([3, 4]),
    5,
    dt: 0.1
    )

    Nr   r7   r7   r7   r8   ro     s   Bro   c                       s  e Zd ZdZdZdZ fddZ fddZdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zedd Zejdd Zedd  Zejd!d  Zed"d# Zejd$d# Zed%d& Zejd'd& Zd(d) Zd*d+ Zd,d- Zd.d/ Z  ZS )0r!   ar	  
    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
    --------
    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.

    Examples
    --------
    >>> from scipy import signal
    >>> import numpy as np
    >>> a = np.array([[0, 1], [0, 0]])
    >>> b = np.array([[0], [1]])
    >>> c = np.array([[1, 0]])
    >>> d = np.array([[0]])

    >>> sys = signal.StateSpace(a, b, c, d)
    >>> print(sys)
    StateSpaceContinuous(
    array([[0, 1],
           [0, 0]]),
    array([[0],
           [1]]),
    array([[1, 0]]),
    array([[0]]),
    dt: None
    )

    >>> sys.to_discrete(0.1)
    StateSpaceDiscrete(
    array([[1. , 0.1],
           [0. , 1. ]]),
    array([[0.005],
           [0.1  ]]),
    array([[1, 0]]),
    array([[0]]),
    dt: 0.1
    )

    >>> a = np.array([[1, 0.1], [0, 1]])
    >>> b = np.array([[0.005], [0.1]])

    >>> signal.StateSpace(a, b, c, d, dt=0.1)
    StateSpaceDiscrete(
    array([[1. , 0.1],
           [0. , 1. ]]),
    array([[0.005],
           [0.1  ]]),
    array([[1, 0]]),
    array([[0]]),
    dt: 0.1
    )

    g      Y@Nc                    sx   t |dkr&t|d tr&|d  S | tu rl|ddu rTtjtg|R i |S tjtg|R i |S t	 | S )z4Create new StateSpace object and settle inheritance.r   r   rB   N)
rU   rF   r-   rG   r!   rz   rX   r0   rp   r/   r1   r5   r7   r8   r0      s    zStateSpace.__new__c                    sX   t |d trdS t jf i | d| _d| _d| _d| _t| \| _	| _
| _| _dS )z+Initialize the state space lti/dlti system.r   N)rF   r-   r/   r;   _A_B_C_Dr   ABCDr   r5   r7   r8   r;   2  s    zStateSpace.__init__c              	   C   s6   d | jjt| jt| jt| jt| jt| jS )z1Return representation of the `StateSpace` system.z{}(
{},
{},
{},
{},
dt: {}
))	r   r6   rL   r   r   r   r   r   rB   r?   r7   r7   r8   r   B  s    zStateSpace.__repr__c                 C   s   t |ttjtttjtfS rr   )rF   r!   r   ndarrayfloatcomplexnumberintr@   otherr7   r7   r8   _check_binop_otherM  s    zStateSpace._check_binop_otherc           	   	   C   s\  |  |stS t|trt|t| ur,tS | j|jkr@td| jjd }|jjd }t	
t	| jt	| j|jft	t||f|jff}t	
t	| j|j|jf}t	| jt	| j|jf}t	| j|j}n(| j}t	| j|}| j}t	| j|}t	|j|j|j|j}tt	j||dt	j||dt	j||dt	j||d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   NotImplementedrF   r!   typerB   	TypeErrorr   r   r   vstackr   r   r   r   r   r   result_typer   r   rC   )	r@   r   Zn1Zn2abcdcommon_dtyper7   r7   r8   __mul__Q  s6    

zStateSpace.__mul__c                 C   s   |  |rt|trtS | j}| j}t|| j}t|| j	}t
|j|j|j|j}ttj||dtj||dtj||dtj||dfi | jS )z4Pre-multiply a scalar or matrix (but not StateSpace)r   )r   rF   r!   r   r   r   r   r   r   r   r   r   r   rC   r@   r   r   r   r   r   r   r7   r7   r8   __rmul__  s    zStateSpace.__rmul__c                 C   s$   t | j| j| j | j fi | jS )z8Negate the system (equivalent to pre-multiplying by -1).)r!   r   r   r   r   rC   r?   r7   r7   r8   __neg__  s    zStateSpace.__neg__c                 C   sD  |  |stS t|trt|t| urDtdt|  dt| | j|jkrXtdt| j	|j	}t
| j|jf}t
| j|jf}| j|j }nRt
|}| jj|jkr| j	}| j}| j}| j| }ntd| jj d|j dt
|j|j|j|j}tt
j||dt
j||dt
j||dt
j||dfi | jS )zM
        Adds two systems in the sense of frequency domain addition.
        zCannot add z and z'Cannot add systems with different `dt`.z1Cannot add systems with incompatible dimensions ()r   )r   r   rF   r!   r   r   rB   r   Z
block_diagr   r   r   r   r   r   r   
atleast_2dr   rY   r   r   r   rC   r   r7   r7   r8   __add__  s<    



zStateSpace.__add__c                 C   s   |  |stS | | S rr   r   r   r   r   r7   r7   r8   __sub__  s    
zStateSpace.__sub__c                 C   s   |  |stS | |S rr   r   r   r7   r7   r8   __radd__  s    
zStateSpace.__radd__c                 C   s   |  |stS |  |S rr   r   r   r7   r7   r8   __rsub__  s    
zStateSpace.__rsub__c                 C   sD   |  |rt|trtS t|tjr6|jdkr6td| d| S )z$
        Divide by a scalar
        r   z3Cannot divide StateSpace by non-scalar numpy arraysr   )	r   rF   r!   r   r   r   ndimrY   r   r   r7   r7   r8   __truediv__  s
    zStateSpace.__truediv__c                 C   s   | j S )z(State matrix of the `StateSpace` system.)r   r?   r7   r7   r8   r     s    zStateSpace.Ac                 C   s   t || _d S rr   )r   r   )r@   r   r7   r7   r8   r     s    c                 C   s   | j S )z(Input matrix of the `StateSpace` system.)r   r?   r7   r7   r8   r     s    zStateSpace.Bc                 C   s   t || _| jjd | _d S )Nr   )r   r   r   r   r<   )r@   r   r7   r7   r8   r     s    
c                 C   s   | j S )z)Output matrix of the `StateSpace` system.)r   r?   r7   r7   r8   r     s    zStateSpace.Cc                 C   s   t || _| jjd | _d S )Nr   )r   r   r   r   r=   )r@   r   r7   r7   r8   r     s    
c                 C   s   | j S )z.Feedthrough matrix of the `StateSpace` system.)r   r?   r7   r7   r8   r     s    zStateSpace.Dc                 C   s   t || _d S rr   )r   r   )r@   r   r7   r7   r8   r     s    c                 C   s$   |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[   r7   r7   r8   r     s    
zStateSpace._copyc                 K   s*   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   rC   r@   r4   r7   r7   r8   rJ   )  s
    zStateSpace.to_tfc                 K   s*   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   rC   r   r7   r7   r8   rD   ;  s
    zStateSpace.to_zpkc                 C   s
   t | S )z
        Return a copy of the current `StateSpace` system.

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

        r   r?   r7   r7   r8   rG   M  s    
zStateSpace.to_ss)rL   rM   rN   rm   Z__array_priority__Z__array_ufunc__r0   r;   r   r   r   r   r   r   r   r   r   r   rO   r   ry   r   r   r   r   rJ   rD   rG   rP   r7   r7   r5   r8   r!     sF   V<1







r!   c                   @   s   e Zd ZdZdddZdS )rX   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
    --------
    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.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy import signal

    >>> a = np.array([[0, 1], [0, 0]])
    >>> b = np.array([[0], [1]])
    >>> c = np.array([[1, 0]])
    >>> d = np.array([[0]])

    >>> sys = signal.StateSpace(a, b, c, d)
    >>> print(sys)
    StateSpaceContinuous(
    array([[0, 1],
           [0, 0]]),
    array([[0],
           [1]]),
    array([[1, 0]]),
    array([[0]]),
    dt: None
    )

    rh   Nc                 C   s2   t t| j| j| j| jf|||d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   rB   )r!   r   r   r   r   r   ri   r7   r7   r8   rl     s    
z StateSpaceContinuous.to_discrete)rh   Nr   r7   r7   r7   r8   rX   Z  s   8rX   c                   @   s   e Zd ZdZdS )rp   a  
    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
    --------
    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.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy import signal

    >>> a = np.array([[1, 0.1], [0, 1]])
    >>> b = np.array([[0.005], [0.1]])
    >>> c = np.array([[1, 0]])
    >>> d = np.array([[0]])

    >>> signal.StateSpace(a, b, c, d, dt=0.1)
    StateSpaceDiscrete(
    array([[ 1. ,  0.1],
           [ 0. ,  1. ]]),
    array([[ 0.005],
           [ 0.1  ]]),
    array([[1, 0]]),
    array([[0]]),
    dt: 0.1
    )

    Nr   r7   r7   r7   r8   rp     s   :rp   Tc              
   C   s  t | tr|  }n t | tr(tdnt|   }t|}t|jdkrRtdt	t
j|j|j|j|jf\}}}}	|jd }
|jd }|j}|du rt|
|jj}t
||
f|jj}|d dkr||d< n6|d dkrt|tt||d  |d< ntd|du p4t |ttfr*|dkp4t
| }|dkrtt||j }|sf|t||	j 7 }||t|fS |d |d  }t
t
||std|rt|j| }td|D ]}||d  | ||< qt||j }||t|fS t|}|j dkr|ddt
j!f }|jd |kr6td	|jd |krNtd
|st
"t
#|| || gt
||
| fg}t|j}|d|
d|
f }||
dd|
f }td|D ]*}||d  | ||d  |  ||< qnt
"t
#|| || t
|
|fgt
#t
||
| ft
$|gt
||
d|  fg}t|j}|d|
d|
f }||
| dd|
f }||
|
| d|
f | }td|D ]6}||d  | ||d  |  || |  ||< qt||j t||	j  }||t|fS )al  
    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]``).

    Examples
    --------
    We'll use `lsim` to simulate an analog Bessel filter applied to
    a signal.

    >>> import numpy as np
    >>> from scipy.signal import bessel, lsim
    >>> import matplotlib.pyplot as plt

    Create a low-pass Bessel filter with a cutoff of 12 Hz.

    >>> b, a = bessel(N=5, Wn=2*np.pi*12, btype='lowpass', analog=True)

    Generate data to which the filter is applied.

    >>> t = np.linspace(0, 1.25, 500, endpoint=False)

    The input signal is the sum of three sinusoidal curves, with
    frequencies 4 Hz, 40 Hz, and 80 Hz.  The filter should mostly
    eliminate the 40 Hz and 80 Hz components, leaving just the 4 Hz signal.

    >>> u = (np.cos(2*np.pi*4*t) + 0.6*np.sin(2*np.pi*40*t) +
    ...      0.5*np.cos(2*np.pi*80*t))

    Simulate the filter with `lsim`.

    >>> tout, yout, xout = lsim((b, a), U=u, T=t)

    Plot the result.

    >>> plt.plot(t, u, 'r', alpha=0.5, linewidth=1, label='input')
    >>> plt.plot(tout, yout, 'k', linewidth=1.5, label='output')
    >>> plt.legend(loc='best', shadow=True, framealpha=1)
    >>> plt.grid(alpha=0.3)
    >>> plt.xlabel('t')
    >>> plt.show()

    In a second example, we simulate a double integrator ``y'' = u``, with
    a constant input ``u = 1``.  We'll use the state space representation
    of the integrator.

    >>> from scipy.signal import lti
    >>> A = np.array([[0.0, 1.0], [0.0, 0.0]])
    >>> B = np.array([[0.0], [1.0]])
    >>> C = np.array([[1.0, 0.0]])
    >>> D = 0.0
    >>> system = lti(A, B, C, D)

    `t` and `u` define the time and input signal for the system to
    be simulated.

    >>> t = np.linspace(0, 5, num=50)
    >>> u = np.ones_like(t)

    Compute the simulation, and then plot `y`.  As expected, the plot shows
    the curve ``y = 0.5*t**2``.

    >>> tout, y, x = lsim(system, u, t)
    >>> plt.plot(t, y)
    >>> plt.grid(alpha=0.3)
    >>> plt.xlabel('t')
    >>> plt.show()

    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.rR   )%rF   r   rH   r   AttributeErrorr   rU   r   rY   mapr   r   r   r   r   r   sizer   r   emptyr   r   Zexpmr   r   r   anyr   r^   allcloser   ranger   newaxisr   r   identity)r3   r`   r^   r]   interpsysr   r   r   r   Zn_statesZn_inputsZn_stepsxoutno_inputyoutrB   ZexpAT_dtiMZexpMTZAdZBdZBd1ZBd0r7   r7   r8   r"     s    l



$


"


	*4r"   c                 C   sB   t | }ttt|}|dkr&d}d| }tdd| |}|S )a  Compute a reasonable set of time samples for the response time.

    This function is used by `impulse` and `step`  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         ?   )r   Zeigvalsminabsr   r   )r   re   valsrZtcru   r7   r7   r8   _default_response_times  s    
r   c                 C   s   t | tr|  }n t | tr(tdnt|   }|du rHt|j}nt|j| }|du rbd}|du rxt|j|}nt	|}t
|d||dd\}}}||fS )a  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]``).

    Examples
    --------
    Compute the impulse response of a second order system with a repeated
    root: ``x''(t) + 2*x'(t) + x(t) = u(t)``

    >>> from scipy import signal
    >>> system = ([1.0], [1.0, 2.0, 1.0])
    >>> t, y = signal.impulse(system)
    >>> import matplotlib.pyplot as plt
    >>> plt.plot(t, y)

    z6impulse can only be used with continuous-time systems.Nrb   r   F)r   )rF   r   rH   r   r   r   r   r   r   r   r"   )r3   r]   r^   rZ   r   X_hr7   r7   r8   r#     s    0



r#   c                 C   s   t | tr|  }n t | tr(tdnt|   }|du r@d}|du rVt|j|}nt|}t|j	|jj
}t||||d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]``).

    Examples
    --------
    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt
    >>> lti = signal.lti([1.0], [1.0, 1.0])
    >>> t, y = signal.step(lti)
    >>> plt.plot(t, y)
    >>> plt.xlabel('Time [s]')
    >>> plt.ylabel('Amplitude')
    >>> plt.title('Step response for 1. Order Lowpass')
    >>> plt.grid()

    z3step can only be used with continuous-time systems.Nrb   F)r]   r   r   r   )rF   r   rH   r   r   r   r   r   r   r   r   r"   )r3   r]   r^   rZ   r   r`   r   r7   r7   r8   r$     s    1



r$   rb   c                 C   sN   t | ||d\}}dtt| }tt|j|jd tj }|||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]

    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]``).

    .. versionadded:: 0.11.0

    Examples
    --------
    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt

    >>> sys = signal.TransferFunction([1], [1, 1])
    >>> w, mag, phase = signal.bode(sys)

    >>> plt.figure()
    >>> plt.semilogx(w, mag)    # Bode magnitude plot
    >>> plt.figure()
    >>> plt.semilogx(w, phase)  # Bode phase plot
    >>> plt.show()

    rc         4@g     f@)	r&   numpylog10r   unwrapZarctan2imagr   pi)r3   rd   re   ymagphaser7   r7   r8   r%   a  s    8 r%   rg   c                 C   s   t | tr(t | ttfr| }qH|  }n t | tr<tdnt|   }|jdks\|jdkrdt	d|durr|}n|}t |trt
|j |j|d\}}n$t |trt|j|j|j|d\}}||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]``).

    Examples
    --------
    Generating the Nyquist plot of a transfer function

    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt

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

    >>> s1 = signal.ZerosPolesGain([], [1, 1, 1], [5])

    >>> w, H = signal.freqresp(s1)

    >>> plt.figure()
    >>> plt.plot(H.real, H.imag, "b")
    >>> plt.plot(H.real, -H.imag, "r")
    >>> plt.show()
    z7freqresp can only be used with continuous-time systems.r   z@freqresp() requires a SISO (single input, single output) system.N)worN)rF   r   r   r    rI   r   r   r<   r=   rY   r	   r}   ravelr~   r   r   rE   r   )r3   rd   re   r   r   r   r7   r7   r8   r&     s"    6





r&   c                   @   s   e Zd Zdd ZdS )Bunchc                 K   s   | j | d S rr   )__dict__update)r@   kwdsr7   r7   r8   r;     s    zBunch.__init__N)rL   rM   rN   r;   r7   r7   r7   r8   r     s   r   c           	      C   sP  t |}|jdkrtdt|}| jdkr6td|jdkrHtd| jd | jd krdtdt|| jd krtd| jd t|f t|| jd k rtd	t|| jd f t j|}|D ]}t	||k|krtd
qt
}|dvrtd|dkr$t}tt |s$td|dk r6td|dkrHt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.rR   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'r   z'Complex poles are not supported by KNV0z#maxiter must be at least equal to 1zrtol can not be greater than 1)r   r   r   rY   _order_complex_polesr   rU   r   matrix_ranksum_YT_loop
_KNV0_loopallisreal)	r   r   rE   rj   rtolmaxiterr   pupdate_loopr7   r7   r8   _valid_inputs  sD    







r   c                 C   s   t | t |  }g }t | t | dk  D ]&}t || v r0||t |f q0t ||f}| jd t|krt	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   sortr   r   conjextendr   r   rU   rY   )rE   Zordered_polesZim_polesr   r7   r7   r8   r   0	  s    r   c                 C   s~   t j||dd}t|dd\}}t || || j}t ||dddf }	t |	dsz|	t j|	 }
|
|dd|f< 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   ZaxisfullmodeNr   r   )r   deletes_qrr   r^   r   r   norm)r   ker_poletransfer_matrixjrE   Ztransfer_matrix_not_jQRZ
mat_ker_pjZyjZxjr7   r7   r8   _KNV0D	  s    

r  c              
   C   s  |dddt jf }|dddt jf }t t | | jt ||jt ||j | | }t j|\}}	}
|jddddt jf \}}|
ddddt jf \}}t |dd|t jf |dd|t jf f}t |	d |	d st | | |}t | | |}t ||f}npt t | | t 	| | j
ft t 	| | j
| | ff}t t ||ft ||ff}t ||}t t ||j|}t |ds*t d| t j| }|d|dd|f j
d df |dd|f< ||dd|f j
d ddf |dd|f< n\|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   rR   r   r   )r   r   r   r^   r   Zsvdr   r   r   r   r   sqrtr  )r  r
  r  r   r	  rw   vmZumsmvmmu1mu2Znu1Znu2&transfer_matrix_j_mo_transfer_matrix_jZker_pole_imo_mu1Zker_pole_i_nu1Zker_pole_mu_nuker_pole_ijZmu_nu_matrixZtransfer_matrix_ijr7   r7   r8   _YT_reals	  sb    

	r  c                 C   s&  t d|dddt jf  }t d|dddt jf  }|d|  }| | }t t t |jt |t |jt t ||j |}	t j|	\}
}t t 	|
}|dd|d t jf }|dd|d t jf }|dd|t jf d|dd|t jf   }t 
t 	|
|d  t 	|
|d  sDt ||}nt ||f}t ||}t t |t |j|}t 
|ds|t j| }t |dddf |dd|f< t |dddf |dd|f< nDt |dddf |dd|f< t |dddf |dd|f< dS )zP
    Applies algorithm from YT section 6.2 page 20 related to complex pairs
    rR   Nr  r                 ?r   )r   r  r   r   r   r^   r   eigZargsortr   r   r   r  r   r   )r  r
  r  r   r	  urZuirw   r  r  Ze_valZe_vecZ	e_val_idxr  r  r  Zker_pole_muZmu1_mu2_matrixZtransfer_matrix_i_jr7   r7   r8   _YT_complex	  sD    $	
"$"r  c                 C   s  |t | jd }|d }|dkr2|gdgg}ng g g}t |d t|d d}	t d||d  }
|d d|
  |d d|
 d  |d |	 |d |	d  t d|d }
|d d|
 d  |d d|
  |dkrt |d r|d d |d d |d |	 |d |	d  t d||d  }|D ]<}td|d D ]&}|d | |d ||  qhqV|dkrt |d r|d d |d d |d |	 |d |	d  t d||d  }|D ]Z}t|d |d D ]@}|| }||kr:|| | }|d | |d | qq|dkrt |d r|d d |d d |d |	 |d |	d  td|d D ]&}|d | |d ||  q|dkr t |d r |d d |d d |d |	 |d |	d  t |j	d }d}d}||k r|st 
t j|}|D ]\}}||kr|dksJ dt || sJ dt|| ||| nt j|||fdd}t|dd	\}}t || r2t || s J d
t| t| |||| n2t ||  sTJ d
t| t| |||| q~t t t dt 
t j|f}t 
|| | }||k r|t t dkrd}|d7 }qX|||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   rR   r   Fzi!=0 for KNV call in YTzcalling KNV on a complex poler   r  r  z"mixing real and complex in YT_realT)r   r   r   ZarangerU   r   appendr   arrayr^   r   r   detr  r  r  strr  r  maxr  spacing)r  r  rE   r   r   r   Znb_realZhnbZupdate_orderZr_compZr_pZr_jr	  r   Zidx_1stopnb_trydet_transfer_matrixbZtransfer_matrix_not_i_jr
  r   det_transfer_matrixcur_rtolr7   r7   r8   r   	  s    

 r   c                 C   s   d}d}||k r|st t j|}t|jd D ]}	t|| ||	| q4t t t 	dt t j|f}
t |
| |
 }||k r|
t t 	dkrd}|d7 }q|||fS )zI
    Loop over all poles one by one and apply KNV method 0 algorithm
    Fr   r   T)
r   r   r   r  r   r   r  r   r  r!  )r  r  rE   r   r   r   r"  r#  r$  r	  r%  r&  r7   r7   r8   r   b
  s     

r   r   MbP?   c           "   
   C   s<  t | |||||\}}d}d}t|dd\}	}
tj|}|	ddd|f }|	dd|df }|
d|ddf }
|jd |krbt| j}d}||jd k r(|| }t||||f< t| rt	| |||d f< t|||d |d f< t	|||d |f< |d7 }|d7 }qtjj
|||  ddd }t| jd }tj}tj}ng }d}t|jd D ]}|rd}qxt|j| || t|jd   j}t|dd\}}|dd|jd df }tj|dd	ddtjf }|tj| }t||  rHtt|t	|g}|||g d
}n
|| |dkrb|}nt||f}qx|dkr|||||||\}}}|s|dkrd| d| d}tj|dd |t}d}||jd d k rjt||  r^|dd|f  }|dd|d f }|d|  |dd|f< |d|  |dd|d f< |d7 }|d7 }qzBtj|jtt||jj}tj|
t|j||  }W n4 tjjy }  ztd| W Y d} ~ n
d} ~ 0 0 | }t|}t }!||!_ t!tj"| t|| d |!_#||!_$||!_%||!_&||!_'|!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

    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.

    Examples
    --------
    A simple example demonstrating real pole placement using both KNV and YT
    algorithms.  This is example number 1 from section 4 of the reference KNV
    publication ([1]_):

    >>> import numpy as np
    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt

    >>> A = np.array([[ 1.380,  -0.2077,  6.715, -5.676  ],
    ...               [-0.5814, -4.290,   0,      0.6750 ],
    ...               [ 1.067,   4.273,  -6.654,  5.893  ],
    ...               [ 0.0480,  4.273,   1.343, -2.104  ]])
    >>> B = np.array([[ 0,      5.679 ],
    ...               [ 1.136,  1.136 ],
    ...               [ 0,      0,    ],
    ...               [-3.146,  0     ]])
    >>> P = np.array([-0.2, -0.5, -5.0566, -8.6659])

    Now compute K with KNV method 0, with the default YT method and with the YT
    method while forcing 100 iterations of the algorithm and print some results
    after each call.

    >>> fsf1 = signal.place_poles(A, B, P, method='KNV0')
    >>> fsf1.gain_matrix
    array([[ 0.20071427, -0.96665799,  0.24066128, -0.10279785],
           [ 0.50587268,  0.57779091,  0.51795763, -0.41991442]])

    >>> fsf2 = signal.place_poles(A, B, P)  # uses YT method
    >>> fsf2.computed_poles
    array([-8.6659, -5.0566, -0.5   , -0.2   ])

    >>> fsf3 = signal.place_poles(A, B, P, rtol=-1, maxiter=100)
    >>> fsf3.X
    array([[ 0.52072442+0.j, -0.08409372+0.j, -0.56847937+0.j,  0.74823657+0.j],
           [-0.04977751+0.j, -0.80872954+0.j,  0.13566234+0.j, -0.29322906+0.j],
           [-0.82266932+0.j, -0.19168026+0.j, -0.56348322+0.j, -0.43815060+0.j],
           [ 0.22267347+0.j,  0.54967577+0.j, -0.58387806+0.j, -0.40271926+0.j]])

    The absolute value of the determinant of X is a good indicator to check the
    robustness of the results, both ``'KNV0'`` and ``'YT'`` aim at maximizing
    it.  Below a comparison of the robustness of the results above:

    >>> abs(np.linalg.det(fsf1.X)) < abs(np.linalg.det(fsf2.X))
    True
    >>> abs(np.linalg.det(fsf2.X)) < abs(np.linalg.det(fsf3.X))
    True

    Now a simple example for complex poles:

    >>> A = np.array([[ 0,  7/3.,  0,   0   ],
    ...               [ 0,   0,    0,  7/9. ],
    ...               [ 0,   0,    0,   0   ],
    ...               [ 0,   0,    0,   0   ]])
    >>> B = np.array([[ 0,  0 ],
    ...               [ 0,  0 ],
    ...               [ 1,  0 ],
    ...               [ 0,  1 ]])
    >>> P = np.array([-3, -1, -2-1j, -2+1j]) / 3.
    >>> fsf = signal.place_poles(A, B, P, method='YT')

    We can plot the desired and computed poles in the complex plane:

    >>> t = np.linspace(0, 2*np.pi, 401)
    >>> plt.plot(np.cos(t), np.sin(t), 'k--')  # unit circle
    >>> plt.plot(fsf.requested_poles.real, fsf.requested_poles.imag,
    ...          'wo', label='Desired')
    >>> plt.plot(fsf.computed_poles.real, fsf.computed_poles.imag, 'bx',
    ...          label='Placed')
    >>> plt.grid()
    >>> plt.axis('image')
    >>> plt.axis([-1.1, 1.1, -1.1, 1.1])
    >>> plt.legend(bbox_to_anchor=(1.05, 1), loc=2, numpoints=1)

    r   r  r  Nr   r   )ZrcondFr   TzSConvergence was not reached after maxiter iterations.
You asked for a tolerance of z	, we got .rR   )
stacklevelr  zfThe poles you've chosen can't be placed. Check the controllability matrix and try another set of poles)(r   r  r   r   r   r   r   r   r   r   Zlstsqeyenanr   r   r^   r   r   r  r   r   r  warningswarnZastyper   r   ZsolveZdiagZLinAlgErrorrY   r   gain_matrixr   r  Zcomputed_polesZrequested_polesr   r   nb_iter)"r   r   rE   rj   r   r   r   r&  r0  rw   zZrankBZu0u1Z
diag_polesidxr   r/  r  r  Zskip_conjugater	  Zpole_space_jr
  r   Z
ker_pole_jZtransfer_matrix_jr"  err_msgrelZimgr  eZfull_state_feedbackr7   r7   r8   r'   |
  s     6

(






 
r'   c              	   C   sn  t | trtdn$t | ts8t| dd d| d i} t | t}|  } t|}|jdkrjt	|j
}|du rt|}|d | j }n |d }tt|| j d }t|| jjd f}t|| jjd f}tjd||d}	|du rt| jjd f|dddf< nt||dddf< |du r8|}
n4t|jdkrZ|ddtjf }t||dd	|	}
td|d D ]}t| j||ddf t| j|
|ddf  ||d ddf< t| j||ddf t| j|
|ddf  ||ddf< qzt| j||d ddf t| j|
|d ddf  ||d ddf< |rb|	||fS |	|fS dS )
a  
    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
    --------
    lsim, dstep, dimpulse, cont2discrete

    Examples
    --------
    A simple integrator transfer function with a discrete time step of 1.0
    could be implemented as:

    >>> import numpy as np
    >>> from scipy import signal
    >>> tf = ([1.0,], [1.0, -1.0], 1.0)
    >>> t_in = [0.0, 1.0, 2.0, 3.0]
    >>> u = np.asarray([0.0, 0.0, 1.0, 1.0])
    >>> t_out, y = signal.dlsim(tf, u, t=t_in)
    >>> y.T
    array([[ 0.,  0.,  0.,  1.]])

    z7dlsim can only be used with discrete-time dlti systems.Nr   rB   r   r   r   )r}   )k)rF   r   r   r   r!   rH   r   r   r   r   r^   rU   rB   r   floorr   r   r   r   r   r   r   r   r   r   r   r   )r3   rw   ru   rt   Zis_ss_inputZout_samplesZstoptimer   r   toutZu_dtr   r7   r7   r8   r(     sL    9






"

r(   c           	      C   s   t | tr|  } n2t | tr(tdnt| dd d| d i } |du rRd}|du rttjd|| j |dd}n
t|}d}t	d| j
D ]`}t|jd | j
f}d	|d|f< t| |||d
}|du r|d f}n||d f }|d }q||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
    --------
    impulse, dstep, dlsim, cont2discrete

    Examples
    --------
    >>> import numpy as np
    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt

    >>> butter = signal.dlti(*signal.butter(3, 0.5))
    >>> t, y = signal.dimpulse(butter, n=25)
    >>> plt.step(t, np.squeeze(y))
    >>> plt.grid()
    >>> plt.xlabel('n [samples]')
    >>> plt.ylabel('Amplitude')

    z:dimpulse can only be used with discrete-time dlti systems.Nr   rB   rb   r   FZendpointr   ru   rt   r   )rF   r   rH   r   r   r   r   rB   r   r   r<   r   r   r(   	r3   rt   ru   re   r   r   rw   Z
one_outputr9  r7   r7   r8   r*   O  s(    2





r*   c           	      C   s
  t | tr|  } n2t | tr(tdnt| dd d| d i } |du rRd}|du rttjd|| j |dd}n
t|}d}t	d| j
D ]r}t|jd | j
f}t|jd f|dd|f< t| |||d	}|du r|d
 f}n||d
 f }|d }q||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
    --------
    step, dimpulse, dlsim, cont2discrete

    Examples
    --------
    >>> import numpy as np
    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt

    >>> butter = signal.dlti(*signal.butter(3, 0.5))
    >>> t, y = signal.dstep(butter, n=25)
    >>> plt.step(t, np.squeeze(y))
    >>> plt.grid()
    >>> plt.xlabel('n [samples]')
    >>> plt.ylabel('Amplitude')
    z7dstep can only be used with discrete-time dlti systems.Nr   rB   rb   r   Fr:  r;  r   )rF   r   rH   r   r   r   r   rB   r   r   r<   r   r   r   r(   r<  r7   r7   r8   r)     s(    1





r)   Fc                 C   s   t | ts6t | trtdt| dd d| d i} t | trH|  } t | ttfs^td| j	dksr| j
dkrztd|dur|}n|}t | trt| j | j\}}t||||d\}}n&t | trt| j| j| j||d\}}||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

    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]``).

    .. versionadded:: 0.18.0

    Examples
    --------
    Generating the Nyquist plot of a transfer function

    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt

    Construct the transfer function
    :math:`H(z) = \frac{1}{z^2 + 2z + 3}` with a sampling time of 0.05
    seconds:

    >>> sys = signal.TransferFunction([1], [1, 2, 3], dt=0.05)

    >>> w, H = signal.dfreqresp(sys)

    >>> plt.figure()
    >>> plt.plot(H.real, H.imag, "b")
    >>> plt.plot(H.real, -H.imag, "r")
    >>> plt.show()

    z6dfreqresp can only be used with discrete-time systems.Nr   rB   zUnknown system typer   z?dfreqresp requires a SISO (single input, single output) system.)r   rx   )rF   r   r   r   r!   rK   r   r    rY   r<   r=   r   r}   r   r~   r
   r   r   rE   r   )r3   rd   re   rx   r   r}   r~   r   r7   r7   r8   r+     s*    @





r+   c                 C   sb   t | ||d\}}t| tr$| j}n| d }dtt| }ttt	|}|| ||fS )a7  
    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]

    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]``).

    .. versionadded:: 0.18.0

    Examples
    --------
    >>> from scipy import signal
    >>> import matplotlib.pyplot as plt

    Construct the transfer function :math:`H(z) = \frac{1}{z^2 + 2z + 3}` with
    a sampling time of 0.05 seconds:

    >>> sys = signal.TransferFunction([1], [1, 2, 3], dt=0.05)

    Equivalent: sys.bode()

    >>> w, mag, phase = signal.dbode(sys)

    >>> plt.figure()
    >>> plt.semilogx(w, mag)    # Bode magnitude plot
    >>> plt.figure()
    >>> plt.semilogx(w, phase)  # Bode phase plot
    >>> plt.show()

    rc   r   r   )
r+   rF   r   rB   r   r   r   Zrad2degr   Zangle)r3   rd   re   r   rB   r   r   r7   r7   r8   r,   `  s    >
r,   )NT)NNN)NNN)Nrb   )Nrg   )r   r'  r(  )NN)NNN)NNN)Nrg   F)Nrb   )Frm   r-  Zscipy.linalgr   r  Zscipyr   Zscipy.interpolater   Z_filter_designr   r   r   r	   r
   r   r   Z_lti_conversionr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __all__r-   r   r   r   rV   rn   r    rW   ro   r!   rX   rp   r"   r   r#   r$   r%   r&   r   r   r   r  r  r  r   r   r'   r(   r*   r)   r+   r,   r7   r7   r7   r8   <module>   sj   $$,V ( @ xMA KFF   J>
 X
F
C
@
W3/A3{
  a
t
W
V
d