MathGroup Archive 2007

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

Search the Archive

Re: V6 evaluation inside Table and plot

  • To: mathgroup at smc.vnet.net
  • Subject: [mg76679] Re: [mg76577] V6 evaluation inside Table and plot
  • From: "Lev Bishop" <lev.bishop at gmail.com>
  • Date: Fri, 25 May 2007 06:49:07 -0400 (EDT)
  • References: <200705241004.GAA21232@smc.vnet.net>

On 5/24/07, Gerry Flanagan <flanagan at materials-sciences.com> 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}

Yet another example of how mathematica's variable renaming rules are
just about impossible for the human mind to comprehend, and why it's
safest just to wrap every evaluation you ever do in Module[]. You can
avoid this particular problem by one of:

Table[Evaluate[f''[x]], {x, 0, 1, .2}] (* Evaluate it first *)
Module[{x}, Table[f''[x], {x, 0, 1, .2}]] (* Make the dummy variable
really a dummy *)
f=#^3& (* So there is no dummy variable to clash *)

Unfortunately, none of the above options is very intuitive, and the
more intuitive:
f = Module[{x}, Function[{x}, x^3]]
has no effect.

Mathematica as a programming language has very confusing scoping.


  • Prev by Date: Re: Problem in ContourPlot
  • Next by Date: Re: V6 evaluation inside Table and plot
  • Previous by thread: V6 evaluation inside Table and plot
  • Next by thread: Re: V6 evaluation inside Table and plot