ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 08 Aug 2012 10:40:03 +0200How to speed-up a code containing several symbolic integrations and derivatives?https://ask.sagemath.org/question/9214/how-to-speed-up-a-code-containing-several-symbolic-integrations-and-derivatives/Hi,
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
EDIT.
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
R1=[x1,y1,z1,t1]
R2=[x2,y2,z2,t2]
R3=[x3,y3,z3,t3]
#--------------------------
# empty lists that will be filled with functions defined in the loop below
phi0=[q]
phi1=[q]
phi2=[q]
#-----------------
for i in range(1,5): # i = 1,2,3,4 / indices 1,2,3 are reserved for velocity components and 4 for pressure
phi0.append([])
phi1.append([])
phi2.append([])
for n in range(N+1): # N+1 since range(N+1)=(0,..,N) # here n=0 is required so I don't increase it to 1 !
phi0[i].append(function('phi0_%s_%s' %(i,n), x,y,z,t, latex_name='\phi_%s^{0\,(%s)}' %(i,n)))
phi1[i].append(function('phi1_%s_%s' %(i,n), x,y,z,t,*R1, latex_name='\phi_%s^{1\,(%s)}' %(i,n)))
phi2[i].append(function('phi2_%s_%s' %(i,n), x,y,z,t,*R1+R2, latex_name='\phi_%s^{2\,(%s)}' %(i,n)))
##########################################################
####################\the initial estimations/####################
# let define the nonzero values for initial estimates of phi1's components
p0=1; v0=1 # v0 stans for vx0=vy0=vz0
# specifying phi0[i][0], phi1[i][0], phi2[i][0] as the initial estimates (guesses), order 0 approximation
for i in range(1,4): # which means i=1,2,3, for the velocity components
phi0[i][0]=0*x #0
phi1[i][0]=0*x+v0*sin(x) #v0
phi2[i][0]=0*x #0
phi0[4][0]=0*x #0
phi1[4][0]=0*x+p0+(x-x_B1) #p0
phi2[4][0]=0*x #0
################################################################
#######\defining the differential and integral operators in the equations/#######
var('td,xd1,xd2') # dummy variables
g = lambda i,f: diff(f,R0[i])
It = lambda f: integral(f(t=td),td,0,t) #or use: ,algorithm='mathematica_free'
#or use: ,algorithm='sympy'
Ixx = lambda f: integral(integral(f(x=xd1),xd1,x_B1,xd2),xd2,x_B1,x)
DR1 = lambda f: -1/Re*(diff(f,x,2)+diff(f,y,2)+diff(f,z,2))
DR2 = lambda f: diff(f,y,2)+diff(f,z,2)
IntR1 = lambda f: integral(integral(integral(integral(f,x1,x_B1,x_B2),y1,y_B1,y_B2),z1,z_B1,z_B2),t1,0,T)
IntR2 = lambda f: integral(integral(integral(integral(f,x2,x_B1,x_B2),y2,y_B1,y_B2),z2,z_B1,z_B2),t2,0,T)
IntR3 = lambda f: integral(integral(integral(integral(f,x3,x_B1,x_B2),y3,y_B1,y_B2),z3,z_B1,z_B2),t3,0,T)
#####################################################
###########\the iterative equations being imported/###########
for n in range(N): # note that in equations I have n+1 , so it couldn't be N+1 !
for i in range(1,4): # pressure (i=4) has different equations, so would be denoted individually
phi0[i][n+1] = phi0[i][0] - It(DR1(phi0[i][n])) - It(g(i,phi0[4][n])) \
- It( sum( phi0[j][n]*g(j,phi0[i][n]) + IntR1(phi1[j][n]*g(j,phi1[i][n])) + \
2*IntR2(IntR1(phi2[j][n]*g(j,phi2[i][n]))) for j in range(1,4)) )
show(phi0[i][n+1])
phi1[i][n+1] = phi1[i][0] - It(DR1(phi1[i][n])) - It(g(i,phi1[4][n])) \
- It( sum( phi0[j][n]*g(j,phi1[i][n]) + phi1[j][n]*g(j,phi0[i][n]) + \
2*IntR2(phi1[j][n](x1=x2,y1=y2,z1=z2,t1=t2)*g(j,phi2[i][n]) + \
phi2[j][n]*g(j,phi1[i][n](x1=x2,y1=y2,z1=z2,t1=t2))) for j in range(1,4)) )
show(phi1[i][n+1])
phi2[i][n+1] = phi2[i][0] - It(DR1(phi2[i][n])) - It(g(i,phi2[4][n])) \
- It( sum( phi0[j][n]*g(j,phi2[i][n]) + phi2[j][n]*g(j,phi0[i][n]) \
+ 0.5*( phi1[j][n]*g(j,phi1[i][n](x1=x2,y1=y2,z1=z2,t1=t2)) + \
phi1[j][n](x1=x2,y1=y2,z1=z2,t1=t2)*g(j,phi1[i][n]) ) \
+ 2*IntR3( phi2[j][n](x2=x3,y2=y3,z2=z3,t2=t3) \
*g(j,phi2[i][n](x1=x2,y1=y2,z1=z2,t1=t2, x2=x3,y2=y3,z2=z3,t2=t3)) \
+ phi2[j][n](x1=x2,y1=y2,z1=z2,t1=t2, x2=x3,y2=y3,z2=z3,t2=t3) \
*g(j,phi2[i][n](x2=x3,y2=y3,z2=z3,t2=t3)) ) for j in range(1,4)) )
show(phi2[i][n+1])
phi0[4][n+1] = phi0[4][0] - Ixx(DR2(phi0[4][n])) \
- Ixx( sum( g(i,phi0[j][n+1])*g(j,phi0[i][n+1]) \
+ IntR1(g(i,phi1[j][n+1])*g(j,phi1[i][n+1])) \
+ 2*IntR2(IntR1(g(i,phi2[j][n+1])*g(j,phi2[i][n+1]))) \
for i in range (1,4) for j in range(1,4) ) )
show(phi0[4][n+1])
phi1[4][n+1] = phi1[4][0] - Ixx(DR2(phi1[4][n])) \
- Ixx( sum( 2*g(j,phi0[i][n+1])*g(i,phi1[j][n+1]) \
+ 4*IntR2(g(j,phi1[i][n+1](x1=x2,y1=y2,z1=z2,t1=t2))*g(i,phi2[j][n+1])) \
for i in range (1,4) for j in range(1,4) ) )
show(phi1[4][n+1])
phi2[4][n+1] = phi2[4][0] - Ixx(DR2(phi2[4][n])) \
- Ixx( sum( 2*g(j,phi0[i][n+1])*g(i,phi2[j][n+1]) \
+ g(j,phi1[i][n+1])*g(i,phi1[j][n+1](x1=x2,y1=y2,z1=z2,t1=t2)) \
+ 4*IntR3( g(j,phi2[i][n+1](x2=x3,y2=y3,z2=z3,t2=t3)) \
*g(i,phi2[j][n+1](x1=x2,y1=y2,z1=z2,t1=t2, x2=x3,y2=y3,z2=z3,t2=t3)) ) \
for i in range (1,4) for j in range(1,4) ) )
show(phi2[4][n+1])Tue, 07 Aug 2012 15:19:45 +0200https://ask.sagemath.org/question/9214/how-to-speed-up-a-code-containing-several-symbolic-integrations-and-derivatives/Comment by vdelecroix for <div class="snippet"><p>Hi,</p>
<p>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.</p>
<p>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.</p>
<p>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?</p>
<p>Best regards</p>
<p>EDIT.</p>
<p>Here is the code:</p>
<pre><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
R1=[x1,y1,z1,t1]
R2=[x2,y2,z2,t2]
R3=[x3,y3,z3,t3]
#--------------------------
# empty lists that will be filled with functions defined in the loop below
phi0=[q]
phi1=[q]
phi2=[q]
#-----------------
for i in range(1,5): # i = 1,2,3,4 / indices 1,2,3 are reserved for velocity ...</code></pre><span class="expander"> <a>(more)</a></span></div>https://ask.sagemath.org/question/9214/how-to-speed-up-a-code-containing-several-symbolic-integrations-and-derivatives/?comment=19256#post-id-19256Just 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!Wed, 08 Aug 2012 08:09:53 +0200https://ask.sagemath.org/question/9214/how-to-speed-up-a-code-containing-several-symbolic-integrations-and-derivatives/?comment=19256#post-id-19256Comment by owari for <div class="snippet"><p>Hi,</p>
<p>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.</p>
<p>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.</p>
<p>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?</p>
<p>Best regards</p>
<p>EDIT.</p>
<p>Here is the code:</p>
<pre><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
R1=[x1,y1,z1,t1]
R2=[x2,y2,z2,t2]
R3=[x3,y3,z3,t3]
#--------------------------
# empty lists that will be filled with functions defined in the loop below
phi0=[q]
phi1=[q]
phi2=[q]
#-----------------
for i in range(1,5): # i = 1,2,3,4 / indices 1,2,3 are reserved for velocity ...</code></pre><span class="expander"> <a>(more)</a></span></div>https://ask.sagemath.org/question/9214/how-to-speed-up-a-code-containing-several-symbolic-integrations-and-derivatives/?comment=19251#post-id-19251Thanks 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?Wed, 08 Aug 2012 10:40:03 +0200https://ask.sagemath.org/question/9214/how-to-speed-up-a-code-containing-several-symbolic-integrations-and-derivatives/?comment=19251#post-id-19251Comment by owari for <div class="snippet"><p>Hi,</p>
<p>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.</p>
<p>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.</p>
<p>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?</p>
<p>Best regards</p>
<p>EDIT.</p>
<p>Here is the code:</p>
<pre><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
R1=[x1,y1,z1,t1]
R2=[x2,y2,z2,t2]
R3=[x3,y3,z3,t3]
#--------------------------
# empty lists that will be filled with functions defined in the loop below
phi0=[q]
phi1=[q]
phi2=[q]
#-----------------
for i in range(1,5): # i = 1,2,3,4 / indices 1,2,3 are reserved for velocity ...</code></pre><span class="expander"> <a>(more)</a></span></div>https://ask.sagemath.org/question/9214/how-to-speed-up-a-code-containing-several-symbolic-integrations-and-derivatives/?comment=19253#post-id-19253Thanks 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 a supercomputer around?Wed, 08 Aug 2012 09:54:49 +0200https://ask.sagemath.org/question/9214/how-to-speed-up-a-code-containing-several-symbolic-integrations-and-derivatives/?comment=19253#post-id-19253Comment by vdelecroix for <div class="snippet"><p>Hi,</p>
<p>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.</p>
<p>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.</p>
<p>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?</p>
<p>Best regards</p>
<p>EDIT.</p>
<p>Here is the code:</p>
<pre><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
R1=[x1,y1,z1,t1]
R2=[x2,y2,z2,t2]
R3=[x3,y3,z3,t3]
#--------------------------
# empty lists that will be filled with functions defined in the loop below
phi0=[q]
phi1=[q]
phi2=[q]
#-----------------
for i in range(1,5): # i = 1,2,3,4 / indices 1,2,3 are reserved for velocity ...</code></pre><span class="expander"> <a>(more)</a></span></div>https://ask.sagemath.org/question/9214/how-to-speed-up-a-code-containing-several-symbolic-integrations-and-derivatives/?comment=19252#post-id-19252Symbolic 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 ?Wed, 08 Aug 2012 10:26:16 +0200https://ask.sagemath.org/question/9214/how-to-speed-up-a-code-containing-several-symbolic-integrations-and-derivatives/?comment=19252#post-id-19252