# Replace expressions by self defined symbols

I have a list of expressions like the following:

{{X00 ** X10 + X01 ** X10 - X10 ** X01 - X10 ** X00 },
{X01 ** X10 - X10 ** X01 - X02 ** X10},
{X00 ** X12 -  X12 ** X00}, {X01 ** X12 - X12 ** X01}, {X02 ** X13}}


where all Xij are just symbols. Now suppose we define an expression like

[X0j,X1k] := X0j ** X1k - X1k ** X0j.


Is it possible to let Mathematica substitute all terms in the list that look like the right side of the last expression by its left side?

If necessary I can change the Xij's in the list to something else.

-
[X0j,X1k] is not syntactically correct Mathematica. Could you clarify what you mean by it? Could you please give an exampe of a possible substitution (I'm a little confused about what you need exactly)? – Szabolcs May 10 '12 at 7:19
Ok the "[" and "]" symbols are occupied by mathematica itself. But that was just an example. We could use anything. terms like X0j ** X1k - X1k ** X0j should just be rephrased by something visual more meaning full. – Mark Neuhaus May 10 '12 at 7:24

Let's represent your bracket expression using the head bb, so

bb[x, y] == x ** y - y ** x


Then we can just use a simple replace rule:

{{X00 ** X10 + X01 ** X10 - X10 ** X01 - X10 ** X00 },
{X01 ** X10 - X10 ** X01 - X02 ** X10},
{X00 ** X12 -  X12 ** X00}, {X01 ** X12 - X12 ** X01}, {X02 ** X13}}
//. x_ ** y_ - y_ ** x_ :> bb[x, y]

(* ==>
{{bb[X00, X10] + bb[X01, X10]},
{bb[X01, X10] - X02 ** X10},
{bb[X00, X12]}, {bb[X01, X12]}, {X02 ** X13}}
*)


We can automate the conversion between the two representations using

toBracket[expr_] := expr //. x_ ** y_ - y_ ** x_ :> bb[x, y]
fromBracket[expr_] := expr /. bb[x_, y_] :> x ** y - y ** x


If you wish to have a prettier notiation, you could for example use AngleBracket instead of bb. It is formatted like this:

You can enter the brackets using the key sequence Esc<Esc.

-
Ok thanks! That solved my problem. – Mark Neuhaus May 10 '12 at 7:46

Mark,

This is not an answer as much as it is a recommendation. I had to manipulate long expressions of non-commutative variables for my PhD going beyond defining a Killing form. If this will be the case for you I really highly recommend the NCAlgebra package someone wrote:

http://www.math.ucsd.edu/~ncalg/

the manuals are pretty much self explanatory and it will solve many problems (like series expansion etc).

-