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:

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)]


  • $\begingroup$ Thanks. Why does it give a ** a+a ** (-b)+a ** (b)+b ** (-b) as output? can we not bring - outside? $\endgroup$ – Chetan Waghela Jun 8 at 11:13
  • $\begingroup$ @Chetan Waghela: What was asked,that was answered. -1 for your edit of your question after my answer.. $\endgroup$ – user64494 Jun 8 at 11:20
  • $\begingroup$ 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. $\endgroup$ – Chetan Waghela Jun 8 at 11:53
  • $\begingroup$ As far as I understand it, $a-b$ is treated as $a+(-b)$: a - b == a + (-b) performs True. $\endgroup$ – user64494 Jun 8 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 - 

or as an image:

enter image description here

  • $\begingroup$ Seems like a nice idea. Though I hope it does not get caught in problem with some other manipulations. $\endgroup$ – Chetan Waghela Jun 9 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.