How to factorize a noncommuting symbol?

12 views
Skip to first unread message

Gerardo Suarez

unread,
Nov 2, 2021, 11:37:35 AM11/2/21
to sympy
So basically I have a lot of objects like H:

a= sympy.Symbol('a', commutative=False)
beta= sympy.Symbol('beta',commutative=True)
gamma= sympy.Symbol('\gamma',commutative=True)
ad = sympy.Symbol('a^{\dagger}', commutative=False)
H=gamma*ad*a+beta*ad*a


and all I wanna do is rewrite it as H=(gamma+beta)ad*a but using collect throws the error

AttributeError: Can not collect noncommutative symbol

is there any way to do this ? 

Oscar Benjamin

unread,
Nov 2, 2021, 11:48:17 AM11/2/21
to sympy
You can use factor_nc for noncommutative factorisation:

In [4]: factor_nc(H)
Out[4]: (\gamma + β)⋅a__{\dagger}⋅a

I wanted to link to the docs but it doesn't look like it shows up in
the web docs. You can see the code for it here:
https://github.com/sympy/sympy/blob/d85530009457e4716ed91bfeee995e63fd057b94/sympy/core/exprtools.py#L1404

--
Oscar

Gerardo Suarez

unread,
Nov 2, 2021, 11:49:52 AM11/2/21
to sympy
Thanks a lot! I've been using a dummy symbol like 

def collect_noncommutative(op,expr):
    dummy= sympy.Symbol('dummy', commutative=True)
    return expr.subs(op,dummy).collect(dummy).subs(dummy,op)

but I knew there had to be a implemented function I just wasn't able to find it
Reply all
Reply to author
Forward
0 new messages