Skip to main content
edited tags
Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
added 16 characters in body
Source Link
eldo
  • 83.2k
  • 6
  • 72
  • 202

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!

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?

Possible way out

One possible way out can be 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!

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!

a possible way out
Source Link
Sumit
  • 16k
  • 2
  • 33
  • 74

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?

Possible way out

One possible way out can be 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!

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?

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?

Possible way out

One possible way out can be 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!

Clarified title
Link
bobthechemist
  • 19.8k
  • 4
  • 56
  • 140
Loading
Removed the bugs tag
Source Link
Öskå
  • 8.6k
  • 4
  • 32
  • 50
Loading
Source Link
Sumit
  • 16k
  • 2
  • 33
  • 74
Loading