# How to find noncommutative product of two expression

I have the following function $$B(n)=x \alpha ^n-y \alpha ^n q^n + \epsilon \left(x \alpha ^{n+1}-y \alpha ^{n+1} q^{n+1}\right).$$ Here, I want to find the product $$B(n)^2$$ or $$B(n)*B(n+1)$$ where the variables $$x$$ and $$y$$ are non-commutative i.e. $$xy\neq yx$$. Is there any way to achieve this in Mathematica ?

For example, when I try to find the product in Mathematica, I find that

$$B(n)^2 = -2 x y \epsilon ^2 \alpha ^{2 n+2} q^{n+1}-2 x y \epsilon \alpha ^{2 n+1} q^{n+1}+y^2 \epsilon ^2 \alpha ^{2 n+2} q^{2 n+2}+2 y^2 \epsilon \alpha ^{2 n+1} q^{2 n+1}+y^2 \alpha ^{2 n} q^{2 n}-2 x y \epsilon \alpha ^{2 n+1} q^n-2 x y \alpha ^{2 n} q^n+x^2 \epsilon ^2 \alpha ^{2 n+2}+2 x^2 \epsilon \alpha ^{2 n+1}+x^2 \alpha ^{2 n}$$ and $$B(n)*B(n+1) = -x y \epsilon ^2 \alpha ^{2 n+3} q^{n+1}-x y \epsilon ^2 \alpha ^{2 n+3} q^{n+2}-2 x y \epsilon \alpha ^{2 n+2} q^{n+1}-x y \epsilon \alpha ^{2 n+2} q^{n+2}-x y \alpha ^{2 n+1} q^{n+1}+y^2 \epsilon ^2 \alpha ^{2 n+3} q^{2 n+3}+2 y^2 \epsilon \alpha ^{2 n+2} q^{2 n+2}+y^2 \alpha ^{2 n+1} q^{2 n+1}-x y \epsilon \alpha ^{2 n+2} q^n-x y \alpha ^{2 n+1} q^n+x^2 \epsilon ^2 \alpha ^{2 n+3}+2 x^2 \epsilon \alpha ^{2 n+2}+x^2 \alpha ^{2 n+1}.$$ But it must be that $$xy \neq yx$$.

• There is a question similar to my topic that is stackoverflow.com/questions/7320735/…. I want to do it in Mathematica.
– drxy
Commented Feb 4, 2020 at 11:13
• Have you seen NonCommutativeMultiply? Commented Feb 5, 2020 at 5:08
• @CATrevillian; Yes I saw it but I couldn't use for my case. If is it possible, could you please explain how to apply NonCommutativeMultiply to my function $B(n)$?
– drxy
Commented Feb 5, 2020 at 8:29
• Shouldn’t it just be B[n]**B[n+1]? Commented Feb 5, 2020 at 12:16

In the light of Mathematica's NonCommutativeMultiply, I have solved the problem as follows:

B[n_] := (a ** x + b ** y) + (b ** x + a ** y ** q^n);

ExpandNCM[(h : NonCommutativeMultiply)[a___, b_Plus, c___]] :=
Distribute[h[a, b, c], Plus, h, Plus, ExpandNCM[h[##]] &]

ExpandNCM[(h : NonCommutativeMultiply)[a___, b_Times, c___]] :=
Most[b] ExpandNCM[h[a, Last[b], c]]

ExpandNCM[a_] := ExpandAll[a]

B[n]**B[n + 1] // ExpandNCM // ExpandAll

= a ** x ** a ** x + a ** x ** b ** x + a ** x ** b ** y +
b ** x ** a ** x + b ** x ** b ** x + b ** x ** b ** y +
b ** y ** a ** x + b ** y ** b ** x + b ** y ** b ** y +
a ** x ** a ** y ** q^(1 + n) + a ** y ** q^n ** a ** x +
a ** y ** q^n ** b ** x + a ** y ** q^n ** b ** y +
b ** x ** a ** y ** q^(1 + n) + b ** y ** a ** y ** q^(1 + n) +
a ** y ** q^n ** a ** y ** q^(1 + n)


The only problem is that you must simplify some expressions with manually.

Thank you @CATrevillian.

• Nice job! I was gonna try to comment on a cool extension wherein you could use TagSet or the-like to define the behavior of Power in the case of this type of NonCommutativeMultiply, but alas, I couldn't make the extension function properly. This is awesome! Great work here, drxy. Commented Feb 5, 2020 at 14:24
• Dear @CATrevillian; I have seen before NonCommutativeMultiply. But my inspiration came from after reading your post. So thank you very much.
– drxy
Commented Feb 5, 2020 at 16:54
• your constructs are quite advanced, for me at least, so I thank you for this in return! If I might ask for clarification, can you explain the pieces of your code, how they work/your choices in how you formatted them? Commented Feb 5, 2020 at 20:12
• @CATrevillian; I have edited the answer. For the sake of simplicity, I define different $B(n)$ but I think that this code is more clear than previous one.
– drxy
Commented Feb 6, 2020 at 7:21