Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 have several equations with many variables, and here and there I would like to do a variable substitution. For example, in the equation:

a (b + 42 c / d)

I might want to remap variables a * b --> p and a * c / d --> q in order to get:

p + 42 q 

Can Mathematica do this? I tried to play with the With[] and Rule[] function and the like, but that doesn't seem to produce anything. I can imagine that Mathematica is not capable of doing such a thing, but I thought it would be worth asking.

And the pro question: And then very much related to this: is there a way to use With[] not to set a single variable but to set variable relations, e.g. the sum or product of two variables. So that this:

With[{a * b = 5},  a (b + 42 c / d)] (* This doesn't work*)

would reduce to:

5 + 42 a * c / d
share|improve this question
up vote 2 down vote accepted
rules = {a b -> p, a c / d -> q}
Expand[a (b + 42 c/d)] /. rules
(*p + 42  q*)
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.