Re: HoldAll for Integrate

*To*: mathgroup at smc.vnet.net*Subject*: [mg97973] Re: HoldAll for Integrate*From*: Szabolcs Horvát <szhorvat at gmail.com>*Date*: Fri, 27 Mar 2009 05:32:41 -0500 (EST)*References*: <gqfknb$kht$1@smc.vnet.net>

Tammo Jan Dijkema wrote: > The following commands yield unexpected output: > > x=10; > Integrate[x^2, {x,0,1}] > > That is because Integrate does not have attributes HoldAll, so that the > second command will be interpreted as Integrate[100, {10,0,1}] which is > not a command that Integrate can work with (so a warning message is > returned). For x to be localized for Integrate, it is a necessary, but not a sufficient condition that Integrate have Hold* attribtues. > > However, I can add the attribute HoldAll to Integrate myself: > > SetAttributes[Integrate, HoldAll]; > > I'm not very sure what to expect when now trying the same experiment > (the correct answer 1/3 would be nice), but the actual output surprised > me: > > x=10; > Integrate[x^2, {x,0,1}] It is generally not a very good idea to modify built-in functions. > > Yields as output: 0 (without any warnings). Could anyone explain why I > should have expected this result? It is not possible to know what the output will be without knowing what Integrate does internally. > > On a side note, I found a similar in the Tech Support column of the > Mathematica Journal Volume 6, Issue 2, by Carl Roy. He tried the > integral without limits: > > x=10; > SetAttributes[Integrate, HoldAll]; > Integrate[x^2, x] > > In the journal, the output 1000/3 is mentioned, whereas in Mathematica > 7.0.1 this outputs 1000. Indeed, the article says things like "The HoldAll attribute has the effect of localizing the symbol x inside of the Plot command." But this is simply not true. What HoldAll does is that it prevents evaluation of a function's arguments before the function itself is evaluated. But *during* the evaluation of the function, the arguments can be evaluated again. > > Again, does anyone understand this? > > Regards, > > Tammo Jan Dijkema > > Instead of trying to explain what might happen in such situations, I'll just give an illustrative example. Note how the order of evaluations change when HoldAll is set for f, but the result stays the same. In[1]:= f[v_] := Head[v] In[2]:= On[] f[x] Off[] During evaluation of In[2]:= On::trace: On[] --> Null. >> During evaluation of In[2]:= f::trace: f[x] --> Head[x]. >> During evaluation of In[2]:= Head::trace: Head[x] --> Symbol. >> Out[3]= Symbol In[5]:= x = 1 Out[5]= 1 In[6]:= On[] f[x] Off[] During evaluation of In[6]:= On::trace: On[] --> Null. >> During evaluation of In[6]:= x::trace: x --> 1. >> During evaluation of In[6]:= f::trace: f[x] --> f[1]. >> During evaluation of In[6]:= f::trace: f[1] --> Head[1]. >> During evaluation of In[6]:= Head::trace: Head[1] --> Integer. >> Out[7]= Integer In[9]:= SetAttributes[f, HoldAll] In[10]:= On[] f[x] Off[] During evaluation of In[10]:= On::trace: On[] --> Null. >> During evaluation of In[10]:= f::trace: f[x] --> Head[x]. >> During evaluation of In[10]:= x::trace: x --> 1. >> During evaluation of In[10]:= Head::trace: Head[x] --> Head[1]. >> During evaluation of In[10]:= Head::trace: Head[1] --> Integer. >> Out[11]= Integer