I try this code on tests: 6 passed and 3 failed (test_sym_expr_eq_3, test_sym_expr_eq_6 and test_sym_expr_eq_7). I don't see how to manage theses cases. Have you any idea?
def syms(e):
if isinstance(e,sympy.Symbol):
return [e]
rv=[]
if isinstance(e,sympy.Number):
return [e]
for i in e.args:
rv.extend(syms(i))
return rv
def reps(a, b, symbols): # return mapping if coherent else None
if len(a) == len(b):
rv = {}
for i,j in zip(a,b):
if i in symbols or j in symbols:
continue
if isinstance(i,sympy.Number) and isinstance(j,sympy.Number): # numbers must be equal
if i != j: return
continue
if rv.get(i,j)!=j: # symbols must be always the same
return
rv[i]=j
return rv
def sym_expr_eq(a, b, symbols = []):
a = sympy.sympify(a)
b = sympy.sympify(b)
d = reps(syms(a), syms(b), symbols)
if (d):
return a.xreplace(d) == b
else:
return a == b