0

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

Michael E2
  • 235,386
  • 17
  • 334
  • 747
DRG
  • 877
  • 7
  • 16
  • 1
    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). – Michael E2 Feb 27 '19 at 12:56
  • There's a list of related questions in one of the answers to https://mathematica.stackexchange.com/questions/3822/can-i-simplify-an-expression-into-form-which-uses-my-own-definitions – Michael E2 Feb 27 '19 at 13:01

1 Answers1

1

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.

Somos
  • 4,897
  • 1
  • 9
  • 15