On 6/6/11 5:34 AM, Ignas Anikevicius wrote:
Hello,

This is my first message to this list, but I hope, that I have not
broken any rules here. I am writing because I have noticed a strange
thing, which I think should not happen.

I needed to test my maths solutions, so I thought I would just use a
loop to get sage generate the first 600 terms of a particular Fourier
series. But then I remembered that sage has a sum function. So I also
tried the sum function. However, I noticed a very big performance
difference which I did not expect. I was wondering if someone could
explain me why there is such a difference in the performance although
the actual result is the same? Or is the native sum function more clever
or what?

The output of the script and the script itself are attached. Note that I
did not bother to wait for the native sum function to sum 1000 terms, as
it took eternity to finish.

One big difference is that if you just are adding things together (your floop function), Sage doesn't have to be smart or do anything complicated; it just puts the terms all together. However, the Sage sum function, the way you are using it, tries to find a nice expression for the sum. Technically (if I followed the code correctly just now), it calls maxima to evaluate the sum, then to try to simplify the sum. This is *much* more work than the floop function, which doesn't call any external program and doesn't try to be smart about getting a nice simplified expression for the sum.

One cool thing about Sage being an open source solution is that I could follow what was happening in the function and figure out where the slowdown was. To do this, I first found the sum source code by evaluating "sum??". Looking at the if statement in the source code, I saw that it first called the .sum() method if an object had a .sum() method. So I can take one of your terms:

sage: n=var('n')
sage: term=(2*(((-1)^n-1)/(n*pi)^2*cos(n*pi*x)-(-1)^n/(n*pi)*sin(n*pi*x))).subs(n=1)

and I ask to see the source code of the sum method:

sage: term.sum??

This shows that it calls the sage.calculus.calculus.symbolic_sum function. Finally, I look up that function:

sage: sage.calculus.calculus.symbolic_sum??

and I see the calls to maxima and the call to simplify (ratsimp) the sum in maxima.

Thanks,

Jason

--
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to 
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to