# Substitution inside an expression [duplicate]

I'm trying to get mathematica to replace an expression with a reduced form. I wanted to replace $$(k/w)$$ with $$h$$ in a particular expression, and I've tried something like:

form = (3 k)/w + (k^3 (1 + w)^3)/w^3 + (3 k^2 (1 + 2 w))/w^2;

y = FullSimplify[Collect[form /. k/w -> h, h]]
test = Collect[y, k]


When I do this, I get a partially reduced expression 'y'. If we look at 'test', output is

$$3h + \frac{k^3(1 + w)^3}{w^3} + \frac{3k^2(1+2w)}{w^2}$$

I would have thought Mathematica would have recast this as

$$3h + h^3(1 + w)^3+ 3h^2(1+2w)$$

but I'm having no joy. Any ideas how I can recast my equation in terms of $$h$$?

• How about k -> w*h? There are similar questions about using ReplaceAll to perform algebraic substitutions. But ReplaceAll replaces only matching expressions, and does no algebraic manipulation (like humans do when doing algebra). Feb 27 '19 at 12:56
• There's a list of related questions in one of the answers to mathematica.stackexchange.com/questions/3822/… Feb 27 '19 at 13:01

y = FullSimplify[Collect[form /. k -> h w, h]];

3*h + h^3*(1 + w)^3 + 3*h^2*(1 + 2*w)