1

I am trying to evaluate resultant matrix by asserting constraint on matrix multiplication using z3py API. The following method works for matrix addition. Following is the code, which have 3 matrices: x, y, and sol. sol is the addition of x and y (sol==x+y), I am interested to restrict the values of resultant matrix (sol) to zero, and check which unknown values in "x" and "y" yield resultant matrix to be zero. Following is the list comprehension approach for addition.

from z3 import*

x = [ [Real("x_%s_%s" %(i+1, j+1))  for j in range(2) ] for i in range(2)]
y = [ [Real("y_%s_%s" %(i+1, j+1))  for j in range(2) ] for i in range(2)]
sol = [ [Real("sol_%s_%s" %(i+1, j+1))  for j in range(2) ] for i in range(2)]
addition = [sol[i][j]==x[i][j]+y[i][j]   for i in range(2) for j in range(2) ]


s = Solver()

s.add(addition)
s.add(x[0][0]>=0)
s.add(x[0][1]>=0)
s.add(x[1][0]>=0)
s.add(x[1][1]>=1)

s.add(And(y[0][0]>=0, y[0][0]<=2))
s.add(And(y[0][1]>=0, y[0][0]<=2))
s.add(And(y[1][0]>=0, y[0][0]<=2))
s.add(And(y[1][1]>=0, y[0][0]<=2))

s.add(sol[0][0]==0)
s.add(sol[0][1]==0)
s.add(sol[1][0]==0)
s.add(sol[1][1]==0)


if s.check()==sat:
    m =s.model()
    print  SAT,'\n', m
    result=[[ m.evaluate(sol[i][j]) for i in range (2)] for j in range (2)]
    print result
if (s.check()==unsat):
    print "UNSAT, no model exists"

Now, is there any way in list comprehension through which I can assert matrix multiplication? (sol==x*y)...?

Rauf
  • 27
  • 6

1 Answers1

0

No, Z3 does not have a built-in function for matrix multiplication. It can still be done in the straightforward way, but the constraints may become quite big.

Christoph Wintersteiger
  • 8,234
  • 1
  • 16
  • 30
  • Thanks, I was able to figure out the solution for multiplication using list comprehension, but even if I take 6*6 matrices, "Solver() " takes quite a long time for adding simple constraints (like a>=0). What can be the reason? – Rauf Mar 21 '15 at 04:00
  • Is it actually the adding that takes much time, or do you end up with a hard problem after everything is asserted? If it's the adding, the problem is likely to be on the Python side; also: what's "quite a long time" for you? – Christoph Wintersteiger Mar 21 '15 at 13:36
  • 30 minutes on average, I guess the issue is on python side. Because list comprehension takes 95% of the time. – Rauf Mar 21 '15 at 20:36