RE: Re: "At long last, Sir, have you no shame?"

*To*: mathgroup at smc.vnet.net*Subject*: [mg18628] RE: [mg18524] Re: "At long last, Sir, have you no shame?"*From*: "Ersek, Ted R" <ErsekTR at navair.navy.mil>*Date*: Tue, 13 Jul 1999 01:01:42 -0400*Sender*: owner-wri-mathgroup at wolfram.com

Colin Rose wrote: -------------------- Consider say: In[1]:= g = Exp[-i]; In[2]:= Sum[g, {i, 1, n}] Out[2]= n/E^i which is nonsense. This happens for almost ANY expression g=g(i). To get the correct answer, you have to wrap Evaluate around g: In[3]:= Sum[Evaluate[g], {i, 1, n}] Out[3]= (-1 + E^n)/(E^n*(-1 + E)) Wolfram support says Out[2] is not a bug, since Sum has attribute HoldAll. I say it is clearly (and obviously) an extremely serious bug, in the sense that it gives the wrong answer to almost any Summation where g is pre-defined. I reported it under v4 alpha, it was fixed in the betas, and it is now back in the v4 final release. But then it isn't a bug, apparently !? -------------------------- I am pretty sure WRI made it work that way on purpose, so if I am right it isn't a bug! I think this is a design decision that you disagree with. However, it seems no matter how WRI designed this function it would give the "wrong" answer in some examples. You can make Sum work the way you prefer by changing the attributes of Sum with the line below. If you like you can put this in your (init.m) file and Sum will always work the way you prefer. In[1]:= ClearAttributes[Sum,HoldAll]; SetAttributes[Sum,HoldRest]; In[3]:= g=Exp[-i]; In[4]:= Sum[g,{i,1,n}] //InputForm Out[4]//InputForm= (-1 + E^n)/((-1 + E)*E^n) In the example above Sum does what you expect, but now Sum gives the wrong answer in the next example. If Sum has the HoldRest attribute instead of HoldAll you have to use Unevaluated[f[i]] in this example. This is sort of the flip side of the problem you noted. Notice when Sum has the default attributes you don't need to use Unevaluated in this example. In[5]:= f[n_Integer]:=n^2 f[_]=0; In[7]:= Sum[f[i],{i,1,5}] Out[7]= 0 In[8]:= Sum[Unevaluated[f[i]],{i,1,5}] Out[8]= 55 ------------------- I think the same issue also applies to Product, FindMinimum, FindRoot, NSum, NProduct and NIntegrate as they also have the attribute HoldAll by default. On a somewhat related subject: I recently posted a package to MathSouce that changes the evaluation procedure of Plot, ContourPlot, DensityPlot, ParametricPlot, ParametricPlot3D, Plot3D and Play. The package sort of gives the best of both worlds between the attributes HoldAll and HoldRest. In particular you never have to use Evaluate on the arguments of Plot, Plot3D, ... after you load my package. The URL for the package is: http://www.mathsource.com/Content/Enhancements/Graphics/General/0209-876 I might write a package that makes similar changes to the way Sum, Product, FindMinimum, FindRoot, NSum, NProduct and NIntegrate evaluate. I will have to think about this some more. Regards, Ted Ersek

**[Q] Extracting patterns**

**Re: Re: Simplifying constants...bug?**

**Re: "At long last, Sir, have you no shame?"**

**Re: Re: Re: "At long last, Sir, have you no shame?"**