# Simplifying with noncommutative multiplication

I am trying to do some quantum algebra computations in Mathematica. Up to now I was able to make the noncommutative multiplication linear and to factorize powers. However, I still have some issues with associativity. In particular, I would like to be able to remove brackets expressions like a ** ( b ** c ) and get a ** b ** c. The reason for that is that I already have a procedure that substitutes a^2 ** b to a ** a ** b, but this doesn't work when I have brackets (a ** ( a ** b )).
Does anyone know how to overcome this problem?

-
Could you provide your procedure? Why do you use noncommutative multiplication ** only in the last formula? –  ybeltukov Sep 24 '13 at 15:15
Take a look at the NCAlgebra package, like here –  Szabolcs Sep 24 '13 at 15:19
I am sorry, that was a typo... I just corrected everything, it's noncommutative multiplication all along. –  user37905 Sep 29 '13 at 16:39
@user37905 Maybe there is * in the last formula? Otherwise I don't understand because a ** (a ** b) automatically converts to a ** a ** b. –  ybeltukov Sep 29 '13 at 16:52

The problem (if I understand correctly) is that you want to make the following replacements:

a * (a * c)  ==> a * a * c   ==> a^2 * c
a * (a ** c) ==> a * a ** c  ==> a^2 ** c


The trick is that in the second line, a * a ** c is parenthesised as a * (a ** c), where a * a is not recognized as a^2. Use the following rule:

rep = (x_*(x_ ** y__) :> x^2 ** y);

a*(a ** c)                 (* ==>   a a ** c)        *)
a*(a ** c ** d) /. rep     (* ==>   a^2 ** c ** d    *)
a*(b ** a) /. rep          (* ==>   a b ** a     NC-ity is maintained    *)

-