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.
Answer
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.
Comments
Post a Comment