15
$\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$
    – Spawn1701D
    Commented Apr 10, 2013 at 18:08

2 Answers 2

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$
    – ayr
    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
7
$\begingroup$

The package NCAlgebra does exactly what you want.

NCExpand[(a + b) ** (a + b)]
(* a ** a + a ** b + b ** a + b ** b *)
$\endgroup$

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.