# grouping common powers of Bose operators

I compute a product of Bose operators and turn it into normal ordering using Boson commutation relations, e.g:

c1 * SuperDagger[a] ** SuperDagger[a] ** a ** a +
c2 * SuperDagger[a] ** SuperDagger[a] ** SuperDagger[a] ** a ** a ** a


and generally more terms. In the above c1, c2  are scalars while a, SuperDagger[a] are the Bose operators.

/.NonCommutativeMultiply[a___] :> Infix[NonCommutativeMultiply[a],"\[InvisibleTimes]"]


at the end of the evaluation command, which makes the ** symbol invisible (output is not displayed correctly here, sorry).

I would like to know whether I can make such expressions more readable by having in the output the various terms in the form SuperDagger[a]^3 a^3 and NOT like SuperDagger[a] SuperDagger[a] SuperDagger[a] a a a

Also how can I isolate specific powers e.g SuperDagger[a]^2 a^2 in a long expression involving many different powers of SuperDagger[a]^n a^n and get their display only, e.g:

(3 * c1 + 5 * c0 + 7 * c6) SuperDagger[a] ** SuperDagger[a] ** a ** a


Collect doesn't do the trick.

• True, I will change it to the original output.
– geom
Dec 5 '20 at 21:18

Would this work?

expr = c1*SuperDagger[a] ** SuperDagger[a] ** a ** a +
c2*SuperDagger[a] ** SuperDagger[a] ** SuperDagger[a] ** a ** a ** a

ClearAll[collectPowers]
collectPowers[expr_] :=
ReplaceRepeated[
expr,
{NonCommutativeMultiply[x_, x_] :> x^2,
NonCommutativeMultiply[Power[x_, i_], x_] :> Power[x, i + 1]}
]

collectPowers[expr]

(* Out: c1 (SuperDagger[a])^2 ** a^2 + c2 (SuperDagger[a])^3 ** a^3 *)

• It is useful, but in longer expressions where you have more coefficients it does not produce e.g (c1 + 2 c2 + 5 c3^3) (SuperDagger[a])^2 ** a^2. It produces each term separately and you have to track all terms one by one.
– geom
Dec 6 '20 at 12:46