Chapter 31 — Numbers at speed: the maths lab

Under the games and gadgets, this machine is a serious scientific instrument: it carries ulab, a working dialect of NumPy — the array library that runs modern science — plus pcmath’s statistics and signal tools, and the plot() you met in chapter 8. This chapter turns them into a laboratory. And a quiet promise as you start: the np. idiom you learn here is the same idiom used on million-core supercomputers — of everything in this book, this chapter’s skill travels furthest.

plot(), properly this time

Chapter 8 borrowed it; here is the whole instrument. It draws data or functions, autoscaled with axes:

plot([3, 1, 4, 1, 5, 9, 2, 6])          # a sequence of values
import math
plot(math.sin, (0, 12.6))               # a FUNCTION over a range

That second form is a graphing calculator in one line — any function of one number, including your own defs. The refinements:

Arrays: the loops vanish

A ulab array is a list that took physics. Import the dialect and make one:

from ulab import numpy as np

x = np.linspace(0, 2 * math.pi, 128)    # 128 evenly spaced values
# sin of ALL of them, at once
y = np.sin(x)
plot(y, x=x)

Read the middle line again: np.sin(x) computed 128 sines with no for in sight — the loop happens inside, in C, at machine speed. That is the whole philosophy: operations apply to every element at once, and they compose like ordinary arithmetic:

# scaled, shifted, all at once
y2 = np.sin(2 * x) * 0.5 + 0.1
plot([y, y2], x=x)

Arrays also answer questions about themselves — np.mean(y), np.std(y), np.min, np.max, np.sort — and slice like lists (y[10:20], chapter 10 unchanged). For elementwise work on big data, the speedup over a Python loop is roughly a hundredfold — and experiment 2 has you measure it rather than believe it.

Where the rest lives

This chapter uses the daily tenth of the library; the other nine-tenths — 2-D matrices, np.linalg (inverses, determinants, eigenvalues), np.fft, interpolation, complex arrays, and the ulab.scipy corner (signal filtering, special functions, solvers) — are real, aboard, and documented. Three signposts, in the order to try them:

That third link is the door this chapter opened: the same np. you use on this shelf-top machine runs the world’s data science, and nothing you learn about it is ever wasted.

The pcmath bench

Four tools from the board’s own kit, chosen for the lab below:

(The bench also holds 3-D vectors and quaternions — vcross, vdot, vrotate, pcmath.Quat — waiting for the day your programs leave flatland, and a PID controller that chapter 32’s world of motors is the natural home for.)

Coming from MMBasic: the MATH command family landed here — MATH CORREL/CHI are pcmath.correl/chi_square, the FFT and window verbs are power_spectrum/window, the V_/Q_ vector and quaternion verbs are the pcmath names above — and MMBasic arrays grew up into ulab, where the maths applies itself.

Project: the maths lab

One program, four experiments, chosen from a menu — the machinery is chapter 10’s dict-of-functions and chapter 11 throughout, and each experiment ends with a picture. edit("mathlab.py"):

import math
import random
import pcmath
from ulab import numpy as np

def ask_float(prompt, fallback):
    try:
        return float(input(prompt).strip())
    except ValueError:
        return fallback

def pause():
    input("\n[Enter] for the menu ")
    cls()

def grapher():
    zoo = {"sin": math.sin,
           "cos": math.cos,
           "damped": lambda t: math.exp(-t / 4) * math.sin(3 * t),
           "squarish": lambda t: (math.sin(t)
                                  + math.sin(3 * t) / 3 +
                                  math.sin(5 * t) / 5)}
    print("the zoo:", ", ".join(zoo))
    name = input("which function? ").strip()
    fn = zoo.get(name, math.sin)
    a = ask_float("from (default 0): ", 0.0)
    b = ask_float("to (default 12.6): ", 12.6)
    plot(fn, (a, b))
    pause()

def stats():
    raw = input("numbers, comma-separated: ")
    try:
        data = [float(v) for v in raw.split(",")]
    except ValueError:
        print("that wasn't numbers -- try 3,1,4,1,5")
        return
    arr = np.array(data)
    print(f"n = {len(data)}   mean = {np.mean(arr):.3f}   "
          f"std = {np.std(arr):.3f}")
    print(f"min = {np.min(arr):.3f}   max = {np.max(arr):.3f}")
    plot(data, style="bar")
    pause()

def detective():
    rate = 128
    hz = random.randint(3, 40)
    sig = [math.sin(2 * math.pi * hz * i / rate) +
           random.randint(-80, 80) / 100 for i in range(rate)]
    plot(sig)
    input("\none second of signal, drowning in noise. [Enter] to "
          "analyse ")
    p = pcmath.power_spectrum(sig)
    # skip slot 0 (the average)
    peak = int(np.argmax(p[1:])) + 1
    plot(p, style="bar")
    print(f"\nloudest slot: {peak} -> {peak} Hz   (the truth: "
          f"{hz} Hz)")
    print("caught it!" if peak == hz else "noise won this round "
                                          "-- rerun me")
    pause()

def kinship():
    n = 40
    heights = [150 + random.randint(0,
               400) / 10 for _ in range(n)]
    weights = [(h - 100) * 0.9 + random.randint(-120, 120) / 10
               for h in heights]
    r = pcmath.correl(heights, weights)
    plot(weights, x=heights, style="scatter")
    print(f"\nheight vs weight, n = {n}:   r = {r:.3f}")
    pause()

MENU = {"1": ("graph a function", grapher),
        "2": ("statistics of your numbers", stats),
        "3": ("the signal detective", detective),
        "4": ("correlation: height vs weight", kinship)}

cls()
while True:
    print("THE MATHS LAB")
    for key in sorted(MENU):
        print(f"  {key}. {MENU[key][0]}")
    choice = input("experiment (q quits): ").strip()
    if choice == "q":
        break
    if choice in MENU:
        cls()
        MENU[choice][1]()

Run all four. The pedagogy hiding in each:

Experiments

  1. Overlay theory on experiment: in the detective, after the spectrum, plot(sig, clear=True) then a pure sine of the caught frequency with clear=False on top. How well does the guess hug the noise?
  2. The promised speed race: square 10,000 numbers with a Python for loop, then as arr * arr — ticks_diff around each (chapter 21). Report the ratio. Then try 100,000 and mind the RAM (chapter 35 will have things to say).
  3. Chapter 20’s beat frequencies, seen at last: build np.sin(2 * math.pi * 440 * t) plus the same at 444 Hz over a tenth of a second, and plot the sum — the wobble you heard in the stereo lab, now visible as an envelope.
  4. The dice court: roll random.randint(1, 6) six hundred times, tally into six counts (chapter 10), and try pcmath.chi_square(counts, [100] * 6). A p-value above 0.05 acquits. Now rig the die (a 7th chance of rolling six…) and watch the p-value convict.
  5. pcmath.crossings as a frequency meter: in the detective, count zero crossings of the raw noisy signal and halve — how close does this two-line method get to the FFT’s answer, and when does it fall apart? (Both methods are used in real instruments; now you know their temperaments.)

Challenges

  1. Numerical calculus. For y = np.sin(x): the slope is (y[1:] - y[:-1]) / dx (array slicing does calculus!), the area a running sum. Plot the slope over the original — it’s the cosine, discovered rather than memorised. Newton, on a shelf.
  2. The projectile gallery. One function: range of a thrown ball vs angle. Plot the trajectory family (five angles, one plot, clear=False) and mark 45°’s crown. Then add air drag as a fudge factor and watch the crown slip below 45° — as it does on real fields.
  3. Lissajous art. plot(np.sin(3 * t), x=np.sin(2 * t), style="scatter") — then vary the two integers. Physics-lab oscilloscope art, one line each; frame the best (chapter 15’s save_image still works under a plot).
  4. The lab notebook. Marry this chapter to chapter 30’s weather diary: after a week of CSV logging, load the columns (chapter 12), plot temperature against hour-of-day as a scatter, and put correl on the case: does your street warm with the clock? You are, at this point, simply doing science.