Integration by Parts
Integration by parts handles many products that substitution cannot, such as , or . It is the product rule for derivatives turned into a rule for integrals: you trade the integral you have for one that is (hopefully) easier.
The formula
The product rule says . Integrate both sides and rearrange:
You split the integrand into two pieces: (which you will differentiate) and (which you will integrate). The method pays off when is simpler than and is no worse than .
Example. . Choose (it becomes simpler) and :
Choosing the other way round (, ) gives , which is worse. If that happens, swap.
Choosing u: LIATE
A handy order for picking , first choice first:
| Letter | Type | Examples |
|---|---|---|
| L | Logarithmic | |
| I | Inverse trig | , |
| A | Algebraic | , , polynomials |
| T | Trigonometric | , |
| E | Exponential | , |
It is a rule of thumb, not a law, but it works for most textbook products.
Classic cases
The last one is the "boomerang": after two rounds, , so .
import sympy as sp
x = sp.symbols("x")
for f in [x * sp.exp(x), sp.log(x), x**2 * sp.sin(x), sp.exp(x) * sp.sin(x)]:
F = sp.integrate(f, x)
ok = sp.simplify(sp.diff(F, x) - f) == 0
print(f"∫ {str(f):14} dx = {str(F):40} check: {ok}")∫ x*exp(x) dx = (x - 1)*exp(x) check: True
∫ log(x) dx = x*log(x) - x check: True
∫ x**2*sin(x) dx = -x**2*cos(x) + 2*x*sin(x) + 2*cos(x) check: True
∫ exp(x)*sin(x) dx = exp(x)*sin(x)/2 - exp(x)*cos(x)/2 check: True
For definite integrals the part is evaluated at the limits: .
In software: expected values and factorials
The mean of an exponential distribution. Waiting times (time to the next request, the next failure) are often modelled with the density , . The mean is . With , , :
So a server with on average requests per second has a mean gap of 0.25 s.
import math
from scipy.integrate import quad
lam = 4.0
mean, _ = quad(lambda x: x * lam * math.exp(-lam * x), 0, math.inf)
print(f"E[X] by integration = {mean:.6f}, 1/λ = {1 / lam}")
# Gamma function: repeated integration by parts gives ∫ x^n e^-x dx = n!
for n in [3, 5, 8]:
val, _ = quad(lambda x: x**n * math.exp(-x), 0, math.inf)
print(f"n={n}: integral = {val:.4f} n! = {math.factorial(n)}")E[X] by integration = 0.250000, 1/λ = 0.25
n=3: integral = 6.0000 n! = 6
n=5: integral = 120.0000 n! = 120
n=8: integral = 40320.0000 n! = 40320
The second result is the Gamma function: each round of parts lowers the power by one and multiplies by it, , which is why . Python's math.gamma extends factorials to non-integers this way, and many probability distributions used in ML (Gamma, Beta, Student t) have it in their formulas.
Note: For repeated parts with a polynomial times or , the tabular method saves writing: list derivatives of the polynomial down to 0 in one column, integrals of the other factor in the next, and combine diagonals with alternating signs .
Exercises
Exercise 1: One round
Find .
Answer
Solution
Exercise 2: Logarithm times x
Find .
Answer
Solution
Exercise 3: Definite
Evaluate .
Answer
Solution
Exercise 4: Tabular method
Find with the tabular method and check it with SymPy.
Answer
Solution
derivatives of x³: x³ 3x² 6x 6 0
integrals of eˣ: eˣ eˣ eˣ eˣ
signs: + − + −
result: x³eˣ − 3x²eˣ + 6xeˣ − 6eˣ + Cimport sympy as sp
x = sp.symbols("x")
mine = sp.exp(x) * (x**3 - 3 * x**2 + 6 * x - 6)
print(sp.simplify(sp.diff(mine, x) - x**3 * sp.exp(x)) == 0)True
Test yourself
For , which choice of follows LIATE?
Integration by parts comes from which rule?
- The product rule
- The chain rule
- The quotient rule
- L'Hôpital's rule
What is ?
- 4
- 1
- 120
- 24