17
$\begingroup$

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?

$\endgroup$
2
  • 4
    $\begingroup$ Maybe Distribute[(a + b) ** (a + b) ] ? $\endgroup$ Commented Apr 10, 2013 at 17:02
  • 2
    $\begingroup$ Look here for some more thoughts about non commutative multiplication and more ... $\endgroup$ Commented Apr 10, 2013 at 18:08

3 Answers 3

14
$\begingroup$

Distribute[] is a useful thing:

Distribute[(a + b) ** (c + d)]
   a ** c + a ** d + b ** c + b ** d
$\endgroup$
8
  • 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$ Commented 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
6
$\begingroup$

The package NCAlgebra does exactly what you want.

NCExpand[(a + b) ** (a + b)]
(* a ** a + a ** b + b ** a + b ** b *)
$\endgroup$
1
  • $\begingroup$ @MarioKrenn please see a response to the issue you created. slim is an interesting choice of word for a 200 page document. $\endgroup$ Commented Jan 2, 2025 at 20:05
1
$\begingroup$

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 *)
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.