28

12

To replace a single variable by another variable, one can simply use the the replace all (`/.`

) operator (e.g., `x/(y*z) /. x -> w`

returns $\displaystyle \frac{w}{yz}$).

How does one replace an expression consisting of multiple variables? Trying to replace the denominator in the previous expression by a single variable fails with the following syntax:

```
x/(y*z) /. y*z -> w
x/(y*z) /. y*z :> w
x/(y*z) /. (y*z) -> w
x/(y*z) /. (y*z) :> w
x/(y*z) /. Times[y, z] -> w
x/(y*z) /. Times[y, z] :> w
```

**Edit:** By applying `FullForm`

, I see that the variable substitution can be made by the following lengthy expression:

```
x/(y*z) /. Times[Power[y, -1], Power[z, -1]] -> w^-1
```

However, this now fails in a case such as the following:

```
(x + Log[y*z])/(y*z) /. Times[Power[y, -1], Power[z, -1]] -> w^-1
```

Now one must use something like the following (which does not work).

```
(x + Log[y*z])/(y*z) /. {Times[Power[y, -1], Power[z, -1]] -> w^-1, Times[y, z] -> w}
```

Is there a more general way to replace variables with delving into the full form representation?

Becoming an FAQ hereabouts. Might have a look at this or that or the other.

– Daniel Lichtblau – 2012-05-10T14:31:58.003The inverse operation can work, that is replacing $\frac{x}{w}$ to $\frac{x}{y*z}$. There must be a way to do it. – CHM – 2012-03-25T03:43:31.630

1Well in your example it does not work as

Mathematicais rewriting the whole term inside the log expression as well. Something like this though will do the correct behavior:`(x + Log[y*z])/(y*z) /. {z y -> w, 1/(z y) -> w}`

That is rewrite the`y*z`

to`w`

first and then rewrite the`1/(y*z)`

. Basically you need to remember thatMathematicalikes to treat all divisions as multiplications by the inverse. – nixeagle – 2012-03-25T03:54:02.007