Sage Speed Issues

60 views
Skip to first unread message

Asad Akhlaq

unread,
Jun 10, 2013, 7:56:04 PM6/10/13
to sage-s...@googlegroups.com
Hi,

I am expanding the Taylor series for an 8-dimensional exponential function and Sage is taking too much time as the the number of terms are increased. I am using Sage in notebook through VirtualBox. If I use Sage through terminal on my own PC, will it faster its execution? Any other suggestion to improve speed? 

Thanks
Assad

William Stein

unread,
Jun 11, 2013, 1:03:31 AM6/11/13
to sage-support support


On Jun 10, 2013 4:56 PM, "Asad Akhlaq" <assad....@gmail.com> wrote:
>
> Hi,
>
> I am expanding the Taylor series for an 8-dimensional exponential function and Sage is taking too much time as the the number of terms are increased. I am using Sage in notebook through VirtualBox. If I use Sage through terminal on my own PC, will it faster its execution? Any other suggestion to improve speed? 
>

Please post code.

> Thanks
> Assad
>
> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
> To post to this group, send email to sage-s...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-support?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  

Asad Akhlaq

unread,
Jun 11, 2013, 3:00:29 AM6/11/13
to sage-s...@googlegroups.com
Hi William,

Thanks for your reply. Actually I have exponential function f(u) in 8-dimensions and I am using the general formula for Taylor series at available at http://en.wikipedia.org/wiki/Taylor_series (Taylor series in several variables). I want to expand summations in this equations for up to 6 or even more as I am using this expansion to calculate the bit error rate for the E8 lattice. The exponential function f(u) is of the following form. 

$$ f(u) = e^{(-(3.15987540437407e-36)*(99446621081482098*u1 + 99446621081482098*u2 + 99446621081482098*u3 + 49723310540741049*u4 + 99446621081482098*u5 + 165744368469136820*u6 + 198893242162964181*u7 +298339863244446264)^2 - (3.15987540437407e-38)*(1790039179466677674*u1 - 2983398632444462490*u2 - 2983398632444462490*u3 - 1491699316222231245*u4 - 2983398632444462490*u5 - 2320421158567915270*u6 - 1988932421629641660*u7 - 994466210814820830)^2 - (3.15987540437407e-38)*(1790039179466677674*u1 + 2983398632444462715*u2 + 2320421158567915470*u3 - 1491699316222231245*u4 - 2983398632444462490*u5 - 2320421158567915270*u6 + 1988932421629641810*u7 - 994466210814820830)^2 - (3.15987540437407e-38)*(1790039179466677674*u1 + 2983398632444462715*u2 + 2320421158567915470*u3 - 1491699316222231245*u4 + 4972331054074104450*u5 - 2320421158567915270*u6 + 1988932421629641810*u7 - 994466210814820830)^2 - (3.15987540437407e-38)*(1790039179466677674*u1 + 2983398632444462715*u2 + 2320421158567915470*u3 + 4475097948666693960*u4 + 4972331054074104450*u5 + 5635308527950651670*u6 + 1988932421629641810*u7 - 994466210814820830)^2 - (3.15987540437407e-38)*(1790039179466677674*u1 + 2983398632444462715*u2 + 4972331054074104450*u3 + 4475097948666693960*u4 + 4972331054074104450*u5 + 5635308527950651670*u6 + 1988932421629641810*u7 - 994466210814820830)^2 - (3.15987540437407e-36)*(99446621081482098*u1 + 99446621081482098*u2 + 99446621081482098*u3 + 49723310540741049*u4 + 99446621081482098*u5 + 165744368469136820*u6 - 198893242162964166*u7 - 397786484325928347*u8 + 298339863244446264)^2 - (3.15987540437407e-36)*(99446621081482098*u1 + 99446621081482098*u2 + 99446621081482098*u3 + 49723310540741049*u4 + 99446621081482098*u5 + 165744368469136820*u6 + 198893242162964181*u7 + 397786484325928347*u8 + 298339863244446264)^2)}$$

I also tried to use simplify_full() command in sage to simplify it which gives me a simplified form. My code for taylor expansion is as follows:

PLM = f(u) 
Taylor_Expansion = []
#Taylor series is expanded at the origin i.e. (0,0,0,0,0,0,0,0)
b1=0;b2=0;b3=0;b4=0;b5=0;b6=0;b7=0;b8=0;

for n1 in [0..sum_limit]:
    for n2 in [0..sum_limit]:
        for n3 in [0..sum_limit]:
            for n4 in [0..sum_limit]:
                for n5 in [0..sum_limit]:
                    for n6 in [0..sum_limit]:
                        for n7 in [0..sum_limit]:
                            for n8 in [0..sum_limit]:
                                    
                                #const1 is the part of the Taylor series without partial derivatives term    
                                const1 = ((u1 - b1)^n1*(u2-b2)^n2*(u3-b3)^n3*(u4-b4)^n4*(u5-b5)^n5*(u6-b6)^n6*(u7-b7)^n7*(u8-b8)^n8)/(factorial(n1)* factorial(n2)*factorial(n3)*factorial(n4)*factorial(n5)*factorial(n6)*factorial(n7)*factorial(n8))

                                f = PLM.full_simplify()
                                    
                                if n8>0:
                                    for dif8 in [1..n8]:
                                        f = derivative(f,u8)
                                if n7>0:
                                    for dif7 in [1..n7]:
                                        f = derivative(f,u7)
                                if n6>0:
                                    for dif6 in [1..n6]:
                                        f = derivative(f,u6)
                                if n5>0:
                                    for dif5 in [1..n5]:
                                        f = derivative(f,u5)
                                if n4>0:
                                    for dif4 in [1..n4]:
                                        f = derivative(f,u4)
                                if n3>0:
                                    for dif3 in [1..n3]:
                                        f = derivative(f,u3)
                                if n2>0:
                                    for dif2 in [1..n2]:
                                        f = derivative(f,u2)
                                if n1>0:
                                    for dif1 in [1..n1]:
                                        f = derivative(f,u1)
                                    
                                    
                                ff = f
#const2 is the term with partial derivatives and after evaluation at point (0,0,0,0,0,0,0,0)
                                const2 = ff.substitute(u1=b1,u2=b2,u3=b3,u4=b4,u5=b5,u6=b6,u7=b7,u8=b8)
                                eqn = const1*const2
                                Taylor_Expansion.append(eqn)

The problem is that if I set sum_limit = 2, it takes hours and still no result. For sum_limit =0,1 it is good enough to give me the results. But I need more terms for Taylor expansion (may be sum_limit = 6 or 8). Also it is not only one function that I have to expand. I have to find Taylor expansion for 232 such function for one SNR value. Now if I want to calculate BER for an SNR range 0 to 20 with a step of 2 I need an expansion of about (232 x 10 = 2320 functions), and now it will take months and may be a year :). After this Taylor expansion, I am integration the expansion in 8-dim to get the BER. I also tried it in MATLAB but it is also very slow. Is it possible to modify my code to make it fast ???

I hope to hear from you soon again.
Thanks
Assad

Asad Akhlaq

unread,
Jun 11, 2013, 3:02:45 AM6/11/13
to sage-s...@googlegroups.com
I am also trying to use Sage's built in 'taylor' command and it is also very slow.

Burcin Erocal

unread,
Jun 11, 2013, 9:15:38 AM6/11/13
to sage-s...@googlegroups.com
On Tue, 11 Jun 2013 00:02:45 -0700 (PDT)
Asad Akhlaq <assad....@gmail.com> wrote:

> I am also trying to use Sage's built in 'taylor' command and it is
> also very slow.

Try the .series() method of symbolic expressions. It should be faster.

Cheers,
Burcin

leif

unread,
Jun 11, 2013, 10:39:04 AM6/11/13
to sage-s...@googlegroups.com
Also, you should probably be aware of the facts that

a^(n+1) = a^n * a

factorial(n+1) = factorial(n) * (n+1)


Just saying... (You're aiming at executing the loop body 9^8=43046721
times for each function [= times ~2320?, about 100 billion].)


And a minor thing:

if N>0:
for M in [1..N]:
...

is identical to

for M in [1..N]:
...

since (e.g.) [1..0] yields the empty list.



-leif

--
() The ASCII Ribbon Campaign
/\ Help Cure HTML E-Mail

Jim Clark

unread,
Jun 11, 2013, 12:00:36 PM6/11/13
to sage-s...@googlegroups.com
Since you are writing your own algorithms in python, I suggest you investigate cython:
to compile your python into machine code, instead of python's quasi-compiled byte code.
Cython is included with sage -- just add the code %cython as the first line of a Sage worksheet cell.
Alternatively, put you Sage code into a file with the extension .spyx, then load the file from the Sage command line.

There are a couple of Sage-isms that won't work in cython, for example, you must replace e^{whatever} with e**{whatever}.

Also, as others have pointed out, you might analyze your code a bit to calculate the number of operations you want to perform.
You can easily reach a point that would require the fastest computer on earth hundreds, thousand, millions,... of years to compute.

Jim Clark

Asad Akhlaq

unread,
Jun 12, 2013, 2:24:00 AM6/12/13
to sage-s...@googlegroups.com, Jim Clark
Hi,

Thank you all for your reply.

@leif 
**************************************************************
Also, you should probably be aware of the facts that 

   a^(n+1) = a^n * a 

   factorial(n+1) = factorial(n) * (n+1) 
*****************************************************************

I know it from high school but how can I use it in my code? I not a good programmer. Just started to work on Sage 2 months ago. Still don't know may things to improve my programming.

And thanks, i have removed those 'if' conditions from my code.

......
Assad
Reply all
Reply to author
Forward
0 new messages