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?
3 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$Jess Riedel– Jess Riedel2020-02-29 13:55:24 +00:00Commented 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$J. M.'s missing motivation– J. M.'s missing motivation2020-03-02 05:38:20 +00:00Commented 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$Jess Riedel– Jess Riedel2020-03-02 11:32:47 +00:00Commented 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$Andy Ayr– Andy Ayr2022-05-19 04:18:11 +00:00Commented 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$J. M.'s missing motivation– J. M.'s missing motivation2022-05-19 12:04:17 +00:00Commented May 19, 2022 at 12:04
$\begingroup$
$\endgroup$
1
The package NCAlgebra does exactly what you want.
NCExpand[(a + b) ** (a + b)]
(* a ** a + a ** b + b ** a + b ** b *)
-
$\begingroup$ @MarioKrenn please see a response to the issue you created. slim is an interesting choice of word for a 200 page document. $\endgroup$Mauricio de Oliveira– Mauricio de Oliveira2025-01-02 20:05:48 +00:00Commented Jan 2, 2025 at 20:05
$\begingroup$
$\endgroup$
Version 14.3 introduced a new set of functionalities for calculations in noncommutative algebra.
For your case, you can use NonCommutativeExpand:
NonCommutativeExpand[(a + b) ** (c + d)]
(* a**c + a**d + b**c + b**d *)
Distribute[(a + b) ** (a + b) ]? $\endgroup$