# Revision history [back]

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[0],lower_bound[0],upper_bound[0])
inputs_inner=[variables[0],lower_bound[0],upper_bound[0]]
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)


have lower_bound=[None,3],upper_bound[3,6], that is you can have only one of the bounds be definite.

Was this all you were looking for?

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[0],lower_bound[0],upper_bound[0])
inputs_inner=[variables[0],lower_bound[0],upper_bound[0]]
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)


have lower_bound=[None,3],upper_bound[3,6], that is you can have only one of the bounds be definite.

Was this all you were looking for?