I am trying to perform some symbolic calculation on matrices (with symbols as an entries of matrices), and after that I will have a number of possible solution. My goal is to select solutions/ solution based upon constraints.
for example, M
is a matrix which have one element as a symbol
.
This matrix will have 2 eigenvalues one is positive an one is negative. Using z3 I am trying to find out only negative value, but I am unable to do so as a is defined as a symbol and I cannot write it as a constraint unless I convert it to a real value.
what should I do? is there any way to convert a (symbol) in to real or integer so that I can use it as a constraint s.add(a>0)
from sympy import*
from z3 import*
from math import*
a=Symbol('a')
M=Matrix([[a,2],[3,4]]) m=M.eigenvals();
s=Solver()
s.add(m<0)
print(s.check())
model = s.model() print(model)
An alternative to
eval
andexec
is to walk the sympy expression and construct the corresponding z3 expression. Here's some code:And here's an example using the code:
Running this produces the output that solves the constraints
y >= 0
and-x^2 + y + 1 == 0
:SAT at x=2, y=3
One possibility is to convert sympy expressions to stings, modify them to represent z3 expressions, then call python's eval to evaluate them to z3 expressions. More precisely:
Below is a function that I wrote to convert a list of strings from sympy expressions to a system of inequalities in z3.
I do not particularly like this approach because it involves string manipulations as well as dynamic calls to eval() and exec(), which makes intensive computation considerably slower if you are dynamically generating your system, but this is what I could come up with.
More con converting strings to z3 expressions: