问题
I want to work out some commutator manipulations and found this tool in sympy. It appears to work as expected (but the documentation is virtually non-existent or at least I found little, but see the comment by Dalton Bentley below), but I ran into the following problem.
from sympy.physics.quantum import Commutator as Comm
from sympy.physics.quantum import Operator
A = Operator('A')
B = Operator('B')
C = Comm(Comm(Comm(A,B),A),B)
D = Comm(Comm(Comm(A,B),B),A)
E = (C-D).expand(commutator=true)
E
>>> [[[A,B],A],B] - [[[A,B],B],A]
instead of the expected simpler result 0
(since [[[A,B],A],B] = [[[A,B],B],A]). So how can I force the simpler result without evaluating the commutators (i.e. w/o calling the doit()
function)? Note that
simplify(E.doit())
>>> 0
gives the desired result.
回答1:
Currently in SymPy, Commutator._eval_expand_commutator, does not know about this identity so it has to expand the commutators (in the .doit
method as you have identified) in order to be able to simplify the expression.
What would be needed for this to work, would be to add special case(s) to the Commutator._eval_expand_commutator
method for when the arguments of a commutator contains commutators, and then check for known identities.
I have opened an issue for this here: https://github.com/sympy/sympy/issues/10892
来源:https://stackoverflow.com/questions/36095622/how-to-use-sympy-physics-quantum-commutator