Replacing a sum of expressions

I have an equation which Mathematica solves but gives a huge output. However I notice in this output that the same expression occurs many times. Namely:

$E=864 a^6 - 432 a^4 b^2 + 54 a^2 b^4 - b^6 + 54 b^4 x^2$

where $a,b,c$ are constants. However this expression can appear in the form $-E$, $E^2$, $\sqrt{E}$, $4E$, ...

I'm struggling to tell Mathematica to make the replacement

$(864 a^6 - 432 a^4 b^2 + 54 a^2 b^4 - b^6 + 54 b^4 x^2) \space \to \space E$

wherever it appears.

All the ways I've tried (I'm not that handy with Mathematica I note) don't change output. Any ideas?

• Please provide a short example of an actual expression that your are truing to simplify. Otherwise there is no way for us to tell if our solution will work in your case. – John McGee Aug 8 '14 at 17:07
• @JohnMcGee What do you mean? It's an incredibly long solution to an equation, and the expression in my question, $E$, appears many, many times. But as I say, it could appear as $-E$, $E^{15}$, $E^{1/3}$... – Phibert Aug 8 '14 at 17:09
• @JohnMcGee I mean I could provide a small portion of the output I need simplifying but I can't even follow the brackets and square roots of Mathematica because they're all so nested. – Phibert Aug 8 '14 at 17:16
• Closely related: 3463, 3822 – Michael E2 Aug 8 '14 at 17:58
• @user13223423 You can just copy and paste the expression to your question and we can just copy and paste it back to our notebooks. Use Ctrl+Shift+C and Ctrl+V to preserve space. – seismatica Aug 8 '14 at 18:25

You must carefully define all desired / expected replacement cases. For example:

poly = 864 a^6 - 432 a^4 b^2 + 54 a^2 b^4 - b^6 + 54 b^4 x^2;

Clear[rep]

rep[p_] := p /. p :> p[]@B
rep[Power[p_, n_]] := p /. p :> Power[B, n]
rep[Times[n_, p_]] := p /. p :> n B

rep[poly]

B

rep[Log@poly]

Log[B]

rep[1/poly]

1 / B

rep[poly*2]

2 B

list = {poly, Log@poly, poly^2, 1/poly};

rep /@ list

{B, Log[B], B^2, 1/B}

I feel there should be easier solutions using the Hold..., Unevaluated, Inactive family. However, I can't find them.

• There's a mistake in your code. For example rep[2x] returns 2B. – Chip Hurst Aug 8 '14 at 20:24
• @ChipHurst Thanks for your attention. I wouldn't call it a mistake. As a matter of fact, 2 x should give 2 B. Your spaceless 2x doesn't exist in MMA (except as a String). – eldo Aug 8 '14 at 20:33
• @ChipHurst BTW (I just noticed): Thanks for solving my InverseFunction-problem – eldo Aug 8 '14 at 22:27
• This doesn't work for a lot expressions containing poly. For example, rep[1 + x^2 + (1/poly)^3] returns B. rep[p_] := p /. p :> p[] @ B is what is causing this. It is too indiscriminate. – m_goldberg Aug 9 '14 at 1:49

One (potentially incorrect, but quick and dirty) way to do it is to instead make the substitution

864 a^6 -> E - (−432 a^4 b^2 + 54 a^2 b^4 − b^6 + 54 b^4x^2)

Also, do you really mean E? E in Mathematica is the symbol for the base of the natural logarithm...

• This is what I would do, but only in the case where the entirety of the expression is expected to boil down to some function of $E$ and not include any leftover $a,b,c,d,e$. – Kellen Myers Aug 9 '14 at 0:25