# symbolic and numeric double integration method

fidbc corrected some of my previous code for double integration. However I am only able to good a good numerical double integral. Is their a sage method that can do both symbolic ie variables as bounds and numeric integration if not what is the symbolic integration method. Using the current integrate method I have I get back the wrong results in bracketed around the word integrate.

Here is my double integration code corrected by fidbc for numerical integration does anyone know a better integration method or methods for symbolic double integration.

from sage.plot.plot3d.shapes import Box
x,y = var('x,y')

@interact
def  interplay(function= input_box(sin(x*y)),lower_x_bound= input_box(0),upper_x_bound = input_box(1),lower_y_bound=input_box(0),upper_y_bound=input_box(1),dydx = checkbox(default = False)):
if dydx==False :
prev = integral(function,x,lower_x_bound,upper_x_bound)
result = n(integral(prev,y,lower_y_bound,upper_y_bound))
q ="$\int_%s^{%s} \int_%s^%s %s \,dx\,dy = %s$" % (lower_y_bound, upper_y_bound,lower_x_bound,upper_x_bound,function,result)
else:
result = n(integral(integral(function,y,lower_y_bound,upper_y_bound),x,lower_x_bound,upper_x_bound))
q = "$\int_%s^{%s} \int_%s^%s %s \,dy\,dx = %s$" % (lower_x_bound,upper_x_bound,lower_y_bound,upper_y_bound,function,result)
html("%s" %q)

edit retag close merge delete

Can you define what you mean by good? Do you mean one that won't return integrate(....)? It seems that sage will always return integrate(...) when there is no closed form solution.

var('x,y')

#No closed form
sage: integral(integral(sin(x*y),x,0,2),y,0,2)
integrate(-cos(2*y)/y + 1/y, y, 0, 2)

#has closed form solution
sage: integral(integral(sin(x)*y,x,0,2),y,0,2)
-2*cos(2) + 2


If what you want is the closed form solution when that is avaliable and a numeric solution the following function should suffice:

def sym_or_num_Integral(f,variable,lower_bound=None,upper_bound=None):
result=integral(f,variable,lower_bound,upper_bound)
if 'integrate' in str(result):
return result.n()
else:
return result


Sort by » oldest newest most voted

I'm interpreting your question as: Is there a sage function which will give a symbolic solution to an integral if a closed form exists and a numeric solution otherwise. To my knowledge there is not though the following function may be what you are asking for:

def sym_or_num_Integral(f,variables,lower_bound=[None],upper_bound=[None]):
if isinstance(variables,list):
if not isinstance(lower_bound,list):
lower_bound=[lower_bound]
while len(lower_bound)!=len(variables):
if len(lower_bound)>len(variables): break
lower_bound.append(None)

if not isinstance(upper_bound,list):
upper_bound=[upper_bound]
while len(upper_bound) != len(variables):
if len(lower_bound)>len(variables): break
upper_bound.append(None)

if len(variables)==1:
return sym_or_num_Integral(f,variables,lower_bound,upper_bound)
inputs_inner=[variables,lower_bound,upper_bound]
inputs_outer=[variables[1:],lower_bound[1:],upper_bound[1:]]
return sym_or_num_Integral(sym_or_num_Integral(f,*inputs_inner),*inputs_outer)

result=integral(f,variables,lower_bound,upper_bound)
if 'integrate' in str(result):
return result.n()
else:
return result


The method of checking if the solution is a closed form is quite hacky and I'd welcome and improvement too it. This function can do any number of integrals so long as you input the parameters as a list, i.e. variables=[x,y], lower_bound=[x_lower,y_lower]. Two examples:

sage: var('x y')
sage: sym_or_num_Integral(sin(x)*y,[x,y],[0,0],[2,2])
-2*cos(2) + 2
sage: sym_or_num_Integral(sin(x*y),[x,y],[0,0],[2,2])
2.1044917239083536


You can have the integral be definite in any subset (must include first variable) of the variable and not the others:

sage: sym_or_num_Integral(sin(x)*y,[x,y],[0,None],[2,None])
-1/2*y^2*(cos(2) - 1)


more