Well, I can't get it to give the different results myself, but I can tell you 
why that sort of thing happens in general.  The reason is that somewhere in the 
computation, it is using a dictionary or a set to store terms, and then it is 
iterating through it.  The problem is that, in Python, dictionaries and sets 
are unordered sequences, and their iteration order can basically be any order 
(nothing is guaranteed).  I have actually seen occurrences of list(set_a) 
giving one thing in an execution and then list(set_a) giving another ordering 
just a few lines down.  

So non-determinism in match() is just one thing that you are going to have to 
deal with.  Now, the best way to deal with it in this case is to do like the 
other people on this thread have said, and use a different method from match to 
solve your problem (I really don't think match was designed to solve your 
problem).  If that method isn't working completely for you, post a fuller 
example here and we can see how to fix it.  

In other cases, the best thing to do is to use the exclude keyword to Wild to 
force the match to have only one possible output.  As a simple example, 

p = Wild('p')
q = Wild('q')

(x*f(x)).match(p*q)

has four possible outputs:

{p:1, q:x*f(x)}
{p:x, q:f(x)}
{p:f(x), q:x}
{p:x*f(x), q:1}

But if you change it to

p = Wild('p', exclude=[f(x)])
q = Wild('q', exclude=[1])

(x*f(x)).match(p*q)

Then there is only one possible match, which is

{p:x, q:f(x)}

Finally, I want to point out that if you run powsimp() on the results of your 
"wrong" match, that it will work for you then (I think somewhere the match() 
algorithm is expecting those terms to cancel).

Aaron Meurer


On Oct 23, 2010, at 10:43 AM, Chenjie Gu wrote:

> 
> 
> On Sat, Oct 23, 2010 at 3:33 AM, Chris Smith <[email protected]> wrote:
> Chenjie Gu wrote:
> >>> Yes to both. I wrote up my notes when trying to understand what was
> >>> going on here in Issue 1601:
> >>>
> >>> http://code.google.com/p/sympy/issues/detail?id=1601
> >>>
> >>> Chris,
> >>
> >> I just went through the above link.
> >> In any case, I think what you described is a "deterministic"
> >> algorithm,
> >> right?
> >> What I feel confused is that it gives "random" results in different
> >> runs. (on the same machine)
> >> Would you explain why that's the case?
> >>
> 
> If you show some code showing that result perhaps that would help. Otherwise, 
> I can't imagine why equivalent expressions would give different result. But 
> note: the ordering of the args matters, so if variable names or any other 
> such thing change the ordering of terms then that will change the match 
> behavior IIRC.
> 
> One example is the script in the first post:
> 
> x0 = Symbol('x0')
> x1 = Symbol('x1')
> 
> p = Wild('p', exclude=[0])
> q = Wild('q', exclude=[0])
> r = Wild('r')
> 
> y = exp(40*x0) - exp(-40*x1 + 40*x0)
> print y.match(p*q+r)
> 
> I ran exactly the same script for multiple times, but different results were 
> obtained for different runs.
> 
> Chenjie
> 
> 
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sympy?hl=en.
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sympy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to