Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

1 Answer 1

rules = {a b -> p, a c / d -> q}
Expand[a (b + 42 c/d)] /. rules
(*p + 42  q*)
share|improve this answer

Your Answer

 
discard

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.