On Fri, Mar 15, 2013 at 6:56 AM, Aaron Meurer <[email protected]> wrote:
> How fast is this? Does it scale well for large expressions with many
> variables? Can it be modified to return the mapping?

My symatch branch does what your asking...it's called `symbol_match`
as I recall. I'm stalled thinking about how to resolve the issue that
some args are ordered while some are not (commutative part of Mul and
Add). It is much faster than using unify because (I assume) it uses
more of the tree information before resorting to using permutations of
symbols.

>
> By the way, Dummy is a subclass of Symbol, so doing .atoms(Symbol,
> Dummy) is redundant.
>
> Aaron Meurer
>
> On Sun, Mar 10, 2013 at 10:44 PM, smichr <[email protected]> wrote:
>> 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.
>>
>>
>
> --
> 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.
>
>

-- 
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.


Reply via email to