Integration with Tables and Computer Algebra
Nobody integrates everything by hand. Engineers used printed tables of integrals for two centuries; today a computer algebra system (CAS) such as SymPy, Mathematica or Maple does the work. Both are only as good as your ability to match a problem to their form and to read what comes back. This lesson shows how to use a table formula, and how to interpret CAS output: different-looking answers, hidden assumptions about symbols, piecewise results, and integrals the CAS cannot do.
Using a table
A table lists antiderivatives of general forms with parameters. Matching a problem to a form usually needs a substitution. A few typical entries ():
| # | Form | Antiderivative |
|---|---|---|
| T1 | ||
| T2 | ||
| T3 | ||
| T4 |
Example. . With , , and , it is ; T1 gives
Differentiating confirms the table entry and our matching:
import sympy as sp
x = sp.symbols('x', positive=True)
f = sp.sqrt(9*x**2 - 4)
F = x / 2 * sp.sqrt(9*x**2 - 4) - sp.Rational(2, 3) * sp.log(3*x + sp.sqrt(9*x**2 - 4))
print("F' - f simplifies to:", sp.simplify(sp.diff(F, x) - f))
print("numeric check at x = 2:", sp.N(sp.diff(F, x).subs(x, 2)), "vs", sp.N(f.subs(x, 2)))F' - f simplifies to: 0
numeric check at x = 2: 5.65685424949238 vs 5.65685424949238
A CAS: two engines, two forms
SymPy's default integrate uses several algorithms (table-like heuristics, the Risch algorithm, Meijer G-functions) and picks whatever succeeds first. With manual=True it uses only the hand-style rules of the strategy lesson (substitution, parts, trig rules), which often gives an answer closer to what a person writes. The two answers can look quite different:
import sympy as sp
x = sp.symbols('x')
for f in [sp.sin(x) * sp.cos(x), sp.tan(x)**3, x / sp.sqrt(1 + x), sp.sec(x)]:
A = sp.integrate(f, x)
B = sp.integrate(f, x, manual=True)
d = [complex(sp.N((A - B).subs(x, v))) for v in (0.3, 0.9)]
diff = [complex(round(z.real, 8) + 0.0, round(z.imag, 8) + 0.0) for z in d]
print(f"∫ {f} dx")
print(f" default : {A}")
print(f" manual=True: {B}")
print(f" A - B at x = 0.3 and 0.9: {diff}")∫ sin(x)*cos(x) dx
default : sin(x)**2/2
manual=True: -cos(x)**2/2
A - B at x = 0.3 and 0.9: [(0.5+0j), (0.5+0j)]
∫ tan(x)**3 dx
default : log(cos(x)) + 1/(2*cos(x)**2)
manual=True: -log(sec(x)**2)/2 + sec(x)**2/2
A - B at x = 0.3 and 0.9: [0j, 0j]
∫ x/sqrt(x + 1) dx
default : 2*x*sqrt(x + 1)/3 - 4*sqrt(x + 1)/3
manual=True: 2*(x + 1)**(3/2)/3 - 2*sqrt(x + 1)
A - B at x = 0.3 and 0.9: [0j, 0j]
∫ sec(x) dx
default : -log(sin(x) - 1)/2 + log(sin(x) + 1)/2
manual=True: log(tan(x) + sec(x))
A - B at x = 0.3 and 0.9: [-1.57079633j, -1.57079633j]
The differences are printed as Python complex numbers (0j is zero). For the two answers differ by the constant (since ); both are correct. Wherever the difference is the same at two points (and in fact at all points of an interval), the two antiderivatives agree up to a constant. Learn to expect this: , and are all antiderivatives of . For the default answer contains log(sin(x) - 1), the logarithm of a negative number on , so the difference is the complex constant : still a constant, but a reminder that SymPy's log is the complex logarithm.
Assumptions about symbols
A CAS cannot assume a parameter is positive, real or nonzero unless you say so, and the answer changes accordingly:
import sympy as sp
x = sp.symbols('x')
a = sp.symbols('a') # could be anything, even complex
ap = sp.symbols('a', positive=True)
print("∫ 1/(x²+a²), a unknown :", sp.integrate(1 / (x**2 + a**2), x))
print("∫ 1/(x²+a²), a > 0 :", sp.integrate(1 / (x**2 + ap**2), x))
print("∫ x^a dx, a unknown :", sp.integrate(x**a, x))
print("∫ x^a dx, a > 0 :", sp.integrate(x**ap, x))∫ 1/(x²+a²), a unknown : (-I*log(-I*a + x)/2 + I*log(I*a + x)/2)/a
∫ 1/(x²+a²), a > 0 : atan(x/a)/a
∫ x^a dx, a unknown : Piecewise((x**(a + 1)/(a + 1), Ne(a, -1)), (log(x), True))
∫ x^a dx, a > 0 : x**(a + 1)/(a + 1)
With no assumptions, SymPy returns complex logarithms (correct, since might be imaginary) and a Piecewise that separates (the case). Declaring positive=True produces the textbook forms. Many "wrong-looking" CAS answers are correct answers to a more general question than the one you meant.
Watch for
| CAS output | Meaning | What to do |
|---|---|---|
log(x - 1) without |
complex log; equals plus a constant on each interval | fine for real work on one interval |
Piecewise(…) |
answer depends on a parameter's value | add assumptions, or pick the branch you need |
Integral(…) returned unevaluated |
no closed form found (maybe none exists) | integrate numerically, or check for special functions |
erf, Si, li, hyper, meijerg |
special functions | evaluate with .evalf() or scipy.special |
| a huge expression | correct but unsimplified | simplify, factor, trigsimp; or compare numerically |
An integral SymPy cannot do, and the right fallback:
import sympy as sp
from scipy.integrate import quad
import math
x = sp.symbols('x')
I = sp.integrate(sp.sin(sp.sin(x)), x)
print("SymPy returns:", I)
print("definite value on [0, π]:", round(quad(lambda t: math.sin(math.sin(t)), 0, math.pi)[0], 12))
print("SymPy numerically :", sp.Integral(sp.sin(sp.sin(x)), (x, 0, sp.pi)).evalf(13))SymPy returns: Integral(sin(sin(x)), x)
definite value on [0, π]: 1.78648748195
SymPy numerically : 1.786487481950
In software
- Symbolic + numeric pipelines. Scientific code often derives formulas symbolically once (SymPy), then generates fast numeric code from them (
lambdify,codegento C/Fortran, JAX). Integration done symbolically removes a numerical quadrature from an inner loop. - Verification. "Differentiate the CAS answer and compare numerically at random points" is a cheap, robust test, the same idea as property-based testing.
- Assumptions are types. Declaring
positive=Trueis like a type annotation: it lets the system choose the specialised, simpler result, and forgetting it leads to general (and confusing) output.
Exercises
Exercise 1: Match a table entry
Use T3 to compute .
Answer
.
Solution
, , : .
Exercise 2: Use T4
Compute from T4 and check it with SymPy.
Answer
.
Solution
, , .
import sympy as sp
x = sp.symbols('x')
F = sp.exp(2*x) * (2*sp.sin(3*x) - 3*sp.cos(3*x)) / 13
print(sp.simplify(sp.diff(F, x) - sp.exp(2*x) * sp.sin(3*x)))0
Exercise 3: Equal up to a constant?
Show that and differ by a constant on .
Answer
They are in fact equal there (the constant is 0).
Solution
, and . Taking of the square gives .
Exercise 4: Assumptions
What does sp.integrate(sp.exp(-a*x), (x, 0, sp.oo)) return when a has no assumptions, and when a is positive?
Answer
Without assumptions a Piecewise (with a convergence condition on a); with positive=True, 1/a.
Solution
import sympy as sp
x = sp.symbols('x')
a = sp.symbols('a')
ap = sp.symbols('a', positive=True)
print(sp.integrate(sp.exp(-a*x), (x, 0, sp.oo)))
print(sp.integrate(sp.exp(-ap*x), (x, 0, sp.oo)))Piecewise((1/a, Abs(arg(a)) < pi/2), (Integral(exp(-a*x), (x, 0, oo)), True))
1/a
The improper integral only converges when the real part of is positive, so SymPy must state the condition.
Test yourself
integrate(f, x) and integrate(f, x, manual=True) return different-looking answers. What is most likely?
- They differ by a constant (both are antiderivatives)
- One of them is wrong
- manual=True is always wrong
- The integral diverges
Why might SymPy return a Piecewise for ?
- It is a bug
- must be negative
- The answer differs when (then it is )
- must be an integer
SymPy returns Integral(sin(sin(x)), x) unevaluated. Best next step for ?
- Give up
- Evaluate it numerically (evalf or scipy quad)
- Differentiate it
- Use partial fractions