o
    ٷiB                     @   s  d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZmZmZ d dlmZ d d	lmZ d d
lmZmZmZmZmZmZ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( d dl)m*Z*m+Z+ edddgie,fdZ-edZ.e%ded de  d eZ/e%eded  de  d eZ0e%eed d eZ1e%ded eZ2e%ded de  d eZ3e%ddeZ4e%de d ded   de  d ded  ded   d eZ5e%ded de e  d eZ6e&e2e%ded eZ7e&e1e%eed eZ8e'e/e0Z9dd Z:d d! Z;d"d# Z<d$d% Z=d&d' Z>d(d) Z?d*d+ Z@d,d- ZAd.d/ ZBd0d1 ZCd2d3 ZDd4d5 ZEd6S )7    isclose)I	all_close)Dummy)Absarg)log)sqrt)spa)pi)import_module)pole_zero_numerical_datapole_zero_plotstep_response_numerical_datastep_response_plotimpulse_response_numerical_dataimpulse_response_plotramp_response_numerical_dataramp_response_plotbode_magnitude_numerical_databode_phase_numerical_data	bode_plotnyquist_plot_exprnichols_plot_expr)TransferFunctionSeriesParallelTransferFunctionMatrix)raisesskip
matplotlibfromlistpyplot)import_kwargscatchnumpy            ?      
         	   皙?      c                 C   s   t | t |fS N)tupler   b r9   b/home/ubuntu/.local/lib/python3.10/site-packages/sympy/physics/control/tests/test_control_plots.py	_to_tuple+   s   r;   c                 C   s   t | |\} }t| dd | t| d t| d d   | dd   t|dd |t|d t|d d   |dd   fS )Nr   r*   r)   )r;   r6   lenr7   r9   r9   r:   _trim_tuple.   s   ::r>   c                    sJ   | \}}t ||\}}t fdd|D }tdd t||D S )zChecks whether the y-coordinate value of the plotted
    data point is equal to the value of the function at a
    particular x.c                 3   s    | ]} |V  qd S r5   r9   ).0x_i
evalf_funcsystemr9   r:   	<genexpr>9   s    z(y_coordinate_equality.<locals>.<genexpr>c                 s   s$    | ]\}}t || d k V  qdS )g:0yE>N)r   )r?   y_exp_iy_ir9   r9   r:   rD   :   s   " )r>   r6   allzip)plot_data_funcrB   rC   xyy_expr9   rA   r:   y_coordinate_equality3   s   rM   c                      s  t std tttgttggdtd d   ttfdd tt fdd tt fdd ttfdd ttfd	d tt fd
d tt fdd ttfdd ttfdd tt	td ttt
fdd tt
fdd tt
fdd tt
fdd tt
fdd tt
fdd tt
fdd tt
fdd tt
fdd tt
dd  tt
dd  tt
dd  tt
dd  tt
dd  tt
dd  d S )Nz"Matplotlib not the default backendr)   r*   c                         t  S r5   r   r9   tfmr9   r:   <lambda>D       ztest_errors.<locals>.<lambda>c                      rN   r5   r   r9   exprr9   r:   rR   E   rS   c                      rN   r5   r   r9   rU   r9   r:   rR   F   rS   c                      rN   r5   r   r9   rP   r9   r:   rR   G   rS   c                      rN   r5   r   r9   rP   r9   r:   rR   H   rS   c                      rN   r5   r   r9   rU   r9   r:   rR   I   rS   c                      rN   r5   r   r9   rU   r9   r:   rR   J   rS   c                      rN   r5   r   r9   rP   r9   r:   rR   K   rS   c                      rN   r5   r   r9   rP   r9   r:   rR   L   rS   c                      rN   r5   rO   r9   tf_ar9   r:   rR   P   rS   c                      rN   r5   rT   r9   r^   r9   r:   rR   Q   rS   c                      rN   r5   rW   r9   r^   r9   r:   rR   R   rS   c                      rN   r5   rX   r9   r^   r9   r:   rR   S   rS   c                      rN   r5   rY   r9   r^   r9   r:   rR   T   rS   c                      rN   r5   rZ   r9   r^   r9   r:   rR   U   rS   c                      rN   r5   r[   r9   r^   r9   r:   rR   V   rS   c                      rN   r5   r\   r9   r^   r9   r:   rR   W   rS   c                      rN   r5   r]   r9   r^   r9   r:   rR   X   rS   c                   S      t tddS )Nlower_limit)r   tf1r9   r9   r9   r:   rR   [       c                   S   r`   )N皙rb   )r   rd   r9   r9   r9   r:   rR   \   re   c                   S   r`   )NgUUUUUUrb   r   rd   r9   r9   r9   r:   rR   ]   re   c                   S   r`   )Nrf   )sloperg   r9   r9   r9   r:   rR   `   re   c                   S   r`   )Nhz)	freq_unitr   rd   r9   r9   r9   r:   rR   c   re   c                   S   r`   )Ndegree)
phase_unitrk   r9   r9   r9   r:   rR   d   re   )r#   r"   r    tf6tf5r   r!   NotImplementedErrorr   r   
ValueErrorr9   r9   )rV   r_   rQ   r:   test_errors=   s:   rr   c                  C   s   dd } g ddgg}dgddgg}dgg dg}g g d	g}g d
g dg}g ddgg}| t |s3J | t|s:J | t|sAJ | t|sHJ | t|sOJ | t|sVJ d S )Nc                 S   s0   t | \}}t||d }t||d }|o|S )Nr   r)   )r   r   )sysexpected_value_z_pz_checkp_checkr9   r9   r:   	pz_testeri   s   z!test_pole_zero.<locals>.pz_testeryϿhdEyϿhdE?        y      п4Կy      п4?)y?        y     ࿲LXzy     ࿲LXz?)rz   rz   rz   g      @)gv|?g     g8hֿ)yϿ4ԿyϿ4?yϿhdEyϿhdE?yZEf;]yJu3;]@)rd   tf2tf3ser1par1tf8)ry   exp1exp2exp3exp4exp5exp6r9   r9   r:   test_pole_zerog   s"   r   c                     sj   t std dd dd   fdd} | tsJ | ts!J | ts'J | ts-J | ts3J d S )NNumPy is required for this testc                 S   s>   |   }tddd}|| jt| i}t|||i S )NwTreal)to_exprr   subsvarr   r   evalfrC   pointrV   _ww_exprr9   r9   r:   bode_phase_evalf   s   z#test_bode.<locals>.bode_phase_evalfc                 S   sH   |   }tddd}|| jt| i}dtt|d||i  S )Nr   Tr      r.   )r   r   r   r   r   r	   r   r   r   r9   r9   r:   bode_mag_evalf   s    z!test_bode.<locals>.bode_mag_evalfc                    s   t t | ot t| S r5   )rM   r   r   )rs   r   r   r9   r:   test_bode_data   s   
z!test_bode.<locals>.test_bode_data)r(   r"   rd   r{   r|   tf4ro   )r   r9   r   r:   	test_bode   s   r   c                 C   s   t dd t| |D S )Nc                 s   s     | ]}t |d ddV  qdS )r2   gư>)rel_tolabs_tolNr   )r?   _r9   r9   r:   rD      s    z'check_point_accuracy.<locals>.<genexpr>)rG   rH   r7   r9   r9   r:   check_point_accuracy   s   r   c                  C   s   t std dd } d}d}d}d}d}d	}d
}| t|sJ | t|s&J | t|s-J | t|s4J | t|s;J | t|sBJ | t|sIJ d S )Nr   c                 S   :   t t| ddd \}}t||d }t||d }|o|S NFr.   )adaptivenr   r)   )r;   r   r   rs   rt   rJ   rK   x_checky_checkr9   r9   r:   impulse_res_tester      z1test_impulse_response.<locals>.impulse_res_tester)
rz   grq?grq@g
@grq@g88@g@gqq@grq!@      $@)
rz   gϡ(h?g j?gyӿgTHfg'?g*Vrv?g>ﹿg)c gkz?)r   )
gvWUU?gV!y?g7?gfꦎgJIPgWMJĤg"Gga6ߞgiރgP=))r   )
g   2>g?gxL#V
@gĮU"@g^<@gTU@g|/u^p@gjD#;݈@gi@gNU(@)r   )
rz   gf@gf8@gsqK@gfX@gOEJc@gsqk@gr@gfx@g     @@)r   )
rz   gٵ:/ĺgy%L?g2ƜgGLK&ы?gybŔugΕ_?g-~HFgof.?g&;)r   )
g   `n9g&l?gqɔ
@g(w\=(@gl%)k9@g+<Z=@glއ~>gLqggf}L)r   )
rz   g6qD2@g7մ@gO/ߡ 5AgsYAgzC5Bgl̹˵BgC/6CgJ@Cg"-|6D)	r(   r"   rd   r{   r|   r   ro   tf7r}   )r   r   r   r   r   r   r   exp7r9   r9   r:   test_impulse_response   s"   r   c                  C   s   t std dd } d}d}d}d}d}d	}| t|sJ | t|s$J | t|s+J | t|s2J | t|s9J | t|s@J d S )
Nr   c                 S   r   r   )r;   r   r   r   r9   r9   r:   step_res_tester   r   z+test_step_response.<locals>.step_res_tester)r   )
g   jϛTgڴW?g?gg	?gM?g#J?g"@te?g ?g/P?gc?)r   )
rz   gRul?gql?g."aG?g?gb?gVߴ?g-b[S?g7 ގ?g\7' m)r   )
rz   gNw4?gh&7|@g."@gs<@gHvU@grnT^p@gh%݈@gs@g{ct'@)r   )
rz   gۍ^6J@gۍ^6J2@gb<N@gۍ^6Jb@gVJyq@gb<~@go@gۍ^6J@gg
@)r   )
g   .1gъQ?gUZ?gup?grBz?g5k?g뎸?g(%
- ?gj?g9U ?)r   )
g   ΃g
V?g}{?g.cRz	@g į"^"@gy;+<@gNU@g@ lp@g&O-@gN2q@)r(   r"   rd   r{   r|   r   ro   ser2)r   r   r   r   r   r   r   r9   r9   r:   test_step_response   s   r   c                  C   s   t std ddd} d}d}d}d}d	}d
}| td|sJ | td|ds(J | td|ds1J | td|ds:J | td|dsCJ | td|sKJ d S )Nr   r)   c                 S   s<   t t| |d|d \}}t||d }t||d }|o|S )NF)rh   r   r   r   r)   )r;   r   r   )rs   
num_pointsrt   rh   rJ   rK   r   r   r9   r9   r:   ramp_res_tester  s   z+test_ramp_response.<locals>.ramp_res_tester))rz   g       @g      @g      @g       @r   )rz   g+^p?g C?gmX]@gBOBa@gNʡf@)r   )
g   (Z>gr?g8g((?go!(?gي2?gz$Y?gxMw?g5^)?gv]?gǓ?)r   )
g   WegD/l3?g@g7CЃ(@g!%8D@glZ_@gR×vx@gJ̳ @gs;4O@gk<0݁@)r   )
rz   g?{?g?{>@gQEJc@g?{~@g(|R@gQEJ@g`ޱ@g?{@g     j@)r   )
rz   ggGS@gU-XH"@gEBT1,@gޝn3@gzj8@gS\=@g}A@g5jهǌC@g>6F@)r   r   r,   r.   g333333?g      ?r-   r1   )r)   )r(   r"   rd   r{   r|   r   ro   rn   )r   r   r   r   r   r   r   r9   r9   r:   test_ramp_response  s   
r   c                  C   s`  t t\} }}t t\}}}t t\}}}t t\}	}
}| d|d  d|d  d|d  d   ks4J |d| d|d  d|d  d   ksJJ |d|d  d|d  dd|d   d   ksdJ ||dd|d    d|d  dd|d   d   ksJ ||d  |d d  ksJ || |d d  ksJ |	d	ksJ |
d
|d  ksJ d S )Nr*   g      ?g      r-   r1   r)   r,   r0   r   r.   )r   rd   r{   r|   r   )r1i1w1r2i2w2r3i3w3r4i4w4r9   r9   r:   test_nyquist_plot_expr1  s   0,4<r   c               	   C   s  t t\} }}t t\}}}t t\}}}t t\}	}
}| dtdt|d d|d   d   td ks8J |dtd|d  d| t  d   t	 ksQJ |dtt
|td	|d  d
|d   d   td ksqJ |dt|t d|d  d
| t  d   t	 ksJ |dtt
|t|d d   td ksJ |dt| t |d
 t d   t	 ksJ |	dtd|d t
|   td ksJ |
dtt|d
   t	 ksJ d S )Nr   r)   r0   g      @r*   r.      r+   $   r-   ir,   )r   rd   r{   r|   r   r	   r
   r   r   r   r   )m1p1r   m2p2r   m3p3r   m4p4r   r9   r9   r:   test_nichols_expr@  s   82@80.,$r   N)Fmathr   sympy.core.numbersr   r   sympy.core.symbolr   $sympy.functions.elementary.complexesr   r   &sympy.functions.elementary.exponentialr	   (sympy.functions.elementary.miscellaneousr
   	sympy.abcr   r   r   sympyr   sympy.externalr   #sympy.physics.control.control_plotsr   r   r   r   r   r   r   r   r   r   r   r   r   sympy.physics.control.ltir   r   r   r    sympy.testing.pytestr!   r"   RuntimeErrorr#   r(   rd   r{   r|   r   ro   rn   r   r   r}   r   r~   r;   r>   rM   rr   r   r   r   r   r   r   r   r   r9   r9   r9   r:   <module>   sP    <
 D 

*30*