Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simplify a logic expresion in sympy?

I'm not sure if I'm doing something wrong, I have the following script:

X = sympy.Symbol('X', real=True)
Y = sympy.Symbol('Y', real=True)    
ex = ((X >= 1.4) & (X <= 1.6) & (Y <= 0.5) & (Y > 0.2) & (Y >= 0)).simplify()
ex = sympy.logic.boolalg.simplify_logic(ex, force=True, deep=True)
print(ex)

The result is:

(X >= 1.4) & (X <= 1.6) & (Y <= 0.5) & (Y > 0.2) & (Y >= 0)

But I would expect that the 3 comparisons with the variable Y to be reduced to:

(Y <= 0.5) & (Y > 0.2)

Any advise on how to do this?

like image 698
Germán Castro Avatar asked Dec 14 '25 03:12

Germán Castro


1 Answers

This might be a bug in sympy.

If you use two separate expressions, one with X and one with Y, they simplify properly:

>>> ex1 = (X >= 1.4) & (X <= 1.6)
>>> ex2 = (Y <= 0.5) & (Y > 0.2) & (Y >= 0)
>>> ex1
(X >= 1.4) & (X <= 1.6)
>>> ex2
(Y <= 0.5) & (Y > 0.2) & (Y >= 0)
>>> 
>>> ex = ex1 & ex2
>>> ex
(X >= 1.4) & (X <= 1.6) & (Y <= 0.5) & (Y > 0.2) & (Y >= 0)
>>> ex = ex.simplify()
>>> ex
(X >= 1.4) & (X <= 1.6) & (Y <= 0.5) & (Y > 0.2) & (Y >= 0)
>>> 
>>> ex1 = ex1.simplify()
>>> ex1
(X >= 1.4) & (X <= 1.6)
>>> ex2 = ex2.simplify()
>>> ex2
(Y <= 0.5) & (Y > 0.2)
>>> ex = ex1 & ex2
>>> ex
(X >= 1.4) & (X <= 1.6) & (Y <= 0.5) & (Y > 0.2)

I'd suggest talking to the sympy devs (not sure if that would be a github issue or forum post somewhere) to ask about this.

like image 198
J Earls Avatar answered Dec 15 '25 15:12

J Earls