I have a system of boolean equations (example given below)
p = x & ~y
q = x & y
Now, I want to obtain the expression of x
in terms of other variables in the system (LHS variables if possible) or to find that it can not be obtained. Is this possible in sympy (or z3?)?
import sympy as s
p,q = s.symbols('p,q')
x,y = s.symbols('x,y')
v = s.And(s.Eq(p,x & y), s.Eq(q,x & ~y))
I have looked at solve()
and solveset()
but they seem to be dealing with linear equations.
>>> s.solveset(v, x)
raise ValueError("%s is not a valid SymPy expression" % f)
ValueError: Eq(p, x & y) & Eq(q, x & ~y) is not a valid SymPy expression
Is there any other alternative?
I really do not understand the documentation. According to it, the following should have worked, because it clearly says that in sympy.solvers.solvers.solve(f, *symbols, **flags)
, f can be a relational expression or a boolean.
>>> s.And(s.Equivalent(p, x &y), s.Equivalent(q, x&~y)).simplify()
(x | ~p) & (x | ~q) & (y | ~p) & (~p | ~q) & (~q | ~y) & (p | q | ~x) & (q | y | ~x) & (p | ~x | ~y)
>>> s.solve(s.And(s.Equivalent(p, x &y), s.Equivalent(q, x&~y)),x)
TypeError: unsupported operand type(s) for -: 'And' and 'int'
>>> s.solveset(s.And(s.Equivalent(p, x &y), s.Equivalent(q, x&~y)),x)
ValueError: (Equivalent(p, x & y)) & (Equivalent(q, x & ~y)) is not a valid SymPy expression