How to speed-up a code containing several symbolic integrations and derivatives?

asked 2012-08-07 08:19:45 -0500

owari gravatar image

updated 2012-08-08 02:42:54 -0500


After days of confusion now I have a sage code ready to be used. It consists of a set of iterative equations in a loop, at each step the equations --containing a number of symbolic integration and differentiation-- act on a previous estimation of the unknowns to update them to a newer estimation.

Sounds good up to this point. However, depending on the initial estimation that I introduce to the iterative loop the code may run fast (like when I give a constant or simply x as initial estimation) or too slow (like when I introduce sin(x) or other even simple polynomials). I understand that having such integrations and differentiations make the expressions rapidly grow large so that the computations would be time consuming but it is extremely slow, not one minute or 10 minutes or so, I just waited for some hours and sage did not answered anything new, and I doubt if the 100% cpu usage shown by the system's monitoring app is really spent on the calculations and sage can give the answer at the end.

As much as I read from documentations and elsewhere adding a %cython command at the beginning of the code can speed up the code but it gives me back an error e.g. of the form "undeclared name not builtin: sin". Does %cython command work also with symbolic calculations? Also I saw something about "fast_callable" function and sympyx, what about they, if they can help speed up such calculations where can I find a good intro toward them?

Best regards


Here is the code:

var('x,y,z,t, x1,y1,z1,t1, x2,y2,z2,t2, x3,y3,z3,t3') 

N=5         # N: number of iterations
            # or N=int(input("How many iteration do you need for your Adomian Decomposition code to run? "))
Re=1000000  # Re: global Reynolds number
x_B1=-1; x_B2=1; y_B1=-1; y_B2=1; z_B1=-1; z_B2=1       # the non-dimensional boundaries of the computational region!
T=10        # T: the ending non-dimensional time of computations
# p0, v0: used in definition of initial condition for the unknown functions

assume(x_B1<=x, 0<t)     # needed for our integrations from X_B1 to x and from 0 to t !

# the following lists are defined for, 1st, easier addressing and, 2nd, more compact importing
# of terms like "sum_j u_j du_i/dx_j" as otherwise x_j was not known to the code
var('q')                # q is a very dummy variable to fill the zeroth place in all my lists to avoid confusion while coding, instead of always using i-1 or j+1 as indices!
R0=[q,x,y,z,t]          # Only R0 needs a zeroth element of q

# empty lists that will be filled with functions defined in the loop below
for i in range(1,5):        # i = 1,2,3,4  / indices 1,2,3 are reserved for velocity ...
edit retag flag offensive close merge delete


Just a small remark: it seems that you do not make the difference between a Python variable and a symbolic variable. Each Python variable has a type and among possible types there are: integer, real number, symbolic variable, etc. In your example it is not needed to declare the variables N, Re, x_B1, ... as symbolic variables as you use them as Python variable!

vdelecroix gravatar imagevdelecroix ( 2012-08-08 01:09:53 -0500 )edit

Thanks vdelecroix, I removed the line var('N, Re, x_B1,x_B2, y_B1,y_B2, z_B1,z_B2, T, p0,v0') from the code above. However my main problem still persists. Actually I am a little bit in hurry, my dissertation should be defended before winter and the above code is my simplest case, 12 equations and unknowns. In other cases I have e.g. a set of millions of equations and unknowns to be solved this way and in other (quasi analytic) ways. I am just wondering if Sage can handle such huge calculations and if yes then if there is a way to speed up the calculations? I prefer to use Sage rather than to travel to either Mathematica or Maple, neither i like the nonfree apps nor I am familiar how to use them and lack of time is a serious problem here. If it helps I can even use ...(more)

owari gravatar imageowari ( 2012-08-08 02:54:49 -0500 )edit

Symbolic ring and symbolic functions are not intended to be fast. In particular, cython won't change anything to integration/derivation. Moreover, there is no way to make it work with thousands of variables. If you really care about speed, you should think about a numerical approach using scipy (which is included in sage). Your objective is not clear to me. What do you want to do ?

vdelecroix gravatar imagevdelecroix ( 2012-08-08 03:26:16 -0500 )edit

Thanks again. I am solving a set of coupled nonlinear Integro-PDE's using semi analytic methods like the Adomian Decomposition Method, Variational Iteration Method, and Homotopy Perturbation Method. The equations are too complex (being integro partial deifferential equations, being coupled and nonlinear) that a professor at department of mathematics suggest me not to try numerical solutions and seemingly the semi-analytic methods are my only choices. This is a try to solve turbulence, so it is understandable not to be easily solvable but the code above is too simplified, over simplified, they are even strict in their unknowns, only some integrations and derivatives acting iteratively over some known functions. That shouldn't be that much time consuming I believe. Am I right?

owari gravatar imageowari ( 2012-08-08 03:40:03 -0500 )edit