Sympy: Collect assumes commutation

Created on 6 Apr 2018  路  3Comments  路  Source: sympy/sympy

The following code, returns 0, for the collection of two operators a and ad that do not commute.

from sympy.physics.quantum import Commutator, Dagger
from sympy import collect
from sympy.physics.quantum.boson import BosonOp

a = BosonOp("a")
ad = Dagger(a)
print(collect(a*ad-ad*a,a))

One can check that these operators are non-commutative by the following lines

print(a.is_commutative) print(ad.is_commutative)

returning False.

NonCommutative Wrong Result simplify

Most helpful comment

Can also reproduce with a, b = symbols('a b', commutative=False).

>>> a, b = symbols('a b', commutative=False)
>>> collect(a*b + b*a, a)
2*a*b

All 3 comments

Can also reproduce with a, b = symbols('a b', commutative=False).

>>> a, b = symbols('a b', commutative=False)
>>> collect(a*b + b*a, a)
2*a*b

It's not clear to me what a non-commutative analogue of collect should do. The free (non-commutative, unital) ring on one generator isn't nearly as well-behaved as a polynomial ring. Would it be best to simply reject attempted collection of powers of non-commutative variables, and make note of this in the documentation?

Giving an error would be better than giving a wrong result.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ivanistheone picture ivanistheone  路  6Comments

Huyston picture Huyston  路  3Comments

bitbucketboss picture bitbucketboss  路  4Comments

Smit-create picture Smit-create  路  4Comments

mgenet picture mgenet  路  5Comments