November 23, 2024

Symbolic Integration in Python by Using SymPy Library


In this Python and scientific computing tutorial, we will learn how to perform symbolic integration in Python. We will learn how to compute definite and indefinite integrals in Python by using the symbolic Python library called SymPy. The YouTube tutorial accompanying this post is given here.

Indefinite Integrals in Python

First, we explain how to implement this integral

(1)   \begin{align*}\int x^{2} \mathrm{d}x =\frac{x^{3}}{3}+C\end{align*}

where C is a constant. We compute this integral in Python by using this Python script:

# import all the packages
from sympy import *
# use this function for nice priting of symbolic expressions
init_printing()

# first test, let us integrate y=x^2 function 
x=Symbol('x')
function1=x**2
y=integrate(function1,x)

First, we import all functions and objects from the SymPy library. We call the function init_printing() to nicely print symbolic expressions. We define the symbolic variable x, by using “x=Symbol(‘x’)”. Then, we define our quadratic function, which we call function1. Finally, we integrate the function by using SymPy function “integrate”. The first argument of this function is the function that we want to integrate, and the second argument is the variable that is used for integration. The function “integrate()” returns the function that is obtained after integration.

We can double-check this result by differentiating the function y:

# let us double check, by differentiating the function y
function2=diff(y,x)

function2-function1

We use the SymPy function “diff()” to perform differentiation. The first argument of this function is the function, and the second input argument is the variable that is used for differentiation.

Next, let us solve a more complex integral. We solve the following integral that is usually analytically solved by using the substitution:

(2)   \begin{align*}\int \frac{x}{\sqrt{1-4x^{2}}} \mathrm{d}x =-\frac{1}{4}\sqrt{1-4x^{2}}+C\end{align*}

where C is an integration constant. We can solve this integral in Python by using the script given below


function2=x/sqrt(1-4*x**2)
y2=integrate(function2,x)

function3=diff(y2,x)

function3-function2

Here, we also double-checked the results by differentiating the integrated function.

Next, we solve the following integral

(3)   \begin{align*}\int \tan (x) \mathrm{d}x =\int \frac{\sin (x)}{\cos (x)} \mathrm{d}x = - \ln |\cos(x)| + C\end{align*}

# let us now try to solve tan(x) integral 
function4=tan(x)
y3=integrate(function4,x)

# capital E is the e number...
log(E)
# log is actually ln

function5=diff(y3,x)
simplify(function5-function4)

Here, we have to make a few important comments. The integrate function will return -\log(\cos(x)). The \log() function is actually \ln(\cdot) function. That is, by default, the base of \log() in Python is e. Then, we can observe that the Sympy function is not returning the absolute value.

Definite Integrals in Python

Let us first learn how to integrate a constant

(4)   \begin{align*}\int_{1}^{3} 1\;\mathrm{d}x= x \Big|_{1}^{3}= 3-1=2\end{align*}

# import all the packages
from sympy import *
# use this function for nice priting of symbolic expressions
init_printing()

# first test, let us integrate y=x^2 function 
x=Symbol('x')
# compute definite integral 
a=1
b=3
integrate(1 ,(x, a, b))

We use the function “integrate()”. The first argument is the function that needs to be integrated and the second argument is the tuple. The first entry of the tuple is the symbolic variable that is used for integration, a is the lower bound for integration, and b is the upper bound for integration.

Next, let us compute this integral

(5)   \begin{align*}\int_{0}^{2\pi} \cos(x)\;\mathrm{d}x= \sin(x) \Big|_{0}^{2\pi}= 0\end{align*}

# let us now try this
integrate(cos(x),(x,0,2*pi))

Next, let us solve this integral

(6)   \begin{align*}\int_{0}^{3} x\;\mathrm{d}x= \frac{x^{2}}{2} \Big|_{0}^{3}= \frac{9}{2}\end{align*}

We solve this integral by using the following script:

# let us try this 
integrate(x,(x,0,3))