Symbolic Integration By Parts And Substitution With Python

How to prevent SymPy from doing more than it should

Mathcube
4 min readDec 30, 2021
Photo by Brett Jordan on Unsplash

My favorite Python package for symbolic computation is SymPy. Like other computer algebra systems, it facilitates tedious computations enormously. But often, for my taste, it does too much at once! For instance, suppose you have a complicated integral to evaluate, and you plug it into SymPy, turn the crank and SymPy spits out the end result. So far so good, but maybe that’s more than you wanted! Maybe you just wanted SymPy to do a little simplification of the integral so that you know better what’s going on. A little simplification like integration by parts and substitution.

Integral Substitution

Suppose we have the following integral

from sympy.abc import *
from sympy import *
Ii = Integral(exp(-t**2)*t**(2), (t, 0, oo))

If we tell SymPy to do it, then it spits out the end result:

Ii.doit()

Ok, but why square root of 𝜋? The end result is not particularly enlightening. Instead, I would like to make SymPy help me solve the integral by hand! Specifically, with the present integral, I would like to do a substitution first and see where it leads me. The substitution I want is 𝑡=√u. How can we do…

--

--