Re: RE:? D[f,{x,n}]

*To*: mathgroup at smc.vnet.net*Subject*: [mg25576] Re: [mg25559] RE:[mg25495] ? D[f,{x,n}]*From*: Andrzej Kozlowski <andrzej at tuins.ac.jp>*Date*: Mon, 9 Oct 2000 01:16:41 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

I must make a correction to my earlier message (this is also related, I think, to Allan Hayes's latest message in this thread). First of all, contrary to what I wrote, Derivative[n][f][x] is computed by nesting Derivative[1]'s in Mathematica 4.0. Look at the following example: In[4]:= f /: f' = g Out[4]= g In[5]:= f''[x] Out[5]= g'[x] In fact this was not the case in Mathematica 3.0 (but was true in earlier versions of Mathematica). I remembered reading about this fact in Roman Maeder's "Programming in Mathematica", Third edition (see p. 225) and assumed it must still be the case. Clearly the original behaviour was considered desirable and restored in v.4. However, what is relevant to the example below is this interesting behaviour: In[1]:= flag = True; In[2]:= f'/;flag=g Out[2]= g In[3]:= f'[x] Out[3]= g[x] In[4]:= f''[x] Out[4]= f''[x] In other words, when f'' is evaluated, Derivative[1] is nested but the conditional value of f' is not used. Andrzej -- Andrzej Kozlowski Toyama International University, JAPAN For Mathematica related links and resources try: <http://www.sstreams.com/Mathematica/> on 00.10.7 11:37 PM, Andrzej Kozlowski at andrzej at tuins.ac.jp wrote: > on 10/7/00 4:36 PM, Ersek, Ted R at ErsekTR at navair.navy.mil wrote: > >> seems to prove that D[f, {x, n}] evaluates >> Nest[ D[#, x]&, f, n ] >> or something equivalent. >> ----------------------------- > > I think the point is (as already has been pointed out by Carl Woll) that: > > In[22]:= > MatchQ[Unevaluated[D[f, {x, n}]], Unevaluated[D[f_, y_]]] > Out[22]= > True > > We can see what happens in your case simply by looking at Trace: > > In[28]:= > Trace[D[(x + 2)^4, {x, 2}]] > > Out[28]= > {{{HoldForm[x + 2], HoldForm[2 + x]}, HoldForm[(2 + x)^4]}, HoldForm[D[(2 + > x)^4, {x, 2}]], {HoldForm[flag], HoldForm[True]}, HoldForm[Block[{flag}, > D[Expand[(2 + x)^4], {x, 2}]]], {{HoldForm[Expand[(2 + x)^4]], HoldForm[16 + > 32*x + 24*x^2 + 8*x^3 + x^4]}, HoldForm[D[16 + 32*x + 24*x^2 + 8*x^3 + x^4, > {x, 2}]], HoldForm[48 + 48*x + 12*x^2]}, HoldForm[48 + 48*x + 12*x^2]} > > As you can see, D[(2 + x) , {x, 2}] was found to match the rule for > D[f_,x_]/;flag and the rule was simply applied. There was need to use Nest, > and so on. This is just a case of pattern matching doing its job. > > A similar approach can be used to show that when Mathematica evaluates > Derivative[n][f] it does not do actually do this by nesting Derivative[1]'s. > > Here is a rule for Derivative that is, essentially, equivalent to the above > one for D (note that there is no need to use Unprotect as Derivative does not > have the Protected Attribute!): > > In[1]:= > Derivative[1][Function[p_]] /; flag := > Block[{flag}, Derivative[1][Function @@ {Expand[p] }]] > > In[2]:= > flag = True; > > This works just as above: > > > In[3]:= > (# + 2)^3 &'[x] > > Out[3]= > 2 > 12 + 12 x + 3 x > > But: > > In[4]:= > ((# + 2)^3 &)''[x] > > Out[4]= > 6 (2 + x) > > While on the other hand > > In[5]:= > ((# + 2)^3 &')'[x] > > Out[5]= > 12 + 6 x > > > Of course what one should do is use a more general pattern: > > In[6]:= > Derivative[n_][Function[p_]] /; flag := > Block[{flag}, Derivative[n][Function @@ {Expand[p] }]] > > > so that: > > In[7]:= > ((# + 2)^5 &)'''[x] > > Out[7]= > 2 > 240 + 240 x + 60 x >