Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying out Mathematica as a replacement for LaTeX for typesetting math. It works very well, but I would like to take advantage of Mathematica's computer algebra capabilities as well. Specifically, instead of manually figuring out and typing the result of a transformation to an expression, I would like Mathematica to do it.

For example, suppose I am trying to prove that associativity of multiplication holds in the complex numbers. The first step might be the following:

(a+bi)((c+di)(e+fi)) = (a+bi)(ce + cfi + edi - df)

How can I automate this using transformation rules? I can do the following:

In:  (a+bi)((c+di)(e+fi)) /. x_*(y_*z_)->x+y+z
Out: a + bi + c + di + e + fi

which works as expected, so the pattern is matching properly (I've tried various other simple transformations as well). However, the following does not work as expected:

In:  (a+bi)((c+di)(e+fi)) /. x_*(y_*z_)->x*(Expand[y*z])
Out: (a+bi)(c+di)(e+fi)

What am I missing? Am I even on the right track, or is there a better way to accomplish this?

share|improve this question
Mathematica already knows a lot about parentheses and multiplication, so I would be very wary of using actual parentheses and multiplication for this project. You're likely to trigger simplifications without realizing it. I would instead specify new functions paren and mult that look like parentheses and multiplication, but will not be treated as such by Mathematica. Then you can build up whatever transformations you want. – Xerxes Feb 26 '13 at 1:24
up vote 2 down vote accepted
(a+bi)((c+di)(e+fi)) /. x_*(y_*z_):>x*(Expand[y*z])
(* (a + bi) (c e + di e + c fi + di fi) *)

Check RuleDelayed in docs

x_*(y_*z_) :> x*(Expand[y*z]) 
x_*(y_*z_) -> x*(Expand[y*z])

(* x_ y_ z_ :> x Expand[y z] *)
(* x_ y_ z_ -> x y z *)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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