Re: Uniform design
- To: mathgroup at smc.vnet.net
- Subject: [mg48304] Re: Uniform design
- From: ab_def at prontomail.com (Maxim)
- Date: Sat, 22 May 2004 03:04:37 -0400 (EDT)
- References: <c8kd99$msp$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Bill Rowe <readnewsciv at earthlink.net> wrote in message news:<c8kd99$msp$1 at smc.vnet.net>... > On 5/20/04 at 4:03 AM, ab_def at prontomail.com (Maxim) wrote: > > >Besides, there's a matter of 'optimizations' that Mathematica uses > >for Sum (an issue which was discussed in this newsgroup a while > >ago): > > >In[1]:= Module[{cnt = 0}, Sum[cnt += 1, {i, 10^6}] ] > >Module[{cnt =0}, Sum[cnt += 1, {i, 10^6 + 1}] ] > > >Out[1]= 500000500000 > > >Out[2]= 1000001 > > Clearly, this is a bug. But consider > > In[1]:= > Timing[Module[{cnt = 0}, Sum[cnt += 1, {n, 10^6}]]] > > Out[1]= > {8.69*Second, 500000500000} > > In[2]:= > Timing[Sum[n, {n, 10^6}]] > > Out[2]= > {2.549999999999999*Second, 500000500000} > > In[3]:= > Timing[Sum[n, {n, k}] /. k -> 10^6] > > Out[3]= > {0.019999999999999574*Second, 500000500000} > > It seems to me far more natural to due either Sum[n, {n, 10^6}] or Sum[n, > {n,k}]/.k->10^6 then what you've shown. Not only are these more natural, > they are faster and avoid the bug. I'm not so sure about it being a bug; what is happening here is that for values not greater than 10^6 Sum works as an iterator should (chapter A.4.2 of the Book), but for larger values Sum calls SymbolicSum`SymbolicSum, which pre-evaluates the first argument; so cnt+=1 evaluates to 1 and we're summing 10^6+1 ones. The documentation says that it is done only for symbolic sums (that is, when there are non-numerical quantities in the iterator), but my guess is that Mathematica tries to speed up the computations in this way when the number of terms is too large. Nobody would want to sum more than 10^6 terms, right? SymbolicSum`SymbolicSum adds its share of confusion too, because it is also HoldAll and performs some preliminary operations on the arguments before evaluating them; in particular, it looks for Out in the first argument. As a result, we get the following: In[1]:= a=k; Sum[a,{k,n}] Out[2]= k*n In[3]:= a=k; Sum[%,{k,n}] Out[4]= n*(n+1)/2 That is, there is an incosistency in deciding when the first argument depends on the iterator variable. And here's another pitfall: we know that Module[{a=k},Sum[a,{k,n}]] evaluates to k*n, but what if the Sum remains unevaluated? Mathematica will just return the Sum expression, but with a already evaluated. So Module[{a=k},Sum[a*f[k],{k,n}]] will give Sum[k*f[k],{k,n}], not k*Sum[f[k],{k,n}] as in the previous case. Here's an example: In[5]:= Module[{a = 1/k}, Sum[a/k, {k, 1, Infinity}]] Module[{a = 1/k}, Sum[a/k^2, {k, 1, Infinity}]] Sum::div: Sum does not converge. Out[5]= Pi^2/6 Out[6]= Pi^2/(6*k) What's more, Sum[1/k,{k,1,Infinity}] won't necessarily return unevaluated -- just as well it might evaluate to Infinity, because there are no clear rules for that either: sometimes Mathematica says that a sum diverges, sometimes that it converges to Infinity. That gives yet another possibility for Out[5]: it might return Infinity/k. So it's anybody's guess what the result will be in such cases. Maxim Rytin m.r at prontomail.com
- Follow-Ups:
- Re: Re: Uniform design
- From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
- Re: Re: Uniform design