Replacing composite variables by a single variable

You can't use replacements that way, because Mathematica does not do replacements on expressions the way they appear to you. To see what I mean, take a look at the FullForm of your expression:

x/(y*z) // FullForm
Out[1]= Times[x,Power[y,-1],Power[z,-1]]

Whereas, the replacement that you're using is Times[y, z].

In general, it is not a good idea to use approaches that exploit the structure of expressions to do mathematical replacements. You might think you have nailed the replacement down, but it will break for a slightly different equation or terms.

To do this in a fail safe manner, you can use Simplify as:

Simplify[x/(y z), w == y z]
Out[2]= x/w

For more complicated examples, you might have to use Eliminate. From the documentation:

Eliminate[{f == x^5 + y^5, a == x + y, b == x y}, {x, y}]
Out[3] = f == a^5 - 5 a^3 b + 5 a b^2

Also read the tutorial on eliminating variables.


Since nobody pointed this out I think there is still room for another reply. Note that this works fine

Unevaluated[(x + Log[y*z])/(y*z)] /. (y*z) :> w
(x + Log[w])/w

In more complex cases you may also need to use HoldPattern

Unevaluated[(x + Log[(y*z)/2])/((y*z)/2)] /. HoldPattern[((y*z)/2)] :> w

(x + Log[w])/w

This is not a panacea. Mathematica's pattern matching is purely syntactical so for more complicated replacement you need to use more algebraic functions. The key one is PolynomialReduce. This is what Simplify uses, but using Simplify for replacements is not a good idea in general since you can't readily predict the result (it depends on the setting of the option ComplexityFunction and others). There is a great deal about this in the MathGroup archives, particularly in posts by Daniel Lichtblau and a few of my own.

You will find a discussion and some useful links here.


This stumped me for a few moments until I looked at the FullForm of your expression.

x/(y*z) // FullForm

yields

Times[x,Power[y,-1],Power[z,-1]]

Notice here that the variables are rewritten internally by Mathematica to read as

x*(1/y)*(1/z)

With this knowledge in hand we can now write a working replacement rule.

x/(y*z) /. 1/(y*z) -> w

And this will yield the expected result of

x w.

For mathematical expressions one ought to use Simplify.

Simplify[(x + Log[y*z])/(y*z),w==y*z]

which gives

(x+Log[w])/w