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?
(x + Log[y*z])/(y*z) /. {z y -> w, 1/(z y) -> w}
That is rewrite they*z
tow
first and then rewrite the1/(y*z)
. Basically you need to remember that Mathematica likes to treat all divisions as multiplications by the inverse. $\endgroup$