I began to use Mathematica a few days ago. My problem is: how do I expand expressions like $(a+b)\ast(a+b)$, where the multiplication is noncommutative? Can Mathematica do this?
2 Answers
$\begingroup$
$\endgroup$
8
Distribute[]
is a useful thing:
Distribute[(a + b) ** (c + d)]
a ** c + a ** d + b ** c + b ** d
-
2$\begingroup$ Although this works on the example used by jon, it doesn't really answer the question satisfactorily (although the question was vague). For instance, it does not work on
Distribute[a.(c + d)/2]
. $\endgroup$ Commented Feb 29, 2020 at 13:55 -
$\begingroup$ @Jess, yes, that case is a little problematic.OTOH, a rearrangement of that expression, along with using the second and third arguments of
Distribute[]
succeeds:Distribute[(a/2).(c + d), Plus, Dot]
$\endgroup$ Commented Mar 2, 2020 at 5:38 -
2$\begingroup$ I mean, ok, but the whole point of this is to avoid re-arranging the expressions by hand, because the cases when you really want to use this are when you have 30 terms. $\endgroup$ Commented Mar 2, 2020 at 11:32
-
$\begingroup$ It doesn't work for expression with sum. For example
Distribute[(a + b) ** (c + d) + a ** (b + d)]
$\endgroup$– ayrCommented May 19, 2022 at 4:18 -
1$\begingroup$ @dtn, indeed, so one has to use
/.
in such cases, e.g.(a + b) ** (c + d) + a ** (b + d) /. nc_NonCommutativeMultiply :> Distribute[nc]
. $\endgroup$ Commented May 19, 2022 at 12:04
$\begingroup$
$\endgroup$
The package NCAlgebra does exactly what you want.
NCExpand[(a + b) ** (a + b)]
(* a ** a + a ** b + b ** a + b ** b *)
Distribute[(a + b) ** (a + b) ]
? $\endgroup$