# Simplifying the trace of a matrix expression

I have a long expression involving matrices that I derived using the NCAlgebra package. Can I simplify the trace of the expression?

For example, say b is a scalar (commutes with all) and x and y are matrices (non-commutative). Can I simplify Tr[b x.y.y + b y.x.y] to 2b Tr[x.y.y] using the properties of the trace?

Thanks, that is what I am going for (although I don't quite understand the code) but I only have version 8.

The expression is given below, where e and r are scalars, q and v are matrices and the double star (**) denotes products of non-commutative elements. I've done it by hand, but I want to be able to simplify the trace of this with Mathematica (and others which are much longer).

e^2 q ** q - e r q ** v - e r v ** q + r^2 v ** v -
1/12 e^3 r q ** q ** q ** v + 1/12 e^3 r q ** q ** v ** q +
1/24 e^2 r^2 q ** q ** v ** v + 1/12 e^3 r q ** v ** q ** q -
1/12 e^2 r^2 q ** v ** q ** v - 1/12 e^2 r^2 q ** v ** v ** q +
1/24 e r^3 q ** v ** v ** v - 1/12 e^3 r v ** q ** q ** q +
1/6 e^2 r^2 v ** q ** q ** v - 1/12 e^2 r^2 v ** q ** v ** q -
1/24 e r^3 v ** q ** v ** v + 1/24 e^2 r^2 v ** v ** q ** q -
1/24 e r^3 v ** v ** q ** v + 1/24 e r^3 v ** v ** v ** q

• Can you show the "long expression"? Or at least a snippet, to get an idea of what you're looking at? – Corey Kelly Jun 9 '13 at 2:08
• I added package, as OP has mentioned his use of NCAlgebra, and might want a solution using functions from that package. – J. M.'s ennui Jun 9 '13 at 15:42

Yes, you can simplify the trace this way. First, Tr[x.y.z] is invariant under cyclic permutations, so Tr[x.y.z] = Tr[y.z.x] = Tr[z.x.y], as described in Wikipedia's entry on trace of a product. For the case of interest, this means Tr[y.x.y] is equal to Tr[x.y.y]. Hence

Tr[b x.y.y + b y.x.y]  = Tr[b x.y.y] + Tr[b y.x.y]
= b Tr[x.y.y] + b Tr[y.x.y]
= b Tr[x.y.y] + b Tr[x.y.y]
= 2b Tr[x.y.y]


This property (the invariance of the trace under cyclic permutations) can now be used to define a set of rules that transform the equation. Here all the q and v terms that appear with noncommutative multiplications are placed in a sort of alphabetical order (all the q's coming before the v's, respecting the cyclic permutation constraint).

rules = {q ** v ** v ** q -> q ** q ** v ** v, v ** q ** v ** v -> q ** v ** v ** v,
v ** v ** q ** q -> q ** q ** v ** v, v ** q ** q ** v -> q ** q ** v ** v,
v ** q ** q ** q -> q ** q ** q ** v, v ** v ** q ** v -> q ** v ** v ** v,
v ** v ** v ** q -> q ** v ** v ** v, q ** q ** v ** q -> q ** q ** q ** v,
q ** v ** q ** q -> q ** q ** q ** v, v ** q ** v ** q -> q ** v ** q ** v};


Now, with your equation above defined as eqn, the rules can be applied:

eqn //. rules


and the result is quite satisfyingly short

e^2 q ** q - e r q ** v - e r v ** q + r^2 v ** v +
1/6 e^2 r^2 q ** q ** v ** v - 1/6 e^2 r^2 q ** v ** q ** v


I may not have all the possible rules and remember that this is only true when you are taking the Trace! I don't have version 8 to check, but this doesn't use any advanced functionality, so it ought to work fine.

Well, you certainly can if you have version 9 and don't mind using the (quite verbose) tensor notation:

Assuming[
{b ∈ Complexes, (x | y) ∈ Matrices[{n, n}]},
TensorReduce@
TensorContract[
b TensorContract[TensorProduct[x, y, y], {{2, 3}, {4, 5}}] +
b TensorContract[TensorProduct[y, x, y], {{2, 3}, {4, 5}}],
{{1, 2}}
]
]
(* -> 2 b TensorContract[TensorProduct[x, y, y], {{1, 6}, {2, 3}, {4, 5}}] *)


While Mathematica certainly knows the relevant identities, it apparently doesn't know how Dot and Tr are represented as contractions of tensor products, so you have to write it all out explicitly. I'll leave it to you to decide whether this was worth the trouble or not.

You could use my TensorSimplify package to do this. Install the paclet with:

PacletInstall[
"TensorSimplify",
"Site" -> "http://raw.githubusercontent.com/carlwoll/TensorSimplify/master"
]


Once installed, you can load the package with:

<<TensorSimplify


Simple example

Here is TensorSimplify applied to your simple example:

$Assumptions = b ∈ Complexes && (x|y) ∈ Matrices[{n, n}]; TensorSimplify[ Tr[b x.y.y + b y.x.y] ]  2 b Tr[y.x.y] More complicated example For your more complicated example: expr = e^2 q ** q - e r q ** v - e r v ** q + r^2 v ** v - 1/12 e^3 r q ** q ** q ** v + 1/12 e^3 r q ** q ** v ** q + 1/24 e^2 r^2 q ** q ** v ** v + 1/12 e^3 r q ** v ** q ** q - 1/12 e^2 r^2 q ** v ** q ** v - 1/12 e^2 r^2 q ** v ** v ** q + 1/24 e r^3 q ** v ** v ** v - 1/12 e^3 r v ** q ** q ** q + 1/6 e^2 r^2 v ** q ** q ** v - 1/12 e^2 r^2 v ** q ** v ** q - 1/24 e r^3 v ** q ** v ** v + 1/24 e^2 r^2 v ** v ** q ** q - 1/24 e r^3 v ** v ** q ** v + 1/24 e r^3 v ** v ** v ** q;  To use TensorSimplify we need to change NonCommutativeMultiply to Dot: $Assumptions = (e|r) ∈ Complexes && (v|q) ∈ Matrices[{n, n}];

TensorSimplify[
Tr[expr] /. NonCommutativeMultiply -> Dot
]
`

e^2 Tr[q.q] - 2 e r Tr[v.q] + r^2 Tr[v.v] + 1/6 e^2 r^2 Tr[v.q.q.v] - 1/6 e^2 r^2 Tr[v.q.v.q]