# How to reduce an expression into user-defined variables?

In the following code, I define a set of matrices. Then, I define a function that calculates the commutation relation relation between these matrices

Subscript[T, 1] = 1/2 ({
{0, 1, 0},
{1, 0, 0},
{0, 0, 0}
});
Subscript[T, 2] = 1/2 ({
{0, -I, 0},
{I, 0, 0},
{0, 0, 0}
});
Subscript[T, z
] = 1/2 ({
{1, 0, 0},
{0, -1, 0},
{0, 0, 0}
});
Subscript[V, 1
] = 1/2 ({
{0, 0, 1},
{0, 0, 0},
{1, 0, 0}
});
Subscript[V, 2
] = 1/2 ({
{0, 0, -I},
{0, 0, 0},
{I, 0, 0}
});
Subscript[U, 1 ] = 1/2 ({
{0, 0, 0},
{0, 0, 1},
{0, 1, 0}
});
Subscript[U, 2 ] = 1/2 ({
{0, 0, 0},
{0, 0, -I},
{0, I, 0}
});
Y = 1/3 ({
{1, 0, 0},
{0, 1, 0},
{0, 0, -2}
});
Subscript[T, a] = Subscript[T, 1] + I Subscript[T, 2];
Subscript[T, b] = Subscript[T, 1] - I Subscript[T, 2];
Subscript[V, a] = Subscript[V, 1] + I Subscript[V, 2];
Subscript[V, b] = Subscript[V, 1] - I Subscript[V, 2];
Subscript[U, a] = Subscript[U, 1] + I Subscript[U, 2];
Subscript[U, b] = Subscript[U, 1] - I Subscript[U, 2];
generators = {Subscript[T, a], Subscript[T, b], Subscript[T, z],
Subscript[U, a], Subscript[U, b], Subscript[V, a], Subscript[V, b],
Y};
commutator[x_, y_] = x.y - y.x;
commutator[Subscript[T, a],Subscript[T, b]]


Now, the commutator between the matrix $T_a$ and $T_b$ turns out to be 2$T_z$. It explicitly calculates the result as expected but I want it to write in terms of my variables.

I wish to write 16 such commutation relations in a table column. I'm using the additional code below to generate the table of matrices. I wish to replace them with my variables.

   generators = {Subscript[T, a], Subscript[T, b], Subscript[T, z],
Subscript[U, a], Subscript[U, b], Subscript[V, a], Subscript[V, b],
Y};
relationsTable =
Table[MatrixForm[commutator[i, j]], {i, generators}, {j,
generators}];
Prepend[relationsTable, {"\!$$\*SubscriptBox[\(T$$, $$a$$]\)",
"\!$$\*SubscriptBox[\(T$$, $$b$$]\)",
"\!$$\*SubscriptBox[\(T$$, $$z$$]\)",
"\!$$\*SubscriptBox[\(U$$, $$a$$]\)",
"\!$$\*SubscriptBox[\(U$$, $$b$$]\)",
"\!$$\*SubscriptBox[\(V$$, $$a$$]\)",
"\!$$\*SubscriptBox[\(V$$, $$b$$]\)", "Y"}];
column1 =
Prepend, {header1, {"", "\!$$\*SubscriptBox[\(T$$, $$a$$]\)",
"\!$$\*SubscriptBox[\(T$$, $$b$$]\)",
"\!$$\*SubscriptBox[\(T$$, $$z$$]\)",
"\!$$\*SubscriptBox[\(U$$, $$a$$]\)",
"\!$$\*SubscriptBox[\(U$$, $$b$$]\)",
"\!$$\*SubscriptBox[\(V$$, $$a$$]\)",
"\!$$\*SubscriptBox[\(V$$, $$b$$]\)", "Y"}}];
Grid[column1, Frame -> All]


How do i go about replacing the elemts of this table with the variables that I've defined above?

Any help is appreciated!

• Welcome! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
– user9660
Sep 15, 2015 at 18:47
• Thank you for this kind reminder Lou. I shall keep this in mind!
– user34128
Sep 15, 2015 at 18:49
• What is the definition of generators? - is it the list of matrices that you've written? Is the commutator defined as A.B - B.A? What is "Y"? What are header1 and column supposed to be? Why do you say there are 16 such commutation relations when you have 7 matrices defined, and therefore there are 28 different possible pairs of matrices with which to form commutators? Sep 15, 2015 at 20:25
• I'm sorry, I got the number wrong. It isn't 16, its 64 to be precise. I've attached an image of the result as well now. Also, I've included the generators definition!
– user34128
Sep 15, 2015 at 20:32
• Okay. I think you can take advantage of the fact that your matrices are orthogonal under the trace product Tr[ConjugateTranspose[a].b], and use that fact to select from the symbols. I will work on this. Sep 15, 2015 at 20:38

## Preamble

You should be able to adapt this solution to your specific case, but I have taken the liberty of altering your code somewhat and even changing the definitions of some of the matrices. I will explain why I did what I did so that you can alter things to fit with your definitions.

Main alterations:

• Instead of Setting the values of your Subscripted symbols, I am using a list of replacement rules that we can use to replace the Subscripted symbols with the corresponding matrices. There are two reasons for this. First, it allows us to use the Subscripted symbols for formatting, making it so that they are not automatically replaced by the corresponding matrices. Second, there are essentially no computational use-cases for Subscript, and I recommend only using Subscripts for formatting purposes. Never use them to name your variables, because unless you are familiar with DownValues, UpValues, and so on, they might act strangely.

• I have re-scaled some of your matrix definitions (for instance Subscript[T, z]) in order to make the algebra easier. In particular, we want a set of normalized (under the trace-norm) basis vectors, so I have re-scaled to make this happen.

Note that at the end of the post is the fix that allows you to use your original definitions.

## Solution

The big idea is to use the facts that (1) the set of generators compose a basis for the set of traceless 3-by-3 matrices that is orthonormal under the trace norm, and (2) the commutators of the generators can always be expanded in the generators, because the trace of a commutator is always zero. Basically we compute the expansion coefficients of the commutators of the generators in the generator-basis by using the trace-inner-product.

Here is the list of replacement rules (note that I have re-scaled both Subscript[T, z] and Y):

Clear["Subscript"]
rules = {
Subscript[T, 1] -> 1/2 ({{0, 1, 0}, {1, 0, 0}, {0, 0, 0}})
, Subscript[T, 2] -> 1/2 ({{0, -I, 0}, {I, 0, 0}, {0, 0, 0}})
, Subscript[T, z] -> 1/Sqrt[2] ({{1, 0, 0}, {0, -1, 0}, {0, 0, 0}})
, Subscript[V, 1] -> 1/2 ({{0, 0, 1}, {0, 0, 0}, {1, 0, 0}})
, Subscript[V, 2] -> 1/2 ({{0, 0, -I}, {0, 0, 0}, {I, 0, 0}})
, Subscript[U, 1] -> 1/2 ({{0, 0, 0}, {0, 0, 1}, {0, 1, 0}})
, Subscript[U, 2] -> 1/2 ({{0, 0, 0}, {0, 0, -I}, {0, I, 0}})
, Y -> 1/Sqrt[6] ({{1, 0, 0}, {0, 1, 0}, {0, 0, -2}})
, Subscript[T, a] -> Subscript[T, 1] + I Subscript[T, 2]
, Subscript[T, b] -> Subscript[T, 1] - I Subscript[T, 2]
, Subscript[V, a] -> Subscript[V, 1] + I Subscript[V, 2]
, Subscript[V, b] -> Subscript[V, 1] - I Subscript[V, 2]
, Subscript[U, a] -> Subscript[U, 1] + I Subscript[U, 2]
, Subscript[U, b] -> Subscript[U, 1] - I Subscript[U, 2]};


generators = {Subscript[T, a], Subscript[T, b], Subscript[T, z], Subscript[U, a]
, Subscript[U, b], Subscript[V, a], Subscript[V, b], Y};
commutator[x_, y_] = x.y - y.x;


Finally, we define the inner product on this space of matrices as

traceProduct[a_, b_] := Tr[ConjugateTranspose[a].b]


Note that the set of generators is an orthonormal basis for the space of traceless 3-by-3 matrices under this norm:

Outer[traceProduct[#1, #2] &, generators //. rules, generators //. rules, 1]


yields the identity matrix.

We now generate all of the commutators of the generators using

comms = Outer[commutator[#1, #2] &, generators //. rules, generators //. rules, 1];


For each of the elements in this 8-by-8 matrix of 3-by-3 matrices, we compute the expansion coefficients in the generator-basis using

symbols = generators.Outer[traceProduct[#1, #2] &, generators //. rules, comms, 1, 2] // Simplify;


results in

## Verifying the solution

Finally, we can verify that the calculation has been done correctly by taking the matrix of symbols, applying rules to it, and comparing it to the original comms:

Equal[symbols /. (0 -> ConstantArray[0, {3, 3}]) //. rules // Simplify, comms]
(* True *)


## How to fix the solution so that the original definitions work

The following fix should work. Define the squared-norms of the generators as

norms = traceProduct[#, #] & /@ generators //. rules;


Then, the definition of symbols is modified to

symbols = (generators/norms).Outer[traceProduct[#1, #2] &, generators //. rules, comms, 1, 2] // Simplify;