Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
[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
up vote 6 down vote accepted

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:

Mathematica graphics

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

share|improve this answer
Ok thanks! That solved my problem. – Mark Neuhaus May 10 '12 at 7:46


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:

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.