# Simplify expression with abstract matrices

After defining a commutator function

Commutator[X_, Y_] := X.Y - Y.X;


I would like to be able to simplify abstract matrix algebra expressions such as

Simplify[Commutator[A + B, Commutator[A + B, A - B]] -
Commutator[A - B, Commutator[A + B, A - B]]]


-(A - B).(-(A - B).(A + B) + (A + B).(A - B)) + (A +
B).(-(A - B).(A + B) + (A + B).(A - B)) + (-(A - B).(A + B) + (A +
B).(A - B)).(A -
B) - (-(A - B).(A + B) + (A + B).(A - B)).(A + B)


Try TensorReduce

\$Assumptions = {A ∈ Matrices[{n, n}], B ∈ Matrices[{n, n}]};
expr = Commutator[A + B, Commutator[A + B, A - B]] -
Commutator[A - B, Commutator[A + B, A - B]];
TensorReduce[expr]
(*
A.MatrixPower[A - B, 2] + 2 A.MatrixPower[B, 2] +
A.MatrixPower[A + B, 2] + B.MatrixPower[A - B, 2] -
B.MatrixPower[A + B, 2] + 6 MatrixPower[B, 2].A - 6 B.A.B -
2 MatrixPower[A, 3] *)


I don't know why TensorExpand is not expanding MatrixPower, but you can distribute all the dot products first with:

distributeDot[expr_] := FixedPoint[MapAll[Distribute[#, Plus, Dot] &, #] &, expr]


After this it simplifies much further:

TensorReduce[distributeDot[expr]]
(* 4 A.MatrixPower[B, 2] + 4 MatrixPower[B, 2].A - 8 B.A.B *)