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?
Answer
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[[0]]@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.
Comments
Post a Comment