I have defined the commutator between two matrices as


I have defined the nested commutator as

nestcomm[A_,B_,n_]:= ToExpression[ 
                        StringRepeat["comm[a,", n] <> "b" <> StringRepeat["]", n] 

where $n$ indicates how many time the commutator must be nested.

Does exists a more simple way to define an $n$-time nested operation between two elements $A$ and $B$? I tried to use the Nest function but without success.


2 Answers 2


Redefine your comm function to take in and put out a pair of matrices, then Nest can be called easily:

comm[{A_, B_}] := {A, A.B - B.A}

Then just invoke Nest with the number of terms in the final position. For n=2:

Rest[Nest[comm, {a, b}, 2]]
{a.(a.b - b.a) - (a.b - b.a).a}
  • $\begingroup$ Thank you for your help! $\endgroup$
    – Galuoises
    Oct 25, 2018 at 22:47
  • $\begingroup$ @Galuoises Nest can be tricky when you first encounter it... but it can be very powerful! $\endgroup$
    – bill s
    Oct 26, 2018 at 0:29

Would something like the following work?

comm[A_,B_,1] := A.B-B.A
comm[A_, B_, n_Integer?Positive] := comm[comm[A, B, n-1], B, 1]

For example:

comm[A, B, 2] //TensorExpand
comm[A, B, 3] //TensorExpand

A.B.B - 2 B.A.B + B.B.A

A.B.B.B - 3 B.A.B.B + 3 B.B.A.B - B.B.B.A


comm[A_, B_, n_Integer?Positive] := comm[A, comm[A, B, n-1], 1]

to obtain the same definition as in @bill's answer.

  • $\begingroup$ Thank you! I appreciate your answer $\endgroup$
    – Galuoises
    Oct 25, 2018 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.