# Rearrange the list with some rules

I am trying to solve physic problem on operators which are not commute. However, I am not good at coding, so I am having some problem with Mathematica code.

Let's define my list such that

b[0] a[1] a[0] a[0]


The rules are that b and a are commute, same elements are commute(a[0] and a[0]), but a[1] and a[0] or b[1] and b[0] are not commute.

a[0] a[1] = a[1] a[0] + 1
b[0] b[1] = b[1] b[0] + 1


My goal is place b at the end, and place element with [1] at the end.

b[0]a[1]a[0]a[0] -> b[0] (a[0]a[1] + 1) a[0] -> b[0]a[0]a[1]a[0] + b[0]a[0]
-> b[0]a[0] (a[0]a[1] + 1) + b[0]a[0] -> b[0]a[0]a[0]a[1] + 2 b[0]a[0]
-> a[0]a[0]a[1]b[0] + 2 a[0]b[0]


I thought it might be good for me to use list-manipulation.

{b[0], a[1], a[0], a[0] } ->  { a[0], a[0], a[1], b[0] } + 2 { a[0] , b[0] }


Similarly

{b[1], b[0], a[0], a[0]} -> { a[0], a[0], b[0], b[1] } +{a[0] a[0]}

{b[1], b[1], b[0], a[0]} -> { a[0], b[0], b[1], b[1] } + 2 {a[0] b[1]}

{a[1], a[1], a[1], a[0]} -> { a[0], a[1], a[1], a[1]} +3 {a[1],a[1]}

{a[1], a[1], a[0], a[0]} -> { a[0], a[0], a[1], a[1]} +2 {a[1],a[0]} +2 {a[0]+a[1]}
->{ a[0], a[0], a[1], a[1]} + 4{a[0]+a[1]} + 2


Anyone can suggest me what command shall I use? I think I can use If[], MemberQ[] and Select[]. However, I am not sure where to start.

Thank you

• For me your example is a bit too cryptic. Can't you come up with a more simple/clear example?
– Kay
Apr 14, 2016 at 15:31
• You should look up NonCommutativeMultiply and the many questions about boson algebra on this site. For instance, this one and this one. Apr 14, 2016 at 15:52
• Start with this example rule = {{h___, b[n_], a[m_], t___} -> {h, a[m], b[n], t}, {h___, a[n_], a[m_], t___} /; n < m -> {h, a[m], a[n], 1, t}, {h___, b[n_], b[m_], t___} /; n < m -> {h, b[m], b[n], 1, t}, {h___, 1, a[n_], t___} -> {h, a[n], 1, t}, {h___, 1, b[n_], t___} -> {h, b[n], 1, t}}; {b[0], a[0], a[1], a[0]} //. rule and study this to understand it. Try this on various lists. Then edit your post to show how you used this and examples where more rules are needed.
– Bill
Apr 14, 2016 at 16:01
• Thank you @bill for the generous guide! Apr 14, 2016 at 20:55

You could use NonCommutativeMultiply. For instance:

Unprotect[NonCommutativeMultiply];

o_ ** 1 := o;
1 ** o_ := o;

o_ ** p_Plus := Plus @@ (o ** # & /@ p);
p_Plus ** o_ := Plus @@ (# ** o & /@ p);

o1_ ** Times[k : Except[_a | _b], o2_] := k o1 ** o2;
Times[k : Except[_a | _b], o1_] ** o2_ := k o1 ** o2;

o1_b ** o2_a := o2 ** o1;

MakeBoxes[NonCommutativeMultiply[e1_, e2_], StandardForm] :=
RowBox[{MakeBoxes[e1, StandardForm], MakeBoxes[e2, StandardForm]}];

Protect[NonCommutativeMultiply];

a /: a[1] ** a[0] := a[0] ** a[1] + 1;
b /: b[1] ** b[0] := b[0] ** b[1] + 1;


b[0] ** a[1] ** a[0] ** a[0]
(* 2 (a[0] b[0]) + a[0] a[0] a[1] b[0] *)

b[1] ** b[0] ** a[0] ** a[0]
(* a[0] a[0] + a[0] a[0] b[0] b[1] *)

b[1] ** b[1] ** b[0] ** a[0]
(* 2 (a[0] b[1]) + a[0] b[0] b[1] b[1] *)

a[1] ** a[1] ** a[1] ** a[0]
(* 3 (a[1] a[1]) + a[0] a[1] a[1] a[1] *)

a[1] ** a[1] ** a[0] ** a[0]
% // Expand
(* 2 (a[0] a[1]) + 2 (1 + a[0] a[1]) + a[0] a[0] a[1] a[1] *)
(* 2 + 4 (a[0] a[1]) + a[0] a[0] a[1] a[1] *)

• @SaesunKim Thanks for the accept! :-)
– user31159
Apr 14, 2016 at 21:51