1

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
Rahul Gopinath
  • 808
  • 10
  • 24
  • I'm not sure about sympy. But you cannot do these sorts of simplifications in z3 in general. It will *simplify* expressions as per it's internal invariants, but it will not be in any form you'd consider simplified. SMT solvers aren't usually good for this sort of problem. – alias Aug 17 '20 at 18:42
  • @alias, found that I could translate the [boolean expression to integer domain](https://www.researchgate.net/publication/220314229_Solution_of_systems_of_Boolean_equations_via_the_integer_domain), and solve it using sympy. – Rahul Gopinath Sep 09 '20 at 10:09
  • In the SMT context, that would be a terrible thing to do: SAT/SMT solvers work a lot better with pure booleans compared to integers, in general. But perhaps that's the best way to go with sympy. – alias Sep 10 '20 at 19:30

0 Answers0