Comment #5 on issue 1358 by wflynny: (e*f*e*f).subs(e*f,f*e+h) fails if e*f  
is not commutative
http://code.google.com/p/sympy/issues/detail?id=1358

We were looking deeper into this problem today and we traced the problem to  
the
following snippet of code in the '_eval_subs' definition in mul.py:

...
         l1, l2 = len(terms_self), len(terms_old)
         if l2 == 0:
             # if old is just a number, go through the self.args one by one
             return Mul(*[x._eval_subs(old, new) for x in self.args])
         elif l2 < l1:
             # old is some something more complex, like:
             # (a*b*c*d).subs(b*c,x) -> a*x*d
             # then we need to search where in self.args the "old" is, and  
then
             # correctly substitute both terms and coefficients.

vvvvvvvvvvvvvvvvvvvvv BEGIN: BROKEN CODE vvvvvvvvvvvvvvvvvvvvvvvvvvvv
             """
             self_set = set(terms_self)
             old_set = set(terms_old)
             if old_set < self_set:
                 ret_set = self_set - old_set
                 return Mul(new, coeff_self/coeff_old, *[s._eval_subs(old,  
new) for s
in ret_set])
^^^^^^^^^^^^^^^^^^^ END: BROKEN CODE ^^^^^^^^^^^^^^^^^^^^^^

                 out_set = [element._eval_subs(old,new) for element in  
self_set]
                 return Mul(new, coeff_self/coeff_old,  
*[element._eval_subs(old,new)
for element in out_set])

The method is using sets, which is the source of the problem. Sets in  
python, as I
understand, are unordered and only contain one unique instance of an  
element. Thus,
two problems arise. Firstly, there is my problem of it ignoring  
commutativity. But
secondly, there is the larger problem of ignoring multiplicity. I have  
tried my hand
at writing a patch using lists but it doesn't work perfectly yet. I would  
like to
have it working properly tomorrow and will append my solution to this  
thread. Any
experience help would be greatly appreciated.

I hope this helps pinpoint the problem and help solve a seemingly  
widespread problem.
This could possible fix a bunch of problems in match, subs, collect and the  
like.

Thanks!

-wflynny

--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings

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

Reply via email to