# 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.

A B- B A, A B+B A

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