Is there a better way to replace a variable, not powers of it, than first to replace all unwanted terms by dummy terms and replacing them back later?
Input: expr = x + x^2 + x^3
Expected output: y + x^2 + x^3
Replace Replace Replace: expr /. Power[x_, n_] :> z^n /. {x -> y} /. {z -> x}
Edit:
It should also work on this:
Input: expr = a x + b x^2 +c x^3
Expected output: a y +b x^2 +c x^3
CoefficientRules
andFromCoefficientRules
or some other combination that utilizes the actual semantics of polynomials.$\endgroup$expr - x + y
?$\endgroup$ReplaceAll
will replace a subexpression only once in a single pass through, you can do:x + x^2 + x^3 /. {x^n_ :> x^n, x -> y}
. I'm sure there is a duplicate somewhere.$\endgroup$expr/. x^n_. :> If[n == 1, y, x^n]
$\endgroup$expr - a x + a y
$\endgroup$