Representations of Functions as Power Series
Starting from one series you already know, the geometric series, you can build series for , and many more by substituting, multiplying, differentiating and integrating term by term. This lesson states the theorem that allows that, sketches why it holds, and uses the results to compute and to see why numerical libraries ship a log1p function.
The Power Series lesson covered radius and interval of convergence; read it first.
The seed: the geometric series
Algebraic substitutions turn it into series for related functions. The radius follows from the substitution.
| Function | Substitution | Series | Valid for |
|---|---|---|---|
| , times |
Term-by-term differentiation and integration
Theorem. If has radius of convergence , then is differentiable on and
and both new series have the same radius .
Why the radius does not change. By Cauchy–Hadamard, . Multiplying by or dividing by multiplies the -th root by or , and both tend to 1 (take logs: ). So the lim sup, and hence , is unchanged.
Why the derivative is the series of derivatives (sketch). Fix with . On the terms obey , and converges. So the series converges uniformly there (the Weierstrass M-test), and the same holds for the differentiated series. Uniform convergence lets limits pass through integrals: integrating the differentiated series term by term gives back , and the Fundamental Theorem of Calculus then says equals the differentiated series. A full proof is in Proofs of Theorems.
Note: Endpoints can change. converges on , but its derivative converges only on . Integration can gain endpoints, differentiation can lose them, so recheck them each time.
Two important series
. Integrate from 0 to :
At the series still converges (alternating harmonic), and Abel's theorem on continuity at the endpoint gives .
. Integrate :
At this is Leibniz's series .
import sympy as sp
x, t = sp.symbols("x t")
geo = sum((-t**2) ** k for k in range(6)) # 1/(1 + t²) up to t¹⁰
arctan_poly = sp.integrate(geo, (t, 0, x))
print("integrated:", arctan_poly)
print("SymPy :", sp.series(sp.atan(x), x, 0, 12).removeO())
print("difference:", sp.expand(arctan_poly - sp.series(sp.atan(x), x, 0, 12).removeO()))integrated: -x**11/11 + x**9/9 - x**7/7 + x**5/5 - x**3/3 + x
SymPy : -x**11/11 + x**9/9 - x**7/7 + x**5/5 - x**3/3 + x
difference: 0
SymPy lists terms from highest to lowest degree; the difference 0 confirms the two derivations agree.
Computing with arctan series
Leibniz's series converges far too slowly (the computed is off by about after terms, as the run below shows). The fix is to evaluate arctan only at small , where shrinks fast. Machin's formula (1706)
follows from the tangent addition formula. Each term of gains about 1.4 decimal digits (a factor 25 per term).
from fractions import Fraction
import math
def arctan_series(x, terms):
return sum(Fraction((-1) ** n, 2 * n + 1) * x ** (2 * n + 1) for n in range(terms))
for N in (5, 10, 20):
leibniz = 4 * float(arctan_series(Fraction(1), N))
machin = 4 * (4 * arctan_series(Fraction(1, 5), N) - arctan_series(Fraction(1, 239), N))
print(f"N={N:2d} Leibniz error={abs(leibniz - math.pi):.1e} Machin error={abs(float(machin) - math.pi):.1e}")N= 5 Leibniz error=2.0e-01 Machin error=2.9e-08
N=10 Leibniz error=1.0e-01 Machin error=1.3e-15
N=20 Leibniz error=5.0e-02 Machin error=0.0e+00
Twenty Machin terms already exhaust double precision (the printed error 0 means "below float resolution"). Machin-type formulas with exact rational or big-integer arithmetic computed to record lengths for 250 years; today's records use faster series (Chudnovsky), but the principle, a rapidly convergent power series evaluated at a small argument, is the same.
In software: log1p and expm1
For tiny , computing log(1 + x) in floating point first rounds to the nearest double, destroying most of 's digits. The series shows the true answer is almost exactly , so a library can return the series directly for small arguments. That is what math.log1p (and np.log1p, and C's log1p) is for; exists for the same reason.
import math
for x in (1e-5, 1e-10, 1e-15):
naive = math.log(1 + x)
series = x - x**2 / 2 + x**3 / 3
print(f"x={x:.0e} log(1+x)={naive:.15e} log1p={math.log1p(x):.15e} series={series:.15e}")x=1e-05 log(1+x)=9.999950000398841e-06 log1p=9.999950000333332e-06 series=9.999950000333334e-06
x=1e-10 log(1+x)=1.000000082690371e-10 log1p=9.999999999500001e-11 series=9.999999999500001e-11
x=1e-15 log(1+x)=1.110223024625156e-15 log1p=9.999999999999995e-16 series=9.999999999999995e-16
At the naive result is 11% too large, while log1p and the three-term series agree to the last digit. Machine-learning code meets this constantly: the softplus activation , log-likelihoods of probabilities near 1, and binary cross-entropy are written with log1p to stay accurate (see Logistic Regression).
Exercises
Exercise 1: Substitute
Find a power series for and its radius of convergence.
Answer
, .
Solution
Exercise 2: Differentiate
Differentiate the geometric series to find in closed form, then evaluate .
Answer
for , so .
Solution
print(sum(n / 3**n for n in range(1, 60)))0.75
Exercise 3: Integrate for a logarithm
Find the Maclaurin series of and use it to compute with 5 terms. (Hint: when .)
Answer
for ; 5 terms at give , error about .
Solution
This converges much faster than the series at , because makes the terms shrink by a factor 9 each time.
import math
x = 1 / 3
approx = 2 * sum(x ** (2 * n + 1) / (2 * n + 1) for n in range(5))
print(f"{approx:.6f} error {abs(approx - math.log(2)):.1e}")0.693146 error 1.1e-06
Exercise 4: Endpoint behaviour
The series for converges at . Does the series for its derivative, , converge at ?
Answer
No: it becomes , which diverges.
Solution
The terms do not tend to 0, so the Test for Divergence applies. The radius stays 1, but differentiating lost the endpoint (and ), as the note above warned.
Test yourself
has radius 4. What is the radius of the series for ?
- 2
- 4
- 16
- It depends on the
Why is Machin's formula much faster than Leibniz's series for ?
- It uses more terms
- It avoids fractions
- It evaluates arctan at small arguments, so shrinks fast
- It is not alternating
Why does math.log1p(x) exist when log(1 + x) already works?
- It is faster for large
- For tiny , rounds and loses 's digits; log1p uses the series directly
- It handles negative logarithms
- It returns exact fractions