2
$\begingroup$

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

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

$\endgroup$

3 Answers 3

3
$\begingroup$

There is more than one way to do this. I prefer a simple approach such as:

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 example:

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

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

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

to evaluate to True also.

Note that this method 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.

$\endgroup$
1
$\begingroup$

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

ac+ad+bc+bd

$\endgroup$
4
  • $\begingroup$ Thanks. Why does it give a ** a+a ** (-b)+a ** (b)+b ** (-b) as output? can we not bring - outside? $\endgroup$ Commented Jun 8, 2019 at 11:13
  • $\begingroup$ @Chetan Waghela: What was asked,that was answered. -1 for your edit of your question after my answer.. $\endgroup$
    – user64494
    Commented Jun 8, 2019 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$ Commented Jun 8, 2019 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
    Commented Jun 8, 2019 at 13:14
1
$\begingroup$

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:

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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