MathGroup Archive 1999

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

Search the Archive

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

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.



Sum[g,{i,1,n}] //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.






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:

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.

Ted Ersek

  • Prev by Date: [Q] Extracting patterns
  • Next by Date: Re: Re: Simplifying constants...bug?
  • Previous by thread: Re: "At long last, Sir, have you no shame?"
  • Next by thread: Re: Re: Re: "At long last, Sir, have you no shame?"