Skip to content

Chapter 6: Precalculus

Precalculus is the toolbox the rest of the mathematics in this book is written in: exponents and logarithms, trigonometry and complex numbers, sequences and series, and polynomials. None of it is new if you took algebra at school, but this chapter treats each topic the way a language-model system uses it, and each one has a concrete call site. Logarithms measure information, so a model’s loss in nats becomes bits by dividing by ln⁡2\ln 2, and bits per byte is the number every tokenizer and model in AI Engineering is judged by. Rotations encode position: rotating a 2D pair by θ\theta is multiplying x+iyx + iy by eiθe^{i\theta}, and RoPE applies that to every pair of a query and a key. Geometric ladders set frequencies: RoPE’s inverse frequencies b−2i/db^{-2i/d} and ALiBi’s head slopes are geometric sequences, whose sums and ratios decide how far a position signal reaches. And polynomials approximate the functions a C kernel cannot call.

Concepts from other sections (14)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

A second theme runs underneath: how you evaluate a formula matters as much as the formula. Horner’s rule is the cheapest and most stable way to evaluate a polynomial, and the textbook quadratic formula can lose most of its digits for the small root when b2≫4acb^2 \gg 4ac. Several sections show a formula that is correct on paper and wrong in floating point, and the rewrite that fixes it.

The chapter opens with a problem set that practises all of these skills by hand, and then builds each topic into a small, tested Python module. Each build section derives its solution from first principles and walks through the code line by line.

Learning objectives

  • Manipulate exponents and logarithms, convert between nats and bits, and compute bits per byte for a model.
  • Rotate vectors with matrices and with complex numbers, and explain why rotations make attention scores depend only on relative position.
  • Sum geometric series accurately, including near a ratio of 1, and build RoPE’s frequency ladder and ALiBi’s slopes.
  • Evaluate polynomials with Horner’s rule and compute both roots of a quadratic without catastrophic cancellation.
  • Find inverses of functions, use monotonicity, and solve inequalities by hand.

You need high-school algebra: solving linear equations and manipulating fractions and powers. The build sections use Python and numpy; if you are new to numpy arrays, dtypes, and broadcasting, read Python and numpy in Programming and Algorithms first. Read each section with a notebook open and check every identity numerically before you trust it, with math.log, cmath.exp, or numpy.

Problem set: functions, logarithms, trigonometry, series, polynomials, and inequalities is the pen-and-paper side of the chapter: sixty problems with a worked answer for every one, including the most common wrong answer and why it is wrong. It also teaches functions, inverses, monotonicity, and inequalities, which have no build section because none of that becomes code. Work it first, or alongside the sections it points to.

Exponents, logarithms, and units of information derives the laws of exponents and logarithms and the change-of-base formula, shows that the base of a logarithm is a unit of information, and builds the bits-per-byte conversion that compares models across tokenizers.

Trigonometry, rotations, complex numbers, and Euler’s formula builds rotations from the unit circle, proves that the dot product of two rotated vectors depends only on the difference of their angles, and implements the pairwise rotation that RoPE calls, in matrix and complex form.

Sequences, geometric series, and frequency ladders sums geometric series, shows why the closed form loses digits near a ratio of 1 and how expm1 and log1p fix it, and builds RoPE’s inverse frequencies and ALiBi’s head slopes.

Polynomials, Horner’s rule, and stable quadratic roots evaluates polynomials with Horner’s rule, bounds its error, and replaces the textbook quadratic formula with one that never cancels.

Calculus 1 builds limits and derivatives on functions, exponentials, and logarithms. In Numerical Methods and Floating Point, range reduction and polynomial approximation of exp start from Horner’s rule. In Information Theory, entropy and perplexity are logarithms with a unit. And in AI Engineering, RoPE, ALiBi, and bits per byte are the first places this mathematics runs.