2

I have the following expression:

A = (-(e + p) pe v + 
   v ((1 + pe) u^2 + pe v^2) \[Rho])/(-(e + p) pe u + 
   u (-prho + (1 + pe) u^2 + pe v^2) \[Rho]);

and I want to insert the following sub-expression in the above expression:

H = (e + p)/\[Rho]

How do I do this? I've tried using:

FullSimplify[A, H == (e + p)/\[Rho]]
Eliminate[{A, H = (e + p)/\[Rho]}, {e, p, \[Rho]}]
Eliminate[{A, H = (e + p)/\[Rho]}, {e, p}]

and a number of other solutions. None have worked.

I'm also a little confused on the difference between "=" and "==", but I've tried both cases.

tshepang
  • 12,111
  • 21
  • 91
  • 136
john
  • 41
  • 1
  • 7
  • Forget Mathematica for a moment and explain in really simple terms what "insert the following sub-expression into the above expression" means. Do you mean "find every string of characters "(e+p)/\[Rho]" and replace them with the string "H"? (I don't see any example of that string of characters in there.) Now, "left=right" means "calculate the value of right and store it in a box named left. And "left==right" means calculate the value of left and the value of right. If those are exactly the same then the result is True, otherwise it is False. (It is more complicated, but that is a good start) – Bill Jan 22 '14 at 19:43
  • Thanks, Bill. If you notice, (e+p) occurs twice throughout the expression, and multiplying the expression by rho/rho results in two occurrences of (e+p)/rho. I would like Mathematica to recognize this, and then replace (e+p)/rho with H. I do understand (on a basic level) left=right (define right as variable left) and left==right (check if they are equal, 1 or 0) from other programming. Where I'm stuck is when to use "=" or "==" in the FullSimplify or Eliminate cases I noted above. I suppose from your explanation, "=" would be correct, and "==" would not be. – john Jan 22 '14 at 20:17

1 Answers1

0

This brilliant answer from Rojo does the trick :-

Clear[pe, e, v, u, prho, p, \[Rho], H, A];

A = (-(e + p) pe v + v ((1 + pe) u^2 + pe v^2) \[Rho])/
   (-(e + p) pe u + u (-prho + (1 + pe) u^2 + pe v^2) \[Rho])

enter image description here

doThat[expr_, vars_List] := Expand[Simplify[expr /. Flatten[
      Solve[# == ToString@#, First@Variables@#] & /@ vars]],
   Alternatives @@ ToString /@ vars] /. Thread[ToString /@ vars -> vars];

done = doThat[A, {(e + p)/\[Rho]}];

ans = Simplify[done //. (e + p)/\[Rho] -> H]

The above yields the required algebraic expression,

enter image description here

Now checking :-

H = (e + p)/\[Rho];

FullSimplify[A] == FullSimplify[ans]

is True.

Also testing with various values :-

pe = 2; e = 3; v = 4; u = 5; prho = 6; p = 7; \[Rho] = 8;

A

836/985

H = (e + p)/\[Rho]

5/4

Clear[e, p, \[Rho]]

ans

836/985

So same answers. Seems to work amazingly well.

Community
  • 1
  • 1
Chris Degnen
  • 8,443
  • 2
  • 23
  • 40