TExcelTAD
Home › Calculus in Depth › Integration with Tables and Computer Algebra

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 (a>0a > 0):

# Form Antiderivative
T1 ∫u2−a2 du\int \sqrt{u^2 - a^2}\,du u2u2−a2−a22ln⁡∣u+u2−a2∣+C\frac{u}{2}\sqrt{u^2 - a^2} - \frac{a^2}{2} \ln\lvert u + \sqrt{u^2 - a^2} \rvert + C
T2 ∫duua+bu\int \frac{du}{u\sqrt{a + bu}} 1aln⁡∣a+bu−aa+bu+a∣+C\frac{1}{\sqrt{a}} \ln\Big\lvert \frac{\sqrt{a + bu} - \sqrt{a}}{\sqrt{a + bu} + \sqrt{a}} \Big\rvert + C
T3 ∫dua2+u2\int \frac{du}{a^2 + u^2} 1atan⁡−1ua+C\frac{1}{a} \tan^{-1}\frac{u}{a} + C
T4 ∫eausin⁡bu du\int e^{au} \sin bu\,du eau(asin⁡bu−bcos⁡bu)a2+b2+C\frac{e^{au}(a \sin bu - b \cos bu)}{a^2 + b^2} + C

Example. ∫9x2−4 dx\int \sqrt{9x^2 - 4}\,dx. With u=3xu = 3x, du=3 dxdu = 3\,dx, and a=2a = 2, it is 13∫u2−4 du\frac{1}{3}\int \sqrt{u^2 - 4}\,du; T1 gives

13[3x29x2−4−2ln⁡∣3x+9x2−4∣]+C=x29x2−4−23ln⁡∣3x+9x2−4∣+C\begin{aligned} &\frac{1}{3}\Big[\frac{3x}{2}\sqrt{9x^2 - 4} - 2 \ln\lvert 3x + \sqrt{9x^2 - 4} \rvert\Big] + C \\ &= \frac{x}{2}\sqrt{9x^2 - 4} - \frac{2}{3} \ln\lvert 3x + \sqrt{9x^2 - 4} \rvert + C \end{aligned}

Differentiating confirms the table entry and our matching:

python · try it yourself
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:

python · try it yourself
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 sin⁡xcos⁡x\sin x \cos x the two answers differ by the constant 12\frac{1}{2} (since sin⁡2x2+cos⁡2x2=12\frac{\sin^2 x}{2} + \frac{\cos^2 x}{2} = \frac{1}{2}); both are correct. Wherever the difference A−BA - B 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: sin⁡2x2\frac{\sin^2 x}{2}, −cos⁡2x2-\frac{\cos^2 x}{2} and −cos⁡2x4-\frac{\cos 2x}{4} are all antiderivatives of sin⁡xcos⁡x\sin x \cos x. For sec⁡x\sec x the default answer contains log(sin(x) - 1), the logarithm of a negative number on (−π/2,π/2)(-\pi/2, \pi/2), so the difference is the complex constant −πi/2≈−1.5708i-\pi i/2 \approx -1.5708i: 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:

python · try it yourself
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 aa might be imaginary) and a Piecewise that separates a=−1a = -1 (the ln⁡x\ln x 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 ∣…∣\lvert \ldots \rvert complex log; equals ln⁡∣x−1∣\ln\lvert x - 1 \rvert 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:

python · try it yourself
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, codegen to 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=True is 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 ∫dx25+4x2\int \frac{dx}{25 + 4x^2}.

Answer

110tan⁡−12x5+C\frac{1}{10} \tan^{-1}\frac{2x}{5} + C.

Solution

u=2xu = 2x, du=2 dxdu = 2\,dx, a=5a = 5: 12∫du25+u2=12⋅15tan⁡−1u5+C\frac{1}{2}\int \frac{du}{25 + u^2} = \frac{1}{2} \cdot \frac{1}{5} \tan^{-1}\frac{u}{5} + C.

Exercise 2: Use T4

Compute ∫e2xsin⁡3x dx\int e^{2x} \sin 3x\,dx from T4 and check it with SymPy.

Answer

e2x(2sin⁡3x−3cos⁡3x)13+C\frac{e^{2x}(2\sin 3x - 3\cos 3x)}{13} + C.

Solution

a=2a = 2, b=3b = 3, a2+b2=13a^2 + b^2 = 13.

python · try it yourself
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 ln⁡∣sec⁡x+tan⁡x∣\ln\lvert \sec x + \tan x \rvert and 12ln⁡1+sin⁡x1−sin⁡x\frac{1}{2} \ln\frac{1 + \sin x}{1 - \sin x} differ by a constant on (−π/2,π/2)(-\pi/2, \pi/2).

Answer

They are in fact equal there (the constant is 0).

Solution

sec⁡x+tan⁡x=1+sin⁡xcos⁡x\sec x + \tan x = \frac{1 + \sin x}{\cos x}, and (1+sin⁡x)2cos⁡2x=(1+sin⁡x)2(1−sin⁡x)(1+sin⁡x)=1+sin⁡x1−sin⁡x\frac{(1 + \sin x)^2}{\cos^2 x} = \frac{(1 + \sin x)^2}{(1 - \sin x)(1 + \sin x)} = \frac{1 + \sin x}{1 - \sin x}. Taking 12ln⁡\frac{1}{2} \ln of the square gives ln⁡∣sec⁡x+tan⁡x∣\ln\lvert \sec x + \tan x \rvert.

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
python · try it yourself
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 aa 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?

  1. They differ by a constant (both are antiderivatives)
  2. One of them is wrong
  3. manual=True is always wrong
  4. The integral diverges

Why might SymPy return a Piecewise for ∫xa dx\int x^a\,dx?

  1. It is a bug
  2. xx must be negative
  3. The answer differs when a=−1a = -1 (then it is ln⁡x\ln x)
  4. aa must be an integer

SymPy returns Integral(sin(sin(x)), x) unevaluated. Best next step for ∫0πsin⁡(sin⁡x) dx\int_0^{\pi} \sin(\sin x)\,dx?

  1. Give up
  2. Evaluate it numerically (evalf or scipy quad)
  3. Differentiate it
  4. Use partial fractions