# get (anti)commutator

Is it possible to get (anti)commutator directly? I have a try, for example

{a,b,c,d,e,f,g,h}::NonCommuting.
ex:=a b c d-c d b a+e f g h+g h f e;
substitute(_,$A?? B??-B?? A??->\commutator{A??}{B??},A?? B??+B?? A??->\anticommutator{A??}{B??}$);

but it doesn't work.

edited

+1 vote

These type of problems are typically handled by writing a product in terms of the commutator and the swapped product. So e.g.

{a,b,c,d,e,f,g,h}::NonCommuting.
ex:=a b c d-c d a b + e f g h+g h e f;
substitute(ex, $a b c d = \commutator{a b}{c d} + c d a b$);

Something similar is done at the bottom of the example at https://cadabra.science/notebooks/beginners.html to rewrite derivatives of the vector potential in terms of the (antisymmetric) field strength.

by (71.6k points)

I want to make all expressions that has the following form

A B- B A, A B+B A

can become

\commutator{A}{B},\anticommutator{A}{B}

I'm sorry that example above I give may be too simple. The problem I'm dealing with is more complicated. I want the method to be generic.This quesion is similar to the question of cyclicity of trace and I also want to know if there is any progress on this issue.