If two expression trees are structurally the same they should unify under
some mapping of symbols. usympy makes this very easy to test for:
>>> def structurally_equal(a, b):
... from sympy.unify import usympy
... if isinstance(a, (list, tuple)):
... if type(a) != type(b): return False
... a = Tuple(*a)
... b = Tuple(*b)
... # disambiguate symbols that are in common
... bsym = b.atoms(Symbol, Dummy)
... common = a.atoms(Symbol, Dummy) & bsym
... if common:
... b = b.xreplace(dict(zip(common, [Dummy() for c in common])))
... try:
... next(usympy.unify(a, b, {}, variables = list(bsym)))
... return True
... except:
... return False
...
>>> structurally_equal([1], [1])
True
>>> structurally_equal([1], (1,))
False
>>> structurally_equal(Sum(x, (x, 1, 2)), Sum(y, (y, 1, 2)))
True
>>> structurally_equal(Sum(x, (x, 1, 2)), Sum(y, (y, 1, 21)))
False
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.