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$?

  • 1
    $\begingroup$ 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). $\endgroup$
    – Michael E2
    Feb 27 '19 at 12:56
  • $\begingroup$ There's a list of related questions in one of the answers to mathematica.stackexchange.com/questions/3822/… $\endgroup$
    – Michael E2
    Feb 27 '19 at 13:01

This code will do what you want:

y = FullSimplify[Collect[form /. k -> h w, h]];
(test = Map[Factor, Collect[y, h], {2}]) // InputForm

which retuns

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

There are probably several other ways to do what you want, but this seems to me to be the simplest most direct way.


Not the answer you're looking for? Browse other questions tagged or ask your own question.