Consider this given example for ReplaceAll
1 + x^2 + x^4 /. x^p_ -> f[p]
This returns 1+f[2]+f[4]
- works good (!). But if you choose
In[175]:= 1 +x+ x^2 + x^4 /. x^p_ -> f[p]
Out[175]= 1+x+f[2]+f[4]
It doesn't not work for 1
and x
. The correct output should be f[0]+f[1]+f[2]+f[4]
.
What is wrong here and to cure it?
EDIT
Possible way out
One possible way out can be to not use ReplaceAll
(inspired by @eldo). The coefficient and the power of x
can be combined in this way:
fun[x] = a - b x + c x^2 + d x^4
nfun = Exponent[fun[x], x];
Sum[Coefficient[fun[x], x, i] f[i], {i, 0, nfun}]
And the result is a f[0] - b f[1] + c f[2] + d f[4]
.
However the replace rule for $x^0$ is still a mystery!