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 , 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 is multiplying by , and RoPE applies that to every pair of a query and a key. Geometric ladders set frequencies: RoPE’s inverse frequencies 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.
- Complex number: a point of the plane with the multiplication defined by . Read more: 6.2 Trigonometry, rotations, complex numbers, and Euler’s formula
- Bits per byte: a text’s total negative log-likelihood in nats divided by ; independent of the tokenizer. Read more: 6.1 Exponents, logarithms, and units of information
- RoPE: rotary position embedding, which rotates queries and keys by position to encode relative position. Read more: 5.3 Model landscape and lifecycle in AI Engineering
- Geometric sequence: a sequence that multiplies by the same ratio at every step, . Read more: 6.3 Sequences, geometric series, and frequency ladders
- Horner’s rule: evaluating from the inside out, with multiplications and additions. Read more: 6.4 Polynomials, Horner’s rule, and stable quadratic roots
- Geometric series: , with partial sum and limit for . Read more: 8.2 Series convergence, the EMA as a geometric series, and bias correction
- frequency ladder: the geometric sequence of angular speeds, from 1 down to about . Read more: 11.4 Positional encodings: sinusoidal, learned in AI Engineering
- Catastrophic cancellation: losing most significant digits by subtracting two nearly equal rounded numbers. Read more: 6.4 Polynomials, Horner’s rule, and stable quadratic roots
- Unit circle: the circle of radius 1 about the origin; the point at angle is . Read more: 6.2 Trigonometry, rotations, complex numbers, and Euler’s formula
- Dot product: ; it measures how much two vectors point the same way, and it is zero exactly when they are perpendicular. Read more: Chapter 10: Calculus 3
- Range reduction: rewriting so the polynomial only ever sees a small argument . Read more: 8.1 Taylor series, remainder bounds, and range reduction
- Entropy: , the average surprise of a distribution. Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator
- Perplexity: , the geometric mean of ; the effective number of choices per token. Read more: 17.2 Perplexity, bits per byte, NLL accumulator
- ALiBi: a per-head bias that falls linearly with query-key distance, used instead of rotations. Read more: 13.4 Context Extension: PI, NTK, YaRN, Llama 3 Scaling, and ALiBi in AI Engineering
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 . 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.
Before you begin
Section titled “Before you begin”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.
In this chapter
Section titled “In this chapter”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.
Where this chapter leads
Section titled “Where this chapter leads”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.
Further reading
Section titled “Further reading”- OpenStax, Precalculus 2e (free, CC BY), the primary reference for the chapter.
- OpenStax, College Algebra 2e (free), for the algebra review.
- Paul’s Online Notes, Algebra and Trig Review (free).
- 3Blue1Brown, Euler’s formula with introductory group theory (free).