a
    {dQ                     @   sx  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlZd dlmZm	Z	m
Z
 d dlmZmZ d dlmZmZmZmZmZmZmZ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! d d	l"m#Z#m$Z$ ed
\Z%Z&Z'G dd deZ(e j)ej*e( dddZ+dd Z,dd Z-dd Z.dd Z/dd Z0dd Z1dd Z2dd Z3d d! Z4d"d# Z5d$d% Z6d&d' Z7d(d) Z8dS )*    N)Enum)Callable)AddImpliessqrt)MulPow)SpisymbolsFunctionRationalIntegerSymbolEqNeLeLtGtGe)	Piecewiseexpsincossmtlib_code)raisesFailedzx,y,zc                   @   s6   e Zd ZedejZedejZedejZdS )_Wz2Could not infer type of `.+`. Defaulting to float.z.Non-Symbol/Function `.+` will not be declared.zPNon-Boolean expression `.+` will not be asserted. Converting to SMTLib verbatim.N)	__name__
__module____qualname__recompileIDEFAULTING_TO_FLOATZWILL_NOT_DECLAREWILL_NOT_ASSERT r'   r'   i/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/sympy/printing/tests/test_smtlib.pyr      s   r   )expectedc              	   c   s   g }|j }|V  g }tt|| D ]v\}\}}|sN|d| d| dg7 }q$|sn|d| d|j dg7 }q$|j|s$|d| d| d|j dg7 }q$|rtd|d S )	N[z] Received unexpected warning `z`.z$] Did not receive expected warning `z] Warning `z` does not match expected .
)	append	enumerate	itertoolszip_longestnamevaluematchr   join)r)   Zwarnslog_warnerrorsiwer'   r'   r(   _check_warns   s    "r:   c                  C   s   t tjgd @} ttd| ddks*J ttd| ddksBJ W d    n1 sV0    Y  t g 8} ttddks~J ttddksJ W d    n1 s0    Y  d S )N   C   r5   Z67z-1)r:   r   r&   r   r   r8   r'   r'   r(   test_Integer,   s    6
r@   c                  C   s  t tjgd x} ttdd| ddks,J ttdd| ddksFJ ttdd	| dd
ks`J ttdd	| ddkszJ W d    n1 s0    Y  t tjtjgd N} tttdd d| ddksJ ttddt | ddksJ W d    n1 s0    Y  d S )N         r=   z(/ 3 7)   	   2iz(/ -3 7)r;   FZauto_declarer5   z(+ (/ 3 7) x)z$(declare-const x Real)
(* (/ 3 7) x))r:   r   r&   r   r   r%   xr?   r'   r'   r(   test_Rational5   s    8 rJ   c                  C   s   t tjgd } ttttd| ddks.J ttttd| ddksJJ ttttd| ddksfJ tt	ttd| ddksJ tt
ttd| ddksJ ttttd| dd	ksJ W d    n1 s0    Y  d S )
N   FrH   z(assert (= x y))z(assert (not (= x y)))z(assert (<= x y))z(assert (< x y))z(assert (> x y))z(assert (>= x y)))r:   r   r%   r   r   rI   yr   r   r   r   r   r?   r'   r'   r(   test_RelationalB   s    rM   c                  C   s4  t tjtjg2} ttttt d| ddks4J W d    n1 sH0    Y  t tjg@} ttttt	t
tit	ditjdi| ddksJ W d    n1 s0    Y  td}t tjg8} t|t|ttgtf i| d	d
ksJ W d    n1 s0    Y  t g } t|t|ttgtf i| d	dks@J tt|ttt
|tt	tgtf i| d	dkstJ tt|ttt
|tt	tgtf i|dtdi| ddksJ W d    n1 s0    Y  t tjgd <} tt|ttt
|dtdi| ddksJ W d    n1 s&0    Y  d S )NFrH   z(pow (sin x) (cos x))ZINTEGER_TYPEZABSOLUTE_VALUE_OF)symbol_tableZknown_typesknown_functionsr5   z4(declare-const x INTEGER_TYPE)
(ABSOLUTE_VALUE_OF x)f1rN   r5   z:(declare-const x Bool)
(declare-fun f1 (Bool) Real)
(f1 x)zC(declare-const x Bool)
(declare-fun f1 (Bool) Bool)
(assert (f1 x))z|(declare-const x Int)
(declare-const y Bool)
(declare-const z Bool)
(declare-fun f1 (Int Bool) Bool)
(assert (= (f1 x z) y))ZMY_KNOWN_FUNz==)rN   rO   r5   zf(declare-const x Int)
(declare-const y Bool)
(declare-const z Bool)
(assert (== (MY_KNOWN_FUN x z) y))rB   )rO   r5   zg(declare-const x Real)
(declare-const y Real)
(declare-const z Real)
(assert (== (MY_KNOWN_FUN x z) y)))r:   r   r%   r&   r   r   rI   r   absintrL   boolsympyZAbsr   r   floatr   z)r8   Zmy_fun1r'   r'   r(   test_FunctionL   sh    @
&	(




*

rX   c                  C   s  t tjtjg*} ttd d| ddks,J W d    n1 s@0    Y  t tjtjtjg.} tttd  d| ddks~J W d    n1 s0    Y  t tjtjg} tttdd d| ddksJ tdd	d
}tdd	d}td}dd }d||d |||    |d |  }W d    n1 s40    Y  t g V} tt	|dk |t	||k||d	@ t	|dtdd g| ddksJ W d    n1 s0    Y  t tjtjgF} tt
d|tt
||ddddddd| ddksJ W d    n1 s0    Y  d S )NrB   FrH   z	(pow x 3)z(pow x (pow y 3))r;   z(pow x (/ 2 3))aTintegerbrealcc                 S   s   d|  S )Nr;   r'   )rI   r'   r'   r(   g       ztest_Pow.<locals>.g   g      @r=   z(declare-const a Int)
(declare-const b Real)
(declare-const c Bool)
(assert (= (< a 2) c))
(assert (= (> b a) c))
(assert c)
(assert (= (* (pow (* 7. a) (+ (pow b a) (* -1 a))) (pow (+ b (pow a 2)) -1)) (/ 7 3)))evaluater>   zG(declare-const b Real)
(declare-const c Real)
(* -2 c (pow (* b b) -1)))r:   r   r%   r&   r   rI   rL   r   r   r   r   r   )r8   rY   r\   r_   r`   exprr'   r'   r(   test_Pow   s:    8< H
*"rg   c                  C   s   t tjtjtjg*} ttt d| ddks0J W d    n1 sD0    Y  t tjtjtjg*} ttt d| ddks~J W d    n1 s0    Y  t tjtjg(} tt d| ddksJ W d    n1 s0    Y  d S )NFrH   z(* x y)z(+ x y)z(* -1 x))r:   r   r%   r&   r   rI   rL   r?   r'   r'   r(   test_basic_ops   s    88rh   c                     s  ddl m  ddlmmm G  fddd td} ttj	gN}t
tddft| t| t| ttgtf i|d	d
ksJ W d    n1 s0    Y  ttj	gd d}t
tddftddfttttt| t| t| ttgtf i|d	dksJ W d    n1 s*0    Y  tddd}tddd}td}tg T}t
|ddf|ddft||k t||k |B |ddksJ W d    n1 s0    Y  d S )Nr   )Boolean)IntervalTuplesympifyc                       sB   e Zd ZfddZedd Zedd Z fddZd	S )
z*test_quantifier_extensions.<locals>.ForAllc                    s0    fdd| j D } d d|| jgS )Nc              	      s6   g | ].\}}} |jjj|   ||gqS r'   )_s_exprr1   Z_known_typesrN   ).0symstartend)rj   printerr'   r(   
<listcomp>   s
   zFtest_quantifier_extensions.<locals>.ForAll._smtlib.<locals>.<listcomp>forall )limitsrm   function)selfrr   Zbound_symbol_declarations)rj   )rr   r(   _smtlib   s    
z2test_quantifier_extensions.<locals>.ForAll._smtlibc                 S   s   dd | j D S )Nc                 S   s   h | ]\}}}|qS r'   r'   )rn   s_r'   r'   r(   	<setcomp>   ra   zKtest_quantifier_extensions.<locals>.ForAll.bound_symbols.<locals>.<setcomp>)rv   rx   r'   r'   r(   bound_symbols   s    z8test_quantifier_extensions.<locals>.ForAll.bound_symbolsc                    s&   dd | j D   fdd| jjD S )Nc                 S   s   h | ]
}|j qS r'   r1   rn   rz   r'   r'   r(   r|      ra   zJtest_quantifier_extensions.<locals>.ForAll.free_symbols.<locals>.<setcomp>c                    s   h | ]}|j  vr|qS r'   r   r   Zbound_symbol_namesr'   r(   r|      s   
)r~   rw   free_symbolsr}   r'   r   r(   r      s    
z7test_quantifier_extensions.<locals>.ForAll.free_symbolsc                    s   fdd|D } fdd|D }t |t | t |ksDJ t |dksTJ |d }t|rjg||j |jR  S  | }t||g |_||_||_|S )Nc                    s(   g | ] }t |tst | r|qS r'   )
isinstancetuplern   rY   )rk   rl   r'   r(   rs      ra   zFtest_quantifier_extensions.<locals>.ForAll.__new__.<locals>.<listcomp>c                    s   g | ]}t | r|qS r'   )r   r   )ri   rl   r'   r(   rs      ra   rb   r   )lenr   __new__rv   rw   r   _args)clsargsrv   rw   inst)ri   ForAllrk   rl   r'   r(   r      s"    
z2test_quantifier_extensions.<locals>.ForAll.__new__N)r   r    r!   ry   propertyr~   r   r   r'   ri   r   rj   rk   rl   r'   r(   r      s   

r   fi   rQ   z,(assert (forall ( (x Real [-42, 21])) true))r;   irB   zs(declare-fun f (Real) Real)
(assert (forall ( (x Real [-42, 21]) (y Real [-100, 3])) (=> (= x y) (= (f x) (f y)))))rY   TrZ   r\   r]   r_   d   r=   zx(declare-const c Bool)
(assert (forall ( (a Int [2, 100]) (b Real [2, 100])) (or c (=> (< a b) (< (pow a (/ 1 2)) b))))))Zsympy.logic.boolalgri   rU   rj   rk   rl   r   r:   r   r%   r   rI   r   r   rV   rL   r   r   r   )r   r8   rY   r\   r_   r'   r   r(   test_quantifier_extensions   sL    +&*

r   c                  C   s  t tjg.} tdt tdi| ddks,J W d    n1 s@0    Y  t tjg<} tttddddt gtdi| ddksJ W d    n1 s0    Y  t tjgh} tttjtj	tj
tjtjtjtjddtjd	tjd
tjditdtdid| ddksJ W d    n1 s0    Y  t tjg\} tttjtj	tj
tjtjtjtjddtjd	itdtdid| ddksJ W d    n1 s0    Y  t tjgP} tttjtj	tj
tjtjtjtjddtdid| ddksJ W d    n1 s0    Y  t tjgX} tttjtj	tj
tjtjtjtjddtjditdid| ddksfJ W d    n1 s|0    Y  d S )Nrb   ZMY_PI)known_constantsr5   z(pow MY_PI -1)gQ	@Frd   z&(assert (= MY_PI 3.14))
(pow MY_PI -1)pr`   r9   plusr   rB   )r   rO   	precisionr5   z!(plus 0 1 -1 (/ 1 2) (exp 1) p g)z$(plus 0 1 -1 (/ 1 2) (exp 1) p 1.62))rO   r   r5   z$(plus 0 1 -1 (/ 1 2) 2.72 3.14 1.62)z!(plus 0 1 -1 (/ 1 2) e 3.14 1.62))r:   r   r&   r   r
   r   r   r	   ZZeroZOneZNegativeOneZHalfZExp1PiZGoldenRatior   r?   r'   r'   r(   test_mix_number_mult_symbols!  s    &&
***r   c                  C   s  t g r} ttt@ | ddks"J tttB | ddks:J tt | ddksPJ ttt@ t@ | ddkslJ W d    n1 s0    Y  t tjg2} ttt @ tdkB | ddksJ W d    n1 s0    Y  td}td	}td
}t tjg\} tt|ttt	t|tg|t
tgtf |t
tgtf i| ddksJJ W d    n1 s`0    Y  t g \} tt|ttt	t|tg|t
tgtf |t
tgtf i| ddksJ W d    n1 s0    Y  t g } tt|ttt||ttt|||ttg|t
tgtf |t
tgtf |t
tgtf i| ddks\J W d    n1 sr0    Y  d S )Nr=   z@(declare-const x Bool)
(declare-const y Bool)
(assert (and x y))z?(declare-const x Bool)
(declare-const y Bool)
(assert (or x y))z'(declare-const x Bool)
(assert (not x))zY(declare-const x Bool)
(declare-const y Bool)
(declare-const z Bool)
(assert (and x y z))rB   zj(declare-const x Bool)
(declare-const y Bool)
(declare-const z Real)
(assert (or (> z 3) (and x (not y))))r   r`   hrQ   z(declare-const x Bool)
(declare-const y Real)
(declare-const z Bool)
(declare-fun f (Bool) Int)
(declare-fun g (Bool) Int)
(assert (> (f x) y))
(assert (< y (g z)))z(declare-const x Bool)
(declare-const y Int)
(declare-const z Bool)
(declare-fun f (Bool) Int)
(declare-fun g (Bool) Int)
(assert (= (f x) y))
(assert (< y (g z)))z(declare-const x Real)
(declare-const y Int)
(declare-const z Bool)
(declare-fun f (Real) Int)
(declare-fun g (Int) Bool)
(declare-fun h (Bool) Real)
(assert (= (f x) y))
(assert (= (g (f x)) z))
(assert (= (h (g (f x))) x)))r:   r   rI   rL   rW   r   r%   r   r   r   r   rT   rS   r   rV   )r8   r   r`   r   r'   r'   r(   test_booleang  s\    
:@*
*

r   c                      sN  t tjtjg<ttttdk ftd dfdddks>J W d    n1 sR0    Y  t tjtjg\tttd tdk ftd tdk ftd tdk ftd	 dfddd
ksJ W d    n1 s0    Y  tttdk ftd tdkftttdkf t tjtjg$tt	 fdd W d    n1 s@0    Y  d S )Nrb   r;   TFrH   z(ite (< x 1) x (pow x 2))rB   rA      zQ(ite (< x 1) (pow x 2) (ite (< x 2) (pow x 3) (ite (< x 3) (pow x 4) (pow x 5))))r   c                      s   t  dS Nr=   r   r'   rf   r8   r'   r(   <lambda>  ra   z'test_smtlib_piecewise.<locals>.<lambda>)
r:   r   r%   r&   r   r   rI   r   r   AssertionErrorr'   r'   r   r(   test_smtlib_piecewise  s2    
&
&,r   c                  C   sJ  t ttdk ftd df} ttjtjg(}td|  |ddksDJ W d    n1 sX0    Y  ttjtjg(}t| t |ddksJ W d    n1 s0    Y  ttjtjtjg,}t| tt  |ddksJ W d    n1 s0    Y  ttjtjg*}t| d |dd	ks&J W d    n1 s<0    Y  d S )
Nrb   r;   Tr=   z6(declare-const x Real)
(* 2 (ite (< x 1) x (pow x 2)))z?(declare-const x Real)
(* (pow x -1) (ite (< x 1) x (pow x 2)))za(declare-const x Real)
(declare-const y Real)
(* (pow x -1) (pow y -1) (ite (< x 1) x (pow x 2)))rB   z<(declare-const x Real)
(* (/ 1 3) (ite (< x 1) x (pow x 2))))r   rI   r:   r   r%   r&   r   rL   )pwr8   r'   r'   r(   !test_smtlib_piecewise_times_const  s    66:r   c                  C   s   t g } tdd| ddks J td| ddks4J ttj| ddksJJ ttj| ddks`J td| ddkstJ tdd| ddksJ W d    n1 s0    Y  d S )	NTF)Zauto_assertr5   truer=   z(assert true)z(assert false)false)r:   r   r	   r   r   r?   r'   r'   r(   test_smtlib_boolean  s    
r   c                      s   t d ttjtjg$tt fdd W d    n1 sB0    Y  ttjg"ttfdd W d    n1 s0    Y  d S )Nr   c                      s&   t  tt ttgtf idS )NrQ   )r   rI   diffr   rV   r'   r   r8   r'   r(   r     ra   z$test_not_supported.<locals>.<lambda>c                      s   t tj dS r   )r   r	   ZComplexInfinityr'   r?   r'   r(   r     ra   )r   r:   r   r%   r&   r   KeyErrorr'   r'   r   r(   test_not_supported  s
    2r   )9
contextlibr/   r"   typingenumr   r   rU   r   r   r   Z
sympy.corer   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   Zsympy.functionsr   r   r   r   Zsympy.printing.smtlibr   Zsympy.testing.pytestr   r   rI   rL   rW   r   contextmanagerIterabler:   r@   rJ   rM   rX   rg   rh   r   r   r   r   r   r   r   r'   r'   r'   r(   <module>   s:   <	
@.XFX#
