A310 of 29
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

A3 · Lesson 10 · Lab: complex numbers in Python

Python already speaks complex numbers

Python has complex numbers built in. Type 3+4j and you get the point (3,4), with the engineer's j and everything. The cmath module adds the complex versions of sqrt, exp and the conversions to polar form. In this lab you'll use them to solve any quadratic, find all the roots of a complex number, and watch a ringing circuit's natural response decay exactly as its roots say it should. If you haven't started A4 yet, its lessons on functions, loops and lists (lessons 1 to 8) cover the Python used here.

  • The curve is Reest for a root s=−σ+jω: a ringing response.
  • Move σ and ω. The dashed envelope is e−σt, and the curve rings at ω inside it.
  • The readout gives the ratio of one peak to the next. It depends on σ and ω together, through 2πσ/ω.

Predict

What does Python print for (1j)**2?

Name it

Complex numbers in Python

  • Making one: z = 3 + 4j, or complex(3, 4). The j must follow a number directly, so write 1j, never j.
  • Parts: z.real is 3.0, z.imag is 4.0, and z.conjugate() is (3-4j).
  • Length: abs(z) gives 5.0.
  • Arithmetic: +, -, *, / and ** all work as in the lessons.

The cmath module

import cmath adds:

  • cmath.phase(z): the angle in radians, from −π to π (it uses atan2, so the quadrant is right);
  • cmath.polar(z): the pair (r, angle);
  • cmath.rect(r, angle): back from polar, with the angle in radians;
  • cmath.exp(z), cmath.sqrt(z) and cmath.log(z): these accept complex numbers, where the math versions raise an error.

math.degrees and math.radians convert angles.

The pieces this lab builds

  • Roots of as2+bs+c: with cmath.sqrt, the quadratic formula works whatever the sign of b2−4ac.
  • The n roots of z: r1/nej(θ+2πk)/n for k=0,1,…,n−1.
  • A natural response: Reest sampled at many times. Its successive peaks shrink by exactly e−2πσ/ω, because they're one period, 2π/ω, apart.

Worked example

What does Python give for cmath.polar(-4+3j), and what is the angle in degrees?

Show the solution
  1. Python's answer. It gives (5.0, 2.498...): the length is 16+9=5 and the angle is 2.498 rad.
  2. Convert to degrees. math.degrees(2.498) is 143.13°, in the second quadrant where (−4,3) sits.
  3. The wrong way. A plain math.atan(3 / -4) would give −36.87°, in the wrong quadrant.

Answer (5.0, 2.498...), a length of 5 and an angle of 2.498 rad, which is 143.13°.

Worked example

Find the cube roots of 8j the way the lab's function will.

Show the solution
  1. Polar form. 8j has length 8 and angle 90°.
  2. Length of the roots. 81/3=2
  3. Angles of the roots. For k=0,1,2: 90°+360°k3=30°,150°,270°
  4. Rectangular form. They're 1.732+j, −1.732+j and −2j.
  5. Check in Python. Cubing any of them, as in (-2j)**3, gives back 8j (to rounding).

Answer the cube roots are 1.732+j, −1.732+j and −2j.

Worked example

A response has s=−50+j628 per second. By what factor does each peak shrink?

Show the solution
  1. Time between peaks. The peaks are one period apart: T=2π628s=10ms
  2. Envelope over one period. Over that time the envelope e−50t falls by e−50×0.01=e−0.5=0.607

Answer each peak is 60.7% of the one before.

Check

What does cmath.phase return for the complex number −3+(3)j?

Check

A natural response is Reest with s=−50+j300. What is the ratio of each peak to the one before?

Check

Write polar_deg(z), returning (r, degrees), and from_polar_deg(r, deg), returning the complex number. Use abs, cmath.phase, cmath.rect and math.radians/math.degrees.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write quadratic_roots(a, b, c), returning the two roots of as2+bs+c=0 as a pair, the one with the + sign first. Use cmath.sqrt so it works for any discriminant.

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(z, n), returning a list of all n roots of z, in order of k=0,1,…,n−1: length |z|1/n and angle (θ+2πk)/n, with θ = cmath.phase(z).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write peak_ratio(s, T, steps). It samples v(t)=Reest at steps evenly spaced times from 0 to T, finds the local maxima (samples bigger than both neighbors), and returns the average ratio of each peak to the one before. The test checks it against e−2πσ/ω.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does math.sqrt(-4) raise an error while cmath.sqrt(-4) returns 2j? When is each the right tool?

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.