Re: V6 evaluation inside Table and plot

*To*: mathgroup at smc.vnet.net*Subject*: [mg76669] Re: V6 evaluation inside Table and plot*From*: Szabolcs <szhorvat at gmail.com>*Date*: Fri, 25 May 2007 06:43:56 -0400 (EDT)*Organization*: University of Bergen*References*: <f33onq$l4v$1@smc.vnet.net>

Gerry Flanagan wrote: > Define a simple function > > In[7]:= f = Function[{x}, x^3] > > Out[7]= Function[{x}, x^3] > > Taking two derivatives works as it should > > In[2]:= f''[x] > > I was converting some packages to work with V6, and immediately ran into > some evaluation issues I don't understand. It looks like forms like > f''[x] (derivative) are evaluated differently inside of Table and Plot > than outside. > > Out[2]= 6 x > > But this does not? > > In[20]:= Table[f''[x], {x, 0, 1, .2}] > > Out[20]= {0, 0, 0, 0, 0, 0} > > Strangely, this works > > In[4]:= Table[f'[x], {x, 0, 1, .2}] > > Out[4]= {0., 0.12, 0.48, 1.08, 1.92, 3.} > > Determining the derivative outside the Table makes it work again. > > In[12]:= g = f''; > > In[21]:= tmp = Table[g[x], {x, 0, 1, .2}] > > Out[21]= {0., 1.2, 2.4, 3.6, 4.8, 6.} > > Gerry Flanagan > I get the same result in Mathematica 5.2. This is a really strange issue. You can of course work around it by avoiding x in the definition of f, f= #^3 & or by avoiding x in the Table command, Table[f''[y], {y, 0, 1, .2}] or by using Table[f''[x]//Evaluate, {x, 0, 1, .2}] (which is recommended anyway to avoid computing the derivative several times) But the real question still remains: why doesn't it work when x is used both in Table and Function? And why *does* it work with a first order derivative? The docs say: "An important implicit use of Block in Mathematica is for iteration constructs such as Do, Sum and Table. Mathematica effectively uses Block to set up local values for the iteration variables in all of these constructs." And Block has the same effect: In[17]:= Block[{x = 1}, f'] Out[17]= Function[{x}, 3 x^2] In[18]:= Block[{x = 1}, f''] Out[18]= Function[{x}, 0] Some more experiments: In[1]:= On[a] (You can also say On[] instead of On[a] to see a bit more ...) In[2]:= f=Function[{a},a^3] Out[2]= Function[{a},a^3] In[3]:= f Out[3]= Function[{a},a^3] In[4]:= f' Out[4]= Function[{a},3 a^2] In[5]:= f'' Out[5]= Function[{a},6 a] In[6]:= a=1 Out[6]= 1 In[7]:= f Out[7]= Function[{a},a^3] In[8]:= f' Out[8]= Function[{a},3 a^2] In[9]:= f'' During evaluation of In[9]:= a::trace: a --> 1. >> Out[9]= Function[{a},0] In[10]:= Clear[a] In[11]:= f' Out[11]= Function[{a},3 a^2] In[12]:= f'' Out[12]= Function[{a},0] In[13]:= ?a Global`a In[14]:= Update[a] In[15]:= f' Out[15]= Function[{a},3 a^2] In[16]:= f'' Out[16]= Function[{a},6 a] It seems that Derivative[] fails to hold the symbol "a" unevaluated when computing higher derivatives. This is a very ugly bug! Recommended reading: http://reference.wolfram.com/mathematica/tutorial/ModularityAndTheNamingOfThingsOverview.html and sections 8.3 and 8.4 from http://www.cs.berkeley.edu/~fateman/papers/mma.review.pdf P.S. It is also interesting to note that while the documentation says that "Whenever Derivative[n][f] is generated, Mathematica rewrites it as D[f[#],{#,n}]&", Derivative[] doesn't seem to work this way. When something more complicated is put inside f, Derivative can fail with various errors, e.g. In[1]:= f=Function[{x},Print[x];Module[{k=1},k=k+x;k-1]] Out[1]= Function[{x},Print[x];Module[{k=1},k=k+x;k-1]] In[2]:= f[z] During evaluation of In[2]:= z Out[2]= z In[3]:= D[f[p],p] During evaluation of In[3]:= p Out[3]= 1 In[4]:= f'[p] During evaluation of In[4]:= #1 During evaluation of In[4]:= p During evaluation of In[4]:= p During evaluation of In[4]:= Set::write: Tag Slot in #1 is Protected. >> During evaluation of In[4]:= $RecursionLimit::reclim: Recursion depth of 256 exceeded. >> During evaluation of In[4]:= $RecursionLimit::reclim: Recursion depth of 256 exceeded. >> During evaluation of In[4]:= Module::lvsym: Local variable specification {#1} contains #1, which is not a symbol or an assignment to a symbol. >> Out[4]= 0 Derivative[] probably looks inside Function[], and operates on its second argument directly.

**Follow-Ups**:**Re: Re: V6 evaluation inside Table and plot***From:*Andrzej Kozlowski <akoz@mimuw.edu.pl>