Question

In SymPy, is it possible to apply limits to an indefinite integral and evaluate it?

import sympy
from sympy.abc import theta

y = sympy.sin(theta)

Y_indef = sympy.Integral(y)
Y_def = sympy.Integral(y, (theta, 0, sympy.pi / 2))

Y_def.evalf() produces a number.

I'm looking for something like Y_indef.evalf((theta, 0, sympy.pi/2)) to get the same answer.

Was it helpful?

Solution

I do not know of a direct way, however you can extract the information from Y_indef in order to create a definite integral:

>>> indef = Integral(x)
>>> to_be_integrated, (free_var,) = indef.args
>>> definite = Integral(to_be_integrated, (free_var, 1, 2))

.args is a general attribute containing anything needed to construct most SymPy objects.

Edit: To address the comments to the questions.

  1. SymPy may succeed evaluating definite integral and at the same time fail to solve their indefinite version. This is due to the existence of additional algorithms to be applied to definite integrals.

  2. Both definite and indefinite integrals are instances of the same class. The only difference is what they contain in their .args. The need for different classes is not yet felt, given that SymPy mostly uses Integral as a flag to say that it can not solve the integral (i.e. the integrate function returns Integral when all of the implemented algorithms fail).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top