Re: Bug: Derivative[] does not work with functions having slots in

*To*: mathgroup at smc.vnet.net*Subject*: [mg86667] Re: Bug: Derivative[] does not work with functions having slots in*From*: Andrew Moylan <andrew.j.moylan at gmail.com>*Date*: Mon, 17 Mar 2008 00:22:20 -0500 (EST)*References*: <frituo$io$1@smc.vnet.net>

Even more interesting: this particular "bug" even follows from the documentation for Derivative: "Whenever Derivative[n][f] is generated, Mathematica rewrites it as D[f[#],{#,n}]&." Thus, for your example, f[#] evaluates to Sin[2] and g[#] evaluates to Sin[1 + #1]. Thus, the issue is related to the scoping of "Slot[1]". Perhaps the bug could be fixed by effectively changing "Mathematica rewrites it as D[f[#],{#,n}]&." to "Mathematica rewrites it as Module[{expr, x}, Function[expr] /. expr -> (D[f[x], {x,n}] /. x -> #)]." That should work. So here is a function that replaces Derivative[1] and works properly in your example: fixedDerivative[f_] := Module[{expr, x}, Function[expr] /. expr -> (D[f[x], x] /. x -> #)] On Mar 16, 9:48 pm, Szabolcs Horv=E1t <szhor... at gmail.com> wrote: > There is a bug in Derivative. It does not seem to like slots. > > For example, let f[x_] := Sin[x + #]&[1] (Yes, I know that this looks > silly. It is a simplified example.) > > D can be used for calculating the derivative, but Derivative cannot. > > In[2]:= D[f[a], a] > Out[2]= Cos[1 + a] > > In[3]:= f'[a] > Out[3]= 0 > > A workaround is to use pure functions with explicitly named parameters: > > In[4]:= g[x_] := Function[{p}, Sin[x + p]][1] > > In[5]:= g'[a] > Out[5]= Cos[1 + a]