MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How does a rule get applied?




Yacine Ait-Sahalia <fyaitsah@gsbux1.uchicago.edu> wrote in article
<6jf2mg$3vl@smc.vnet.net>...
> Hi,
> 
> I'm having trouble understanding how rules are applied after /. Consider
> the following example:
>           x^a + x^(1+a)  /. x^(n_) + x^(n_+1) ->0
>           x^3 + x^(1+3)  /. x^(n_) + x^(n_+1) ->0
>           x^3            /. x^(n_) ->0
> 
> Out[388]= 
>                 0
> 
> Out[389]=
>                  3    4
>                 x  + x
> 
> Out[390]=
>                 0
> 
> 
> Could anyone please explain to me why 
> 
>         x^a + x^(1+a)  /. x^(n_) + x^(n_+1) ->0 
> 
> returns 0 (as expected), whereas
> 
>         x^3 + x^(1+3)  /. x^(n_) + x^(n_+1) ->0
> 
> returns
> 
>                  3    4
>                 x  + x 
> 
> instead of 0?
> 
> Thanks for your help.   
> 
> 

I'm fighting a similar problem.
Unfortunately I've come up with an answer but no solution:

The key is that you should view the "full form" of your rule.

FullForm[x^(n_) + x^(n_+1) ->0]
	Rule[Plus[Power[x, Pattern[n, Blank[]]], 
		   Power[x, Plus[1, Pattern[n, Blank[]]]]], 0]

This is what Mathematica tries to match, using not too much more than
simple pattern matching.

Then you try this:

FullForm[x^a + x^(1+a)]
	Plus[Power[x, a], Power[x, Plus[1, a]]]

This does match the given rule, so the replacement takes place and you
get "0". OK!

But this does not work:

FullForm[x^3 + x^(1+3)]
	Plus[Power[x, 3], Power[x, 4]]

The "1+3" gets evaluates. The resulting subexpression
	Power[x, 4]
	             ^^^
does no longer match the required
	Power[x, Plus[1, a]]]
	              ^^^^^^^^^^^ !!

I get the same problem with

	f[x] + f[-x]     /.   f[-x_] -> -f[x]

which gives 0, OK,
but this fails for the same reason (compare FullForms!):
	f[2] + f[-2x]     /.   f[-x_] -> -f[x]


-Kay
(Kay-Uwe.Kasemir@Physik.Uni-Osnabrueck.DE)



  • Prev by Date: help about klotoid-integral-solution
  • Next by Date: Re: Basic Statistics in Mathematica 3.0
  • Prev by thread: How does a rule get applied?
  • Next by thread: Re: How does a rule get applied?