# 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)? Commented May 10, 2012 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. Commented May 10, 2012 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. Commented May 10, 2012 at 7:46
• "//." cannot start another line, otherwise the program cannot run. Commented Jul 27, 2022 at 14:16
• @lotus2019 Thanks for pointing that out. I fixed it now. Commented Jul 27, 2022 at 17:41

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).

Here is an alternative solution which takes advantage of the support for Subscript in NCAlgebra 5.0.0. Unfortunately it does not look as cool as it does once you put that on a Notebook. Try:

expr = {{Subscript[x, 0, 0] ** Subscript[x, 1, 0] +
Subscript[x, 0, 1] ** Subscript[x, 1, 0] -
Subscript[x, 1, 0] ** Subscript[x, 0, 1] -
Subscript[x, 1, 0] ** Subscript[x, 0, 0]},
{Subscript[x, 0, 1] ** Subscript[x, 1, 0] -
Subscript[x, 1, 0] ** Subscript[x, 0, 1] -
Subscript[x, 0, 2] ** Subscript[x, 1, 0]},
{Subscript[x, 0, 0] ** Subscript[x, 1, 2] -
Subscript[x, 1, 2] ** Subscript[x, 0, 0]},
{Subscript[x, 0, 1] ** Subscript[x, 1, 2] -
Subscript[x, 1, 2] ** Subscript[x, 0, 1]},
{Subscript[x, 0, 2] ** Subscript[x, 1, 3]}}


and the single substitution:

NCReplaceRepeated[expr, Subscript[x, 0, j_] ** Subscript[x, 1, k_] -
Subscript[x, 1, k_] ** Subscript[x, 0, j_] ->
f[Subscript[x, 0, j], Subscript[x, 1, k]]]