Actually, it looks like if you are using SymPy 0.6.7, there is a bug that makes 
this return a wrong result:

In [1]: x, y = symbols('x y', commutative=False)

In [2]: a = expand((x + y)**3)

In [3]: a.subs(x*y, y*x + 1).expand().subs(x*y, y*x + 1).expand()
Out[3]: 
     2    2        
1 + x  + y  + 2⋅y⋅x

The output below was from the latest development branch, and will be present in 
the next release.  

Aaron Meurer

On May 23, 2011, at 11:06 PM, Aaron S. Meurer wrote:

> Hi.
> 
> On May 23, 2011, at 10:55 PM, Rajeev Singh wrote:
> 
>> Hi,
>> 
>> I asked this question on sage mailing list already and it seems appropriate 
>> to ask here as well. I wish to simplify some calculation that appear in 
>> quantum mechanics. To begin we use non-commutative variables as -
>> 
>> sage: x, y = sympy.symbols('xy', commutative=False)
>> sage: sympy.expand((x+y)**3)
>> x**2*y + y**2*x + x*y**2 + y*x**2 + x**3 + y**3 + x*y*x + y*x*y
> 
> Just a heads up, starting in the next release, symbols('xy') will create one 
> symbol named xy, not two symbols x and y.  To get around this, you should do 
> symbols('x y') or symbols('x, y') (this works in the older release too, so 
> you can start to change your code now). 
> 
>> 
>> I want to impose the commutation relation [x,y]=1 and bring the expression 
>> to normal form (i.e. in all terms y appears before x, e.g. x*y gets replaced 
>> by y*x + 1). Is it possible to do this?
> 
> You can do this by repeatedly calling subs and expanding, i.e.,
> 
> In [9]: x, y = symbols('x y', commutative=False)
> 
> In [10]: a = expand((x + y)**3)
> 
> In [11]: a
> Out[11]: 
>            2    2      3              2    2      3
> x⋅y⋅x + x⋅y  + x ⋅y + x  + y⋅x⋅y + y⋅x  + y ⋅x + y 
> 
> In [12]: a.subs(x*y, y*x + 1)
> Out[12]: 
>                3      2                  2      3                            
> x⋅(1 + y⋅x) + x  + y⋅x  + y⋅(1 + y⋅x) + y ⋅x + y  + (1 + y⋅x)⋅x + (1 + y⋅x)⋅y
> 
> In [13]: a.subs(x*y, y*x + 1).expand()
> Out[13]: 
>                3                      2      2      3
> 2⋅x + x⋅y⋅x + x  + 2⋅y + y⋅x⋅y + 2⋅y⋅x  + 2⋅y ⋅x + y 
> 
> In [16]: a.subs(x*y, y*x + 1).expand().subs(x*y, y*x + 1)
> Out[16]: 
>        3              2                    2      3              
> 2⋅x + x  + 2⋅y + 2⋅y⋅x  + y⋅(1 + y⋅x) + 2⋅y ⋅x + y  + (1 + y⋅x)⋅x
> 
> In [17]: a.subs(x*y, y*x + 1).expand().subs(x*y, y*x + 1).expand()
> Out[17]: 
>        3              2      2      3
> 3⋅x + x  + 3⋅y + 3⋅y⋅x  + 3⋅y ⋅x + y 
> 
> This could easily be automated with a while loop (repeat until a is 
> unchanged).
> 
>> 
>> If not then can I get the expression such that x*y**2 appears as x*y*y?
> 
> That would be more difficult to do, because y*y is automatically converted to 
> y**2.  But, as you can see, subs is smart enough to handle x*y**2 correctly, 
> so there's no need to use this much less simple form.
> 
> Aaron Meurer
> 
>> 
>> Thanks in advance.
>> 
>> Regards,
>> Rajeev
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "sympy" group.
>> To post to this group, send email to sympy@googlegroups.com.
>> To unsubscribe from this group, send email to 
>> sympy+unsubscr...@googlegroups.com.
>> 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 sympy@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to