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?

  • $\begingroup$ 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. $\endgroup$ – John McGee Aug 8 '14 at 17:07
  • $\begingroup$ @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}$... $\endgroup$ – Phibert Aug 8 '14 at 17:09
  • $\begingroup$ @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. $\endgroup$ – Phibert Aug 8 '14 at 17:16
  • 1
    $\begingroup$ Closely related: 3463, 3822 $\endgroup$ – Michael E2 Aug 8 '14 at 17:58
  • $\begingroup$ @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. $\endgroup$ – 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;


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






1 / B


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.

  • $\begingroup$ There's a mistake in your code. For example rep[2x] returns 2B. $\endgroup$ – Chip Hurst Aug 8 '14 at 20:24
  • $\begingroup$ @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). $\endgroup$ – eldo Aug 8 '14 at 20:33
  • $\begingroup$ @ChipHurst BTW (I just noticed): Thanks for solving my InverseFunction-problem $\endgroup$ – eldo Aug 8 '14 at 22:27
  • $\begingroup$ 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[[0]] @ B is what is causing this. It is too indiscriminate. $\endgroup$ – 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...

  • $\begingroup$ 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$. $\endgroup$ – Kellen Myers Aug 9 '14 at 0:25

How about

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

expr /. f_ /; PossibleZeroQ[poly - f] -> "E"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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