# Expand and Simplify do not work for NonCommutativeMultiply

Expand and Simplify do not work for NonCommutativeMultiply[] then how do we expand an expression like

(a+b)**(a-b) ?

There is more than one way to do this in Mathematica. I prefer a simple approach as in the following code:

Unprotect[NonCommutativeMultiply];
a___ ** (b_ + c_) ** d___ := a ** b ** d + a ** c ** d;
a___ ** (n_?NumericQ b_) ** c___ := n (a ** b ** c);
a__ ** (n_?NumericQ) := n a; (n_?NumericQ) ** a__ := n a;


which should do what you expect. For exmaple:

(a + b) ** (a - b) == a ** a - a ** b + b ** a - b ** b


evaluates to True. The last line of code allows the code

x ** 0 == 0 == 0 ** x && x ** -1 == -x == -1 ** x


to evaluate to True. Note that this solution automatically expands expressions containing **. If you don't want this to happen, then an alternative way to do this is to change the := to :> instead and make them into a list of rules.

Up to the documentation to **, "Expand and Simplify do not operate on expressions with NonCommutativeMultiply". According to this documentation, the following works.

ClearAll["Global*"];ExpandNCM[(h : NonCommutativeMultiply)[a___, b_Plus, c___]] :=
Distribute[h[a, b, c], Plus, h, Plus, ExpandNCM[h[##]] &];
ExpandNCM[a_] := ExpandAll[a]; ExpandNCM[(a + b) ** (c + d)]


• Thanks. Why does it give a ** a+a ** (-b)+a ** (b)+b ** (-b) as output? can we not bring - outside? Jun 8 '19 at 11:13
• Though the edit was for better understanding, and it does not change my question. I do not care for -1. Please solve the query, as I requested in the comment. No hard feelings. Jun 8 '19 at 11:53
• As far as I understand it, $a-b$ is treated as $a+(-b)$: a - b == a + (-b) performs True. Jun 8 '19 at 13:14

Maybe you could use TensorProduct instead of NonCommutativeMultiply?

TensorExpand @ TensorProduct[a+b, a-b]

a\[TensorProduct]a - a\[TensorProduct]b + b\[TensorProduct]a -
b\[TensorProduct]b
`

or as an image:

• Seems like a nice idea. Though I hope it does not get caught in problem with some other manipulations. Jun 9 '19 at 11:56