Re: Subject: Re: Rational[a,b] vs Rational[1,2]
- To: mathgroup at smc.vnet.net
- Subject: [mg117833] Re: Subject: Re: Rational[a,b] vs Rational[1,2]
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Fri, 1 Apr 2011 02:33:33 -0500 (EST)
Peter Pein wrote: > Am 25.02.2011 12:34, schrieb Jack L Goldberg 1: >> I am a bit puzzled. In view of the on-going debate, when ought one >> use Rational[a,b]. Put a bit differently, is there a snippet of code >> in which one would prefer Rational[a,b] to a/b? >> > > well, this is constructed but could happen in praxis: > > In[1]:= ser=Normal[Series[(Sqrt[1+x]+Sqrt[x])^2,{x,0,5}]] > Out[1]= 1+2 Sqrt[x]+2 x+x^(3/2)-x^(5/2)/4+x^(7/2)/8-(5 x^(9/2))/64 > > In[2]:= Cases[ser,Except[_.*Power[x,Rational[_,2]]],1] > Out[2]= {1,2 x} > > In[3]:= Cases[ser,Except[_.*Power[x,_ /2]],1] > Out[3]= {1,2 Sqrt[x],2 x,x^(3/2),-(x^(5/2)/4),x^(7/2)/8, > -((5 x^(9/2))/64)} > > this happens, because the internal representation of ser is: > > In[4]:= FullForm @ ser > Out[4]//FullForm= Plus[1, Times[2, Power[x, Rational[1, 2]]], Times[2, > x], Power[x, Rational[3, 2]], Times[Rational[-1, 4], Power[x, > Rational[5, 2]]], Times[Rational[1, 8], Power[x, Rational[7, 2]]], > Times[Rational[-5, 64], Power[x, Rational[9, 2]]]] > > So it can be (and mostly is) essential in pattern matching. > > Peter [Sorry to co-opt this idea, but what I show below is quite relevant to the post that began this thread, even if not applicable to the response above. -DL] A similar example illustrates a related concept, one that I've not managed to get across in many attempts. We start with that series. In[1]:= ser = Series[(Sqrt[1 + x] + Sqrt[x])^2, {x, 0, 5}] 5/2 7/2 9/2 3/2 x x 5 x 11/2 Out[1]= 1 + 2 Sqrt[x] + 2 x + x - ---- + ---- - ------ + O[x] 4 8 64 Now we use a simple replacement rule, close to the one that worked for you above, attempt to change all those fractional powers to their negatives. In[2]:= ser2 = ser /. x^Rational[a_, 2] :> x^-a/2 5/2 7/2 9/2 3/2 x x 5 x 11/2 Out[2]= 1 + 2 Sqrt[x] + 2 x + x - ---- + ---- - ------ + O[x] 4 8 64 Didn't work. Nor should it, because the visual OutputForm or StandardForm representation is not so closely related to the internal form: In[3]:= InputForm[ser] Out[3]//InputForm= SeriesData[x, 0, {1, 2, 2, 1, 0, -1/4, 0, 1/8, 0, -5/64}, 0, 11, 2] Is this a bug? No. Is it bad design? No. (Why not? Because the software requires a useful internal form. And people who use the software want a visual form closer to what would be seen in a math book. The most sensible choice, in this case, is to accomodate both.) I show this example because the original post in this thread, as best I recall, would strongly imply that the behavior above IS a bug. Daniel Lichtblau Wolfram Research