A12 · Lesson 9 · Lab: complex functions in code
Checking analyticity, branches, roots and maps numerically
Lessons 1 to 8 built up the complex derivative, the Cauchy–Riemann equations, branches of the logarithm, roots and bilinear maps. Python works with complex numbers directly (1 + 2j, with cmath for functions), so each of those ideas can be tested by computation. In this lab you'll take derivatives in two directions to see analyticity hold and fail, pick branches of the logarithm, place a filter's poles as roots, check the Smith chart's circles to rounding error, and measure how a conformal map stretches area.
- A central difference has an error that shrinks as . For the error is exactly .
- The curve shows that error against h on log axes, together with rounding error, which grows as with .
- The two meet near , where the total error is smallest. That's the step the lab uses.
Predict
You estimate the derivative of at a point once by stepping along the real axis and once by stepping along the imaginary axis. What do you get?
1 and −1. Along the real axis, , so the quotient is 1. Along the imaginary axis, , and dividing by gives −1. A complex derivative must not depend on the direction, so has none, anywhere.
Name it
Complex numbers in Python
Write as 1j. abs(z) is the size, cmath.phase(z) the principal angle in , z.conjugate() the conjugate, and cmath.exp, cmath.log and cmath.sqrt the principal-branch functions. NumPy arrays of complex numbers work the same way, element by element.
Testing analyticity numerically
Take central differences along x and along y: and . For an analytic f both approach , and their difference is about . For a non-analytic f they differ by a fixed amount however small h gets.
Checking a map
To test a claim like "this line maps onto that circle", map many points along the line and compute the largest distance from the circle. A correct claim leaves only rounding error, around ; a wrong one leaves a visible gap.
Worked example
Find the poles of a fourth-order Butterworth filter with numerically.
Show the solution
- The equation. The poles are the left-half-plane roots of
- Find them in code. Compute all eight roots with
r = [cmath.exp(1j*(math.pi + 2*math.pi*k)/8) for k in range(8)], then keep those with negative real part. - The four poles. at angles of 112.5° and 157.5° and their mirror images.
- Multiply out the pairs. gives the familiar Butterworth polynomial; the coefficients and come straight from the pole angles.
Answer the poles are and , at angles of 112.5° and 157.5° and their mirror images.
Worked example
Check numerically that maps the line onto the circle .
Show the solution
- Map a long stretch of the line.
y = np.linspace(-100, 100, 4001), thenw = 1/(0.5 + 1j*y). - Measure the worst miss.
np.max(np.abs(np.abs(w - 1) - 1))comes out about : every mapped point lies on the circle to rounding error. - A control. Test against the wrong circle : the largest gap is then 0.1.
- Why bother. Numerical checks like this catch sign slips and wrong centers quickly, before they reach a design.
Answer every mapped point is within about of the circle , which is rounding error.
Check
For , the central difference equals exactly. What is its error with h = 0.001?
Check
maps a tiny square near . By what factor does the square's area grow?
Check
Write deriv_x(f, z, h) and deriv_y(f, z, h), central differences stepping along the real and imaginary directions. With h = 1e-5, set a_x, a_y for at , and b_x, b_y for there.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write log_branch(z, k) returning . Use it to set jj0 to on the principal branch and jj1 on the branch k = 1, both as .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write nth_roots(w, n) returning the n roots of w. Set poles to the left-half-plane roots of , the poles of a fourth-order Butterworth filter, sorted by imaginary part.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Map the constant-resistance line , (2001 points), through . Set dev to the largest distance of the image from the circle with center and radius , and back to the largest error after mapping back with .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write image_area(f, z0, h): map the corners of the square with corners , , , through f and return the area of the resulting quadrilateral (the shoelace formula). Set ratio to that area over for , and .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why is the best step for a numerical derivative neither very large nor very small?
A difference quotient has two kinds of error. Truncation error comes from the curvature the formula ignores and shrinks as the step gets smaller, as for a central difference. Rounding error comes from subtracting two nearly equal function values, each stored to about 16 digits; that absolute error, about times the function's size, is then divided by h, so it grows as the step shrinks. The total is smallest where the two are about equal, which for a central difference is near , about for a function of ordinary size, with an error around .
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.