How to write polynomial expression as commutator form?

I want to write some polynomial expressions as commutator form. For example : $$\frac{\text{BA}}{2}-\frac{\text{AB}}{2} = -\frac{1}{2}[A,B]$$ or $$\frac{\text{AAB}}{6}-\frac{\text{ABA}}{3}-\frac{\text{ABB}}{3}+\frac{\text{BAA}}{6}+\frac{2 \text{BAB}}{3}-\frac{\text{BBA}}{3} = \frac{1}{6}[A,[A,B]] - \frac{1}{3}[B,[B,A]].$$ Here, $A$ and $B$ 's are the constant matrices. $AB$ is the product of $A$ and $B$. Also $[A,B]$ is Lie bracket of $A$ and $B$. So, how can I write the following expression as Nested Commutators with the help of Mathematica? Is there any generalized mathematica package or code?

$$-\frac{\text{AAAB}}{24}+\frac{\text{AABA}}{8}+\frac{\text{AABB}}{8}-\frac{\text{ABAA}}{8}-\frac{\text{ABAB}}{4}-\frac{\text{ABBB}}{8}+\frac{\text{BAAA}}{24}+\frac{\text{BABA}}{4}+\frac{3 \text{BABB}}{8}-\frac{\text{BBAA}}{8}-\frac{3 \text{BBAB}}{8}+\frac{\text{BBBA}}{8}$$ or $$\frac{\text{AAAAB}}{120}-\frac{\text{AAABA}}{30}-\frac{\text{AAABB}}{30}+\frac{\text{AABAA}}{20}+\frac{\text{AABAB}}{20}+\frac{\text{AABBA}}{20}+\frac{\text{AABBB}}{20}-\frac{\text{ABAAA}}{30}+\frac{\text{ABAAB}}{20}-\frac{\text{ABABA}}{5}-\frac{\text{ABABB}}{5}+\frac{\text{ABBAA}}{20}+\frac{3 \text{ABBAB}}{10}-\frac{\text{ABBBA}}{5}-\frac{\text{ABBBB}}{30}+\frac{\text{BAAAA}}{120}-\frac{\text{BAAAB}}{30}+\frac{\text{BAABA}}{20}+\frac{\text{BAABB}}{20}+\frac{\text{BABAA}}{20}-\frac{\text{BABAB}}{5}+\frac{3 \text{BABBA}}{10}+\frac{2 \text{BABBB}}{15}-\frac{\text{BBAAA}}{30}+\frac{\text{BBAAB}}{20}-\frac{\text{BBABA}}{5}-\frac{\text{BBABB}}{5}+\frac{\text{BBBAA}}{20}+\frac{2 \text{BBBAB}}{15}-\frac{\text{BBBBA}}{30}.$$

With my best regards.

• Can you give us an idea of what "commutator form" means? Are the A's and B's separate variables, or are ABAA, AAAB, etc variables names in themselves? Commented Dec 19, 2015 at 17:18
• Please post copy-and-pastable, properly formatted (click the question mark on the right-side of the editing toolbar for help) Mathematica code instead of screen-shots. People like to be able to copy and paste into their own copies of Mathematica for fast answering. Commented Dec 19, 2015 at 17:20
• I have corrected the mistakes in the topic. Thank you for suggestions.
– drxy
Commented Dec 19, 2015 at 19:22
• Note that Mathematica reorders B*A to A*B freely, so how do plan to input such polynomials? Commented Dec 19, 2015 at 19:50
• You can produce the polynomials using this paper arxiv.org/pdf/math-ph/0603016v1.pdf. There is a mathematica procedure in page 5. You can use $n=4$ or higher.
– drxy
Commented Dec 19, 2015 at 20:13

Update, December 22

I've developed a version that is more robust. I believe it should work in general, but I'm sure there are some corner cases that won't work (and probably some-not-so-corner cases too).

As before we add rules to the almost-no-built-in-meaning NonCommutativeMultiply (**):

Clear[ncm]
ncm[a__] := NonCommutativeMultiply[a]
Unprotect@NonCommutativeMultiply;
NonCommutativeMultiply[a___, b_ + c_, d___] := NonCommutativeMultiply[a, b, d] + NonCommutativeMultiply[a, c, d]
a_?NumericQ ** b_ := a b
a_ ** b_?NumericQ := a b
(-a_) ** b_ := -(a ** b)
a_ ** (-b_) := -(a ** b)
Protect@NonCommutativeMultiply

For convenience, we define a format for the commutator:

Format[comm[a_, b_]] := DisplayForm@RowBox[{"[", ToString@a, ",", ToString@b, "]"}]

Then, in order to generate expressions from commutator forms, we define:

Clear[expandCommutator]
expandCommutator[expr_] := expr //. comm[a_, b_] :> a ** b - b ** a // Expand

We also define the function that converts OP's form into NonCommutativeMultiply form:

Variables[expr] -> ncm @@@ (ToExpression /@ # &@*Characters@*ToString) /@ Variables[expr]]

Finally, we define the function that puts expressions in commutator form as

generateCommutatorForm[expr_, vars_List] := Module[{
rules = Join[
ncm[#2, #1] :> ncm[#1, #2] - comm[#1, #2] & @@@ Subsets[vars, {2}]
, {
comm[a : _comm, b : Except[_comm]] :> -comm[b, a]
, comm[a : _comm, b : _comm] /; Depth[a] > Depth[b] :> -comm[b, a]
, ncm[a : _comm, b : Except[_comm]] :> ncm[b, a] - comm[b, a]
, comm[ncm[a__, b_], c_] :> ncm[a, comm[b, c]] + ncm[comm[a, c], b]
, ncm[a : _comm, b : _comm] /; Signature[{a, b}] == -1 :> ncm[b, a] - comm[b, a]
, comm[x : comm[a_, b_], c : _comm] /; Depth[x] <= Depth[c] :> comm[a, comm[b, c]] + comm[b, comm[c, a]]
}
]
}, FixedPoint[Expand[# /. rules] &, expr]]

Examples:

• A simple one:

expr = -1/2 comm[a, b]
expr2 = expr // expandCommutator
generateCommutatorForm[expr2, {a, b}]

• A more complicated one, from the post:

expr = 1/6 comm[a, comm[a, b]] - 1/3 comm[b, comm[b, a]]
expr2 = expr // expandCommutator
generateCommutatorForm[expr2, {a, b}]

• A more-than-two-operator example:

expr = 1/2 comm[a, comm[b, c]] - 1/3 comm[b, comm[c, a]]
expr2 = expr // expandCommutator
expr3 = generateCommutatorForm[expr2, {a, b, c}]
expr2 === expandCommutator[expr3]

• A four-variable, three-times nested one (this does illustrate a limitation in that I don't simplify the resulting expression if there is a simpler expression in terms of commutators; clearly, the representation in commutators is not unique):

expr = (1/2) comm[a, comm[b, comm[c, d]]]
expr2 = expr // expandCommutator
expr3 = generateCommutatorForm[expr2, {a, b, c, d}]
expr2 === expandCommutator[expr3]

• The most complicated one from the post:

expr2 = AAAAB/120 - AAABA/30 - AAABB/30 + AABAA/20 + AABAB/20 +
AABBA/20 + AABBB/20 - ABAAA/30 + ABAAB/20 - ABABA/5 - ABABB/5 +
ABBAA/20 + 3 ABBAB/10 - ABBBA/5 - ABBBB/30 + BAAAA/120 -
BAAAB/30 + BAABA/20 + BAABB/20 + BABAA/20 - BABAB/5 +
3 BABBA/10 + 2 BABBB/15 - BBAAA/30 + BBAAB/20 - BBABA/5 -
BBABB/5 + BBBAA/20 + 2 BBBAB/15 - BBBBA/30 // convertToNCMForm;

generateCommutatorForm[expr2, {A, B}]

Original Post

For this problem, we choose a normal-order: let's move all A's to the left of all commutators, which are all moved to the left of all B's. We will use NonCommutativeMultiply in order to be able to use symbols that don't commute.

First of all, if your expressions are in the form of

expr = BA/2 - AB/2;

then we can use the following converter to get into a form that uses NonCommutativeMultiply:

Variables[expr] -> ncm @@@ (ToExpression /@ # &@*Characters@*ToString) /@ Variables[expr]]

(This is sort of silly, since it should be put in this form to begin with, but since the OP has it in that form, we convert it first.) So, for instance,

convertToNCMForm[expr]
(* -(A ** B/2) + B ** A/2 *)

We need some rules for NonCommutativeMultiply that allow expressions to be automatically simplified. (For instance, we want the operation to distribute over addition.) Here's a set of rules that work:

ncm[a__] := NonCommutativeMultiply[a]
Unprotect@NonCommutativeMultiply;
NonCommutativeMultiply[a___, b_ + c_, d___] := NonCommutativeMultiply[a, b, d] + NonCommutativeMultiply[a, c, d]
a_?NumericQ ** b_ := a b
a_ ** b_?NumericQ := a b
(-a_) ** b_ := -(a ** b)
a_ ** (-b_) := -(a ** b)
Protect@NonCommutativeMultiply

Finally, we choose a nice format for the commutator:

Format[comm[a_, b_]] := DisplayForm@RowBox[{"[", ToString@a, ",", ToString@b, "]"}]

or

Format[comm[a_, b_]] := "[" <> ToString@a <> "," <> ToString@b <> "]"

Then,

comm[A, B]
(* [A, B] *)

Now, here's the meat of the problem. We use replacement Rules to move symbols past other symbols, introducing commutators along the way in the form of comm. We put these rules in a function as follows:

Clear@generateCommutatorForm
generateCommutatorForm[expr_, {A_, B_}] :=
Expand@expr //. {ncm[B, A] :> ncm[A, B] - comm[A, B],
ncm[B, comm[a__]] :> ncm[comm[a], B] - comm[comm[a], B],
ncm[comm[a__], A] :> ncm[A, comm[a]] - comm[A, comm[a]]
} // Expand

Now!

expr = expr = BA/2 - AB/2;
expr2 = convertToNCMForm[expr]
(* -(A ** B/2) + B ** A/2 *)

and

generateCommutatorForm[expr2, {A, B}]
(* -(1/2) [A,B] *)

For a more complicated expression,

expr = AAB/6 - ABA/3 - ABB/3 + BAA/6 + (2 BAB)/3 - BBA/3;
expr2 = convertToNCMForm[expr]
(* A ** A ** B/6 - A ** B ** A/3 - A ** B ** B/3 + B ** A ** A/6 + (2 B ** A ** B)/3 - B ** B ** A/3 *)

and

generateCommutatorForm[expr2, {A, B}]
(* [A, [A, B]]/6 - [[A, B], B]/3 *)

Finally,

expr = -(AAAB/24) + AABA/8 + AABB/8 - ABAA/8 - ABAB/4 - ABBB/8 + BAAA/24 + BABA/4 + 3/8 BABB - BBAA/8 - 3/8 BBAB + BBBA/8;
expr2 = generateCommutatorForm[convertToNCMForm[expr], {A, B}]

If you want it in the form where the nested commutators are always in the second slot, apply the following function to the final expression:

toCanonicalForm[expr_] :=
expr //. {comm[comm[x__], y__] :> -comm[y, comm[x]],
comm[-a_, b_] :> -comm[a, b],
comm[a_, -b_] :> -comm[a, b]
} // Expand

Then,

toCanonicalForm[expr2]

• You are of course great. Thank you for help.
– drxy
Commented Dec 21, 2015 at 7:56
• @drxy. Thanks for the accept! But note that this method is not very robust. For instance, it won't work for your last example. I may be able to work on this again some time in the future, but maybe I've given you enough to go on, and you can go the rest of the way to making this robust. Commented Dec 21, 2015 at 17:17
• I'll try to make robust code for the calculation. If I have any problem, I will write here. Again and again thanks.
– drxy
Commented Dec 22, 2015 at 9:33
• @drxy. New version! It's much better now. I took advantage of some of the commutator identities and figured out how to deal with some of the "stopping expressions", things like when there were commutators of commutators with a product of operators and when there were commutators multiplied together. Commented Dec 24, 2015 at 4:34
• Oh my god! You are of course awesome.
– drxy
Commented Dec 24, 2015 at 9:08