A Programmers Introduction To Mathematics PDF
A Programmers Introduction To Mathematics PDF
Jeremy Kun
Copyright © 2018 Jeremy Kun
All rights reserved. This book or any portion thereof may not be reproduced or used in
any manner whatsoever without the express written permission of the publisher except
for the use of brief quotations in a book review.
All images used in this book are either the author’s original works or in the public
domain. In particular, the only non-original images are in the chapter on group theory,
specifically the textures from The Grammar of the Orient, M.C. Escher’s Circle Limit IV,
and two diagrams in the public domain, sourced from Wikipedia.
pimbook.org
To my wife, Erin.
My unbounded, uncountable thanks goes out to the many people who read drafts at
various stages of roughness and gave feedback, including (in alphabetical order by first
name), Aaron Shifman, Adam Lelkes, Alex Walchli, Ali Fathalian, Arun Koshy, Ben Fish,
Craig Stuntz, Devin Ivy, Erin Kelly, Fred Ross, Ian Sharkey, Jasper Slusallek, Jean-Gabriel
Young, João Rico, John Granata, Julian Leonardo Cuevas Rozo, Kevin Finn, Landon Kavlie,
Louis Maddox, Matthijs Hollemans, Olivia Simpson, Pablo González de Aledo, Paige Bai-
ley, Patrick Regan, Patrick Stein, Rodrigo Zhou, Stephanie Labasan, Temple Keller, Trent
McCormick.
Special thanks to Devin Ivy for a thorough technical review of two key chapters.
Contents
Our Goal i
2 Polynomials 5
2.1 Polynomials, Java, and Definitions . . . . . . . . . . . . . . . . . . . . . . 5
2.2 A Little More Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3 Existence & Uniqueness . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4 Realizing it in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.5 Application: Sharing Secrets . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.6 Cultural Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.8 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4 Sets 39
4.1 Sets, Functions, and Their -Jections . . . . . . . . . . . . . . . . . . . . . 40
4.2 Clever Bijections and Counting . . . . . . . . . . . . . . . . . . . . . . . 48
4.3 Proof by Induction and Contradiction . . . . . . . . . . . . . . . . . . . . 51
4.4 Application: Stable Marriages . . . . . . . . . . . . . . . . . . . . . . . . 54
4.5 Cultural Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
4.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.7 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
6 Graphs 69
6.1 The Definition of a Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.2 Graph Coloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
6.3 Register Allocation and Hardness . . . . . . . . . . . . . . . . . . . . . . 73
6.4 Planarity and the Euler Characteristic . . . . . . . . . . . . . . . . . . . . 75
6.5 Application: the Five Color Theorem . . . . . . . . . . . . . . . . . . . . 77
6.6 Approximate Coloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
6.7 Cultural Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
6.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
6.9 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
16 Groups 299
16.1 The Geometric Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . 301
16.2 The Interface Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
16.3 Homomorphisms: Structure Preserving Functions . . . . . . . . . . . . . 307
16.4 Building Blocks of Groups . . . . . . . . . . . . . . . . . . . . . . . . . . 310
16.5 Geometry as the Study of Groups . . . . . . . . . . . . . . . . . . . . . . 312
16.6 The Symmetry Group of the Poincaré Disk . . . . . . . . . . . . . . . . . 321
16.7 The Hyperbolic Isometry Group as a Group of Matrices . . . . . . . . . . 327
16.8 Application: Drawing Hyperbolic Tessellations . . . . . . . . . . . . . . . 328
16.9 Cultural Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
16.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
16.11 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Index 363
Our Goal
This book has a straightforward goal: to teach you how to engage with mathematics.
Let’s unpack this. By “mathematics,” I mean the universe of books, papers, talks, and
blog posts that contain the meat of mathematics: formal definitions, theorems, proofs,
conjectures, and algorithms. By “engage” I mean that for any mathematical topic, you
have the cognitive tools to actively progress toward understanding that topic. I will
“teach” you by introducing you to—or having you revisit—a broad foundation of topics
and techniques that support the rest of mathematics. I say “with” because mathematics
requires active participation.
We will define and study many basic objects of mathematics, such as polynomials,
graphs, and matrices. More importantly, I’ll explain how to think about those objects
as seasoned mathematicians do. We will examine the hierarchies of mathematical ab-
straction, along with many of the softer skills and insights that constitute “mathematical
intuition.” Along the way we’ll hear the voices of mathematicians—both famous histor-
ical figures and my friends and colleagues—to paint a picture of mathematics as both a
messy amalgam of competing ideas and preferences, and a story with delightfully sur-
prising twists and connections. In the end, I will show you how mathematicians think
about mathematics.
So why would someone like you1 want to engage with mathematics? Many software
engineers, especially the sort who like to push the limits of what can be done with pro-
grams, eventually come to realize a deep truth: mathematics unlocks a lot of cool new
programs. These are truly novel programs. They would simply be impossible to write
(if not inconceivable!) without mathematics. That includes programs in this book about
cryptography, data science, and art, but also to many revolutionary technologies in in-
dustry, such as signal processing, compression, ranking, optimization, and artificial in-
telligence. As importantly, a wealth of opportunity makes programming more fun! To
quote Randall Munroe in his XKCD comic Forgot Algebra, “The only things you HAVE to
know are how to make enough of a living to stay alive and how to get your taxes done.
All the fun parts of life are optional.” If you want your career to grow beyond shuffling
data around to meet arbitrary business goals, you should learn the tools that enable you
to write programs that captivate and delight you. Mathematics is one of those tools.
Programmers are in a privileged position to engage with mathematics. As a program-
1
Hopefully you’re a programmer; otherwise, the title of this book must have surely caused a panic attack.
i
ii
mer, you eat paradigms for breakfast and reshape them into new ones for lunch. Your
comfort with functions, logic, and protocols gives you an intuitive familiarity with ba-
sic topics such as boolean algebra, recursion, and abstraction. You can rely on this to
make mathematics less foreign, progressing all the faster to more nuanced and stimulat-
ing topics. Contrast this to most educational math content aimed at students with no
background and focusing on rote exercises and passing tests. As a bonus, programming
allows me to provide immediate applications that ground the abstract ideas in code. In
each chapter of this book, we’ll fashion our mathematical designs into a program you
couldn’t have written before, to dazzling effect. The code is available on Github,2 with a
directory for each chapter.
All told, this book is not a textbook. I won’t drill you with exercises, though drills have
their place. We won’t build up any particular field of mathematics from scratch. Though
we’ll visit calculus, linear algebra, and many other topics, this book is far too short to
cover everything a mathematician ought to know about these topics. Moreover, while
much of the book is appropriately rigorous, I will occasionally and judiciously loosen
rigor when it facilitates a better understanding and relieves tedium. I will note when this
occurs, and we’ll discuss the role of rigor in mathematics more broadly.
Indeed, rather than read an encyclopedic reference, you want to become comfortable
with the process of learning mathematics. In part that means becoming comfortable with
discomfort, with the struggle of understanding a new concept, and the techniques that
mathematicians use to remain productive and sane. Many people find calculus difficult,
or squeaked by a linear algebra course without grokking it. After this book you should
have a core nugget of understanding of these subjects, along with the cognitive tools that
will enable you dive as deeply as you like.
As a necessary consequence, in this book you’ll learn how to read and write proofs. The
simplest and broadest truth about mathematics is that it revolves around proofs. Proofs
are both the primary vehicle of insight and the fundamental measure of judgment. They
are the law, the currency, and the fine art of mathematics. Most of what makes math-
ematics mysterious and opaque—the rigorous definitions, the notation, the overloading
of terminology, the mountains of theory, and the unspoken obligations on the reader—is
due to the centrality of proofs. A dominant obstacle to learning math is an unfamiliarity
with this culture. In this book I’ll show you why proofs are so important, cover the basic
methods, and display examples of proofs in each chapter. To be sure, you don’t have to
understand every proof to finish this book, and you will probably be confounded by a
few. Embrace your humility. I hope to convince you that each proof contains layers of
insight that are genuinely worthwhile, and that no single person can see the complete
picture in a single sitting. As you grow into mathematics, the act of reading even previ-
ously understood proofs provides both renewed and increaseed wisdom. So long as you
identify the value gained by your struggle, your time is well spent.
I’ll also teach you how to read between the mathematical lines of a text, and understand
the implicit directions and cultural cues that litter textbooks and papers. As we proceed
2
pimbook.org
iii
through the chapters, we’ll gradually become more terse, and you’ll have many opportu-
nities to practice parsing, interpreting, and understanding math. All of the topics in this
book are explained by hundreds of other sources, and each chapter’s exercises include ex-
plorations of concepts beyond these pages. In addition, I’ll discuss how mathematicians
approach problems, and how their process influences the culture of math.
You will not learn everything you want to know in this book, nor will you learn ev-
erything this book has to offer in one sitting. Those already familiar with math may find
early chapters offensively slow and detailed. Those genuinely new to math may find the
later chapters offensively fast. This is by design. I want you to be exposed to as much
mathematics as possible, to learn the definitions of central mathematicl ideas, to be intro-
duced to notations, conventions, and attitudes, and to have ample opportunity to explore
topics that pique your interest.
A number of topics are conspicuously missing from this book, my negligence of which
approaches criminal. Except for a few informal cameos, we ignore complex numbers,
probability and statistics, differential equations, and formal logic. In my humble opinion,
none of these topics is as fundamental for mathematical computer science as those I’ve
chosen to cover. After becoming comfortable with the topics in this book, for example,
probability will be very accessible. The chapter on eigenvalues will include a miniature
introduction to differential equations. The chapter on groups will briefly summarize com-
plex numbers. Probability will echo in your brain when we discuss random graphs and
machine learning. Moreover, many topics in this book are prerequisites for these other ar-
eas. And, of course, as a single human self-publishing this book on nights and weekends,
I have only so much time.
The first step on our journey is to confirm that mathematics has a culture worth be-
coming acquainted with. We’ll do this with a comparative tour of the culture of software
that we understand so well.
Chapter 1
–David Hilbert
Do you remember when you started to really learn programming? I do. I spent two
years in high school programming games in Java. Those two years easily contain the
worst and most embarrassing code I have ever written. My code absolutely reeked.
Hundred-line functions and thousand-line classes, magic numbers, unreachable blocks
of code, ridiculous comments, a complete disregard for sensible object orientation, and
type-coercion that would make your skin crawl. The code worked, but it was filled with
bugs and mishandled edge-cases. I broke every rule in the book, and for all my shortcom-
ings I considered myself a hot-shot (at least, among my classmates!). I didn’t know how
to design programs, or what made a program “good,” other than that it ran and I could
impress my friends with a zombie shooting game.
Even after I started studying software in college, it was another year before I knew
what a stack frame or a register was, another year before I was halfway competent with
a terminal, another year before I appreciated functional programming, and to this day I
still have an irrational fear of systems programming and networking. I built up a base of
knowledge over time, with fits and starts at every step.
In a college class on C++ I was programming a Checkers game, and my task was to gen-
erate a list of legal jump-moves from a given board state. I used a depth-first search and a
few recursive function calls. Once I had something I was pleased with, I compiled it and
ran it on my first non-trivial example. Lo’ and behold (even having followed test-driven
development!), a segmentation fault smacked me in the face. Dozens of test cases and
more than twenty hours of confusion later, I found the error: my recursive call passed
a reference when it should have been passing a pointer. This wasn’t a bug in syntax
or semantics—I understood pointers and references well enough—but a design error. As
most programmers can relate, the most aggravating part was that changing four charac-
ters (swapping a few ampersands with asterisks) fixed it. Twenty hours of work for four
characters! Once I begrudgingly verified it worked, I promptly took the rest of the day
off to play Starcraft.
1
2
Such drama is the seasoning that makes a strong programmer. One must study the top-
ics incrementally, learn from a menagerie of mistakes, and spend hours in a befuddled
stupor before becoming “experienced.” This gives rise to all sorts of programmer culture,
Unix jokes, urban legends, horror stories, and reverence for the masters of C that make
the programming community so lovely. It’s like a secret club where you know all the
handshakes, but should you forget one, a crafty use of grep and sed will suffice. The
struggle makes you appreciate the power of debugging tools, slick frameworks, histori-
cally enshrined hacks, and new language features that stop you from shooting your own
foot.
When programmers turn to mathematics, they seem to forget these trials. The same
people who invested years grokking the tools of their trade treat new mathematical tools
and paradigms with surprising impatience. I can see a few reasons why. One is that
they’ve been taking classes called “mathematics” for far longer than they’ve been learn-
ing to program (and mathematics was always easy!). The forced prior investment of
schooling engenders a certain expectation. The problem is that the culture of mathemat-
ics and the culture of mathematics education—elementary through lower-level college
courses—are completely different.
Even math majors have to reconcile this. I’ve had many conversations with such stu-
dents, many of whom are friends, colleagues, and even family, who by their third year
decided they didn’t really enjoy math. The story often goes like this: a student who was
good at math in high school (perhaps because of its rigid structure) reaches the point
of a math major at which they must read and write proofs in earnest. It requires an
earnest, open-ended exploration that they don’t enjoy. Despite being a stark departure
from high school math, incoming students are never warned in advance. After coming
to terms with their unfortunate situation, they decide that their best option is to hold on
until they can return to the comfortable setting of their prior experiences, this time in
the teacher’s chair.
I don’t mean to insult teaching as a profession—I love teaching and understand why
one would choose to do it full time. There are many excellent teachers who excel at both
the math and the trickier task of engaging aloof teenagers to think critically about it.
But this pattern of disenchantment among math teachers is prevalent, and it widens the
conceptual gap between secondary and “college level” mathematics. Programmers often
have similar feelings, that the math they were once so good at is suddenly impenetrable.
It’s not a feature of math, but a bug in the education system (and a negative feedback
loop!) that gets blamed on math as a subject.
Another reason programmers feel impatient is because they do so many things that
relate to mathematics in deep ways. They use graph theory for data structures and search.
They study enough calculus to make video games. They hear about the Curry-Howard
correspondence between proofs and programs. They hear that Haskell is based on a
complicated math thing called category theory. They even use mathematical results in an
interesting way. I worked at a “blockchain” company that implemented a Bitcoin wallet,
which is based on elliptic curve cryptography. The wallet worked, but the implementer
didn’t understand why. They simply adapted pseudocode found on the internet. At the
3
risk of a dubious analogy, it’s akin to a “script kiddie” who uses hacking tools as black
boxes, but has little idea how they work. Mathematicians are on the other end of the
spectrum, caring almost exclusively about why things work the way they do.
While there’s nothing inherently wrong with using mathematics as a black box, espe-
cially the sort of applied mathematics that comes with provable guarantees, many pro-
grammers want to understand why they work. This isn’t surprising, given how much
time engineers spend studying source code and the internals of brittle, technical systems.
Systems that programmers rely on, such as dependency management, load balancers,
search engines, alerting systems, and machine learning, all have rich mathematical foun-
dations. We’re naturally curious about how they work and how to adapt them to our
needs.
Yet another hindrance to mathematics is that it has no centralized documentation. In-
stead it has a collection of books, papers, journals, and conferences, each with discrep-
ancies of presentation, citing each other in a haphazard manner. A theorem presented
at a computer science conference can be phrased in completely unfamiliar terms in a dy-
namical systems journal—even though they boil down to the same facts! In subfields like
network science that straddle disciplines, one often sees “translation tables” for jargon.
Dealing with this is not easy. Students of mathematics solve these problems with
knowledgeable teachers. Working mathematicians just “do it.” They work out the transla-
tion details themselves with coffee and contemplation. Advanced books also lean toward
terseness, despite being titled as “elementary” or an “introduction.” They opt not to re-
define what they think the reader must already know. The purest fields of mathematics
take a sort of pretentious pride in how abstract and compact their work is (to the point
where many students spend weeks or months understanding a single chapter!).
What programmers would consider “sloppy” notation is one symptom of the prob-
lem, but there there are other expectations on the reader that, for better or worse, de-
celerate the pace of reading. Unfortunately I have no solution here. Part of the power
and expressiveness of mathematics is the ability for its practitioners to overload, rede-
fine, and omit in a suggestive manner. Mathematicians also have thousands of years of
“legacy” math that require backward compatibility. Enforcing a single specification for
all of mathematics—a suggestion I frequently hear from software engineers—would be
horrendously counterproductive.
Indeed, ideas we take for granted today, such as algebraic notation, drawing functions
in the Euclidean plane, and summation notation, were at one point actively developed
technologies. Each of these notations had a revolutionary effect, not just on science, but
also, to quote Bret Victor, on our capacity to “think new thoughts.” One can even draw a
line from the proliferation of algebraic notation and the computational questions it raised
to the invention of the computer.1 Borrowing software terminology, algebraic notation is
1
Leibniz, one of the inventors of calculus, dreamed of a machine that could automatically solve mathematical
problems. Ada Lovelace (up to some irrelevant debate) designed the first program for computing Bernoulli
numbers, which arise in algebraic formulas for computing sums of powers of integers. In the early 1900’s
Hilbert posed his Tenth Problem on algorithms for computing solutions to Diophantine equations, and later
his Entscheidungsproblem, which was solved concurrently by Church and Turing and directly led to Turing’s
4
among the most influential and scalable technologies humanity has ever invented. And as
we’ll see in Chapter 10 and Chapter 16, we can find algebraic structure hiding in exciting
places. Algebraic notation helps us understand this structure not only because we can
compute, but also because we can visually see the symmetries in the formulas. This makes
it easier for us to identify, analyze, and encapsulate structure when it occurs.
Finally, the best mathematicians study concepts that connect decades of material, while
simultaneously inventing new concepts which have no existing words to describe them.
Without flexible expression, such work would be impossible. It reduces cognitive load,
a theme that will follow us throughout the book. Unfortunately, it only does so for the
readers who have already absorbed the basic concepts of discussion. By contrast, good
software practice encourages code that is simple enough for anyone to understand. As
such, the uninitiated programmer often has a much larger cognitive load when reading
math than when reading a program.
Taken together, mathematical notation is closer to spoken language than to code. It
can reduce one’s mental burden via rigorous rules applied to an external representation,
coupled with context and convention. All of this, the notation, the differences among sub-
fields, the tradeoff between expressiveness and cognitive load, has grown out of hundreds
of years of mathematical progress.
Equipped with this understanding, that mathematics has culturally relevant reasons
for its strange practices, let’s begin our journey through the mists of math with renewed
openness.
Read on, and welcome to the club.
code-breaking computer.
Chapter 2
Polynomials
We are not trying to meet some abstract production quota of definitions, theorems and
proofs. The measure of our success is whether what we do enables people to understand
and think more clearly and effectively about mathematics.
–William Thurston
We begin with polynomials. In studying polynomials, we’ll reveal some of the im-
plicit assumptions behind mathematical definitions, work carefully through two nontriv-
ial proofs, and learn about how to “share secrets” using something called polynomial
interpolation.
To whet your appetite, this secret sharing scheme allows one to encode a secret mes-
sage in 10 parts so that any 6 can be used to reconstruct the secret, but with fewer than 6
pieces it’s impossible to determine even a single bit of the original message. The numbers
10 and 6 are just examples, and the scheme we’ll present works for any pair of integers.
This almost magical application turns out to be possible using nothing more than poly-
nomials.
/******************************************
* Compilation: javac HelloWorld.java
* Execution: java HelloWorld
*
* Prints "Hello, World".
******************************************/
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to stdout on the terminal.
System.out.println("Hello, World");
}
}
It was roughly six months before I understood what all the different pieces of this
program did, despite the fact that I had written ‘public static void main’ so many times I
had committed it to memory. One nice thing about programming is that you don’t have to
understand a code snippet before you can start using it. But at some point, I stopped to ask,
“what do those words actually mean?” That’s the step when my eyes stop glazing over.
That’s the same procedure we need to invoke for a mathematical definition, preferably
faster than six months.
Now I’m going to throw you in the thick of the definition of a polynomial. But stay
with me! I want you to start by taking out a piece of paper and literally copying down
the definition (the entire next paragraph), character for character, as one would type out
a program from scratch. This is not an idle exercise. Taking notes by hand uses a part of
your brain that both helps you remember what you wrote, and helps you read it closely.
Each individual word and symbol of a mathematical definition affects the concept being
defined, so it’s important to parse everything slowly.
Definition 2.1. A single variable polynomial with real coefficients is a function f that
takes a real number as input, produces a real number as output, and has the form
f (x) = a0 + a1 x + a2 x2 + · · · + an xn ,
where the ai are real numbers. The ai are called coefficients of f . The degree of the
polynomial is the integer n.
Let’s analyze the content of this definition in three ways. First, syntactically, which
also highlights some general features of written definitions. Second, semantically, where
we’ll discuss what a polynomial should represent as a concept in your mind. Third, we’ll
inspect this definition culturally, which includes the unspoken expectations of the reader
upon encountering a definition in the wild.
Syntax
A definition is an English sentence or paragraph in which italicized words represent the
concepts being defined. In this case, Definition 2.1 defines three things: a polynomial with
real coefficients (the function f ), coefficients (the numbers ai ), and a polynomial’s degree
(the integer n).
7
A proper mathematical treatment might also define what a “real number” is, but we
simply don’t have the time or space.1 For now, think of a real number as a floating point
number without the emotional baggage that comes from trying to fit all decimals into a
finite number of bits.
An array of numbers a, which in most programming languages would be indexed using
square brackets like a[i], is almost always indexed in math using subscripts ai . For two-
dimensional arrays, we place the indices comma separated in the subscript, i.e. ai,j is
equivalent to a[i][j]. Hence, the coefficients are just an array of real numbers.
To say f “has the form” means that f is restricted to some choice of the unbound
variables in its formula. In this case those are, in order:
1. A choice of names for all the variables involved. The definition has chosen f for the
function, x for the input variable name (usually called the “variable,” but we won’t
overload that term for now), a for the array of coefficients, and n for the degree.
One can choose other names as desired.
Using the curves they “carve out” in space, polynomials can be regarded as geometric
objects with geometric properties like “curvature” and “smoothness.” In Chapter 8 we’ll
return to this more formally, but until then one can guess how they might faithfully de-
scribe a plot like the one in Figure 2.1. The connection between polynomials as geometric
objects and their algebraic properties is a deep one that has occupied mathematicians for
centuries. For example, the degree gives some information about the shape of the curve.
For example, Figure 2.2 shows plots of generic polynomials of degrees 3 through 6. As
the degree goes up, so does the number of times the polynomial “changes direction” be-
tween increasing and decreasing. Turning this into a mathematically rigorous theorem
requires more nuance, but a pattern is clear.
Finally, polynomials can be thought of as “building blocks” for complicated structures.
That is, polynomials are families of increasingly expressive objects, which get more com-
plex as the degree increases. This idea is the foundation of the application for this chapter
(sharing secrets), and it will guide us to Taylor polynomials as a hammer for every nail
in Chapters 8 and 14.
Polynomials occur with stunning ubiquity across mathematics. It makes one wonder
10
exactly why they are so central, but to reiterate, polynomials encapsulate the full expres-
sivity of addition and multiplication. As programmers, we know that even such simple
operations as binary AND, OR, and NOT, when combined arbitrarily, yield the full gamut
of algorithms. Polynomials fill the same role for arithmetic. Indeed, polynomials with
multiple variables can represent AND, OR, and NOT, if you restrict the values of the
variables to be zero and one (interpreted as false and true, respectively).
AND(x, y) = xy
NOT(x) = 1 − x
OR(x, y) = 1 − (1 − x)(1 − y)
Any logical condition, again assuming the inputs are binary, can be represented using
a combination of these three polynomials. Polynomials are expressive enough to capture
all of boolean logic. This suggests that even single-variable polynomials should have
strikingly complex behavior. The rest of the chapter will display bits of that dazzling
performance.
Culture
The most important cultural expectation, one every mathematician knows, is that the
second you see a definition in a text you must immediately write down examples.
Generous authors provide examples of genuinely new concepts, but an author is never
obligated to do so. The unspoken rule is that the reader may not continue unless the
reader understands what the definition is saying. That is, you aren’t expected to master
the concept, most certainly not at the same speed you read it. But you should have some
idea going forward of what the defined words refer to.
The best way to think of this is like testing in software. You start with the simplest
possible tests, usually setting as many values as you can to zero or one, then work your
way up to more complicated examples. Later, when you get stuck on some theorem
or proof—an occupational hazard faced by gods and mortals alike—you return to those
examples and test how the claims in the proof apply to them. This is how one builds
so-called “mathematical intuition.” In the long term, one uses that intuition to speed up
the process of absorbing new ideas.
So let’s write down some definitions of polynomials according to Definition 2.1, start-
ing from literally the simplest possible thing. To make you pay attention, I’ll slip in some
examples that are not polynomials and your job is to run them against the definition.
Take your time, and you can check your answers in the Chapter Notes.
11
f (x) = 0
g(x) = 12
h(x) = 1 + x + x2 + x3
i(x) = x1/2
1
j(x) = + x2 − 2x4 + 8x8
2
1 5
k(x) = 4.5 + − 2
x x
1 5
l(x) = π − x + eπ 3 x10
e
m(x) = x + x2 − xπ + xe
Like software testing, examples weed out pesky edge cases and clarify what is permit-
ted by the definition. For example, the exponents of a polynomial must be nonnegative
integers, though I only stated it implicitly in the definition.
When reading a definition, one often encounters the phrase “by convention.” This can
be in regard to a strange edge case or a matter of taste. A common example is the factorial
n! = 1 · 2 · · · · · n, where 0! = 1 by convention. This makes formulas cleaner and provides
a natural default value of an “empty product,” an idea programmers understand when
choosing a base case for a loop that computes the product of a (possibly empty) list of
numbers.
For polynomials, convention strikes when we inspect the example f (x) = 0 given
above. What is the degree of f ? On one hand, it makes sense to say that the zero poly-
nomial has degree n = 0 and a0 = 0. On the other hand, it also makes sense (in a strict,
syntactical sense) to say that f has degree n = 1 with a0 = 0 and a1 = 0, or n = 2
with three zeros. But we don’t want a polynomial to have multiple distinct possibilities
for degree. Indeed, this would allow f (x) to have every positive degree (by adding extra
zeros), depriving the word “degree” of a consistent interpretation.
To avoid this, we amend Definition 2.1 so that the last coefficient an is required to
be nonzero. But then the function f (x) = 0 is not allowed to be a polynomial! So, by
convention, we define a special exception, the function f (x) = 0, as the zero polynomial.
By convention, the zero polynomial is defined to have degree −1. One recurring theme
is that every time a definition includes the phrase “by convention,” it becomes a special
edge-case in the resulting program.
Dealing with this edge case made us think hard about the right definition for a poly-
nomial, but it was mostly a superficial change. Other times, as we will confront head on
in Chapter 8 when we define limits, dealing with an edge case reveals the soul of a con-
cept. It’s curious how mathematical books tend to start with the final product, instead
of the journey to the right definition. Perhaps teaching the latter is much harder and
more time consuming, with fewer tangible benefits. But in advanced mathematics, deep
understanding comes in fits and starts. Often, no such distilled explanation is known.
12
In any case, examples are the primary method to clarify the features of a definition.
Having examples in your pocket as you continue to read is important, and coming up
with the examples yourself is what helps you internalize a concept.
It is a bit strange that mathematicians choose to write definitions with variable names
by example, rather than using the sort of notation one might use to define a programming
language syntax. Using a loose version of Backus-Naur form (BNF), which is a mostly
self-explanatory language for describing syntax, I might define a polynomial as:
coefficient = number
variable = 'x'
term = coefficient * variable ^ int
polynomial = term
| term + polynomial
The problem is that this definition doesn’t tell you what polynomials are all about. It
doesn’t communicate anything to the reader about the semantics of the definition, but
rather how a computer should parse it. While Definition 2.1 isn’t perfect—I still had to ex-
plain the semantics—it signals that a polynomial is a function of a single input. BNF only
provides a sequence of named tokens. This theme, that most mathematics is designed
for human-to-human communication, will follow us throughout the book. Mathematical
discourse is about getting a vivid idea from your mind into someone else’s mind.
That’s why an author usually starts with a conceptual definition like Definition 2.1
many pages before discussing a programmatic representation of a polynomial. It’s why
mathematicians will seamlessly convert between representations—such as the functional,
set-theoretic, and geometric representations I described earlier—as if mathematics were
the JavaScript type system on methamphetamines. In Java you have to separate an in-
terface from the class which implements it, and in C++ templates are distinct from their
usage. In math, much of conceptual understanding happens at the level of interfaces and
templates, while particular representations are used for computation.
I want to make this extremely clear because in mathematics it’s implicit. My math
teachers in college and grad school never explicitly discussed why one would use one
definition over another, because somehow along the arduous journey through a math
education, the folks who remained understood it.
Polynomials may seem frivolous to illustrate the difference between an object-as-
abstract-concept and the representational choices that go into understanding a defini-
tion, but the same pattern lurks behind more complicated definitions. First the author
will start with the best conceptual definition—the one that seems to them, with the hind-
sight of years of study, to be the most useful way to communicate the idea behind the
concept. For us that’s Definition 2.1. Often these definitions seem totally useless from a
programming perspective.
Then ten pages later (or a hundred!) the author introduces another definition, often a
data definition, which turns out to be equivalent to the first. Any properties defined in
the first definition automatically hold in the second and vice versa. But the data definition
is the one that allows for nice programs. You might think the author was crazy not to
13
start with the data definition, but it’s the conceptual definition that sticks in your mind,
generalizes, and guides you through proofs.
This interplay between intuitive and data definitions will take center stage in Chap-
ter 10, our first exposure to linear algebra. We’ll see that so-called linear maps are equiv-
alent to matrices in a formal sense. While linear maps are easy to conceptualize, the
corresponding operations on matrices are complicated and best suited for a computer.
But a mathematician would argue you can’t see the elegance or truly grok linear algebra
if you only ever see a matrix without conceptualizing it as a linear map. In linear algebra,
the line between interface and implementation is crisp. Even better, few areas of math
are as widely applicable.
It’s also worth noting that the multiplicity of definitions arose throughout history.
Polynomials have been studied for many centuries, but parser-friendly forms of polyno-
mials weren’t needed until the computer age. Likewise, algebra was studied before the
graphical representations of Descartes allowed us to draw polynomials as curves. Other
perspectives on polynomials were developed to enable useful approximations and calcu-
lations on the positions of planets, the path of projectiles, and many other tasks. We’ll
get a taste of this in Chapter 8. Each new perspective and definition was driven by an
additional need. As a consequence, what’s thought of as the “best” definition of a concept
can change. Throughout history math has been shaped and reshaped to refine, rigorize,
and distill the core insights, often to ease calculations in fashion at the time.
In any case, the point is that we will fluidly convert between the many ways of thinking
about polynomials: as expressions defined abstractly by picking a list of numbers, or as
functions with a special structure. Effective mathematics is flexible in this way.
int f(int x) {
return 2*x;
}
The possible inputs include all integers, and the type of the output is also “integer.” But
it’s obvious that 3 is not a possible output of this particular function.
In math we disambiguate this with two words. Range is the set of actual outputs of a
function, and the “type” of outputs is called the codomain. So the notation f : A → B
specifies the domain A and codomain B, whereas the range depends on the semantics of
f . When one introduces a function, as programmers do with type signatures and function
headers, we state the notation f : A → B first, and the actual function definition second.
Because mathematicians were not originally constrained by ASCII, they developed
14
other symbols for types. The symbol for the set of real numbers is R. The font is called
“blackboard-bold,” and it’s the standard font for denoting number systems. Applying the
arrow notation, a polynomial is f : R → R. A common phrase is to say a polynomial
is “over the reals” to mean it has real coefficients. As opposed to, say, a polynomial over
the integers that has integer coefficients.
Most famous number types have special symbols. The symbol for integers is Z, and
the positive integers are denoted by N, often called the natural numbers.2 There is an
amusing dispute of no real consequence between logicians and other mathematicians on
whether zero is a natural number, with logicians demanding it is.
Finally, I’ll use the ∈ symbol, read “in,” to assert or assume membership in some set.
For example q ∈ N is the claim that q is a natural number. It is literally short hand for
the phrase, “q is in the natural numbers,” or “q is a natural number.” It can be used in a
condition (preceded by “if”), an assertion (preceded by “suppose”), or a question.
rem we’ll prove, stated in its most precise form. Don’t worry, we’ll go carefully through
every bit of it, but try to read it now.
The one piece of new notation is the exponent on R2 . This just means “pairs” of real
numbers, each of which is in R. Likewise, Z3 would be triples of integers, and N10 tuples
of size ten, each entry of which is a natural number.
A briefer, more informal way to state the theorem: there is a unique degree n poly-
nomial passing through a choice of n + 1 points.4 Now just like with definitions, the
first thing we need to do when we see a new theorem is write down the simplest possi-
ble examples. In addition to simplifying the theorem, it will give us examples to work
with while going through the proof. Write down some examples now. As mathematician
Alfred Whitehead said, “We think in generalities, but we live in details.”
Back already? I’ll show you examples I’d write down, and you can compare your pro-
cess to mine. The simplest example is n = 0, so that n + 1 = 1 and we’re working with
a single point. Let’s pick one at random, say (7, 4). The theorem asserts that there is a
unique degree zero polynomial passing through this point. What’s a degree zero polyno-
mial? Looking back at Definition 2.1, it’s a function like a0 +a1 x+a2 x2 +· · ·+ad xd (I’m
using d for the degree here because n is already taken), where we’ve chosen to set d = 0.
Setting d = 0 means that f has the form f (x) = a0 . So what’s such a function with
f (7) = 4? There is no choice but f (x) = 4. It should be clear that it’s the only degree
zero polynomial that does this. Indeed, the datum that defines a degree-zero polynomial
is a single number, and the constraint of passing through the point (7, 4) forces that one
piece of data to a specific value.
Let’s move on to a slightly larger example which I’ll allow you to work out for yourself
before going through the details. When n = 1 and we have n + 1 = 2 points, say
(2, 3), (7, 4), the theorem claims a unique degree 1 polynomial f with f (2) = 3 and
f (7) = 4. Find it by writing down the definition for a polynomial in this special case and
solving the two resulting equations.5
Alright. A degree 1 polynomial has the form
f (x) = a0 + a1 x.
Writing down the two equations f (2) = 3, f (7) = 4, we must simultaneously solve:
4
To say a function f (x) “passes” through a point (a, b) means that f (a) = b. When we say this we’re thinking
of f as a geometric curve. It’s ‘passing’ through the point because we imagine a dot on the curve moving
along it. That perspective allows for colorful language in place of notation.
5
If you’re more than comfortable solving basic systems of equations, you may want to skip ahead to Section 2.3.
This introductory chapter is intended to be much more gradual than the average math book.
16
a0 + a1 · 2 = 3
a0 + a1 · 7 = 4
If we solve for a0 in the first equation, we get a0 = 3 − 2a1 . Substituting that into the
second equation we get (3 − 2a1 ) + a1 · 7 = 4, which solves for a1 = 1/5. Plugging
this back into the first equation gives a0 = 3 − 2/5. This has forced the polynomial to be
exactly
( )
2 1 13 1
f (x) = 3− + x= + x.
5 5 5 5
Geometrically, a degree 1 polynomial is a line. So despite all our work above, we’re
just stating a fact we already know, that there is a unique line between any two points.
Well, it’s not quite the same fact. What is different about this scenario? The statement of
the theorem said, “x0 < x1 < · · · < xn ”. In our example, this means we require x0 < x1 .
So this is where we run a sanity check. What happens if x0 = x1 ? Think about it, and
if you can’t tell then you should try to prove it wrong: try to find a degree 1 polynomial
passing through the points (2, 3), (2, 5).
The problem could be that there is no degree 1 polynomial passing through those points,
violating existence. Or, the problem might be that there are many degree 1 polynomials
passing through these two points, violating uniqueness. It’s your job to determine what
the problem is. And despite it being pedantic, you should work straight from the defini-
tion of a polynomial! Don’t use any mnemonics or heuristics you may remember; we’re
practicing reading from precise definitions.
In case you’re stuck, let’s follow our pattern from before. If we call a0 + a1 x our
polynomial, saying it passes through these two points is equivalent to saying that there
is a simultaneous solution to the following two equations f (2) = 3 and f (2) = 5.
a0 + a1 · 2 = 3
a0 + a1 · 2 = 5
What happens when you try to solve these equations like we did before? Try it.
What about for three points or more? Well, that’s the point at which it might start to
get difficult to compute. You can try by setting up equations like those I wrote above, and
with some elbow grease you’ll solve it. Such things are best done in private so you can
make plentiful mistakes without being judged for it.
Now that we’ve worked out two examples of the theorem in action, let’s move on to
the proof. The proof will have two parts, existence and uniqueness. That is, first we’ll
show that a polynomial satisfying the requirements exists, and then we’ll show that if
two polynomials both satisfied the requirements, they’d have to be the same. In other
words, there can only be one polynomial with that property.
17
f (x) = y1
On to two points. Call them (x1 , y1 ), (x2 , y2 ) (note the variable is just plain x, and my
example inputs are x1 , x2 , . . . ). Now here’s an interesting idea: I can write the polyno-
mial in this strange way:
x − x2 x − x1
f (x) = y1 + y2
x1 − x2 x2 − x1
Let’s verify that this works. If I evaluate f at x1 , the second term gets x1 − x1 =
0 in the numerator and so the second term is zero. The first term, however, becomes
y1 xx11 −x
−x2 = y1 · 1, which is what we wanted: we gave x1 as input and the output was y1 .
2
Also note that we have explicitly disallowed x1 = x2 by the conditions in the theorem,
so the fractions will never be 0/0.
Likewise, if you evaluate f (x2 ) the first term is zero and the second term evaluates
to y2 . So we have both f (x1 ) = y1 and f (x2 ) = y2 , and the expression is a degree 1
polynomial. How do I know it’s degree one when I wrote f in that strange way? For one,
I could rewrite f like this:
y1 y2
f (x) = (x − x2 ) + (x − x1 ),
x1 − x2 x2 − x1
and simplify with typical algebra to get the form required by the definition:
( )
x1 y2 − x2 y1 y1 − y2
f (x) = + x
x1 − x2 x1 − x2
What a headache! Instead of doing all that algebra I, could observe that no powers of x
appear in the formula for f that are larger than 1, and we never multiply two x’s together.
Since these are the only ways to get degree bigger than 1, we can skip the algebra and be
confident that the degree is 1.
The key to the above idea, and the reason we wrote it down in that strange way, is
so that each constraint (i.e. f (x1 ) = y1 ) could be isolated in its own term, while all the
18
other terms evaluate to zero. For three points (x1 , y1 ), (x2 , y2 ), (x3 , y3 ) we just have to
beef up the terms to maintain the same property: when you plug in x1 , all terms except
the first evaluate to zero and the fraction in the first term evaluates to 1. When you plug
in x2 , the second term is the only one that stays nonzero, and likewise for the third. Here
is the generalization that does the trick.
Now you come in. Evaluate f at x1 and verify that the second and third terms are zero,
and that the first term simplifies to y1 . The symmetry in the formula should convince
you that the same holds true for x2 , x3 without having to go through all the steps two
more times.
Again, it’s clear that the polynomial we defined is degree 2, because each term consists
of a product of two degree-1 terms like (x − xi ) and taking their product gives at most x2 .
This has saved me the effort of rearranging that nonsense to get something in the form
of Definition 2.1.
The general formula for (x1 , y1 ), . . . , (xn , yn ) should follow the same pattern. Add
up a bunch of terms, and for the i-th term you multiply yi by a fraction you construct
according to the rule: the numerator is the product of x − xj for every j except i, and the
denominator is a product of all the (xi − xj ) for the same js as the numerator. It works
for the same reason that our formula works for three terms above. In fact, the process is
clear enough that you could write a program to build these polynomials quite easily, and
we’ll walk through such a program together at the end of the chapter.
Here is the notation version of the process we just described in words. It’s a mess, but
we’ll break it down.
∑n ∏ x − xj
f (x) = yi ·
xi − xj
i=0 j̸=i
∑ ∏
What a mouthful! I’ll assume the , symbols are new to you. They are read seman-
tically as “sum” and “product,” or typographically as “sigma” and ∑ “pi”. They essentially
represent loops of arithmetic. That is, if I have a statement like ni=0 (expr), it is equiva-
lent to the following code snippet.
int i;
sometype theSum = defaultValue;
return theSum;
I wrote it this way because defaultValue is whatever the conventional ‘zero object’
is in that setting. For adding numbers the zero object is zero, for concatenating lists it’s
19
the empty list, and for adding polynomials it’s the zero polynomial. It can get much more
exotic with more advanced mathematics,
∑ which we’ll see in Chapter 16 when we study
groups. The point is that the notation does not imply a specific type of the thing being
“summed.” It’s just a shorthand for the symbol +, and when you know what things are
being added, there’s always a contextually relevant zero.
∑ ∏
Moreover, explaining using code allows me to define by analogy: you just re-
place += with *= and reinterpret the “default value” as what makes sense for multiplica-
tion. Functional programmers will know this pattern well, because both are a “fold” (or
“reduce”) function with a particular choice of binary operation and initial value.
∏
The notation j̸=i adds three caveats. First, recall that in this context i is fixed by the
outer loop, so j is the looping variable (unfortunately, the reader is required to keep track
of scope when it comes to nested sums and products). Second, the bounds on j are not
stated; we have to infer them from the context. There are two hints: we’re comparing j
to i, so it should probably have the same range as i unless otherwise stated, and we can
see where in the expression we’re using j. We’re using it as an index on the x’s. Since
the x indices go from 0 to n, we’d expect j to have that range. It might seem totally
nonrigorous to a programmer, but if mathematicians consider it “easy” to infer the intent
of a notation, then it is considered rigorous enough.6
Though it sometimes makes me cringe to say it, give the author the benefit of the doubt.
When things are ambiguous, pick the option that doesn’t break the math. In this respect,
you have to act as both the tester, the compiler, and the bug fixer when you’re reading
math. The best default assumption is that the author is far smarter than we are, and
if we the reader don’t understand something, it’s likely a user error and not a bug. In
the occasional event that the author is wrong, it’s more often than not a simple mistake
or typo, to which an experienced reader would say, “The author obviously meant ‘foo’
because otherwise none of this makes sense,” and continue unscathed.
int i, j;
sometype theSum = defaultSumValue;
return theSum;
Compare the math and code, and make sure you can connect the structural pieces.
Often the inner∑parentheses
∏ are omitted, with the default assumption that everything to
the right of a or is in the body of that loop.
If the formula on the right still seems impenetrable, take solace in your own experience:
the reason you find the left side so easy to read is that you’ve spent years building up
the cognitive pathways in your brain for reading code. You can identify what’s filler
and what’s important; you automatically filter out the noise in the syntax. Over time,
you’ll achieve this for mathematical formulas, too. You’ll know how to zoom in to one
expression, understand what it’s saying, and zoom out to relate it to the formula as a
whole. Everyone struggles with this, myself included.
One additional difficulty of reading mathematics is that the author will almost never
go through these details for the reader. It’s a rather subtle point to be making so early
in our journey, but it’s probably the first thing you notice when you read math books.
Instead of doing the details, a typical proof of the existence of these polynomials looks
like this.
Proof. Let (x1 , y1 ), . . . , (xn , yn ) be a list of points with no two xi the same. To show
existence, construct f (x) as
∑
n ∏ x − xj
f (x) = yi
xi − xj
i=0 j̸=i
Clearly the constructed polynomial f (x) has degree n because each term has degree n.
For each i, plugging in xi kills7 all but the i-th term in the sum, and the i-th term clearly
evaluates to yi , as desired.
… Uniqueness part (we’ll complete this proof in the next section) …
7
“Kills” is an informal term for “this thing evaluates to zero.”
21
Theorem 2.3. The zero polynomial is the only polynomial over R of degree at most n which
has more than n distinct roots.
Proof. Let (x1 , y1 ), . . . , (xn , yn ) be a list of points with no two xi the same. To show
existence, construct f (x) as
∑
n ∏ x − xj
f (x) = yi
xi − xj
i=0 j̸=i
Clearly the constructed polynomial f (x) is degree ≤ n because each term has degree
n. For each i, plugging in xi kills all but the i-th term in the sum, and the i-th term clearly
evaluates to yi , as desired.
To show uniqueness, let g(x) be another such polynomial. Then f − g is a polynomial
with degree at most n which has all of the n + 1 values xi as roots. This implies that f − g
is the zero polynomial, or equivalently that f = g.
We spent quite a few pages expanding the details of a ten-line proof. This is par for the
course. When you encounter a mysterious or overly brief theorem or proof it becomes
your job to expand and clarify it as needed. Much like with reading programs written by
others, as your mathematical background and experience grows you’ll need less work to
fill in the details.
Now that we’ve shown the existence and uniqueness of a degree n polynomial passing
through a given list of n + 1 points, we’re allowed to give “it” a name. It’s called the
interpolating polynomial of the given points. The verb interpolate means to take a list of
points and find the unique minimum-degree polynomial passing through them.
Now we write the main interpolate function. It uses the yet-to-be-defined function
singleTerm that computes a single term of the interpolating polynomial for a given
degree. Note we use Python list comprehensions, for which [EXPRESSION for x in
myList] is a shorthand expression for the following.
output_list = []
for x in my_list:
output_list.append(EXPRESSION)
def interpolate(points):
""" Return the unique degree n polynomial passing through the given n+1 points.
"""
if len(points) == 0:
raise ValueError('Must provide at least one point.')
The first two blocks check for the edge cases: an empty input or repeating x-values.
Finally, the last block creates a list of terms, each one being a term of the sum from
the proof of Theorem 2.2. The return statement sums all the terms, with the second
argument being the starting value for the sum, in this case the zero polynomial. Now for
the singleTerm function.
24
Arguments:
- points: a list of (float, float)
- i: an integer indexing a specific point
"""
theTerm = Polynomial([1.])
xi, yi = points[i]
for j, p in enumerate(points):
if j == i:
continue
xj = p[0]
theTerm = theTerm * Polynomial(
[-xj / (xi - xj), 1.0 / (xi - xj)]
)
then they’d really have a good chance of guessing the rest and excluding the other two
daughters.9 So what I really want is a scheme that has the following properties.
2. If any four of the daughters gets together, they cannot use their shares to recon-
struct the secret.
3. If all five of the daughters get together, they can reconstruct the secret.
In fact, I’d be happier if I could prove, not only that any four out of the five daughters
couldn’t pool their shares to determine the secret, but that they’d provably have no infor-
mation at all about the secret. They can’t even determine a single bit of information about
the secret, and they’d have an easier time breaking open the safe with a jackhammer.
The magical fact is that there is such a scheme. Not only is it possible, but it’s possible
no matter how many daughters I have (say, n), and no matter what minimum size group
I want to allow to reconstruct the secret (say, k). So I might have 20 daughters,10 and I
may want any 14 of them to be able to reconstruct the secret, but prevent any group of
13 or fewer from doing so.
Polynomial interpolation gives us all of these guarantees. Here is the scheme. First
represent your secret s as an integer. Now construct a random polynomial f (x) so that
f (0) = s. We’ll say in a moment what degree d to use for f (x). If we know d, generating
f is easy. Call a0 , . . . , ad+1 the coefficients of f . Set a0 = s and randomly pick the
other coefficients. If you have n people, the shares you distribute are values of f (x) at
f (1), f (2), . . . , f (n). In particular, to person i you give the point (i, f (i)).
What do we know about subsets of points? Well, if any k people get together, they
can construct the unique degree k − 1 polynomial g(x) passing through all those points.
The question is, will the resulting g(x) be the same as f (x)? If so, they can compute
g(0) = f (0) to get the secret!
This is where we pick d, to control how many shares are needed. If we want k to be the
minimum number of shares needed to reconstruct the secret, we make our polynomial
degree d = k − 1. Then if k people get together and reconstruct g(x), they can appeal
to Theorem 2.2 to be sure that g(x) = f (x). For example, a degree 3 polynomial would
prevent any trio of people from reconstructing f (x), but allow 4 people to reconstruct
the secret. A degree 17 polynomial would stop any group of size ≤ 17 from obtaining
f (x).
Let’s be more explicit and write down an example. Say we have n = 5 daughters,
and we want any k = 3 of them to be able to reconstruct the secret. Then we pick a
polynomial f (x) of degree d = k − 1 = 2. If the secret is 109, we generate f as
Note that if you’re going to actually use this to distribute secrets that matter, you need
to be a bit more careful about the range of these random numbers. For the sake of this ex-
ample let’s say they’re random 10-bit integers, but in reality you’d want to do everything
with modular arithmetic. See the Chapter Notes for further discussion.
Next, we distribute one point to each daughter as their share.
(1, f (1)), (2, f (2)), (3, f (3)), (4, f (4)), (5, f (5))
To give concrete numbers to the examples, if
(1, 325), (2, 1083), (3, 2383), (4, 4225), (5, 6609).
The polynomial interpolation theorem tells us that with any three points we can com-
pletely reconstruct f (x), and then plug in zero to get the secret.
For example, using our polynomial interpolation algorithm, if we feed in the first, third,
and fifth shares we reconstruct the polynomial exactly:
At this point you should be asking yourself: how do I know there’s not some other
way to get f (x) (or even just f (0)) if you have fewer than k points? You should clearly
understand the claim being made. It’s not just that one can reconstruct f (0) when given
enough points on f , but also that no algorithm can reconstruct f (0) with fewer than k
points.
Indeed it’s true, and I’ll make two little claims to show why. Say f is degree d and you
have d points (just one fewer than the theorem requires to reconstruct). The first claim
is that there are infinitely many different degree d polynomials passing through those
same d points. Indeed, if you pick any new x value, say x = 0, and any y value, and
you add (x, y) to your list of points, then you get an interpolated polynomial for that list
whose “decoded secret” is different. Moreover, for each choice of y you get a different
interpolating polynomial (this is due to Theorem 2.3).
The second claim is a consequence of the first. If you only have d points, then not only
can f (0) be different, but it can be anything you want it to be! For any value y that you
think might be the secret, there is a choice of a new point that you could add to the list
to make y the “correct” decoded value f (0).
Let’s think about this last claim. Say your secret is an English sentence s = “Hello,
world!” and you encode it with a degree 10 polynomial f (x) so that f (0) is a binary
27
representation of s, and you have the shares f (1), . . . , f (10). Let y is the binary rep-
resentation of the string “Die, rebel scum!” Then I can take those same 10 points,
f (1), f (2), . . . , f (10), and I can make a polynomial passing through them and for which
y = f (0). In other words, your knowledge of the 10 points give you no information
to distinguish between whether the secret is “Hello world!” or “Die, rebel scum!” Same
goes for the difference between “John is the sole heir” and “Joan is the sole heir,” a case
in which a single-character difference could change the entire meaning of the message.
To drive this point home, let’s go back to our small example secret 109 and encoded
polynomial
I give you just two points, (2, 1083), (5, 6609), and a desired “fake” decrypted message,
533. The claim is that I can come up with a polynomial that has f (2) = 1083 and f (5) =
6609, and also f (0) = 533. Indeed, we already wrote the code to do this! Figure 2.3
demonstrates this with four different “decoded secrets.”
You should notice that the coefficients of the fake secret polynomial are no longer inte-
gers, but this problem is fixed when you do everything with modular arithmetic instead
of floating point numbers (again, see the Chapter Notes).
This scheme raises some interesting security questions. For example, if the secret is,
say, the text of a document instead of the key-code to a safe, and if one of the daughters
sees the shares of two others before revealing her own, she could compute a share that
produces whatever “decoded message” she wants, such as a will giving her the entire
inheritance!
This property of being able to decode any possible plaintext given an encrypted text
is called perfect secrecy, and it’s an early topic on a long journey through mathematical
cryptography.
2. Whenever you see a definition, you must immediately write down examples. They
are your test cases and form a foundation for intuition.
28
Figure 2.3: A plot of four different curves that agree on the two points (2, 1083), (5, 6609),
but have a variety of different “decoded secret” values.
2.7 Exercises
2.3 Verify the following theorem using the examples from the previous exercise. If a, n
are relatively prime integers, then aφ(n) has remainder 1 when dividing by n. This result
is known as Euler’s theorem (pronounced “OY-lurr”), and it is the keystone of the RSA
cryptosystem.
2.4 A number x is called algebraic if it is the root of a polynomial whose coefficients are
rational
√ number (fractions of integers). Otherwise it is called transcendental. Numbers
like 2 are algebraic, while√numbers like π and e are famously not algebraic. The golden
√ √
ratio is the number ϕ = 1+2 5 . Is it algebraic? What about 2 + 3?
2.5 Prove the product and sum of algebraic numbers is algebraic. Despite the fact that
π and e are not algebraic, it is not known whether π + e or πe are algebraic. Prove that
they cannot both be algebraic.
∑
n
an−1
ri = −
an
i=1
∏n
a0
ri = (−1)n .
an
i=1
Hint: if r is a root, then f (x) can be written as f (x) = (x − r)g(x) for some smaller
degree g(x). This formula (and its extensions) shows how the coefficients of a polynomial
encode information about the roots.
2.7 Look up a proof of Theorem 2.3. There are many different proofs. Either read one and
understand it using the techniques we described in this chapter (writing down examples
and tests), or, if you cannot, then write down the words in the proofs that you don’t
understand and look for them later in this book.
2.8 Bezier curves are single-variable polynomials that draw a curve controlled by a given
set of “contol points.” The polynomial separately controls the x and y coordinates of the
11
This also works for possibly complex roots.
30
Bezier curve, allowing for complex shapes. Look up the definition of quadratic and cubic
Bezier curves, and understand how it works. Write a program that computes a generic
Bezier curve, and animates how the curve is traced out by the input. Bezier curves are
most commonly seen in vector graphics and design applications as the “pen tool.”
2.9 It is a natural question to ask whether the roots of a polynomial f are sensitive to
changes in the coefficients of f . Wilkinson’s polynomial, defined below, shows that it is:
∏
20
w(x) = (x − i)
i=1
The coefficient of in w(x) is −210, and if it’s decreased by 2−23 the position of many
x19
of the roots change by more than 0.5. Read more details online, and find an explanation
of why this polynomial is so sensitive to changes in its coefficients.12
2.10 Write a web app that implements the distribution and reconstruction of the secret
sharing protocol using the polynomial interpolation algorithm presented in this chapter,
using modular arithmetic modulo and a 32-bit modulus p.
2.11 The extended Euclidean algorithm computes the greatest common divisor of two
numbers, but it also works for polynomials. Write a program that implements the Eu-
clidean algorithm to compute the greatest common divisor of two monic polynomials.
Note that this requires an algorithm to compute polynomial long division as a subrou-
tine.
2.12 Perhaps the biggest disservice in this chapter is ignoring the so-called Fundamental
Theorem of Algebra, that every single-variable monic polynomial of degree k can be
factored into linear terms p(x) = (x − a1 )(x − a2 ) · · · (x − ak ). The reason is that the
values ai are not necessarily real numbers. They might be complex. Moreover, all of
the proofs of the Fundamental Theorem are quite hard. In fact, one litmus test for the
“intellectual potency” of a new mathematical theory is whether it provides a new proof
of the Fundamental Theorem of Algebra! There is an entire book dedicated to these often-
repeated proofs.13 Sadly, we will completely avoid complex numbers in this book, with
the exception of a few exercises in Chapter 16 for the intrepid reader. Luckily, there is
a “baby” fundamental theorem, which says that every single-variable polynomial can be
factored into a product of linear and degree-2 terms
where none of the quadratic terms can be factored into smaller degree-1 terms. One
of the most famous mathematicians of all time, Carl Friedrich Gauss, provided the first
12
In “The Perfidious Polynomial,” Wilkinson wrote, “I regard [the discovery of this polynomial] as the most
traumatic experience in my career as a numerical analyst.”
13
Fine & Rosenberger’s “The Fundamental Theorem of Algebra.”
31
proof that this decomposition is possible as his doctoral thesis in 1799. As part of this
exercise, look up some different proofs of the Fundamental Theorem, but instead of trying
to understand them, take note of the different areas of math that are used in the proofs.
Twin Primes
The Twin Prime Conjecture, the assertion that there are infinitely many pairs of prime
numbers of the form p, p + 2, is one of the most famous open problems in mathematics.
Its origin is unknown, though the earliest record of it in print is in the mid 1800’s in a text
of de Polignac. In an exciting turn of events, in 2013 an unknown mathematician named
Yitang Zhang14 published a breakthrough paper making progress on Twin Primes.
His theorem is not about Twin Primes, but a relaxation of the problem. This is a typical
strategy in mathematics: if you can’t solve a problem, make the problem easier until you
can solve it. Insights and techniques that successfully apply to the easier problem often
work, or can be made to work, on the harder problem.
Zhang successfully solved the following relaxation of Twin Primes, which had been
attempted many times before Zhang.
Theorem. There is a constant M , such that infinitely many primes p exist such that the
next prime q after p satisfies q − p ≤ M .
if M is replaced with 2, then you get Twin Primes. The thinking is that perhaps it’s
easier to prove that there are infinitely many primes pairs with distance 6 of each other,
or 100. In fact, Zhang’s paper established it for M approximately 70 million. But it was
the first bound of its kind, and it won Zhang a MacArthur “genius award” in addition to
his choice of professorships.
As of this writing, subsequent progress, carried out by some of the world’s most famous
mathematicians in an online collaboration called the Polymath Project, brought M down
to 264. Assuming a conjecture in number theory called the Elliott-Halberstam conjecture,
they reduced this constant to 6.
14
Though he had a Ph.D, Zhang had worked in a motel, as a delivery driver, and at a Subway sandwich shop
when he was unable to find an academic job.
32
Impossibility of Clustering
A clustering algorithm is a program f that takes as input:
• A list of points S,
• A distance function d that describes the distance between two points d(x, y) where
x, y are in S,
and produces as output a clustering of S, i.e., a choice of how to split S into non-
overlapping subsets. The individual subsets are called “clusters.”
The function d is also required to have some properties that make it reasonably inter-
pretable as a “distance” function. In particular, all distances are nonnegative, d(x, y) =
d(y, x), and the distance between a point and itself is zero.
The Kleinberg Impossibility Theorem for Clustering says that no clustering algorithm
f can satisfy all of the following three properties, which he calls scale-invariance, richness,
and consistency.15
One can interpret this theorem as an explanation (in part) for why clustering is a hard
problem for computer science. While there are hundreds of clustering algorithms to
choose from, none quite “just works” the way we humans intuitively want one to. This
may be, as Kleinberg suggests, because our naive brains expect these three properties to
hold, despite the fact that they are mutually exclusive.
It also suggests that the “right” clustering function depends more on the application
you use it for, which raises the question: how can one pick a clustering function with
principle?
It turns out, if you allow the required number of output clusters to be an input to the
clustering algorithm, you can avoid impossibility and instead achieve uniqueness. For
more, see the 2009 paper “A Uniqueness Theorem for Clustering” of Zadeh and Ben-David.
The authors proceeded to study how to choose a clustering algorithm “in principle” by
studying what properties uniquely determine various clustering algorithms; meaning if
you want to do clustering in practice, you have to think hard about exactly what prop-
erties your application needs from a clustering. Suffice it to note that this process is a
superb example navigating the border separating impossibility, existence, and unique-
ness in mathematics.
15
Of incidental interest to readers of this book, Jon Kleinberg also developed an eigenvector-based search
ranking algorithm that was a precursor to Google’s PageRank algorithm.
33
a ≡ n mod p.
The syntactical operator precedence is a bit weird here: “mod” is not a binary opera-
tion, but rather describes the entire equation, as if to say, “everything here is considered
modulo p.”
We chose a prime p for the modulus because doing so allows you to “divide.” Indeed,
for a given n and prime p, there is a unique k such that (n · k) ≡ 1 mod p. Again,
an interesting example of existence and uniqueness. Note that it takes some work to
find k, and the extended Euclidean algorithm is the standard method. When evaluating a
polynomial function like f (x) at a given x, the output is taken modulo p and is guaranteed
to be between 0 and p.
Modular arithmetic is important because (1) it’s faster than arithmetic on arbitrarily
large integers, and (2) when evaluate f (x) at an unknown integer x not modulo p, the size
of the output and knowledge of the degree of f can give you some information about the
input x. In the case of secret sharing, seeing the sizes of the shares reveals information
about the coefficients of the underlying polynomial, and hence information about f (0),
the secret. This is unpalatable if we want perfect secrecy.
Moreover, when you use modular arithmetic you can prove that picking a uniformly
random (d + 1)-th point in the secret sharing scheme will produce a uniformly random
decoded “secret” f (0). That is, uniformly random between 0 and p. Without bounding
the allowed size of the integers, it doesn’t make sense to have a “uniform” distribution.
As a consequence, it is harder to define and interpret the security of such a scheme.
Finally, from discussions I’ve had with people using this scheme in industry, polyno-
mial interpolation is not fast enough for modern applications. For example, one might
want to do secret sharing between three parties at streaming-video rates. Rather, one
should use so-called “linear” secret sharing schemes, which are based on systems of lin-
ear equations. Such schemes are best analyzed from the perspective of linear algebra, the
topic of Chapter 10.
Chapter 3
You enter the first room of the mansion and it’s completely dark. You stumble around
bumping into the furniture but gradually you learn where each piece of furniture is.
Finally, after six months or so, you find the light switch, you turn it on, and suddenly
it’s all illuminated. You can see exactly where you were. Then you move into the next
room and spend another six months in the dark. So each of these breakthroughs, while
sometimes they’re momentary, sometimes over a period of a day or two, they are the
culmination of, and couldn’t exist without, the many months of stumbling around in the
dark that precede them.
We learned a lot in the last chapter. One aspect that stands out is just how slow the
process of learning unfamiliar math can be. I told you that every time you see a definition
or theorem, you had to stop and write stuff down to understand it better. But this isn’t
all that different from programming. Experienced coders know when to fire up a REPL
or debugger, or write test programs to isolate how a new feature works.
The main difference for us is that mathematics has no debugger or REPL. There is no
reference implementation. Mathematicians often get around this hurdle by conversation,
and I encourage you to find a friend to work through this book with. As William Thurston
writes in his influential essay, “On Proof and Progress in Mathematics,” mathematical
knowledge is embedded in the minds and the social fabric of the community of people
thinking about a topic. Books and papers support this, but the higher up you go, the
farther the primary sources stray from textbooks.
If you are reading this book alone, you have to play the roles of the program writer,
the tester, and the compiler. The writer for when you’re conjuring new ideas and asking
questions; the tester for when you’re reading theorems and definitions; and the compiler
to check your intuition and hunches for bugs. This often slows reading mathematics down
to a crawl, for novices and experts alike. Mathematicians always read with a pencil and
notepad handy.
When you first read a theorem, you expect to be confused. Let me say it again: the
rule is that you are confused, the exception is that everything is clear. Mathematical
culture requires being comfortable being almost continuously in a state of little to no
35
36
understanding. It’s a humble life, but once you nail down what exactly is unclear, you
can make progress toward understanding. The easiest way to do this is by writing down
lots of examples, but it’s not always possible to do that. We’ve already seen an example,
a theorem about the impossibility of having a nonzero polynomial with more roots than
its degree.
In the quote at the beginning of this chapter, Andrew Wiles discusses what it’s like to do
mathematical research, but the same analogy holds for learning mathematics. Speaking
with experienced mathematicians and reading their books makes you feel like an idiot.
Whatever they’re saying is the most basic idea in the world, and you barely stumble along.
My favorite dramatic embodiment of this feeling is an episode of a YouTube series called
Kid Snippets in which children are asked to pretend to be in a math class, while adult
actors act it out using dubbed voices.1 The older child tries to explain to the younger child
how to subtract, and the little kid just doesn’t get it. Aside from being absolutely hilarious,
the video has a deep and probably unintentional truth, that the more mathematics you
try to learn the more you feel like the poor student! The video especially resonates when,
toward the end, the teacher asks, “Do you get it now?” and the student pauses and slowly
says, “Yes.” That yes is the fledgling mathematician saying, “I obviously don’t understand,
but I’ve accepted it and will try to understand it later.”
I’ve been in the student’s shoes a thousand times. Indeed, if I’m not in those shoes at
least once a day then it wasn’t a productive day! I say at least a dozen stupid things daily
and think countlessly many more stupid thoughts in search of insight. It’s a rare moment
when I think, “I’m going to solve this problem I don’t already know how to solve,” and
there is no subsequent crisis. Even in reading what should be basic mathematical material
(there’s a huge list of things that I am embarrassed to be ignorant about) I find myself
mentally crying out, “How the hell does that statement follow⁉”
I had a conversation with an immensely talented colleague, a far more talented math-
ematician than I, in which she said (I paraphrase), “If I spend an entire day and all I do is
understand this one feature of this one object that I didn’t understand before, then that’s a
great day.” We all have to build up insight over time, and it’s a slow and arduous process.
In Andrew Wiles’s analogy, my friend is still in the dark room, but she’s feeling some
object precisely enough to understand that it’s a vase. She still has no idea where the
light switch is, and the vase might give her no indication as to where to look next. But
if piece by piece she can construct a clear enough picture of the room in her mind, then
she will find the switch. What keeps her going is that she knows enough little insights
will lead her to a breakthrough worth having.
Though she is working on far more complicated and abstract mathematics than you
are likely to, we must all adopt her attitude if we want to learn mathematics. If it sounds
like all of this will take way too much of your time (all day to learn a single little thing!),
remember two things. First, my colleague works on much more abstract and difficult
mathematics than the average programmer interested in mathematics would encounter.
She’s looking for the meta-insights that are many levels above the insights found in this
1
You can watch it at http://youtu.be/KdxEAt91D7k
37
book. As we’ll see in Chapter 11, insights are like a ladder, and every rung is useful.
Second, the more you practice reading and absorbing mathematics, the better you get
at it. When my colleague says she spent an entire day understanding something, she
efficiently applied tools she had built up over time. She knows how to cycle through
applicable proof techniques, and how to switch between different representations to see
if a different perspective helps. She has a bank of examples to bolster her. Her time
budget just balances out to a day because of the difficulty of her work.
But most importantly, she’s being inquisitive! Her journey is led as much by her task
as by her curiosity. As mathematician Paul Halmos said in his book, “I Want to be a
Mathematician,”
Don’t just read it; fight it! Ask your own questions, look for your own examples, discover
your own proofs.
Mathematician Terence Tao expands on this in his essay, “Ask yourself dumb
questions—and answer them!”
When you learn mathematics, whether in books or in lectures, you generally only see
the end product—very polished, clever and elegant presentations of a mathematical topic.
However, the process of discovering new mathematics is much messier, full of the pursuit
of directions which were naive, fruitless or uninteresting.
While it is tempting to just ignore all these “failed” lines of inquiry, actually they turn out
to be essential to one’s deeper understanding of a topic, and (via the process of elimination)
finally zeroing in on the correct way to proceed.
So one should be unafraid to ask “stupid” questions, challenging conventional wisdom on
a subject; the answers to these questions will occasionally lead to a surprising conclusion,
but more often will simply tell you why the conventional wisdom is there in the first place,
which is well worth knowing.
So you’ll get confused. We all do. A good remedy is finding the right pace to make
steady progress. And when in doubt, start slow.
Chapter 4
Sets
God created infinity, and man, unable to understand infinity, created finite sets.
– Gian-Carlo Rota
In this chapter we’ll lay foundation for the rest of the book. Most of the chapter is
devoted to the mathematical language of sets and functions between sets. Sets and func-
tions serve not only as the basis of most mathematics related to computer science, but
also as a common language shared between all mathematicians. Sets are the modeling
language of math. The first, and usually simplest, way to convert a real world problem
into math involves writing down the core aspects of that problem in terms of sets and
functions. Unfortunately set theory has a lot of new terminology, The parts that are new
to you are best understood by writing down lots of examples.
After converting an idea into the language of sets, you may use the many existing tools
and techniques for working with sets. As such, the work one invests into understanding
these techniques pays off across all of math. It’s largely the same for software: learning
how to decompose a complex problem into simple, testable, maintainable functions pays
off no matter the programming language or problem you’re trying to solve. The same
goes for the process of modeling business rules in software in a way that is flexible as the
business changes. Sets are a fundamental skill.
At the end of the chapter we’ll see the full modeling process for an application called
stable marriages, which is part of an interdisciplinary field of mathematics and economics
called market design. In economics, there are occasionally markets in which money can’t
be used as a medium of exchange. In these instances, one has to find some other mech-
anism to allow the market to function efficiently. The example we’ll see is the medical
residency matching market, but similar ideas apply to markets like organ donation and
housing allocation. As we’ll see, the process of modeling these systems so they can be
analyzed with mathematics requires nothing more than fluency with sets and functions.
The result is a Nobel-prize winning algorithm used by thousands of medical students
every year, and the algorithm.
39
40
S = {x : x ∈ N, x is divisible by 7}
The notation reads like the sentence in words, where the colon stands for “such that.”
I.e., “The set of values x such that x is in N and x is divisible by 7.” Sometimes a vertical
pipe | is used in place of the colon. The symbols separate the constructive expression
from the membership conditions (it’s not an output-input pipe as in shell scripting). As
with sets of numbers, the ∈ symbol denotes membership in a set, and the objects in a set
are often called elements.
In a language with infinite list comprehensions, say Haskell, the above would be im-
plemented as follows:
Of course, lists made with list comprehensions need not have unique elements, while
mathematical sets must, but the notation is similar enough. In set-builder notation you
can add whatever conditions you like after the colon, even if you don’t know how to
compute them! The left hand side of the colon may also be an expression, as in
1
There are some strange “meta” things you are not allowed to describe as sets, such as the set of all sets. It
turns out this is not a set, and it caused a lot of grief to early 20th century mathematicians who really care
about the logical foundations of mathematics. This book omits these topics, since all of our sets will be
comfortably finite or concrete like R.
41
Now we turn to some definitions you may already be familiar with. If not, remember
it’s your job to write down examples. In either case, mathematical texts typically define
something once and only once. I will occasionally repeat definitions that are used across
chapters, but generally authors will not. You’re expected to have understood a definition
to an appropriate degree of comfort before continuing.
Definition 4.1. The cardinality or size of a set A, denoted |A|, is the number of elements
in A when that number is finite, and otherwise we say A has infinite cardinality.2 The
set of cardinality zero is called the empty set.
Most of the time proving one set is a subset of another is trivial, but not always. The
standard technique is to fix b to be an arbitrary element of B, and use whatever charac-
teristic defines B to show that b ∈ A as well. Here’s a brief example: the set of integers
divisible by 57 is a subset of the set of integers divisible by 3, because any number b di-
visible by 57 has the form b = 57 · k = 3 · (19 · k), which means it’s also divisible by 3.
No alarms and no surprises.
If I have a binary boolean-valued operator like ∈, then putting a slash through it like
̸∈ denotes the negation of that claim or query. Other slashed operators include ̸=, ̸⊂, ̸∼.
Definition 4.3. Given two sets A and B, the complement of B in A is the set {a ∈ A :
a ̸∈ B}. The complement is denoted either by A \ B or A − B, and sometimes B C when
B ⊂ A and A is clear from context.
You can already see I’m starting to be creatively flexible with set-builder notation. Here
a ∈ A might be interpreted as a boolean-valued expression, suggesting the set has only
boolean-valued members. However, reading it as a sentence makes sense of it instead
as an assertion: “The set of a in A such that a is not in B.” Writing it more verbosely,
{a : a ∈ A and a ̸∈ B} is extra work without significant gain for the reader. If you prefer
the verbose version, it’s likely because you’ve spent so long phrasing your thoughts to
be machine readable. Appeal to your inner voice here, not your inner type-checker.
2
See the exercises for more about infinite sets.
3
Mathematicians are divided on whether A ⊂ B allows A to be equal to B. Some authors insist, drawing
from the ≤ and < notation for numbers, that only A ⊆ B allows for A = B, and they call ⊂ the “strict
subset” operator. I have never heard a convincing argument that the matter warrants debate, and so I opt
for the briefest: ⊂ allows equality. This book never requires a strict subset operator, but if it did I would use
⊊.
42
If you want some practice working with basic set definitions, prove that for any two
sets A, B, the following containments hold: A ∩ B ⊂ A and A ⊂ A ∪ B.
Definition 4.5. The product of two sets A, B denoted A × B, is the set of all ordered
pairs of elements in a and elements in b. In set-builder notation it is:
A × B = {(a, b) : a ∈ A and b ∈ B}
The product is the usual way we turn the real line R into the real plane R2 . That is, R2
is defined to be R × R and R3 = R × R × R. Unpacking this, there is a little confusion
over where the parentheses go. That is, should it be (R × R) × R or R × (R × R)? These
give rise to two different sets. The first is
(R × R) × R = {((a, b), c) : a ∈ R, b ∈ R, c ∈ R}
We want these sets to be considered the same. Indeed, the difference between the two
is the kind of distinction that programmers are very familiar with, because compilers will
refuse to proceed unless the parentheses align. But mathematicians, for reasons we’ll see
shortly,5 brush aside the difference and just say they’re the “same” set, and they’re both
equivalent to
(R × R) × R = R × (R × R) = {(a, b, c) : a ∈ R, b ∈ R, c ∈ R}
We will return later in this chapter, and again in Chapters 9 and 16 when complexity
will beg for a rigorous and useful abstraction called the quotient, to understand why it’s
okay to call these two sets “the same.” For now, simply define an n-fold product to collapse
pairs into tuples of length n:
Rn = R
| × ·{z
· · × R} = {(a1 , . . . , an ) : ai ∈ R for every i}
n times
This notation can be used for any set. Next we define functions as special subsets of a
product.
4
Hence the name of my blog, Math ∩ Programming.
5
There’s a bijection!
43
F = {(1, 1), (2, 4), (3, 9), (4, 16), . . . } = {(x, x2 ) : x ∈ N}.
It’s a subset of N × N. Now we can add a bit of notation: instead of saying that (3, 9) ∈
F we use the mapping notation F (3) = 9. With this, we could describe F the way
we wanted to all along, as F (x) = x2 . The conditions in the definition ensure that every
input x has some output F (x), and that each input x has only one output F (x). Providing
a concrete algorithm to compute the output from the input makes these conditions trivial,
as is the case with squared integers, but an algorithm is not needed to define a function.
So why go through all the trouble of defining functions in terms of sets? Part of this is
historical. The concept of sets as a modeling tool has probably existed for as long as math-
ematics, but it was primarily used in its language form (“I declare, considereth only those
heavenly numbers whose factorisation into prymes containeth nary a repeated factor!”).
Indeed, the notation y = f (x) was invented in the 1700’s by Leonhard Euler. It was not
until the late 19th century that mathematicians formally studied sets, and proposed them
as a logical foundation for all of mathematics. To do so requires restating all existing
concepts in terms of sets. Definition 4.6 does this for functions. Similar definitions exist
defining, e.g., integers and ordered tuples in terms of sets. However, our initial definition
of a set was compltely imprecise. There is a more precise definition, but it is the sort
that only a logician would love. In brief, its base concepts are just the empty set and set
containment, and a restricted choice of ways to build sets from smaller sets. Using this
one can define numbers, functions—even all of calculus—from “first principles.” To instill
this idea in future mathematicians, many introductory proof textbooks define everything
in terms of sets, and do formal proofs to a degree of precision most mathematicians avoid
in their day to day work.
In theory, mathematicians quite like the idea that everything can be reduced to sets.
Actually doing it in practice will drive you mad. It’s like writing all your programs in
pure binary. We need abstractions to keep ourselves productive, and we take comfort in
the idea that, if we wanted, we could peel back the layers to reveal the raw instructions.
Defining the entirety of mathematics in sets is like “bare metal” programming, but with-
out any of the speed benefits of the finished program. Someone did it once, and we have
44
a record of their work that has been checked many times (and we could check it if we
wanted to), and social consensus is good enough.
For us, the special notation for functions highlights our conceputal emphasis. We think
of functions differently than regular sets, with a semantic input-output dependence that
set notation doesn’t natively convey. For most of the book, we’ll ditch the mindset that
all ideas must be modeled as a set. Rather, set-builder notation is a tool to help clarify an
idea, or iron out a particularly tricky crease in a definition.
Now we turn to a few useful definitions about subsets of inputs and outputs of a func-
tion. A seasoned programmer is less likely to be familiar with the remainder of the defi-
nitions in this chapter, but we will rely on them throughout the book.
Definition 4.7. Given a function f : A → B, we define the image of f (or the image of
A under f ) as the set
f (A) = {f (a) : a ∈ A}
This is denoted f (A) to signify that we’re putting everything in A through f , though
it is also denoted im(f ) or just im f . If C ⊂ A is a subset, we can similarly define the
image of C, denoted f (C), as f (C) = {f (c) : c ∈ C}. The image of A is equivalent to
the range of a function with domain A, but we use a different word so we can speak of
the image of a particular subset as well.
As a shorthand for “there exists,” mathematicians often use the symbol ∃. So an equiv-
alent definition of im f is
A B
Figure 4.1: An example of an injection, where different inputs are mapped to different
outputs. The dots are elements of the set, and the arrows show the mapping. This example
is also a non-surjection.
A B
Figure 4.2: An example of an surjection, where every element of the codomain is hit by
some element of the domain mapped through f . The dots are elements of the set, and the
arrows show the mapping. This example is also a non-injection.
46
A B
in generality. This can grease the wheels of a complicated proof in an advanced setting,
but more importantly it separates the aspects of a topic concerning sets with additional
structure that are intrinsic to that structure from those that are “merely” questions about
the underlying sets.
Indeed, here are two propositions we’ll use much later in our study of linear algebra
concerning the existence and structure of inverses. If you feel emotionally drained by all
the definitions in this chapter so far, feel free to skip these and come back when we refer
to them in Chapter 12.
Proof. It’s crucial here that f is surjective (otherwise the theorem is not true!). Given
b ∈ B, we need to show that f (g(b)) = b. Start by choosing an a ∈ A for which
f (a) = b. Then g(b) = g(f (a)) = a. Apply f to both sides to get f (g(b)) = f (a) = b,
as desired.
there is a bijection (R × R) × R → R × (R × R) that maps ((a, b), c) to (a, (b, c)). Often
when mathematicians want to “call” two things the same, they’ll come up with such a
bijection, and say the two things on either side of such a bijection should be considered
the same. It’s kind of like a typecast that’s built into the programming language, but it’s
always reversible here. The formal idea is called a “quotient,” which we’ll make formal in
Chapter 9.
Let’s rephrase that elegant argument in the language of sets, if X is the set of games
and Y is the set of players, then we can define a function f : X → Y by calling f (x)
the loser of game x. Then the image f (X) is the subset L ⊂ Y of losers. In fact, f is an
injection (two different games have different losers), and f defines a bijection between
X and L. This means that X and L have the same size, and the fact that there is only
one winner of the entire tournament means that |L| = |Y | − 1. So if there are n players
then there will always be n − 1 games.
To make sure you understand this argument, extend it to the case of a double-
elimination tournament. In double-elimination, you are ousted from the tournament once
you’ve lost two games, and a player who loses one game might still ultimately win the
tournament. In this case you won’t have an injection, but a so-called “double-cover” of
the set of players. What I mean by double-cover is that every y ∈ Y has a preimage
f −1 (y) = {x ∈ X : f (x) = y} of size exactly 2. Another minor complication here is
that the winner may have lost zero games or one game. So you can’t count the number
of games exactly, but you can compute tight bounds instead.
This general strategy for counting has applications any time you need to count or
estimate the size of a set. Imagine you want to estimate the number of homeless people
in a city, a problem the Census Bureau faces regularly. You have to find a clever way to
implicitly count them by observing the residual effects of their actions. This is precisely
looking for functions between sets that are close to bijections, or double- or triple-covers
of the set you want to count.
Here is another magnificent
( ) example of finding a clever bijection. Given a set X let’s
define the quantity X2 , read “X choose two,” to be the set of all unordered pairs of
distinct elements of X. I.e.,
( )
X
= {{x, y} : x, y ∈ X and x ̸= y}.
2
( ) ( )
If X is a finite set of size n = |X|, we denote the size of X2 by n2 . Obviously this
value doesn’t depend on the(particular
) elements in X, just its size, so it’s okay to use n
n
in place of X. Colloquially, 2 is the number of ways to choose two objects from( a)set
of n objects.8 So the question is, can we come up with an arithmetic formula for n2 in
terms of n? You may have already seen the answer: it’s n(n−1) 2 . But we’ll show by way
of a bijection that it’s equal to the quantity
1 + 2 + · · · + n − 1.
In fact, the bijection is easiest to understand by the picture in Figure 4.4. Here’s how
we read this picture. We’re setting n = 7 and calling the lightly shaded balls Y , and
calling the( n) squares in the last row X. The picture shows how to define a bijection
g : Y → X2 : given any ball y ∈ Y , you draw two diagonals as in the picture and you
get g(y) as the pair of squares at the end of both diagonals. The picture should convince
8
(n)
In general, k
is the number of different ways to choose k objects from an n object set.
50
( )
Figure 4.4: A picture proof that | X2 | = 1 + 2 + · · · + n − 1. Each pair of squares in the
bottom row corresponds to a unique ball in the pyramidal arrangement above it.
you that two different choices of balls give you different diagonals, so that means g is
an injection. Likewise, if you give me a pair of squares x1 , x2 ∈ X, the inner diagonals
meet at a ball y that maps under g back to (x1 , x2 ). So g is a surjection, and together with
being an injection this makes g a bijection.
Now we count: how many balls and squares are there? The last row has n − 1 = 6
squares, and each row has one fewer ball than ( the
) row( )underneath it, so |Y | = 1 + 2 +
· · · + n − 1. Moreover, X has n balls in it, so | X2 | = n2 . The bijection tells us that these
two values must be equal.
You might be wondering: how can we use a picture as the central part of our proof?
Didn’t we only prove that this bijection works for n = 7? Technically you’re right: no
mathematician would consider a picture as a rigorous proof in of itself. However, when
the goal is to communicate the central nugget of wisdom in a proof, a small example with
all the essential features of a general proof is often good enough. Consider one alternative.
You could actually make the balls points inside R2 somewhere. You’d need a generic way
to construct coordinates for them, and a generic way to describe the diagonals. That’s a
huge pain in the ass for something so simple! Every mathematician would agree it could
be done but it would be a colossal waste of time to actually do it.
This is a common feature of more advanced mathematics. Mathematicians are con-
stantly reading papers, and there is rarely enough time to verify all the details of every
argument. If you’re not an official reviewer of the paper before it’s been published, it is
usually enough to be convinced that something should be true, especially if the details
are messy but clear, while focusing on the high level picture. An example with all the
essential features of a general solution is an effective substitute. And this doubles for
readers of mathematics too: finding a simple example with the essential features of a
general solution, and testing claims on the example, is one of the best ways to read a
proof!
51
1. First show the base case, in this case that P (6) is true.
2. Second, do the inductive step, where one uses the assumption that P (n) is true to
prove that P (n + 1) is true. Equivalently, one can use P (n − 1) to prove P (n).
Just like with recursion, you get a chain of proofs: P (6) implies P (7) implies . . . im-
plies P (n) for any n you like. One bit of terminology: one often invokes the inductive
hypothesis, which is the assumption that P (n) is true. It’s helpful when P (n) is cumber-
some to restate. ( )
Let’s use induction for a second proof that n2 = 1 + 2 + · · · + n − 1.
Proof. Call the statement to be proved P (n). We prove this by induction for n ≥ 2. For
the base case10 n = 2, we need to prove
( )
2
P (2) : =1
2
()
We argue 22 is trivially 1. There is only one way to choose two items from a set of
two items. Now assume the inductive hypothesis P (n) holds:
9
It also displays some of the variety of programming approaches. Fibonacci sequences can be computed in-
place with an array, using recursion (and hopefully memoization), or with a closed-form formula. Each has
advantages and disadvantages that show how we think about tradeoffs in software.
10
When n = 0 or 1 we are asking how many ways there are to choose two things from a set of fewer than two
things. According to our definition this is zero (which you saw if you wrote your test cases starting from the
simplest ones), and one usually calls an empty summation to be zero. But the first n that’s not “vacuously”
true is n = 2 so we start there.
52
( )
n
P (n) : = 1 + 2 + · · · + n − 1.
2
We must now prove that P (n + 1) follows, i.e.:
( )
n+1
P (n + 1) : = 1 + 2 + · · · + n.
2
( )
Take the set X = {1, 2, . . . , n + 1} of size n + 1, and consider the set X2 of ways to
pick two elements from X. Note that we are using numbers as elements of X instead of
“arbitrary objects.” We might have instead called them “ball 1, ball 2, ball 3” and discuss
how many ways to select two balls from a bin. (For )simplicity of notation we’ll just use
n+1
the numbers themselves. Now
(n) X is a set of size 2 and we want to express it in terms
of our known formula for 2 . Pick any element of X, say n + 1, and define Y to be the
set that remains after removing that element from X.
Y = X − {n + 1} = {1, 2, . . . , n}.
( )
Now I will split up the elements of X2 into two parts: the parts where both chosen
elements are in Y , and the part where one of the two chosen elements is n + 1. Since
there are no other options and (no )overlap between the two options, I can add the sizes of
both parts to count the size of X2 . ( )
( )
The first part, where both chosen elements are in Y , has size Y2 = n2 , which by
the inductive hypothesis is 1 + 2 + · · · + n − 1. The second part, where one of the chosen
elements is guaranteed to be n + 1, has size n by the following reasoning: if you had to
choose n + 1 as one of your two elements, then there are only n remaining choices for
your second element, and so there are only n ways to do this.11
Adding up the sizes of the two parts gives exactly
1 + 2 + · · · + (n − 1) + n,
which is what we set out to prove.
true, we can still prove it by induction.” Be that as it may, induction is a central tool for
proving theorems.
The second proof technique is called “proof by contradiction.” There’s a simple puzzle
I often use to illustrate the technique.
You’re at a party. You’re chatting with your friend, and out of curiosity you ask how
many friends he has at the party. He counts them up, there are five, and you realize that
you also have five friends at the party. What a coincidence! Putting on your mathemati-
cian hat, you poll everyone at the party and you’re shocked to find that a few other people
also have five friends at the party. The puzzle is: is this true of every party? Maybe not
five exactly, but will there always be at least two people with the same number of friends
who are at the party?
Before I give the solution by contradiction, let’s iron out what I mean by “friendship.” I
insist that friendship is symmetric: you can’t be friends with someone who is not friends
with you. And moreover you can’t be friends with yourself.12
You’ll appreciate the answer to this problem best if you spend some time trying to
solve it first.
Back already? Okay, so the answer is yes, there will always be a pair of people with the
same number of friends. The technique we use to prove it is called proof by contradiction.
It works by assuming the opposite of what you want to prove it true, and using that
assumption to deduce nonsense.
Proof. Suppose for the sake of contradiction that there is some party where everybody has
a different number of friends at the party. Say the party has n people, then everyone must
have between zero and n − 1 friends. Since there are n people and n different numbers
between zero and n − 1, we can map each person to the number of friends they have,
and this map will be a bijection. Now here comes the contradiction: that means someone
must have zero friends at the party, and someone must have n − 1 friends, i.e., someone
must be friends with everyone. But the person who is friends with everyone must be
friends with the person that has no friends! The only way to resolve this contradiction
is if the original assumption is actually false. That is, there must be two people with the
same number of friends.
This is how every proof by contradiction goes, but they’re usually a bit more concise.
They always start with, “Suppose to the contrary” to signal the method. And there is
no warning when the contradiction will come. A proof writer usually just states the
contradiction and follows it with “which is a contradiction,” ending the proof.13
12
Looking forward to Chapter 6 on graph theory, we’re saying that the social connections at our “party” form
a simple, undirected graph.
13
A professor of mine had a funny refrain to end his proofs by contradiction. If, say, x was assumed to be
prime, he’d arrive at a contradiction and say, “and this is very embarrassing for x because it was claiming to
be prime.”
54
The point of a proof by contradiction is to get an object with a property that you can
work with. If you’re trying to prove that no object with some special property exists, a
proof by contradiction gives you an instance of such an object, and you can use its special
property to go forward in the proof. In this case the object was a special friendship count
among partygoers, and in the next section we’ll apply the same logic to “marriages.”
2. The pair m and w mutually prefer each other over their assigned mathces. I.e., both
rankm (w) < rankm (f (m)) and rankw (m) < rankw (f −1 (w)).
In other words, the bijection is called stable if there is no pair of people with mutual
incentive to cheat on their assigned spouses. This is not to say cheating can’t happen, but
if it does one of the two involved will be “lowering their standards.”
The algorithmic question is, given lists of preferences as input, can we find a stable
marriage? Can we even guarantee a stable marriage will exist for any set of preferences?
The answer to both questions is yes, and it uses an algorithm called deferred acceptance.
Here is an informal description of the algorithm. It goes in rounds. In each round,
each man “proposes” to the highest-preferred woman that has not yet rejected him. On
the other side, each woman holds a reference to a man at all times. If a woman gets new
proposals in a round, she immediately rejects every proposer except her most preferred,
but does not accept that proposal. She “defers” the acceptance of the proposal until the
very end.
The rejected men are sad, but in the next round they recover and propose to their next
most preferred woman, and again the women reject all but one. The men keep proposing
until every man is tentatively held by some woman, or until all women have rejected
them. That is not a happy place to imagine. But actually, the theorem that we’ll prove
says that this process always ends with each woman holding onto a man, and no men are
left out; the set of women’s held picks forms a stable bijection.
Before we prove that the algorithm works, let’s state it more formally in Python code. A
complete working program is available on this book’s Github repository.15 In the interest
of generality, I’ve defined classes Suitor and Suited to differentiate: Suitors propose
to Suiteds.
class Suitor:
def __init__(self, id, preference_list):
self.preference_list = preference_list
self.index_to_propose_to = 0
self.id = id
def preference(self):
return self.preference_list[self.index_to_propose_to]
def post_rejection(self):
self.index_to_propose_to += 1
15
See pimbook.org
56
The Suitor class is simple. Instances are uniquely identified by an id, which I’m defin-
ing to be the index in a global list of Suitors. A Suitor has a preference_list, which
is a list of integers so that the i-th entry in the list is the index of a global list of Suiteds
containing the i-th most preferred Suited. In other words, the id of the most preferred
Suited is the first entry in preference_list. The index_to_propose_to variable si-
multaneously counts the number of rejections and which index in the preference_list
to use for the next proposal.
class Suited:
def __init__(self, id, preference_list):
self.preference_list = preference_list
self.held = None
self.current_suitors = set()
self.id = id
def reject(self):
"""Return the subset of Suitors in self.current_suitors to reject,
leaving only the held Suitor in self.current_suitors.
"""
if len(self.current_suitors) == 0:
return set()
self.held = min(self.current_suitors,
key=lambda suitor: self.preference_list.index(suitor.id))
rejected = self.current_suitors - set([self.held])
self.current_suitors = set([self.held])
return rejected
Here current_suitors are the new proposals in a given round, and held is the
Suited’s held pick. In the method reject, a Suited looks at all her current suitors,
chooses the minimum according to the index in her preference_list, and returns all
but the best as rejected Suitors.
Finally, we have the main routine for the deferred acceptance algorithm.
57
The dictionary at the end is the type we use to represent a bijection. Now let’s prove
this algorithm always produces a stable marriage.
Theorem 4.14. The deferred acceptance algorithm always terminates, and the bijection
produced at the end is stable.
Proof. We argue that the algorithm will terminates by monotonicity. Here’s what I mean
by that: say you have a sequence of integers a1 , a2 , . . . which is monotonic increasing,
meaning that a1 < a2 < · · · . Say moreover that you know none of the ai are larger than
50—ai is bounded from above—but each ai+1 ≥ ai + C for some constant C > 0. Then
it’s trivial to see that either the sequence stops before it hits 50, or eventually it hits 50.
To show an algorithm terminates, you can cleverly choose an integer at for each step t,
and show that at is monotonic increasing (or decreasing) and bounded. Then show that
if the algorithm hits the bound then it’s forced to finish, and otherwise it finishes on its
own.
For the deferred acceptance algorithm we have a nice sequence. For round t set at
to be the sum of all the Suitor’s index_to_propose_to variables. Recall that this
variable also represents the number of rejections of each Suitor. Since there are exactly
n preferences in the list and exactly n Suitors, we get the bound at ≤ n2 (each Suitor
could be at the very end of their list; come up with an example to show this can happen!).
Moreover, in each round one of two things happens. Either no Suitor is rejected
by a Suited and by definition the algorithm finishes, or someone is rejected and their
index_to_propose_to variable increases by 1, so at+1 ≥ at +1. Now in the case where
all the Suitors are at the end of their lists, that means that every Suited was proposed
to by every Suitor. In other words, each of the Suiteds gets their top pick: they only
reject when they see a better option, and they got to consider all proposals! Clearly the
algorithm will stop in this case.
Now that we’ve shown the algorithm will stop, we need to show the bijection f pro-
duced as output is stable. The definition of stability says there is no Suitor m and
58
Suited w with mutual incentive to have an affair, so for contradiction’s sake we’ll
suppose that the f output by the algorithm does have such a pair, i.e., for some m, w,
prefm (w) < prefm (f (m)) and prefw (m) < prefw (f −1 (w)).
What had to happen to w during the algorithm? Well, m ended up with f (m) instead
of w, and if prefm (f (m)) > prefm (w), then m must have proposed to w at some earlier
round. Likewise, the held pick of w only increases in quality when w rejects a Suitor,
but w ended up with some Suitor f −1 (w) while prefw (m) < prefw (f −1 (w)). So at some
point in between being proposed to by m and choosing to hold on to f −1 (w), w had to
go the wrong way in her preference list, contradicting the definition of the algorithm.
>>> suitors = [
Suitor(0, [3, 5, 4, 2, 1, 0]),
Suitor(1, [2, 3, 1, 0, 4, 5]),
Suitor(2, [5, 2, 1, 0, 3, 4]),
Suitor(3, [0, 1, 2, 3, 4, 5]),
Suitor(4, [4, 5, 1, 2, 0, 3]),
Suitor(5, [0, 1, 2, 3, 4, 5]),
]
>>> suiteds = [
Suited(0, [3, 5, 4, 2, 1, 0]),
Suited(1, [2, 3, 1, 0, 4, 5]),
Suited(2, [5, 2, 1, 0, 3, 4]),
Suited(3, [0, 1, 2, 3, 4, 5]),
Suited(4, [4, 5, 1, 2, 0, 3]),
Suited(5, [0, 1, 2, 3, 4, 5]),
]
>>> stable_marriage(suitors, suiteds)
{
Suitor(0): Suited(3),
Suitor(1): Suited(2),
Suitor(2): Suited(5),
Suitor(3): Suited(0),
Suitor(4): Suited(4),
Suitor(5): Suited(1),
}
5. A picture or example that captures the spirit of a fully general proof is often good
enough.
4.6 Exercises
4.1 Write down examples for the following definitions. A set A (finite or infinite) is
called countable if there is a surjection N → A. The power set of a set A, denoted 2A , is
the set of all subsets of A. For two sets A, B, we denote by B A the set of all functions
from A to B. This makes sense with the previous notation 2A if we think of “2” as the
set of two elements 2 = {0, 1}, and think of a function f : A → {0, 1} as describing a
subset C ⊂ A by sending elements of C to 1 and elements of A − C to 0. In other words,
the subset defined by f is C = f −1 (1).
4.2 Prove De Morgan’s law for sets, which for A, B ⊂ X states that (A ∩ B)C =
AC ∪ B C , and (A ∪ B)C = AC ∩ B C . Draw the connection between this and the
corresponding laws for negations of boolean formulas (e.g., not (a and b) == (not
a) or (not b)).
( )
4.3 Look up a formula online for the quantity nk , the number of ways to choose k
elements from a set of size n, in terms of factorials m! = 1 · 2 · 3 · · · · · m. Find a proof
that explains why this formula is true.
4.4 Look up a statement of the pigeonhole principle, and research how it is used in proofs.
4.6 Suppose that for each n ∈ N we picked a countable set An . Prove that the union of
all the An is countable. Hint: use the previous problem and write the elements of all the
An in a grid.
4.7 Is there a bijection between 2N and the interval [0, 1] of real numbers x with 0 ≤
x ≤ 1? Is there a bijection between (0, 1] = {x ∈ R : 0 < x ≤ 1} and [1, ∞) = {x ∈
R : x ≥ 1}?
4.8 I would be remiss to omit Georg Cantor from a chapter on set theory. Cantor’s The-
orem states that the set of real numbers R is not countable. The proof uses a famous
technique called “diagonalization.” There are many expositions of this proof on the inter-
net ranging in difficulty. Find one that you can understand and read it. The magic of this
theorem is that it means there is more than one kind of infinity, and some infinities are
bigger than others.
4.9 The principle of inclusion-exclusion is a technique used to aid in counting the size of
a set. Look of a description of this principle (it is a family of theorems) and find ways it
is used to help count.
60
4.10 There is a large body of mathematics work related to configurations of sets with
highly symmetric properties. Let n, k, t be integers. A Steiner system is a family F of size-
k subsets of an n-element set, say {1, . . . , n}, such that every size-t subset is in exactly
one member of F . For example, for (n, k, t) = (7, 3, 2), the corresponding Steiner system
is a choice of triples in {1, 2, 3, 4, 5, 6, 7}, such that every pair of numbers is in exactly
one of the chosen triples. Find such a (7, 3, 2)-system.
4.11 Using the previous exercise, a Steiner system may not exist for every choice of
n > k > t. Prove that if an (n, k, t)-system exists, then so must an (n − 1, k − 1, t − 1)-
system. Determine under what conditions a Steiner (n, 3, 2)-system exists.
4.12 Continuing the previous exercise, the non-existence of Steiner systems for some
choices of n suggests a modified problem of finding a minimal size family F of size-k
subsets such that every t-size subset is in at least one set in F . For (n, k, t) arbitrary, find
a lower bound on the size of F . Try to come up with an algorithm that gets close to this
lower bound for small values of k, t.
4.13 A generalization of Steiner systems are called block designs. A block design F is
again a family of size-k subsets of X = {1, . . . , n} covering all size-t subsets, but also
with parameters controlling: the number of sets in F that contain each x ∈ X, and the
number of sets covering each size-t subset (i.e., it can be more than one). Block designs
are used in the theory of experimental design in statistics when, for example, one wants
to test multiple drugs on patients, but the outcome could be confounded by which subset
of drugs each patient takes, as well as which order they are taken in, among other factors.
Research how block designs are used to mitigate these problems.
4.15 The formal mathematical foundations for set theory are called the Zermelo-Fraenkel
axioms (also called ZF-set theory, or ZFC). Research these axioms and determine how
numbers and pairs are represented in this “bare metal” mathematics. Look up Russell’s
paradox, and understand why ZF-set theory avoids it.
4.17 Write a program that extends the deferred acceptance algorithm to the setting of
“marriages with capacity.” That is, imagine now that instead of men and women we have
61
medical students and hospitals. Each hospital may admit multiple students as residents,
but each student attends a single hospital. Find the most natural definition for what a
stable marriage is in this context, and modify the algorithm in this chapter to find stable
marriages in this setting. Then implement it in code. See the chapter notes for historical
notes on this algorithm.
4.18 Come up with a version of stable marriages that includes the possibility of same-sex
marriage. This variant is sometimes called the stable roommate problem. In this setting,
there is simply a pool of people that must be paired off, and everybody ranks everyone
else. Perform the full modeling process: write down the definitions, design an algorithm,
prove it works, and implement it in code.
4.19 Is the stable marriage algorithm biased? Come up with a concrete measure of how
“good” a bijection is for the men or the women collectively, and determine if the stable
marriage algorithm is biased toward men or women for that measure.
Marriage
Please don’t treat marriage as an allocation problem in real life. I hope it’s clear that
the process of doing mathematics—and the modeling involved in converting real world
problems to sets—involves deliberately distilling a problem down to a tractable core. This
often involves ignoring features that are quite crucial to the real world. A quote often
attributed to Albert Einstein speaks truth here, that “a problem should be made as simple
as possible, but no simpler.” Indeed, the unstated hope is that by analyzing the simplified,
distilled problem, one can gain insights that are applicable to the more complex, realistic
problem. Don’t remove the core of the problem when phrasing it in mathematics, but
remove as much as you need to make progress. Then gradually restore complexity until
16
https://youtu.be/wvG5b2gmk70
62
you have solved the original problem, or fail to make more progress. Marriage is used
as a communication device for this particular simplification. It’s not the problem being
solved.
The idea that one can reduce complex human relationships to a simple allocation prob-
lem is laughable, and borderline offensive. In the stable marriage problem the actors
are static, unchanging symbols that happen to have preferences. In reality, the most
important aspect of human relationships is that people can grow and improve through
communication, introspection, and hard work.
Chapter 5
– Henri Poincaré
of a mathematical theory doesn’t depend on whether the next idiot that comes along un-
derstands it.1 In fact, mathematical sophistication in the business world is extraordinary.
And while having tests (providing worked-out examples) is a sign of a good mathemati-
cal writer, there’s no manager staking their job or a salary bonus on the robustness of a
bit of notation. If someone gets confused reading your paper, it doesn’t siphon out the
window the same way it does at Twitter during an outage. There’s just not the same
sense of urgency in mathematics.
I should make a side note that saying “mathematics isn’t a business” is overly naive.
Mathematicians need to make money just like everyone else, and this manifests itself in
some strange practices in academic journals, conferences, and the multitude of commit-
tees that decide who is worth hiring and giving tenure. Mathematicians, like folks in
industry, bend over backwards to game (or accommodate) the system. But all of that is
academia. What I’m talking about is established mathematics which has been around for
decades, or even centuries, which has been purified of political excrement. This applies
to basically every topic in this book.
That’s not to say that mathematics isn’t designed to scale. To the contrary, the inven-
tion of algebraic notation was one of humanity’s first massively scalable technologies.
On the other end of the spectrum, category theory—which you can think of as a newer
foundation for math roughly based on a new notation that goes beyond what sets and
functions can offer—provides the foundation for much of modern pure mathematics. It’s
considered by many as a major advancement.
Rather than being designed to scale to millions of average users, mathematics aims to
scale far up the ladder of abstraction. Algebra—literally, the marks on paper—boosted
humanity from barely being able to do arithmetic through to today’s machine learning
algorithms and cryptographic protocols. Sets, which were only invented in the late 1800’s,
hoisted mathematical abstraction even further. Category theory is a relative rocket fuel
boosting one through the stratosphere of abstraction (for better or worse).
The result of this, as the argument goes, is that mathematicians have optimized their
discourse for more relevant metrics. Indeed, it’s optimized for maximizing efficiency and
minimizing cognitive load after deep study.
Let me map out a few areas where this shows up:
• Variable names
• Operator overloading
• Sloppy notation
Variable names. Variable names are designed to transmit a lot of information: types,
behavior, origin, and more. Every mathematician knows that n is a natural number, and
1
I mean this in a practical sense, not a social sense. If your math is so hard to understand that nobody but
you learns it, it will be lost to history. But from a practical standpoint, calculus doesn’t stop being a good
foundation for a video game engine just because the programmer doesn’t understand the math.
65
that f is a function. Or at least, they know that when they see these letters out of con-
text, they should at least behave like a natural number and a function. Seeing n(f ) out of
context would momentarily startle me, though I can imagine situations making it appro-
priate.2 . Similarly, if f is a function and you can use f to construct another function in
a “canonical” (forced, unique) way, then a mathematician might typically adorn f with
a star like f ∗ . Two related objects often inhabit the same letter with a tick, like x and x′ .
Even if you forget what they represent, you know they’re related.
Every field of mathematics has its own little conventions that help save time. This is
especially true since mathematics is often done in real time (talking with colleagues in
front of a blackboard, or speaking to a crowd). The time it takes to write f ∗ while saying
out loud “the canonical induced homomorphism,3 ” is much faster than writing down
InducedHomomorphismF in ten places. And then when you need an h∗ to compose
f ∗ h∗ , half of the characters help you distinguish it from h∗ f ∗ . Whereas determining the
order of
InducedHomomorphismF.compose(InducedHomomorphismH)
is harder with more characters, and Gauss forbid you have to write down an identity
about the composition of three of these things! A single statement would fill up an entire
blackboard, and you’d never get to the point of your discussion.
More deeply, there is often nothing more a name can do to elucidate the na-
ture of a mathematical object. Does saying f ∗ really tell you less about what an
object is than something like InducedF? It’s related to f , its definition is some-
how “induced,” and what? The further up the ladder of abstraction you go, the
more contrived these naming conventions would get. Rather than say, for example,
FirstCohomologyGroupOfInvertibleSubsheavesOfX, you say H 1 (X, O∗ ) because
you would rather claw your eyes out than read the first thing, which could easily be just
one part of a larger expression, with maybe ten more similar copies of the notation. For
example, here is an actual snippet from a chapter of a graduate algebra textbook cheeily
titled, “Algebra: Chapter 0.”
It is a bit ridiculous that L and L refer to different mathematical things, despite being
the same letter. Here L is an object and L (short for “left”) describes a kind of function.
But this is a trade-off: use long words that make it difficult to put everything you want
to say in front of your face at the same time—thus making it harder to reason—or use
fonts and foreign alphabets to differentiate concepts. Sans-serif is for one purpose, the
curly-scripty font is for another.
2
For example, n could represent some integer-valued property of a function, like the so-called winding number
3
For example. You don’t need to know what a homomorphism is.
66
The claim that a variable name in mathematics can do what programmers claim they
must is naive. In fact, because the expression H 1 (X, O∗ ) is so important in this field
called algebraic geometry, it was further shortened to Pic(X) named after Picard who
studied them. But it might take decades to get to the point where you realize this object
is worth giving a name, and in the mean time you just can’t use 80 character names and
expect to get things done.
One reason mathematicians can get away with single-character variable names is that
they spend so much time studying them. When a mathematician comes up with a new
definition, it’s usually the result of weeks of labor, if not months or years! Moreover, these
objects aren’t just variables in some program whose output or process is the real prize.
The variables represent the cool things! It’s as if you returned to rewrite and recheck and
retest the same twenty-line program every day for a month. You’d have such an intimate
understanding of every line that you could recite them all while drunk or asleep. You
could recognize the program even if it were minified. Now imagine that the intimate
understanding of every line of that program was the basis of every program you wrote
for the next year, and you see how ingrained this stuff is in the mind of a mathematician.
Mathematicians don’t just write a proof and file it away under “great tool; didn’t read.”
They constantly revisit the source. It’s effective to gild meaning and subtext into the
bones of single letters, because after years you don’t have to think about it any more. It
eliminates the need to keep track of types. Clearly f is a function, z is probably a complex
variable, and everyone knows that ℵ0 is the countably infinite cardinal. If you use b and
β in the same place, I will know that they are probably related, or at least play analogous
roles in two different contexts, and that will jump-start my understanding in a way that
descriptive variable names do not.
Operator overloading. Much of what I said above for variable names holds for oper-
ator overloading too. One key feature that stands out for operator overloading is that it
highlights the intended nature of an operation.
We’ll get to this more in Chapter 9, but mathematicians use just a handful of boolean
logic operations for almost everything. The standard inequalities, equalities, and weird
ones that look like ∼ = or ≃ that are supposed to represent equality “up to some differ-
ences that we don’t care about.” In Java terms, mathematicians regularly roll their own
.equals() methods, with proofs that their notions behave. Specifically, they prove it
satisfies the properties required of an equivalence relation, which is the mathematical ver-
sion of saying “equals agrees with hashing and toString.”
And so typically mathematicians will drop whatever the original operator symbol was
and replace it with the equal sign. We’ll see this in detail in Chapters 9 and 16, but
the same idea goes behind the reuse of standard arithmetic operations like addition and
multiplication: it’s so that we can know even out of context what behavior to expect
from the operation. For example, it is considered bad form to use the + operator for an
operation that doesn’t satisfy a + b = b + a for every choice of a and b, because this is
true of addition.
With this in mind it’s the mathematician’s turn to criticize programmers. For example,
reading programming style guides has always amused me. It makes sense for a company
67
to impose a style guide on their employees (especially when your IDE is powerful enough
to auto-format your programs) because you want your codebase to be uniform. In the
same way, a mathematician would never change notational convention in the same paper,
unless the point of the paper is to introduce a new notation. But to have a programming
language designer declare style edicts for the entire world, like the following from the
Python Style Guide, is just ridiculous:
Yes: import os
import sys
Okay, so you have an arbitrary idea of what a pretty program looks like, but wouldn’t
you rather spend that time and energy on actually understanding and writing a good
program? Besides, if there were truly a good reason for the first option, why wouldn’t
the language designer just disallow it in the syntax? Of course, programmers get away
with it because they use automated tools to apply style guides automatically. It’s much
harder to do that in math, where the worst offenses are not resolvable (or discoverable!)
from syntax alone. Still, I don’t doubt there could be some progress made in automating
some aspects of a mathematical style guide.
In an ideal world, a compiler would see how I use the “stdout” variable and be able to
infer the semantics from a shared understanding about the behavior of standard output in
basically every program ever. This would eliminate the need to declare module imports or
even define stdout! That’s basically how math solves the problem of overloaded operators.
There is a clarifying and rigorous definition somewhere, but if you’ve forgotten it you can
still understand the basic intent and infer appropriate meaning.
Sloppy notation. This is probably the area where mathematicians get the most flak,
and where they could easily improve their communication with those aiming to learn.
∑
Take summation notation, the symbol. Officially this symbol has three parts: an
index
∑10 variable, a maximum value for the index, and an expression being summed. So
i=0 2i+1 sums the first ten positive odd integers. This is the kind of syntactical rigidity
that makes one itch to write a parser.
However, this notation is so convenient that it’s been overloaded to include many other
syntax forms. A simple one is to replace the increment-by-one range of integers ∑ with a
“all elements in this set” notation. For example, if B is a set, you can write b∈B b to 2
∑
b2i + 3
bi odd
The reason this makes any sense is because, as is often the case, the math notation
often comes from speech. You’re literally speaking, “over all bi that are odd, sum the
terms b2i + 3.” Equations are written to mimic conversation, not the other way around.
You see it when you’re in the company of mathematicians explaining things. They’ll
write their formulas down as they talk, and half the time they’ll write them backwards!
For a sum, they might write the body of the summation first, then add the sum sign and
the index. Because out loud they’ll be emphasizing the novel parts of the equation, filling
the surrounding parts for completeness.
Finally, the things being summed need not be numbers, so long as addition is defined
for those objects and it satisfies the properties addition should satisfy. In Chapter 10 we’ll
see
∑ a new kind of summation for vectors, and it will be clear why it’s okay for us to reuse
in that context. The summing operation needs to have properties that result in the
final sum not depending on the order the operations are applied.
Another prominent example of summation notation being adapted for an expert audi-
ence is the so-called
∑ Einstein notation. This notation is popular in physics. In Einstein
notation the symbol is itself implied from context! For example, rather than write
∑
n
y= ak xk ,
k=1
The sum and the bounds on the indices are implied from the presence of the indices,
as in
y = ak xk .
To my personal sensibilities this is extreme. But I can’t fault proponents for the abuse
when they find it genuinely useful. If it solidifies their intuition of the object of their
study, it’s a good thing.
Indeed, what makes all of this okay is when the missing parts are fixed throughout
the discussion or clear from context. What counts as context is (tautologically) context
dependent. More often than not, mathematicians will preface their abuse to prepare you
for the new mental hoop. The benefit of these notational adulterations is to make the
mathematics less verbose, and to sharpen the focus on the most important part: the core
idea being presented. These “abuses” reduce the number of things you see, and as a
consequence reduce the number of distractions from the thing you want to understand.
Chapter 6
Graphs
One will not get anywhere in graph theory by sitting in an armchair and trying to un-
derstand graphs better. Neither is it particularly necessary to read much of the literature
before tackling a problem: it is of course helpful to be aware of some of the most important
techniques, but the interesting problems tend to be open precisely because the established
techniques cannot easily be applied.
– Tim Gowers
In this chapter we won’t learn any new tools. Instead we’ll apply the tools above to
study graphs. Most programmers have heard about graphs before, perhaps in the context
of breadth-first and depth-first search or data structures like heaps. Instead of discussing
the standard applications of graphs to computer science, we’ll focus on a less familiar
topic that still finds uses in computer science: graph coloring.
In addition to having interesting applications, graph coloring has important theorems
one can prove using only the tools we’ve learned so far. The main theorem we’ll prove
in this chapter is that every planar graph is 5-colorable (I will explain these terms soon).
So think of this chapter as a sort of checkpoint exam. If you’re struggling to understand
the definitions, theorems, and proofs here—and you’ve set your pace appropriately—then
you should go back and review the previous chapters.
v2
e6 e2 v1
v4 e4
e5 e1
e3
v5
e7 v6
v3
Figure 6.2: A graph with labeled vertices
Figure 6.1: An example of a graph
and edges.
there is a flight between the two. Or the things are people and friends have connections.
We draw the things and connections using dots and lines to erase the application from
our minds. All we care about is the structure of the connections.
Let’s lay out the definitions, using sets as the modeling language. The “things” are
called vertices (or often nodes) and the “connections” are called edges (or links). For short-
hand in the definition, I’ll reuse a definition from Chapter 4 for the set of all ways to
choose two things from a set.
( )
V
= {{v1 , v2 } : v1 ∈ V, v2 ∈ V, v1 ̸= v2 }.
2
This is like V × V , but the order of the pair does not matter.
(V )
Definition 6.1. A graph G consists of a set V of vertices, a set E ⊂ 2 of edges. The
entire package is denoted G = (V, E).1
( )
Alternatively, one can think of E as just any set, and require a function f : E → V2
to describe which edges connect which pairs of vertices. This view is used when one
wants to define a graph in a context where the vertices are complicated (we will briefly
see one from compiler design later in this chapter). Despite the definition of an edge
e ∈ E as a set of size two like {u, v}, mathematicians will sloppily write it as an ordered
pair e = (u, v).2
Here’s some notation and terminology used for graphs. We always call n = |V | the
number of vertices and m = |E| the number of edges, and for us these values will always
be finite. When two vertices u, v ∈ V are connected by an edge e = (u, v) we call the
1
This is not the most general definition for a graph, but we will not need graphs with self loops, weights,
double edges, or direction. You’ll explore some of these extensions in the exercises.
2
I have suspicions about why this abuse is commonplace: curly braces are more cumbersome to draw than
parentheses, and in the typesetting language LaTeX, typing them requires an escape character. They’re also
just visually harder to parse when nested.
71
two vertices adjacent, and we say that e is incident to u and v. We call v a neighbor of u
and we define the neighborhood of a vertex N (u) to be the set of all neighbors. I.e.,
N (u) = {v ∈ V : (u, v) ∈ E}
The size of a neighborhood (and the number of incident edges) is called the degree of a
vertex, and the function taking a vertex v to its degree is called deg : V → Z. To practice
the new terms, see Figure 6.2, labeling the graph from Figure 6.1. Vertices have label ‘v’
and edges have lebel ‘e’. Verteces v1 v3 are adjacent, e2 is incident to v1 , deg(v2 ) = 3,
and all of the neighbors of v2 are also neighbors of v3 .
Another concept we’ll need in this chapter is the concept of a connected graph. First,
a path in a graph is a sequence of alternating vertices and edges (v1 , e1 , v2 , e2 , . . . vt ) so
that each ei = (vi , vi+1 ) connects the two vertices next to it in the list. Visually, a path is
just a way to traverse through the vertices of G by following edges from vertex to vertex.
In Figure 6.2, there are many different paths from v4 to v6 , four of which do not repeat
any vertices. Many authors enforce that paths do not repeat vertices by definition, and
give the name “trail” of “walk” to a path which does repeat vertices.
A graph is called connected if there is a path from each vertex to each other vertex, and
otherwise it is called disconnected. Equivalently, G = (V, E) is connected if it is impos-
sible to split V into two subsets X, Y with no edges between X and Y . A disconnected
graph is a union of connected components, where the component of v is the largest con-
nected subgraph3 containing v. A single vertex which forms a connected component is
called an isolated vertex.
By now you should know to write down examples for small n and k before moving
on. Because this is a crucial definition, here is a more complicated example. The Petersen
graph is shown in Figure 6.3. The Petersen graph has a distinguished status in graph
theory as a sort of smallest serious unit test. Conjectures that are false tend to fail on
the Petersen graph.4 The Petersen graph is 3-colorable (find a 3-coloring!) but not 2-
colorable.
3
A subgraph is just a subset of edges and their corresponding vertices.
4
Why? Part of it is that the Petersen graph is highly symmetric, which we’ll see more in the exercises for
Chapter 16.
72
Definition 6.3. The chromatic number of a graph G, denoted χ(G), is the minimum
integer k for which G is k-colorable.
So by the example above, the Petersen graph has chromatic number 3. Here is a simple
fact about the chromatic number.
Proof. We define a greedy algorithm for coloring a graph. Pick an arbitrary ordering
v1 , . . . , vn of the vertices of G, and then for each vi pick the first color j which is unused
by any of the neighbors of vi . In the worst case, a vertex v of degree d will have all of its
neighbors using different colors, and so it will use color d + 1. Otherwise v could reuse
one of the first d colors not used by any neighbor. So the worst-case number of colors is
at most the largest degree in the graph plus one, as claimed.
In fact, a very simple graph meets this bound and has χ(G) = maxv∈V deg(v) + 1. See
if you can find it. Moreover, this bound is quite loose. Consider the “star” graph which
has only one vertex of degree n − 1, pictured in Figure 6.4. Clearly the star graph is
2-colorable, but the max degree is n − 1. The guarantee of the theorem is useless.
One other perspective on graph coloring I want to describe is the partition perspective.
Specifically, if G = (V, E) is a graph and φ is a proper k-coloring, then we can look at
φ−1 (j), the set of all vertices that have color j. By the fact that φ is proper, there will
be no edges among these vertices. Moreover, since φ is a function, the set of all φ−1 (j)
form a partition5 of the set V into “color classes,” and all the edges go between the color
classes. Figure 6.5 shows a picture for the Petersen graph.
5
A partition of X is a set of non-overlapping (disjoint) subsets Ai ⊂ X the union of all of them being
∪i Ai = X.
73
This perspective is important because one can try to properly color a graph by starting
with an improper or unfinished coloring, and fiddle with it to correct the improprieties.
We will do this in the main application of this chapter, coloring planar graphs. But right
now we’re going to take a quick detour to see why graph coloring is useful.
As an illustrative example, say that the almost-compiled program looks like this, where
the dollar sign denotes a variable name:
whileBlock:
$41 = $41 - 1
$40 = $40 + $42
$42 = $41 - $42
BranchIfZero $41 endBlock whileBlock
endBlock:
$43 = $41 + $40
In this example variables 41 and 42 cannot share a physical register. They have different
values and are used in the same line to compute a difference. Call a variable live at a
statement in the code if it’s value is used after the end of that statement. Thinking of it
in reverse: a variable is dead in all of the lines of code between when it was last read
and when it is next written to. Whenever a variable is dead we know it’s safe to reuse its
physical register (storing the value of the dead variable in memory).
Now we can define the edges. Two variables $i and $j “interfere,” and hence we add
the edge (i, j) to G, if they are ever live at the same time in the program. With a bit of
work (uncoincidentally using graphs to do a flow analysis), one can efficiently compute
the places in the code where each variable is live and construct this graph G. Then if
we can compute the chromatic number of G and find an actual χ(G)-coloring, we can
assign physical registers to the variables according to the coloring. Without some deeper
semantic analysis, this provides the most efficient possible use of our physical registers.6
Unfortunately, in general you should not hope to compute the chromatic number of
an arbitrary graph. This problem is what’s called “NP-hard,” which roughly means we
don’t know of any provably correct that is significantly better than brute-force searching
through all possible colorings, and we don’t hope to find one any time soon.
Moreover, it is even NP-hard to get any reasonable approximation of the chromatic
number of a general graph. To be more specific, we can’t hope to find an algorithm
which, when given a graph G with n vertices, can output a number Z with the property
Z
that χ(G) < nc for any 0 < c < 1. This is an asymptotic statement, meaning a hopeful
algorithm might provably work for all graphs with fewer than a thousand nodes. This
may be good enough for some practical purposes.7 But to put the numbers in perspective
with an example, this theorem says that for graphs with n = 105 vertices and with
c = 1/2, algorithms will struggle to output a number guaranteed to be between χ(G)
and 100 · χ(G). That multiplicative factor grows polynomially quickly with the size of the
input graph!
6
In fact, it can happen that the chromatic number of G is greater than the total number of registers on the
target machine. In this case you have to spill some variables into memory, and deciding which variables to
send to memory is both a science and an art.
7
If you had to compute the chromatic number of a graph in a practical setting, you’d probably write it as a
so-called integer linear program throw an industry-strength solver at it. As they say, NP-hard problems are
hard in theory but easy in practice.
75
Figure 6.6: An example of a planar graph which can be drawn with no edges crossing.
But I digress. The takeaway is that coloring is a hard problem. This is a sad result
for people who really want to color their graphs, but there are other ways to attack the
problem. You can assume that your graph has some nice structure. This is what we’ll do
in the next section, and there it turns out that the chromatic number will always be at
most 4. Alternatively, you could assume that you know your graph’s chromatic number,
and try to color it without introducing too many improperly colored edges. We’ll see this
approach in Section 6.6.
F4
F2
F1 F3
K5 K 3,3
Figure 6.7: K5 and K3,3 , two graphs which are not Figure 6.8: Faces of a planar
planar. graph.
shows a graph with four faces, because I’m calling the “outside” of the drawing also a face.
If we call f the number of faces, and remember n is the number of vertices and m is the
number of edges, then we can notice8 a nice little pattern: n − m + f = 2.
The amazing fact is that this equation does not depend on how you draw the graph!
So long as your drawing has no crossing edges, the value n − m + f will always be 2. We
can prove it quite simply with induction.
Theorem 6.5. For any connected planar graph G = (V, E) and any drawing of G in the
plane R2 defining a set F of faces, the quantity |V | − |E| + |F | = 2.
Proof. We proceed by induction on the total number of vertices and edges. The base case
is a single isolated vertex, for which |V | = 1, |E| = 0, and |F | = 1, so the theorem works
out.
Now suppose we have a graph G for which the theorem holds, i.e. |V | − |E| + |F | = 2,
and we will make it larger and show that the theorem still holds. In particular, we will
do induction on the quantity |V | + |E|. There are two cases: either we add a new edge
connecting two existing vertices, or we add a new edge connected to a new vertex (which
now has degree 1).
In the first case, |V | is unchanged, |E| increases by 1, and |F | also increases by one
because the new edge cuts an existing face into two pieces. So
two. So again we get that for the new graph |V | + 1 − (|E| + 1) + |F | = 2. This finishes
the inductive step.
Finally, it should be clear that every connected graph (regardless of whether it’s planar)
can be built up by a sequence of adding edges by these two cases. This completes the
proof.
This was proved by Kenneth Appel and Wolfang Haken in 1976 after being open for
over a hundred years. You may have heard of it because of its notoriety: it was the first
major theorem to be proved with substantial aid from a computer. Unfortunately the
proof is very long and difficult (on the order of 400 pages of text!). Luckily for us there is
a much easier theorem to prove.
If you’re like me and frequently make off-by-one errors, then the five color theorem is
just as good as the four color theorem! In order to prove it we need three short lemmas.
∑
Lemma 6.8. If G is a graph with m edges, then 2m = v∈V deg(v).
Proof. The important observation is that the degree of a vertex is just the number of edges
incident to it, and every edge is incident to exactly two vertices.
This is where the proof would usually end. As a variation on a theme, you can (and
should) think of this as constructing a clever bijection like we did in Chapter 4, but it’s
difficult to clearly define a domain and codomain. Let me try: the domain consists of
“edge stubs” sticking out from each vertex, and the codomain is the set of edges E. We’re
mapping each edge stub to the edge that contains that stub. ∑This map is a surjection and
a double cover of E, and the size of the domain is exactly v∈V deg(v).
78
Lemma 6.9. If a planar graph G has m edges and f faces, then 2m ≥ 3f , i.e. f ≤ (2/3)m.
Proof. Pick your favorite embedding (drawing) of G in the plane. We’ll use a similar
counting argument as in Lemma 6.8: for any planar drawing, every face is enclosed by
at least three edges, and every edge touches at most two faces.9 Hence 3f counts each
edge at most twice, while 2m counts each face at least three times.
You should do what I did for Lemma 6.8 and think about how to express this as an
injection from one set to another. The last lemma is the key to the five color theorem.
Proof. Suppose to the contrary that every vertex of G = (V, E) has degree 6 or more.
Substituting the inequality relating edges and faces from Lemma 6.9 into the Euler char-
acteristic equation gives
As a quick side note that we’ll need in the next theorem, along the way to proving
Lemma 6.10 we get a bonus fact: the complete graph K5 is not planar. This is because
we proved that all planar graphs satisfy |E| ≤ 3|V | − 6, and for K5 , |E| = 10 > 15 − 6.
This argument doesn’t work for showing K3,3 is not planar, but if you’re willing to do a
bit extra work (and take advantage of the fact that K3,3 has no cycles of length 3), then
you can improve the bound from Lemma 6.10 to work. In particular, because K5 is not
planar, no planar graph can contain K5 as a subgraph.
Now we can prove the five color theorem.
Proof. By induction on |V |. For the base case, every graph which has 5 or fewer vertices
is 5-colorable by using a different color for each vertex.
Now let |V | ≥ 6. By Lemma 6.10, G has a vertex v of degree at most 5. If we remove v
from G then the inductive hypothesis guarantees us a 5-coloring. So we want to extend
or modify this coloring and get a good color for v, and this will finish the proof. When v
9
An edge incident to a vertex of degree 1 will touch the “outside” face twice, but this only counts as one face.
79
wi
wi
v v wi = wj
wj wj
Figure 6.9: The “strands of a spider web” image guide the proof that G′ is planar.
has degree at most 4, choose one of the unused colors among v’s neighbors. Otherwise
v has degree exactly 5, and we have to be more clever.
Call v’s five neighbors w1 , w2 , w3 , w4 , w5 . Because K5 is not planar and G is, these five
neighbors can’t form K5 . In particular there must be some i, j for which wi and wj are not
adjacent. We can form a graph G′ (“G prime”10 ) by merging these two vertices, i.e., delete
wi , wj and add a new vertex x which is adjacent to all the vertices in N (wi ) ∩ N (wj ).
I claim that if G′ is planar then we’re done: G′ has |V | − 2 vertices and so it has a 5-
coloring by the inductive hypothesis, and we can use that 5-coloring to color most of G
(everything except wi , wj , and v). Then use the color assigned to x for both wi and wj ;
they had no edge between them in G, so this is okay. These choices ensure the neighbors
of v use only 4 of the 5 colors, so finally pick the unused color for v. This produces a
proper coloring of G.
So why is G′ planar? To argue this, we have to show that for any planar drawing of G,
removing v leaves wi and wj in the same face. This is equivalent to being able to trace
a curve in the plane from wi to wj without hitting any other edges, since we could then
“drag” wi along that curve to wj and “lengthen” the edges incident to wi as we go. The
picture in my head is like the strands of a spider web, shown in Figure 6.9.
The key is that G is planar and that v has all of the w’s as neighbors. If we want to
merge wi to wj , we can use the curve already traced by the edges from wi to v and from v
to wj . By planarity this is guaranteed not to cross any of the other edges of G, and hence
of G′ . To say it a different way, if we took the drawing above and continued drawing
G′ , and the result required an edge to cross one of the edges above, then it would have
crossed through one of the edges going from v to wi or v to wj !
This proves G′ is planar, which completes the proof.
10
The tick is called the “prime” symbol, and it is used to denote that two things are closely related, usually that
the prime’d thing is a minor variation on the un-primed thing. So using G′ here is a reminder to the reader
that G′ was constructed from G.
80
That proof neatly translates into a recursive algorithm for 5-coloring a planar graph.
We’ll finish this section with Python code implementing it. In order to avoid the toil of
writing custom data structures for graphs, we’ll use a Python library called igraph to
handle our data representation. As a very quick introduction, one can create graphs in
igraph as follows.
import igraph
G = igraph.Graph(n=10)
G.add_edges([(0,1), (1,2), (4,5)])
For example, given a graph and a list of nodes in the graph, one might use the following
function to find two nodes which are not adjacent.
Also, the vertices of an igraph graph can have arbitrary “attributes” that are assigned
like dictionary indexing. So if I want to assign colors to the vertices, I can literally do
that. For example, this is the base case of our induction: trivially color each vertex of a
≤ 5 vertex graph with all different colors.
colors = list(range(5))
def planar_five_color(graph):
n = len(graph.vs)
if n <= 5:
graph.vs['color'] = colors[:n]
return graph
...
The igraph library overloads the assignment operator to allow for entry-wise as-
signments by assigning one list to another. So in the statement G.vs['color'] =
colors[:n], the nodes of G are being assigned the first n colors in the list of colors.
The rest of the planar_five_color function involves finding the vertices of the right
degree, forming the graph G′ to recursively color, and keeping track of which vertices
were modified to make G′ so you can use its coloring to color G.
Here is the part where we find vertices of the right degree and do bookkeeping:
81
deg_at_most5_nodes = graph.vs.select(_degree_le=5)
deg_at_most4_nodes = deg_at_most5_nodes.select(_degree_le=4)
deg5_nodes = deg_at_most5_nodes.select(_degree_eq=5)
g_prime = graph.copy()
g_prime.vs['old_index'] = list(range(n))
The select functions are igraph-specific: they allow one to filter a vertex list by
various built-in predicates, such as whether the degree of the vertex is equal to 5. The
old_index attribute keeps track of which vertex in G′ corresponded to which vertex in
G, since when you modify the vertex set of an igraph the locations of the vertices within
the data structure change (which changes the index in the list of all vertices).
Next we have the part where we construct G′ . This is where the two cases in the proof
show up.
if len(deg_at_most4_nodes) > 0:
v = deg_at_most4_nodes[0]
g_prime.delete_vertices(v.index)
else:
v = deg5_nodes[0]
neighbor_indices = [x['old_index'] for x in g_prime.vs[v.index].neighbors()]
g_prime.delete_vertices(v.index)
neighbors_in_g_prime = g_prime.vs.select(old_index_in=neighbor_indices)
We implemented a function called merge_two that merges two vertices, but the im-
plementation is technical and not interesting. The official igraph function we used is
called contract_vertices. The remainder of the function executes the recursive call,
and then copies the coloring back to G, computing the first unused color with which to
color the originally deleted vertex v.
colored_g_prime = planar_five_color(g_prime)
for w in colored_g_prime.vs:
# subset selection handles the merged w1, w2 with one assignment
graph.vs[w['old_index']]['color'] = w['color']
The entire program is in the Github repository for this book.11 The second case of the
algorithm is not trivial to test. One needs to come up with a graph which is planar, and
hence has some vertex of degree 5, but has no vertices of degree 4 or less. Indeed, there is
11
See pimbook.org.
82
a planar graph in which every vertex has degree 5. Figure 6.10 shows one that I included
as a unit test in the repository.
Proof. Let G be a 3-colorable graph. For the first case, where there is a vertex v of degree
√
≥ n, we have to prove that the neighborhood N (v) can be colored with two colors.
But this follows from the assumption that G is 3-colorable: in any 3-coloring of G, v uses
a color that none of its neighbors may use. Only two colors remain.
12
Ideally we might hope to color a 3-colorable graph with 4 colors, but this was shown to be NP-hard as well.
See http://dl.acm.org/citation.cfm?id=793420.
83
√ √
If there are no vertices of degree n, then the maximum degree of a vertex is ≤ n−1,
√
and we proved in Proposition 6.4 that the greedy algorithm will use no more than n
colors on this graph.
Now we have to count how many colors get used total. The first case can only happen
√ √ √
n times, because each time we color v and its neighbors, we remove those n+1 ≥ n
√ √
vertices from G ( n · n = n). Since we add 3 new colors in each step, this part uses at
√ √ √
most 3 n colors. The greedy algorithm uses at most n colors, so in total we get 4 n,
as desired.
√
One might naturally ask whether we can improve n to something like log(n), or even
some very large constant. This is actually an open question. Recent breakthroughs13 have
got the number of colors down to roughly n0.2 colors. For reference, a thousand-node
3-colorable graph would have n0.2 ≈ 4. That’s quite an improvement over 127 colors
√
given by the 4 n bound.
I should make a clarification here: the open problem is on the existence of an algorithm
which is guaranteed to achieve some number of colors (depending on the size of the
graph) no matter what the graph is. As a programmer you are probably somewhat familiar
with this idea that one often measures an algorithm by its worst-case guarantees, but
the point is important enough to emphasize. So when I say a problem is “possible” or
“impossible” to solve, I mean that there exists (or does not exist, respectively) an efficient
algorithm that achieves the desired worst-case guarantee on all inputs. In particular,
there is no evidence for either claim that it is possible or impossible to color a 3-colorable
graph with log(n) colors (or anything close to that order of magnitude, like (log(n))10 ).
A ripe problem indeed.
2. Sometimes if you want to come up with the right rigorous definition for an intuitive
concept (like a planar graph), you need to develop a much more general framework
for that concept. But in the mean time, you can still do mathematics with the
informal notion.
6.8 Exercises
6.1 Write down examples for the following definitions. A graph is a tree if it contains
no cycles. Two graphs G, H are isomorphic if they differ only by relabeling their vertices.
That is, if G = (V, E) and H = (V ′ , E ′ ), then G and H are isomorphic if there is a
bijection f : V → V ′ with the property that (i, j) ∈ E if and only if (f (i), f (j)) ∈ E ′ .
Given a subset of vertices S ⊂ V of a graph G = (V, E), the induced subgraph on S
is the subgraph consisting of all edges with both endpoints in S. Given a vertex v of
degree 2, one can contract it by removing it and “connecting its two edges,” i.e., the two
edges (v, w), (v, u) become (w, u). Likewise, one can contract an edge by merging its
endpoint vertices, or subdivide an edge by adding a vertex of degree two in the middle
of an edge. If H can be obtained as a subgraph of G after some sequence of contractions
and subdivisions, it is called a minor of G.
6.2 Look up the statement of Wagner’s theorem, which characterizes planar graphs in
terms of contractions and the two graphs K3,3 and K5 . Find a proof you can understand.
6.3 Here’s a simple way to make examples of planar graphs: draw some non-overlapping
circles of various sizes on a piece of paper, call the circles vertices, and put an edge be-
tween any two circles that touch each other. Clearly the result is going to be a planar
graph, but an interesting question is whether every planar graph can be made with this
method. Amazingly the answer is yes! This is called Koebe’s theorem. It is a relatively
difficult theorem to prove for the intended reader of this book, but as a consequence it
implies Fáry’s theorem. Fáry’s theorem states that every planar graph can be drawn so
that the edges are all straight lines. Look up a proof of Fáry’s theorem that uses Koebe’s
theorem as a starting point, and rewrite it in your own words.
6.4 Given a graph G, the chromatic polynomial of G, denoted PG (x), is the unique poly-
nomial which, when evaluated at an integer k ≥ 0, computes the number of proper
colorings of G with k colors. Compute the chromatic polynomial for a path on n ver-
tices, a cycle on n vertices, and the complete graph on n vertices. Look up the chromatic
polynomial for the Petersen graph.
6.6 In the chapter I remarked that the Euler characteristic is a special quantity because
it is an invariant. Look up a source that explains why the Euler characteristic is special.
6.7 Find a simple property that distinguishes 2-colorable graphs from graphs that are
not 2-colorable. Write a program which, when given a graph as input, determines if it is
2-colorable and outputs a coloring if it is.
85
√
6.8 Implement the algorithm presented in the chapter to (4 n)-color a 3-colorable
graph. Use the 2-coloring algorithm from the previous problem as a subroutine.
6.9 A directed graph is a graph in which edges are oriented (i.e., they’re ordered pairs
instead of unordered pairs). The endpoints of an edge e = (u, v) are distinguished as
the source u and the target v. A directed graph gives rise to natural directed paths, which
is like a normal path, but you can only follow edges from source to target. A graph is
called strongly connected if every pair of vertices is connected by a directed path. Write
a program that determines if a given directed graph is strongly connected.
6.10 A directed acyclic graph (DAG) is a directed graph which has no directed cycles
(paths that start and end at the same vertex). DAGs are commonly used to represent
dependencies in software systems. Often, one needs to resolve dependencies by evaluat-
ing them in order so that no vertex is evaluated before all of its dependencies have been
evaluated. One often solves this problem by sorting the vertices using what’s called a
“topological” sort, which guarantees every vertex occurs before any downstream depen-
dency. Write a program that produces a topological sort of a given DAG.
6.11 A weighted graph is a graph G for which each edge is assigned a number we ∈ R.
Weights on edges often represent capacities, such as the capacity of traffic flow in a road
network. Look up a description of the maximum flow problem in directed, weighted
graphs, and the Ford-Fulkerson algorithm which solves it. Specifically, observe how the
maximum flow problem is modeled using a graph. Find real-world problems that are
solved via a related max flow problem.
6.12 A hypergraph generalizes the size of an edge to contain more than two vertices.
Hypergraphs are also called set systems or families of sets. Edges of a hypergraph are
called hyperedges, and a k−uniform hypergraph is one in which all of its hyperedges have
size k. Look up a proof of the Erdős-Ko-Rado theorem: let G be a k-uniform hypergraph
with n ≥ 2k vertices,
( ) in which every pair of hyperedges shares a vertex in common. Then
G has at most n−1
k−1 hyperedges in total. Find a construction that achieves this bound
exactly when n > 2k.
• Every fe is injective.
• There are no two fe1 , fe2 and values 0 < t1 , t2 < 1 for which fe1 (t1 ) = fe2 (t2 ),
i.e., the images of fe1 and fe2 do not intersect except possibly at their endpoints.
• Whenever there are two edges (u, v) and (u, w), the corresponding functions must
intersect at one endpoint, and these intersections must be consistent across all the
vertices. I.e., every u ∈ V corresponds to a point xu ∈ R2 such that for every edge
(u, v) incident to u, either f(u,v) (0) = xu or f(u,v) (1) = xu .
us, if a copy of [0, 1] represents an edge, then we’ll want two edges incident to the same
vertex to have one of their two endpoints identified.14
So putting these two ideas together, the abstract space XG corresponding to a graph G
is the disjoint union of copies of [0, 1] for each edge, with endpoints identified when two
edges intersect at a vertex. Then we can define a function f : XG → R2 , enforce it to be
injective (it’s just a function between two sets), and call it continuous if points that are
close in XG , using the natural distance for points in the interval [0, 1], get sent to points
that are close in f (XG ). How do I measure distance between two points a, b ∈ XG that
might be on different edges? Well a, b are either vertices or on some copy of [0, 1], so I can
find a path in the graph G, that gets from one edge to another (if not, then the distance
can be called infinite). Then I could measure the length of each full edge on this path, and
add up the partial edges required to get from a or b to the desired endpoint of the edge
they’re in.
This is a very fancy way to say that I can impose the same geometry that was on [0, 1]
onto the different pieces of XG and patch them together. But once you get comfortable
with that idea, you have a natural way to define an embedding of any abstract space into
any other abstract space: a continuous injective function!
For more mathematics like this, I suggest you pick up a book on topology. Unfortu-
nately I haven’t yet found one that I like particularly better than any other. Most books
tend to be terse and contain few pictures (which is the opposite of how topology is done!).
Topology also aims to generalize much of calculus, so waiting until after Chapter 14 might
be prudent.
14
This foreshadows a topic in a later chapter called the equivalence relation, which formalizes how to identify
points in a consistent way.
Chapter 7
Some people may sit back and say, “I want to solve this problem” and they sit down and
say, “How do I solve this problem?” I don’t. I just move around in the mathematical
waters, thinking about things, being curious, interested, talking to people, stirring up
ideas; things emerge and I follow them up. Or I see something which connects up with
something else I know about, and I try to put them together and things develop. I have
practically never started off with any idea of what I’m going to be doing or where it’s
going to go. I’m interested in mathematics; I talk, I learn, I discuss and then interesting
questions simply emerge. I have never started off with a particular goal, except the goal
of understanding mathematics.
– Alfréd Rényi
There is a fascinating bit of folk lore, which as far as I know originated with a 2010
blog post of Ben Tilly, that you can tell what type of mathematician you are by how you
eat corn on the cob. It turns out there are multiple ways to eat corn, and they are roughly
grouped as “eat in rows like a typewriter, left to right,” and “eat in a spiral, teeth scraping
the corn into your mouth.”
The corresponding two types of mathematicians are roughly grouped as algebraists
and analysts. An algebraist, as we’ll see in Chapters 10, 12, and 16, supposedly prefers
orderliness and working with the inherent structure of the corn cob. Analysis, the topic
of Chapters 8, 14, and 15, alternatively prioritizes efficiency, approximation, and getting
the job done. One’s underlying preference apparently explains both the choice of a math-
ematical domain of study, and the less conscious choice of how to eat corn.
According to Tilly, who surveyed 40-ish mathematicians and received countless more
self-selected responses via the internet, corn eating predicts mathematical preference
with surprising accuracy. Since his post, this observation has become a bit of folk lore that
reinforces the idea that mathematics has many subcultures organized around preference
and character.
89
90
One of the more prominent distinctions is the concept described by mathematician Tim
Gowers and others, between mathematicians who prioritize problem solving versus those
who prioritize theory building. As the quotes at the beginning of the chapter emphasize,
these are very different styles of doing mathematics. Gowers defines them via example
in a 2000 essay:
If you are unsure to which class you belong, then consider the following two statements.
Most mathematicians would say that there is truth in both (1) and (2). Not all problems
are equally interesting, and one way of distinguishing the more interesting ones is to
demonstrate that they improve our understanding of mathematics as a whole. Equally,
if somebody spends many years struggling to understand a difficult area of mathematics,
but does not actually do anything with this understanding, then why should anybody else
care?
The Hungarian mathematician Paul Erdős was a pillar of the problem solving camp.
Though this short essay could not possibly do justice to his outlandish life story, I will
try to summarize. Erdős is the most prolific mathematician in history, by count of papers
published (over 1500). He was able to do this because he renounced every aspect of
life beyond mathematics. He had no home, and lived out of a suitcase while traveling
from university to university. At each stop, he would show up, knock on the department
chair’s office door, and be provided housing and food by an attendant professor. In the
subsequent weeks, Erdős and his host would work on problems and usually publish a
paper or two, until such time as Erdős decided to move on to his next host. As Erdős said,
“Another roof, another proof.” He never married and had no children.
Erdős would often do bizarre things like wake up his host in the middle of the night,
exclaiming, “My mind is open,” meaning he was ready to do mathematics. He was a
serious user of methamphetamines, and since he had no possessions or money, it fell
to his hosts to procure his drugs. Despite being an atheist, he called God the “Supreme
Fascist.” He also claimed God kept a Book of the most beautiful proofs of every theorem.
He didn’t believe in God, but he believed in the Book.
Erdős’s hosts tolerated his idiosyncratic behavior because his presence was a boon to
one’s career. Mathematicians jumped at the chance to work with Erdős, and in turn they
started to track their so-called Erdős number. In the graph whose vertices are people and
whose edges are coauthorship, your Erdős number tracks the length of the shortest path
from you to Erdős.1
1
You didn’t ask, but my Erdős number is three, by way of György Turán → Endre Szemerédi (and others) →
Erdős.
91
His work focused on problems in combinatorics, number theory, graph theory, and in-
cidence geometry (statements about configurations of points and lines), the sort of count-
ing arguments that we saw in Chapters 4 and 6—though much more sophisticated and
interesting. As he spread his ideas from university to university, he both gave combi-
natorics credibility as a field of study, and also established its reputation as a field that
prioritizes problem solving over theory building. To Erdős, mathematics was “conjecture
and proof.”
Indeed, as Tim Gowers writes, graph theory tends not to benefit from extensive theory-
building.
At the other end of the spectrum is, for example, graph theory, where the basic object, a
graph, can be immediately comprehended. One will not get anywhere in graph theory by
sitting in an armchair and trying to understand graphs better. Neither is it particularly
necessary to read much of the literature before tackling a problem: it is of course helpful
to be aware of some of the most important techniques, but the interesting problems tend
to be open precisely because the established techniques cannot easily be applied.
Michael Atiyah is Gowers’s example of a theory builder. Theory builders focus on the
conceptual unity of mathematics, and on connecting disparate subjects and identifying
their commonalities. Atiyah even argues against my claims in this book, that proof is not
necessarily central to mathematics. From Atiyah’s essay, “Advice to a Young Mathemati-
cian.”
I interpret this in more of a metaphysical sense than a literal sense; one needs to know
what questions are worth asking before one can provide a proof answering them. For
whatever reason, Atiyah doesn’t consider the validations or refutations of these initial
ideas as “proofs” in the formal sense.
One person who might be said to be the antithesis to Paul Erdős is the French math-
ematician Alexander Grothendieck. He also lived a curiously eccentric lifestyle involv-
ing radical anti-military politics and an eventual self-exile to a small village in Southern
France. Grothendieck declined various prizes for his life’s work, and decried the mathe-
matical establishment as being obsessed by status to the point of intellectual bankruptcy.
Toward the end of his life he also turned to mysticism and spiritualism, almost starving
himself to death via unusual diets and fasting.
Grothendieck’s work was a complete rebuilding of the foundations of the subfield of al-
gebraic geometry in terms of category theory. These developments concurrently reshaped
the foundations of adjacent and burgeoning fields of cohomology theory, algebraic topol-
ogy, and representation theory. His work also led to the resolution of a number of high-
profile conjectures, and important generalizations of famous theorems.
In particular, his theory elucidated the role of category theory in connecting disparate
fields of mathematics together via universality. In brief, universality is a uniqueness prop-
erty of a particular pattern or structure that occurs within a subfield of mathematics. For
example, the product of two sets has a universal property, and it is the same property
as the product of vector spaces (Chapter 10) as well as groups (Chapter 16). Noticing
these similarities allows one to formalize a “product” in a domain-independent way, and
then prove theorems about it that apply to all relevant domains at once! Grothendieck’s
attitude takes theory-building to the extreme.
Mathematicians David Mumford and John Tate wrote about Grothendieck,
Although mathematics became more and more abstract and general throughout the 20th
century, it was Alexander Grothendieck who was the greatest master of this trend. His
unique skill was to eliminate all unnecessary hypotheses and burrow into an area so deeply
that its inner patterns on the most abstract level revealed themselves—and then, like a
magician, show how the solution of old problems fell out in straightforward ways now
that their real nature had been revealed.
Grothendieck’s ideas were to find out what theorems are important, and then rewrite
the basic definitions of mathematics until those theorems become completely trivial. In
his mind, a theory is powerful only insofar as what it makes obvious. A radical conviction
indeed!
Subcultures and styles go beyond theory-building/problem-solving and algebra/anal-
ysis, deep into subfields of mathematics. Even those working entirely within geometry
having specific styles. Henri Poincaré remarks in his essay, “Intuition and Logic in Math-
ematics,”
Among the German geometers of this century, two names above all are illustrious, those
of the two scientists who have founded the general theory of functions, Weierstrass and
93
Riemann. Weierstrass leads everything back to the consideration of series and their ana-
lytic transformations; to express it better, he reduces analysis to a sort of prolongation of
arithmetic; you may turn through all his books without finding a figure. Riemann, on the
contrary, at once calls geometry to his aid; each of his conceptions is an image that no one
can forget, once he has caught its meaning.
We’ll see the two sides of this analytic/geometric coin in the forthcoming chapters:
the view that geometric ideas should be studied using series is how we will approach
Calculus in Chapter 8 (and to a lesser extent Chapter 14), while the geometric view is
the heart of the study of hyperbolic geometry in Chapter 16. These could have easily
been swapped, with geometric ideas founding calculus and analytic ideas underlying
hyperbolic geometry.
As with most “classifications” of things, the problem-solving and theory-building
groups, along with the algebra/analysis divide, are neither wholly distinct nor discrete.
Styles fall along a spectrum, depending on the occasion and whether one has had break-
fast. Whether Poincaré, Mumford, Atiyah, or Tilly, the mathematical universe is as varied
in attitudes and preferences as any other community, and mathematics reaps the benefits
of diversity.
For the record, I eat corn like a typewriter, and I do prefer algebra. Although, much
of my mathematical research involved analysis-style arguments, and I have come to ap-
preciate the beauty of a good bound. Maybe next time I’m in a rush I’ll try scraping that
corn.
Chapter 8
– William Thurston
Calculus is a difficult subject to introduce. It has a hundred different motivating angles,
a thousand books you could read, and millions of applications. You can start with basic
physics, where position is a function, and derivatives are velocity and acceleration, and
work your way to Newtonian mechanics. You could aim for systems of differential equa-
tions and numerical simulations, tread the probability path and dabble in measure theory,
or take a purely mathematical approach. Your ultimate goal might be machine learning,
weather modeling, the frontiers of theoretical physics, economics, or operations research
and optimization. These all rely on the fundamental idea of calculus: that progressively
better approximations ultimately produce the truth.
Luckily, as a programmer you’re familiar with the existence of these fantastic appli-
cations. You may have seen and played with programmed physics models before, or
programmed a sprite jumping on a screen. You’re probably aware at least in a vague
sense that many widely-used algorithms involve calculus. This makes the job of learning
calculus much easier, because I don’t have to convince you it’s worth learning.
Much of the mastery of calculus (and any subject!) comes with practice. Even so, in this
chapter and the next we can survey most of the important features of a more complete
calculus course and do a bit of machine learning at the end. This chapter will be about
calculus for functions with one input, while Chapter 14 will cover functions with many
inputs.
95
96
If you’ve seen a lot of calculus before, you can probably tell that I don’t regard it as
reverently as most other authors. While I can appreciate its place in history and its ap-
plications to physics and everything else, my esteem for calculus is essentially limited to
“It’s a great tool for computation.” I avoid nonsense rhetoric about calculus like a plague
(“With calculus you can hold infinity in the palm of your hand!”). I’d much rather use it
to do something useful and draw divine inspiration from other areas of math. But that’s
a personal preference.
Besides calculus, in this chapter we’ll dive into more detail about the process of design-
ing a good mathematical definition. In doing this we’ll introduce the idea of a quantifier,
which is the basis for compound (recursive) conditions and claims. We’ll also come to un-
derstand the idea of well-definition in mathematics, which is how a mathematician proves
(or asserts) that the definition of a concept doesn’t depend on certain irrelevant details
in its construction. Finally, we’ll level up our proof skills by using multiple definitions
in conjunction to prove theorems. The application for this chapter is an analysis of the
classic Newton’s method for finding roots of functions.
y2 − y1
slope(L) = .
x2 − x1
The difference in the y’s correspond to a vertical change, while the difference in x’s
correspond to a horizontal change. The slope is an invariant of the line because any choice
of two points because any for any two choices of points you can draw a right triangle
(Figure 8.2), and all of the triangles drawn this way are similar (i.e., they have the same
angles at all vertices). In Figure 8.2, the slope between A and B is the same as between C
and D because if I move point B to D the ratios stay the same (similar triangles), likewise
for A to C.
Lines and other simple functions often represent the 1-dimensional position of an ob-
ject over time, while the steepness—the ratio of the change in position to the change in
time—is the velocity of that object.
97
Figure 8.2: Slope is consistent no matter where you measure, because the triangles are
all similar.
98
A
f
Figure 8.3: For a general curve, steepness depends on where you measure.
Before graduating from lines, let me point out that not all lines are functions from the
x coordinate to the y coordinate.1 If you pick a line which is a function f : R → R, then
the formula for the slope can be written as
f (x2 ) − f (x1 )
slope(f ) = .
x2 − x1
This makes it clear that the slope imposes an orientation on the line, that the x coor-
dinate is “horizontal” while the y coordinate is “vertical.” This is an arbitrary choice of
perspective, albeit the standard one.
Now say we have a function f (x) that isn’t a line. It’s curved, and it has some com-
plicated formula we won’t write down. The curve in Figure 8.3 is steeper at some places
(e.g., A) and less steep at others (B). Despite the self-evident fact that the line is steep
at A and gradual at B, if we were pressed to say precisely and consistently how the two
steepnesses compare, we’d be at a loss. This is because the picture only tells us qualitative
information, and we have to leave the picture behind to get useful quantitative data.
To motivate an exact answer, let’s approximate steepness using tools we know. Focus
on the point labeled A, and call it A = (x, f (x)). After a moment of thought, the idea
naturally occurs to draw a line between (x, f (x)), and a nearby point (x′ , f (x′ )), and
have our approximation be the slope of that line, as in Figure 8.4.
f (x′ ) − f (x)
steepness at A ≈
x′ − x
As a reminder, we adorn a variable with the tick ′ (called a “prime”) to denote a slight
1
such as {(x, y) : x = 1}
99
A'
A
f
x x'
Figure 8.4: We can use the slope of a line as a proxy for the corresponding “steepness”
measurement on a curve.
difference. So x, and x′ play similar roles, but x′ is slightly different from x in some way.2
We also use the ≈ symbol as a stand-in for the phrase “is approximately.” I also went back
to using the word “steepness” instead of slope because we’re using the slope of a line to
reason about this new kind of steepness.
My choice of x′ isn’t that close to x, but I chose it to illustrate a point. The approx-
imation isn’t perfect, but it’s still good enough to concretely distinguish it from my ap-
proximation of a similarly bad approximation of the steepness of f at B, as shown by
Figure 8.5. Concrete numbers for the slopes of these two lines suggest that f is twice as
steep at A as at B. Our brains still nag us to be more precise. Otherwise, how could we
be certain we aren’t fooling ourselves with inadequate picture-drawing skills? To that
effort, let’s try to improve our estimate.
Once blessed with the idea of approximating the steepness of f at A by drawing a line
from x to some other x′ , we neurotically yearn to move x′ closer to x. We could move
x′ halfway closer to x, call this new point x1 , and update our slope approximation, as in
Figure 8.6.
f (x1 ) − f (x)
steepness at A ≈
x1 − x
A'
A B'
x x'
Figure 8.5: Two different lines show how the approximation can be better or worse, de-
pending on where it is.
x x1 x'
How do we reason about the “end” of this process? We get a number at every step.
If we were to run this loop forever, would these approximate numbers would approach
some concrete number? If so, we could reasonably call that number the “true” steepness
of f at A.
That is exactly what limits do. Limits are a computational machinery that allows one
to say “this sequence of increasingly good approximations would, if followed forever,
end up at a specific value.” The limit of this particular line-approximation-scheme is
called the derivative. We’ll return to derivatives in a bit. Note in particular that whether
this “limiting process” works shouldn’t depend on how we move x′ closer to x. A good
definition should work so long as x′ approaches x somehow.
8.2 Limits
In the last section we saw a strong motivation for inventing limits, and an intuitive
understanding for what a limit should look like. It’s the “end result” of iteratively im-
proving an approximation forever. You have some quantity an indexed by a positive
integer n, and as n grows an eventually gets closer and closer to some target. For ex-
ample, if an = 1 − 1/n, the numbers in the sequence 0, 12 , 23 , 43 , 45 , . . . seem to approach
1.
But we need a definition. A definition is like the implementation of a program spec.
From a specification standpoint, you care mostly about how one intends to use an inter-
face. When actually writing the program you have to worry about people misusing your
code, intentionally or not. You have to anticipate and defend against the edge case inputs
which are syntactically allowed but semantically unnatural. Anyone who has spent time
designing a software library has spent hours upon hours thinking about:
• How to avoid writing a mess of extra code just to handle edge cases.
And ideally a library author wants to meet all of these criteria at once! We have the
same problem in mathematics.
Most concepts in math—in this case limits—usually make intuitive sense in the over-
whelming majority of cases you encounter in real life. However, 99% of the work in mak-
ing the math rigorous is converting the concept into concrete definitions that can handle
pathological counterexamples. By pathological, I mean examples that are mathematically
valid, but which nobody would ever encounter in the wild.3 The best pathological ex-
amples are edge cases on steroids, and some mathematicians gain fame for constructing
3
This is relative, of course. Once upon a time complex numbers like 1 + i were thought to be pathological,
but now they’re standard.
102
1/2 1
Figure 8.7: This pathological function admits two different possibilities for the derivative
depending on the sequence of approach.
But I dastardly chose f in such a way that the limit changes depending on how you pick
the sequence x1 , x2 , . . . . In fact, if you pick xk = 1/k, every slope in the sequence is
2, implying the limit is 2. There isn’t even an approximation because the values in the
1
sequence are constant. But if you choose xk = k+0.5 , the slopes are always 1. So should
the limit be 1 or 2? Neither?
This will be the last pathological example I inflict upon you,4 but it emphasizes an im-
portant point. However we choose to define limits, it can’t depend on the arbitrary choice
of which points you choose in the sequence. It should be a definition like “no matter how
your values approach the limit, the limit is the same.” The generic mathematical term for
this is that the limit should be well-defined.
With that thought, let’s start with the limit of a sequence of numbers, which will be
used to define limits for functions. Since sequences of numbers can have repetition, we
won’t use set notation (though some authors do). Instead we’ll use a comma notation
x1 , x2 , . . . which the strongly-typed programmer can think of as the output of an iterator
which never terminates, or a tuple/array of infinite length (x1 , x2 , . . . ). The ε character is
a lower-case Greek epsilon, contextually used across mathematics as an arbitrarily small
positive real number.
This is the first time we’ve encountered a definition that relies heavily on alternating
quantifiers (for every…, there is…), so let’s discuss it in detail. A statement like “for every
FOO there is a BAR,” means there’s a functional relationship. If you give me a FOO as
input, I can produce a BAR with the desired property as output.5 Interpreting this for
Definition 8.1, the input is a real number threshold ε > 0, and the output is an integer k
with a special property. So the relationship is:
The special property of k is that all the sequence elements after k are close to L, in fact
as close as the input ε specified.
As a simple non-pathological example, let’s take the sequence xn = 1 − n1 . This is the
sequence 0, 21 , 23 , 34 , 45 , . . . . Our intuition tells us that the limit should be L = 1, so let’s
prove it strictly by the letter of the definition.
4
If you want more, check out the book “Counterexamples in Calculus.”
5
It isn’t strictly true in math that there’s always a functional relationship that you can compute. Sometimes
you can prove a thing exists without knowing how to compute it. But in most important cases you can
compute, and it makes the explanation here simpler.
104
Proof. Let ε > 0 be fixed. Pick any integer k > 1/ε. We will show that |xn − 1| < ε for
all n ≥ k. Indeed,
( )
1 1 1
|xn − 1| = 1 − − 1 = − = ,
n n n
and because n ≥ k > 1/ε, we have 1/n ≤ 1/k < ε.
You can think of this ε-to-k process as a game. A skeptical contender doesn’t believe xn
converges to L, and challenges you to find the tail of the sequence that stays within ε =
1/2 of L. You provide such a k, but the contender isn’t happy and re-ups the challenge
using ε = 1/100. You comply with a bigger k. The contender retorts with ε = (1/2)99 .
Unfazed, you still produce a working k.
If there’s any way for the contender to stump you in this game, then xn doesn’t con-
verge to L. But if you can always produce a good k no matter what, the sequence con-
verges to L.
As a notational side note, the phrase “for every x there is a y” can be long and an-
noying to write all the time. It also makes it difficult to look at the syntactic structure
of statements like this, since language tends to vary across the world and it can be un-
clear what depends on what. This is exacerbated by slightly ambiguous words like “each”
6
The fraction 1/ε shouldn’t be scary because, looking again at Definition 8.1, we require ε > 0, so we’ll never
divide by zero.
105
and “unless.” Mathematicians designed an unambiguous notation for this situation called
quantifiers. We briefly introduced quantifiers in Chapter 4, and promised we wouldn’t
use them in this book. However, standard textbook definitions in analysis often use the
symbols heavily, so this digression helps put what you might see elsewhere in context.
The first quantifier is the symbol ∀, which means “for all” (the upside-down A stands
for All). The second is ∃, which stands for “there exists” (the backwards E in “Exists”).
Quantifiers may appear in any order. If I claim
∃x ∈ R, ∀y ∈ R, x + y = 3,
I’m saying I can come up with a real number x, such that no matter which y you
produce, it’s true that x + y = 3. Obviously no such x exists, so the statement is false.
Note the statement changes if the order of the quantifiers is reversed: for every y, there
is indeed an x for which x + y = 3, it’s x = 3 − y.
If I were to state the definition of the limit in its briefest form, I might say:
We’ve just packed the math like sardines in a tin box. That being said (and now we’re
really digressing), some situations benefit from writing logical statements in this form.
Particularly in the realm of formal logic, it turns out that as you add more “alternating”
quantifiers (∀x∃y∀z), you get progressively more expressive power. In theoretical com-
puter science this is formalized by the so-called polynomial hierarchy, which conjecturally
asserts that the computational cost of deciding the truth of generic logical statements in-
creases dramatically with the number of alternating quantifiers. That’s why one might
believe factoring integers (∃a, ∃b, ab = n) is easier than deciding if one can force a win
in a two player game like chess (there exists a move for me, such that for every move for
my opponent, there exists a move for me, such that…, such that I have a winning move).
Back to limits. The definition of a limit allows a sequence to have no limit, like the
sequence 0, 1, 0, 1, 0, . . . , which isn’t pathological at all. For this sequence you can’t even
satisfy the limit definition with ε = 1/3 (no matter what you think the limit L might be!).
This fits with our intuition that an alternating (0, 1, 0, 1, . . . ) sequence doesn’t “get closer
and closer” to anything. So now we can add to our definition.
Figure 8.8: Starting in the top left corner, we want to deduce the top right corner. We do
this by taking the longer route down and around.
Definition 8.4. Let f : R → R be a function. Let c and L be real numbers. We say that
limx→c f (x) = L if for every sequence xn that converges to c (and for which xn ̸= c for
all n), the sequence f (xn ) converges to L.
The notation f (xn ) is shorthand for a sequence yn = f (xn ). In this context we’re
implicitly “mapping” f across the sequence xn as one would say in functional program-
ming, or alternatively we’re “vectorizing” f . The notation x → c is used to signify that
xn is a sequence converging to c, and the value of x is used in the expression inside the
limit.
Let’s do another simple example: compute limx→2 x2 − 1. We prove it directly. Given
any sequence xn for which xn → 2, we must prove that f (xn ) → L for a specific L.
Most often L = f (c), which in this example is f (2) = 3.
Proposition 8.5.
lim x2 − 1 = 3.
x→2
Proof. Let ε > 0 be an arbitrarily small threshold required by the definition of f (xn ) → 3.
In this proof we’ll actually need ε to be small enough (say, less than 1/5). What we’re
going to do is use the proof of the fact that xn → 2 as a subroutine for some special ε′
that we choose, and use the index we get as output to prove that f (xn ) → 3.
Figure 8.8 contains a diagram to illustrate the gymnastics. The top row is the theorem
we want to prove, with the input on the left and the desired output on the right. Likewise,
the bottom row is the black box subroutine for xn → 2. So given that first ε > 0 that we
don’t get to pick, we choose a threshold ε′ to use for xn → 2. Picking a useful ε′ is the
tricky part of these kinds of proofs, and I’ll be momentarily opaque and choose ε′ = ε/5.
So the output of our subroutine for xn → 2 gives us an index k ′ after which all xn are
within ε/5 of 2. Now we’ll use that same index k ′ = k for f (xn ) → 3. All we need to
show is that |f (xn ) − 3| < ε for n > k. To that effect, a little algebra:
way bigger than 4. In symbols, since we required ε < 1/5 then it must be the case that
|(xn + 2)| < 5.7
Putting these two facts together gives us
ε
|f (xn ) − 3| = |xn + 2| · |xn − 2| < 5 · < ε.
5
Which proves that f (xn ) → 3.
All of this was a formal way of saying that to compute limx→2 x2 − 1, you may “plug
in” 2 to the expression x2 − 1. Indeed, in almost all cases where the expression inside
the limit is defined at the limiting input (in this case x = 2), you can do that. But there
are non-pathological functions with useful limits (not just the derivative) for which you
can’t simply “plug the value in.” See the exercises for a famous example. To reiterate
from earlier, all of this hefty calculus machinery was invented to deal with those difficult
functions.
As we saw with our pathological “two lines” example from Figure 8.7, not every se-
quence has a limit. For the “two lines” f (x), we computed the slope as f (xxnn)−f −0
(0)
where xn was part of a sequence tending to zero. I.e., we informally computed the limit
f (x)−f (0)
limx→0 x−0 . But then we found two sequences an , bn that both converge to zero,
n −x) f (bn −x)
but their vectorized slope-sequences f (a an −x , bn −x gave different slope values. As a
consequence, the limit does not exist, corroborating our intuition. So we’ve seen that this
definition of the limit passes a litmus test: good functions have limits, and bad functions
do not.
f (x) − f (c)
lim
x→c x−c
This value is denoted f ′ (c).8 In the limit, sequences x → c are taken so that xn ̸= c to
avoid division by zero.
7
Even if we didn’t require ε < 1/5, we can always choose a k at least as large as when we do impose this
restriction, even if it’s larger than k′ . This is a sleight-of-hand that allows us to add extra assumptions that
simplify a computation, and it’s often paired with the phrase “without loss of generality” to signal what’s
going on.
8
Here is where the prime ′ is being used to denote the derivative.
108
f (x) − f (3)
f ′ (3) = lim
x→3 x−3
x − 6x + 9
2
= lim
x→3 x−3
(x − 3)(x − 3)
= lim
x→3 x−3
We can now simplify (x − 3)/(x − 3) = 1. Indeed, recalling the definition of the limit,
the expression (x−3)(x−3)
x−3 is evaluated at the entries of a sequence xn that for which
xn ̸= 3. Hence, we never divide zero by zero and may simplify.
(x − 3)(x − 3)
f ′ (3) = lim
x→3 x−3
= lim x − 3
x→3
=0
This was a nice exercise, but it’s tedious to compute derivatives over and over again for
every input. It would be much more efficient to instead compute a compact representation
of the derivative at all possible points. That is, we want a process which, when given a
function f : R → R as input, produces another function g : R → R as output, such that
g(c) = f ′ (c) for every c. While computing the limit may be tedious, our representation of
g should make subsequent derivative calculations as computationally easy as evaluating
f.
If you ask a mathematician how to come up with such a g, you’d probably receive the
reply, “You just do it.” This means we can calculate directly from the definition. If, for
example, f (x) = x2 ,
f (x) − f (c)
f ′ (c) = lim
x→c x−c
x2 − c2
= lim
x→c x − c
(x − c)(x + c)
= lim
x→c x−c
= lim x + c
x→c
= 2c
Forever after, we may plug in the desired value of c to get the derivative at c. Most
mathematicians don’t switch variables, so they’d call the derivative function f ′ (x) instead
of f ′ (c). This has the added advantage of displaying patterns in derivative computations.
109
For example, if you compute the derivative of x4 , you get 4x3 , and the derivative of x8
is 8x7 , suggesting the correct rule that the derivative of xn is nxn−1 (for a positive integer
n). Here, the notation makes this pattern clear in a way that pictures do not. In fact, if
you want to prove this, the following theorem makes the limit calculation less painful.
Theorem 8.7. For any real numbers x, c and any positive integer n,
I’ll call the sum (xn−1 + xn−2 c + · · · + cn−1 ) “the ugly sum.”
Proof. Start to multiply the right-hand side and notice that each term, except the first and
last, pair off and sum to zero. In particular, you get
Tenderly applying Theorem 8.7 while computing the derivative of f (x) = xn reveals
that in the limit defining f ′ (x) you can cancel two (x − c) terms, as in our previous
examples, leaving just the ugly sum. Plugging x = c in to the ugly sum gives ncn−1 .
At this point in a standard calculus course, a student would spend a few weeks (or
months) learning:
2. When given two functions f, g whose derivatives you know separately, how to
compute the derivative of an elementary combination of f and g, such as f + 3g
and f (g(x)).
3. How to use special values of the derivative (such as zero) to find maxima and min-
ima of various functions, such as maximizing profit from selling a widget subject
to costs for creating certain variations of that widget.
110
Because this book can only give you a taste of calculus, and because we’re rushing to
an interesting application, we’ll skip most of this in favor of stating (what I believe is) the
most important facts for applications.
Let F be the set of all functions R → R that have derivatives. Let D : F → F be the
function that takes as input a function f and produces as output its derivative f ′ .
As a functon, “cf ” is the function that takes as input x and produces as output c · f (x).
Likewise, f + g takes as input x and produces as output f (x) + g(x).
As a quick aside, I hate writing sentences like “the function that on input x produces as
output c · f (x).” Instead I like to use the mathematical analogue of “anonymous function”
notation, using the 7→ symbol. So I can instead say “cf is defined by x 7→ c · f (x),” or
even “D is the function f 7→ f ′ .” When you’re reading this out loud, 7→ is pronounced
“maps to.”
d
This derivative-computing function D is also often written as dx , but this causes in-
d df
consistent notation like dx (f ) versus dx and forces one to choose a variable name x. In
my opinion, this notation exists for bad reasons: backwards compatibility with legacy
math, and trying to trick you into thinking that derivatives are fractions so you’ll guess
the forthcoming chain rule. But it is too widespread to avoid.
Theorem 8.9 immediately lets us compute the derivative of any polynomial, because
we can use Theorem 8.8 to compute the derivatives of each term and add them up. E.g.,
the derivative of 3 + 2x − 5x3 is 2 − 15x2 . Quick spot check exercise: using intuition,
reason that a constant function like f (x) = 3 has derivative f ′ (x) = 0. If your intuition
fails you, use the definition of the limit to compute it.
The other crucial fact, which we’ll use later, is the chain rule.
Theorem 8.10 (The chain rule). Let f, g : R → R be two functions which have derivatives.
Then the derivative of f (g(x)) is f ′ (g(x))g ′ (x).
In the chapter exercises you’ll look up a proof of this theorem. The chain rule makes
it easy to compute derivatives that would require a lot of algebra to compute, such as
50 49
(x2 − 10) . Here f is z 7→ z 50 and g is x 7→ x2 − 10, so the derivative is 50(x2 − 10) ·
(2x). The chain rule also lets us compute derivatives that would otherwise be completely
mysterious, such as that of sin(ex ). If you’re told what the derivatives of sin(x) and ex
are separately, then you can compute the derivative of the composition.
9
I sneer, but if you’re serious about mathematics then at some point you need to become intimately familiar
with specific derivatives of elementary functions. This book is not the place for that, and I suspect many of
my readers will have seen calculus at least once before, and knows how to google “derivative of arctan(x)”
should they forget.
111
As a notational side note, let me explain the “fractions make you guess the chain rule”
remark. Call h(x) = f (g(x)). Then if we use the fraction notation dh dx for the derivative
dg
of h, the standard way to write the chain rule for this would be dx = dh dg · dx . The “hint”
dh
of the notation is that if you’re a reckless miscreant, you might jump to the conclusion
that the dg’s “cancel” like fractions do. Rest assured that is not how it works, but calculus
students the world over are encouraged to do it this way because the resulting rule is
correct. We’ll return to this in Chapter 14.
Historically, symbols like dx had no concrete mathematical meaning. They were called
“infinitesimals” and regarded informally as quantities infinitely smaller than any fixed
value. More recently, dx was retroactively assigned a semantic meaning that allows one
to work with it as the notation suggests. The formalism is beyond the scope of this book.10
A'
A
f
x x'
Figure 8.9: The line between A and A′ does not approximate f well close to A.
passing through (c, f (c)), there is a sufficiently small ε > 0 such that if |x − c| < ε, then
|L(x) − f (x)| ≤ |K(x) − f (x)|.
Notation time: people often write the set of points {x ∈ R : |x − c| < ε} using the
notation (c − ε, c + ε). They also often call this an epsilon-ball around c. Using this,
the last sentence of the theorem might read, “For all x ∈ (c − ε, c + ε), it holds that
|L(x) − f (x)| ≤ |K(x) − f (x)|.” This makes the statement clearer. Instead of saying “if
this then that,” you’re saying what you want to say outright, that “FOO is always true in
my domain of interest.”
Proof. If K is a line passing through (c, f (c)), then it can be written in the same way as
L but with a different slope. I.e., for some m ∈ R, K(x) = m(x − c) + f (c).
Expanding K and L according to their formulas, the theorem’s conclusion requires us
to choose a ε > 0 such that when |x − c| < ε the following inequality is true.
number. This should already make it pretty clear that the inequality above holds, but
let’s prove it formally by contradiction.
Suppose to the contrary that no matter which ε I choose, there is some x in (c−ε, c+ε)
that contradicts the inequality above. I would like to pick a sequence of x values going to
c that violates the definition of the derivative. I will do that by picking a sequence of ε’s,
using the fact that the inequality above is false for every ε, and arriving at the sequence
of x’s needed for my contradiction. Let
We have proved that derivatives provide the best linear approximation to a function at
a point for a concrete sense of “best.” This perspective brings up the natural question of
whether we can improve this approximation by using more complicated functions than
lines. The answer is yes, and it’s called the Taylor polynomial.
Taylor Polynomials
Lines are degree 1 polynomials. One thing that’s nice about polynomials is that they
have a grading. By that I informally mean, if you increase the degree of your polynomial,
you can express a wider variety of functions. There is a rigorous way to state this using
linear algebra (see Definition 10.9), but the gist of it is that the data defining a degree 3
polynomial is four unrelated numbers, while the data defining a degree 4 polynomial is
five unrelated numbers. In principle, higher degree includes more complexity, and allows
better approximations of f .
You can derive exactly how this works by following the steps of Theorem 8.11, and
asking for a degree 2 polynomial whose derivative best approximates f ′ close to a. Indeed,
let our candidate be the following (where below q ∗ ∈ R is the unknown parameter we
must set to get a degree 2 polynomial).
p′ (x) = 2q ∗ (x − a) + f ′ (a).
Plugging in x = a leaves only f ′ (a). If we had used some other number R instead of
f ′ (a),
then p′ (a) = R. In the same way, in Theorem 8.11 we couldn’t avoid using f (a)
for the constant term because the line had to pass through (a, f (a)).
114
And so if we want to optimize p′ (x) by choosing q ∗ , it’s almost exactly the same proof
as Theorem 8.11, with the different being an extra factor of 2. We’ll leave it as an exercise
′′
for the reader to redo the steps, but at the end you get q ∗ = f 2(a) , where f ′′ is the
derivative of the derivative of f (the “second” derivative of f ).
Two quick asides. First, the second derivative only makes sense if f has a first deriva-
tive, and as we saw not all functions have derivatives at all points. Second, adding more
and more primes to denote repeated applications of the derivative operation is cumber-
some. Rather, it’s customary to use a parenthetical superscript notation f (n) (x) for the
n-th derivative of f . You call a function n-times differentiable if it has n derivatives at
every point. Finally, if f has infinitely many derivatives (i.e., it is n-times differentiable
for every n ∈ R), f is called smooth. The typical example of a smooth function is sin(x)
or 2x . A default assumption is that life is smooth, and when it’s not you pay very close
attention.
Our exploration has led us to the Baby Taylor Theorem.
f (2) (a)
p(x) = f (a) + f ′ (a)(x − a) + (x − a)2
2
A proof by induction, which the reader should finish (we just did the step from n = 1
to n = 2 which has all the features of the general induction), extends the Baby Taylor
Theorem to the Adolescent Taylor Theorem. Note that by n! we mean the factorial func-
tion n 7→ n · (n − 1) · (n − 2) · · · · · 2 · 1 where n is a positive integer. We’re not merely
excited about n, though it is bittersweet to have watched n grow up so fast.
∑
k
f (n) (a)
f (a) + (x − a)n
n!
n=1
Definitions are usually introduced in their most general and often-used form, in this
case with a summation. It is almost always helpful to write out the first few terms to
familiarize yourself with the pattern. Here are the first three.
now that the derivative of ex is also ex . In fact, the number e is uniquely defined by this
property.
Then the degree 4 Taylor series for ex at x = 0 is particularly simple because e0 is 1 in
every term:
x2 x3 x4
1+x+ + + .
2 6 24
Figure 8.10 contains a picture of e and its approximation by the degree 4 Taylor poly-
x
nomial. The approximation is faithful to the original function, but only close to x = 0.
Elsewhere it can be arbitrarily bad.
The Taylor polynomial is one of the most often used applications of mathematics to
itself. The reason is because when you’re analyzing a mathematical problem, it’s easy to
define functions with convoluted behavior. One example of this is in machine learning,
when you analyze the probability that some event occurs. You can often write down
the probability as a massive product, but can’t compute it exactly. Instead, one often
uses a small-degree Taylor polynomial to approximate the complicated thing at a point
of interest. With knowledge of whether the Taylor polynomial is an over- or under-
approximation of the truth, one can bound the complicated behavior enough to prove
something useful.
Theorem 8.13 seems to show us that every function can be approximated arbitrarily
well using polynomials. As useful as polynomials are, it turns out this is not entirely true.
Let’s say we’re working with a function where the polynomial approximation does get
progressively better at higher degrees. If you’re in the proper mindset for calculus, you
116
naturally ask what happens in the limit? If I call pk the degree k Taylor polynomial for
f at a = 0, how can we make sense of the expression
lim pk (x) . . .?
k→∞
Remember, we only defined what it means for a sequence of numbers to converge, but
this is a sequence of functions R → R. In order to define convergence for a sequence
of functions, we need to define what it means for two functions to be “close” together,
which is not easy. But suppose we did that and we can make sense of this expression,
we’d hope that this limit was also equal to f , and least close to x = 0. This expression,
the limit of Taylor polynomials, is called the Taylor series of f at that point.
Mathematics is not so kind to us here. There are certain simple functions, like the base
2 logarithm, for which the Taylor series breaks down in certain regions. In particular, if
f (x) = log(1 + x) and you compute the limit at a = 0, the resulting function would only
be equal to f (x) between x = −1 and x = 1. When x > 1 the limit does not converge,
even though log(1 + x) exists for x > 1. In that case, you have to compute a different
Taylor series at, say, a = 2. The complete function is then joined together piece-wise by
enough Taylor series pieces until you get the whole function. The functions which can
be reconstructed in this way (and aren’t sensitive to which points you choose within a
region, again in the interest of well-definition) are called analytic functions.11
There are somewhat natural functions that fail to accommodate Taylor series worse
than the logarithm. Let f (x) = 2−1/x when x ̸= 0, and let f (0) = 0. Figure 8.11
2
contains a plot of this function. You will prove in Exercise 8.8 that f (n) (0) = 0 for every
n ∈ N. As a consequence, all of its Taylor polynomials at x = 0 are the zero function,
and the “limit function” should be the constant zero function.12 In this case, the Taylor
series tells you nothing about the function except its value at x = 0. Polynomials aren’t
able to express what f looks like near zero.
This highlights the shortcomings of Taylor polynomials. They’re not the perfect tool
for every job. It also leads us to ask why, for this mildly pathological f , the Taylor series
fails so spectacularly. Complex analysis provides a satisfactory answer, but the subject is
unfortunately beyond the scope of this book.
8.5 Remainders
The Adolescent Taylor Theorem tells us how to compute the best polynomial of a given
degree that approximates the behavior of a function. In fact, it approximates the behavior
11
There is a more rigorous way to say “not sensitive to the points you choose,” which is to say that computing
the Taylor series of f at every input a in the domain of f converges to f in some open set around a. Saying
what an “open” set is another can of worms, but for most functions R → R this just means “any interval
containing a.” This can fail, e.g., when the Taylor series at a only equals f at a finite set of other points.
12
Indeed, a constant function is defined by a single number, so a sequence of constant functions “is” a sequence
of numbers. A reasonable definition of function convergence should generalize convergence for numbers.
117
Figure 8.11: A function f (x) = 2−1/x , all of whose derivatives are zero at x = 0.
2
of a function’s “slope” (first derivative) and more informally its curvature (higher deriva-
tives), provided you’re willing to compute enough terms.
The Adolescent Taylor Theorem, however, doesn’t allow us to quantify how good the
approximation is. As we just saw, there are pesky functions whose Taylor polynomials
at certain rotten points are all zero. They’re so flat they tricked the poor polynomial!
As you might have guessed, there is an Adult Taylor Theorem—just called the Taylor
Theorem—which gets one much closer to quantifying the error of the Taylor polynomial.
Unfortunately, the proof of this theorem requires the Mean Value Theorem, which does
not fit in this book, but we can state the Taylor theorem easily enough.
f (d+1) (z)
f (x) = pd (x) + (x − a)d+1
(d + 1)!
In words, the exact value of f (x) can be computed from the Taylor polynomial pd (x)
plus a remainder term involving a magical z plugged into the (d+1)-th derivative instead
of x.
The dependence of the variables on each other are a bit confusing. Let’s make it explicit
with some pseudocode. In particular, the needed value of z depends on the specific input
x.
118
Arguments:
f: the function to evaluate
d: the degree for the taylor polynomial
a: the input we can compute f at
x: the input we'd like to compute f at
'''
p = taylor_polynomial(f, d, a)
next_derivative = nth_derivative(f, n=d+1)
One important consequence of the remainder formula is that if f (d+1) is never large
between a and x, then z is irrelevant. For the sake of concreteness, let’s say that f 3 (z) <
100 between a and x. Then |f (x) − p2 (x)|, the error in computing f (x) from its Taylor
polynomial at a, is bounded.
In this case, if x is within 0.1 of a, then the error of the Taylor polynomial is only
about 0.017. Often this coarse z-be-damned bound is enough. This is the viewpoint of
Newton’s method, this chapter’s application.
Figure 8.12: A function whose root does not have a nice formula.
One initial thought is obvious: perform a binary search. That is, pick two guesses c, d,
where f (c) < 0 < f (d), and then let your improved guess be the midpoint (c + d)/2,
updating your upper and lower search bounds in the obvious way depending on whether
f ((c + d)/2) > 0.
Binary search does produce a sequence approaching a root of f , but it turns out to be
much slower than the forthcoming Newton’s method.15 In Newton’s method you choose
your next guess xn+1 depending on the derivative of f at xn . To convince you that this
this could be faster than binary search, suppose you chose bad bounds for binary search
as in Figure 8.13.
The tangent line at the point (d, f (d)) intersects the x-axis quite close to the root,
whereas the midpoint between c and d is rather far away. A binary search would slowly
approach the root from the left, whereas the tangent line guides us close to the root in
the first step.
If this isn’t convincing enough, we can provide something much better: a proof. But
first, we have to make the algorithm explicit. Phrased geometrically, start from some
intermediary x-value guess, calling it xn for the n-th step in the algorithm. Draw the
tangent line at xn , which is y = f (xn ) + f ′ (xn )(x − xn ), and let xn+1 be the intersection
of this line with the x-axis. This is illustrated in Figure 8.14. To find the intersection
point, set y = 0 in the equation for the tangent line, and solve for x:
15
To be precise, binary search requires k iterations to get k digits of precision, whereas Newton’s method gets
k2 digits of precision in k steps, under the right starting conditions.
120
c d
Figure 8.13: And example of Newton’s method outperforming a binary search. The tan-
gent line at d is better than the slow approach from c.
xn x n+1
Theorem 8.15 (Convergence of Netwon’s Method). For every k ∈ N, the error ek+1 ≤
Ce2k , where C is a constant defined as
|f ′′ (z)|
C = max
c≤z≤d 2|f ′ (z)|
122
In other words, the error of Newton’s method vanishes quadratically fast in the number
of steps of the algorithm.
Proof. Fix step k. Compute the degree 1 Taylor polynomial for f at xk . This is exactly
the tangent line to f at xk . Use that Taylor polynomial to approximate f (r), the value of
f at the unknown root r.
Despite all the algebraic brouhaha in the proof above, all we did was take some value
x = xk (though calling it xk was only relevant in hindsight), write down the degree 1
Taylor polynomial that approximates f at x, and use that approximation to guess at the
value of the unknown root r. We needed the notation and formalism to ensure that we
weren’t being tricked by our intuition, and to clearly outline the guarantees, and where
those guarantees break down.
Speak of the devil! The proof allows us to identify the requirements of a “nice enough”
function:
• f ′ (x) can never be zero between c and d, except possibly at the root r itself. Other-
wise we risk dividing by zero, or worse, getting stuck in a loop (as we’ll see in the
example below).
123
• f has to have first and second derivatives everywhere between c and d. Otherwise
the claims in the proof that use those values are false.
• Realistically, f ′ (x) should never be very close to zero, and f ′′ (x) should never be
very far from zero, or else C will be impractically large.
THRESHOLD = 1e-16
def f(x):
return x**5 - x - 1
def f_derivative(x):
return 5 * x**4 - 1
starting_x = 1
approximation = []
i = 0
for x in newton_sequence(f, f_derivative, starting_x):
print((x, f(x)))
i += 1
if i == 100:
break
After only six iterations we have reached the limit of the display precision.
(1, -1)
(1.25, 0.8017578125)
(1.1784593935169048, 0.09440284131467558)
(1.16753738939611, 0.001934298548380342)
(1.1673040828230083, 8.661229708994966e-07)
(1.1673039782614396, 1.7341683644644945e-13)
(1.1673039782614187, 6.661338147750939e-16)
(1.1673039782614187, 6.661338147750939e-16)
(1.1673039782614187, 6.661338147750939e-16)
Let’s see the same experiment with the starting_x changed to 0 instead of 1. This is
an input which, as you can see from Figure 8.15, drives Newton’s method in the wrong
direction! By the end of a hundred iterations, Newton’s method cycles between three
points:
124
Figure 8.15: An example where the starting point of Newton’s method fails to converge
due to an unexpected loop.
...
(0.08335709970125815, -1.083353075191566)
(-1.0002575619492795, -1.001030911349579)
(-0.7503218281592572, -0.4874924386834848)
...
This behavior is allowed by Theorem 8.15, because in between the starting point and
the true root, the derivative f ′ (x) is zero, making the error bound C from Theorem 8.15
undefined (and indeed, unboundedly large for x values close to where f ′ (x) is zero). New-
ton’s method is very powerful, but take care to choose a wise starting point.
Newton’s method stirs up a mathematical hankering: why stop at the degree 1 Taylor
polynomial? Why not degree 2 or higher? All we did to “derive” Newton’s method was
take a random point, write down the degree 1 Taylor polynomial p(x), and solve p(x) = 0.
By rearranging to isolate the error terms, we got the formula for xk+1 for free. For degree
2, why not simply use the degree 2 Taylor polynomial instead?
1 ′′
0 = f (xk ) + f ′ (xk )(x − xk ) + f (xk )(x − xk )2
2!
There are two obstacles: (a) this polynomial might not even hit the x axis; it’s trickier
to nail down for quadratics than lines, and (b) even if it does, it might be hard to find the
intersection, since finding roots is the problem we started with!
Admittedly, finding the root of a degree 2 polynomial isn’t so hard (there’s a formula
with a sing-a-long mnemonic), but if you take this idea up to degree 3, 4, or higher, the
formula approach eventually breaks down. For degree 5, the polynomial we want to
approximate a root for is the Taylor polynomial, and we don’t know how to find its roots.
125
• Much of the murkiness of calculus comes from the fact that it must support a long
history of manual calculations and pathological counterexamples. The “normal”
case is usually easier to understand.
8.8 Exercises
8.2 Prove the following basic facts using the definitions from Exercise 8.1.
126
8.4 Compute the Taylor series for f (x) = e−2x , and compare this to the procdure of
plugging in z = −2x into the Taylor series for ex . Find an explanation of why this
works.
√
8.5 Compute the Taylor series for f (x) = 1 + x2 at x = 0. We will use this in Chap-
ter 12 to simplify a model for a physical system.
8.6 There are some functions which are challenging to compute limits for, but they aren’t
considered “pathological.” One particularly famous function is
f (x) = x sin(1/x).
Compute the limit for this function as x → 0. The difficulty is that sin(1/x) is not defined
at x = 0, and algebra doesn’t provide a way to simplify sin(1/x). Instead, you have to
use “common sense” reasoning about the sine function. This common-sense reasoning is
made rigorous by the so-called Squeeze Theorem. Look it up after trying this problem,
and note that this function is what best motivates the invention of the Squeeze Theorem.
A plot will also help you understand how to prove this.
8.7 Find a differentiable function f : R → R with the property that limx→∞ f (x) = 0,
but limx→∞ f ′ (x) does not exist.
computation is quite involved. These functions are sometimes called flat functions, since
they’re literally so flat that they avoid detection of any curvature by derivatives. Plot the
function to see how flat it means to be flat. Taylor series provide no use at these points.
8.9 There are two definitions of the number e. One is the number used(as an exponent
)n
base ex , for which the derivative of ex is ex . The other is e = limn→∞ 1 + n1 . First,
prove the somewhat surprising fact that this limit is not equal to 1. Second, understand
why these two definitions result in the same quantity.
8.10 Find the maximum of f (x) = x1/x for x ≥ 0. One method: use an approximation
given by the early terms of the Taylor series of ex . Another: maximize the logarithm of
f , which has the same maximizing input.
8.11 Look up a proof of the chain rule on the internet, and try to understand it. Note
that there are many proofs, so if you can’t understand one try to find another. Come up
with a good geometric interpretation.
8.12 Write a program that implements the binary search root-finding algorithm and com-
pare its empirical convergence to Newton’s method. Find an example input for which
(gasp!) they have the same convergence rate, and analyze the statement of Theorem 8.15
to determine why this is possible.
8.13 Look up a proof of the Taylor theorem, which may depend on other theorems in
single-variable calculus like Rolle’s theorem or the Intermediate Value Theorem.
8.14 Look up an exposition of the degree-2 Householder method for finding roots of
differentiable functions, and implement it in code.
Chapter 9
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate
on more advanced problems, and in effect increases the mental power of the race.
– Alfred Whitehead
So far we’ve studied functions with a single input and a single output. This was suffi-
cient to whet our appetites for mathematics, introducing sets, graphs, and basic calculus,
and exploring a few interesting algorithms. However, the overwhelming majority of ap-
plications of mathematics rely on linear algebra and multivariable optimization. Most of
advanced mathematics reaches far beyond the confines of a single variable. We require
both the construction of complicated types—often abstract spaces endowed with geomet-
ric structure—and structured functions mapping between these types. The remainder of
this book will explore a variety of these settings.
In Chapter 8 we worked entirely with functions whose type signature was R → R.
Although we only implicitly understood the formal notion of ‘continuity’—the fact that
the graphs of these functions formed contiguous curves when plotted—we concentrated
intently on the interplay between the algebra (computing limits, derivatives, and using
Taylor series) and geometry (the intrinsic qualitative shapes of curves). There is much
more to be said for single-variable calculus. One of the most common uses of calculus
is to tune parameters. For example, a car manufacturer tunes how many of each car
model to manufacture based on their costs and sales figures. Another example is tuning
an algorithm that fails with some measurable probability depending on a parameter you
can optimize.
The recipe for optimizing parameters is quite simple, bordering on monotonous. The
key insight is that it reduces the optimal parameter choice from a continuum of options
to a discrete set to check by hand.
• The optimal parameter x is the minimum value of f (x) where x is among the
critical points, or x = a or x = b.
The analysis of an algorithm using the above recipe is so routine that authors seldom
remark on it. In research papers they often skip the entire argument assuming the reader
will recognize it! Life is similar for the poor Taylor polynomial, so ubiquitous it is almost
forgotten. Such brevity can seem like malicious obfuscation, but it makes sense as a
cognitive “tail call optimization” for proofs.2
The core of the proof is the primary focus, and requires all your working memory.
Optimizing a parameter using standard tools is easy once you’ve done it enough times.
So leave it to the end and compartmentalize the two jobs: big picture comprehension
versus reproducing a rote computation. Indeed, the ability to maximize an elementary
function rarely depends on memory of how you created that function, so why not shed
a few stack frames full of mathematical baggage while you do the real work?
This is also a justification for why one might write the statement of a theorem like we
did in the last chapter.
Theorem (Convergence of Netwon’s Method). For every k ∈ N, the error ek+1 ≤ Ce2k ,
where C is a constant defined as
|f ′′ (z)|
C = max
c≤z≤d 2|f ′ (z)|
The step between where we’ve been and where we want to go is graduating to func-
tions with more complicated inputs and outputs. In the remainder of this interlude we’ll
introduce two techniques to make more complicated types (realized as sets): products
and quotients.
We touched on products briefly when we introduced sets in Chapter 4. We defined the
direct product of sets, A × B, which is the most common mathematical way to make a
compound data type. It’s just the set of pairs of objects (a, b), where a ∈ A and b ∈ B.
To reiterate from Chapter 4, if we repeat this operation, we tend to ignore the grouping,
so that A × B × C isn’t a pair-of-pairs, but rather a tuple of length 3. We’re skipping the
whole “linked” part of a linked list. Likewise, given a set A, most often R for calculus and
linear algebra, we denote by An the tuples of length n for some fixed n ∈ N. This is just
A × A × · · · × A with the product occurring n times. This may seem sloppy, but there is
a way to make it rigorous using the concept of a quotient.
In order to define quotients, we need the notion of an equivalence relation.3 Given a set
A, an equivalence relation is a function f : A × A → {0, 1} (where {0, 1} are thought
of as booleans) with the following three properties:
In your mind you can replace f (a, b) = 1 with “a and b are equivalent.” A more
common notation for this is a squiggle ∼, so that a ∼ b if and only if f (a, b) = 1, with
a ̸∼ b if f (a, b) = 0. The squiggle is supposed to remind you of the equal sign without
asserting that it’s an equivalence relation before that fact is established.
To define an equivalence relation is to say, “Here are the terms by which I want to
think of different things as the same.” We are literally overloading equality with a specific
implementation. As long as the equivalence relation satisfies these three properties, you
rest assured it has the most important properties of the equality operator.
Let’s do a simple example with R. Let a ∼ b if a − b ∈ Z, and a ̸∼ b otherwise. Check
that this indeed satisfies the three properties of an equivalence relation. This equiva-
lence relation declares that −1/2, 1/2, 3/2, 5/2 are all equivalent, as are −2, −1, 0, 1, 2.
But 1/2 is not equivalent to 1. We call the set of all things equivalent to one object an
equivalence class. So in this case Z is an equivalence class, as is the set of half-fractions
{. . . , −3/2, −1/2, 1/2, 3/2, . . . }. An exercise to the reader: show that given a set X and
an equivalence relation ∼, the equivalence classes partition X into disjoint subsets—i.e.,
every x ∈ X is in exactly one equivalence class. No two classes may overlap.
3
Most math books introduce the generic notion of a relation, and then use relations to define functions. We’ll
instead use functions as the primitive type and jump straight to an equivalence relation without defining
relations at all.
132
Back to our example with R, the quotient R/∼ has a simpler representation. Since
equivalence classes partition R, and every real number shows up in some equivalence
class, we can simply identify each equivalence class in R/∼ with our favorite “represen-
tative” from that class.
Concretely, let’s choose the representative from each class in R/∼ that’s between 0
and 1. For the equivalence class {. . . , −2/3, 1/3, 4/3, 7/3, . . . }, we choose 1/3 as the
representative. Some authors like to abbreviate the equivalence class represented by a
particular element (say, 1/3) using the notation [1/3], so that [1/3] = [−2/3] = [7/3] are
all the same equivalence class. If we also recognize that [0] = [1], then we can summarize:
one can make geometric statements—requires extensive groundwork beyond the scope
of this book. You’ll know you’re treading in these waters if you hear the term “manifold”
or “topology.”
Nevertheless, equivalence relations will be meaningful even in less technical settings,
such as vector spaces (Chapter 10) and groups (Chapter 16). There the structure of the
function defining the relevant equivalence relations are algebraic in nature. This is all
to explain the primary tool mathematicians use to assert that they want to consider two
different things to be the same in a principled manner. You override equality, show it
meets standards of deceny, and then introduce it to your friends.
We can now trivially return to set products. Define the sets
Now define an equivalence relation on L ∪ R so that (a, (b, c)) ∼ ((a, b), c) for any
a, b, c. The resulting quotient (L ∪ R)/∼ is in bijective correspondence with Z.
Another useful example is when working with modular arithmetic. Working in Z,
define a ∼n b if, to use programming syntax, a % n == b % n. Equivalently, a ∼n b if
and only if a−b is a multiple of n. The quotient space for this equivalence relation is called
Z/nZ (where nZ is a shorthand for multiples of n; we’ll revisit this in Chapter 16). The
equivalence relation for modular arithmetic is usually denoted with an operator paired
with “mod n,” as in
a ≡ b mod n
Arithmetic modulo n shares most properties with normal arithmetic on integers, which
makes it extremely convenient. For example, a complex expression like 83000 is extremely
1500
simple mod 9. From 8 ≡ −1 mod 9, you get 83000 ≡ (−1)3000 ≡ ((−1)2 ) ≡ 1
mod 9. This tells you that 83000 is one plus a multiple of 9. Similar tricks with conve-
niently chosen moduli can extract useful information about 83000 without computing it
exactly.
Beyond allowing the study of new structures or enabling convenient computations,
equivalence relations and quotients reduce the mental burden of overriding equality. You
establish once that there’s an equivalence relation, and you pick which new operations
you want to define and prove they’re well-defined in terms of the equivalence classes.
Once that’s done, you can safely continue your mathematical enterprise suppressing the
type difference between [x] and x (in fact, after defining a quotient and proving its well-
definition, mathematicians immediately drop the brackets). As we saw with modular
arithmetic, you can also freely choose the most advantageous equivalence class represen-
tative for your task, often eliminating costly computation. It’s similar to the program-
mer’s adage: work hard now to allow yourself to be lazy later. We set up equivalence
134
relations that focus a mental laser on the aspects we care about. Eliminate annoying and
irrelevant computations, or turn them into tail calls!
Chapter 10
Linear Algebra
There is hardly any theory which is more elementary [than linear algebra], in spite of the
fact that generations of professors and textbook writers have obscured its simplicity by
preposterous calculations with matrices.
– Jean Dieudonné
For a long time mathematicians focused on studying interesting sets, like numbers and
solutions to various equations. In Chapter 6 we saw graphs, which you can think of as
an interesting kind of set. In Chapter 8 we saw sets of numbers (sequences) and sets of
pairs of numbers (functions R → R).
One could spend a lifetime studying interesting graphs or interesting sets of numbers.
However, more recent trends in mathematics have shifted the main focus from studying
sets with interesting structure to studying functions with interesting structure.1
To ease into it, let’s first consider the familiar concept of a compiler. A compiler is a
function mapping the set of programs in a source language to the set of programs in a
target language. Often the target is assembly. Too often it is Javascript. These functions
preserve structure! Namely, a compiler preserves the semantic behavior of a valid input
program in the target language when you run it.
Moreover, a computer program written in a compiled language like C is truly only de-
fined by the behavior of the compiler. This is never more visible than when dealing with
language forms that have “undefined behavior,” on which different compilers produce
programs that behave in myriad unanticipated ways. Languages like C, in which behav-
ior can vary depending on the arbitrary contents of uninitialized memory, widen such
pitfalls. A single program, compiled only once for the same target machine, depends on
the behavior of other programs running on the same machine beyond its own code.
Of course, this isn’t how we want to work with programs. We want to eliminate this
tenuous disconnect as much as possible. Techniques addressing the problem, such as
virtual machines,2 are impressive feats of engineering. The ideal result is that we may
1
In Chapter 8 we did study functions with interesting structure, i.e., differentiable functions, but we didn’t
describe them as structure preserving transformations.
2
I admit, I am not an expert at low-level architecture. The one good example of this I have is the LLVM
135
136
peacefully ponder programs in their most natural environment: the semantics defined by
a language’s documentation.
Just as the burden of manually managing registers encumbered programmers of
decades past, these issues of disparate compilers and uninitialized memory are digres-
sions. It’s nuance in the program-text-to-runtime transformation that we don’t want to
bother with. We judge programs by their semantics: two programs are logically the same
if they behave the same on every input. This understanding shouldn’t depend on idiosyn-
crasies of compilers or varying environments, and we feel unhygienic when it does.
In mathematics, when complexity and notational grime builds up we use essentially
the same tool: abstraction. We add a layer of indirection that allows us to write argu-
ments that say, “these two things are the same” in the context that matters for the task
at hand, and we exhibit bijections and equivalence relations to formalize the connection.
This allows us to identify and isolate structure in new settings, and mentally disregard
impertinent information.
The vector space is a foundational example of such structure. It’s the basic object of
study in linear algebra, a subfield best studied from this function-centric perspective. The
main tool that we use to relate two vector spaces is the linear map. As we will see, linear
maps have a useful computational representation called matrices (singular, matrix). Ma-
trices are “compiled” representations of a linear map in a particular environment (looking
ahead, the particular choice of a basis for the vector space). The magic appears when we
deeply understand how the operations on matrices translate back and forth to operations
on linear maps, and how it all relates to geometry.
Better yet, because linear algebra is relatively elementary, one can appreciate it without
years of prior study. The only machinery we need is the working terminology of sets and
functions. And finally, linear algebra is obscenely practical. The application we’ll see
in this chapter, singular value decomposition, is a staple of data science and machine
learning.
The second, more practical goal of this chapter is to prepare us for multivariable cal-
culus and optimization. These subjects use vectors, vector spaces, and linear maps as
primitive types.
So let’s jump in.
f (x + y) = f (x) + f (y)
Simple, right? There’s something missing from this, so take a moment to identify what
that is.
The problem is that we don’t know what “+” means in this context. Because I used the
+ symbol you may have guessed that A and B are sets of numbers, but this need not be
the case. Instead, we’ll generalize the properties of addition that we care about, and the
result will be called an abstract vector space. Any set might conceivably be a vector space,
and we call the elements of a vector space vectors. Let’s see a mostly complete definition
that establishes the basic rules of a vector space. If you want to prove a set with a chosen
+ is a vector space, you need to establish that all these properties hold.
Definition 10.1. A set V is called a vector space over R if it has two operations + and ·
with the following properties:
2. + obeys all the identities you expect it to, for example, that v + w = w + v and
(v + w) + x = v + (w + x).
3. + and · distribute and “commute” with each other, i.e., identities you expect to be
true from arithmetic, like c · (v + w) = c · v + c · w, are required.
4. There is a special vector denoted 0 in V , which acts like zero should with respect
to addition. In particular, 0 + v = v for every v.
6. Finally, if I take 0 the scalar, and multiply it by any vector v, I must get the zero
vector as a result. If I make the zero vector bold,5 this is the same as requiring
0 · v = 0 for every v ∈ V . Likewise, 1 · v = v for all v ∈ V .
4
Another word commonly used here is that V is closed under this operation: applying + to vectors in V stays
in V . We ensure this by stating the codomain of + is V , but it is a more stringent requirement if the vector
space is built from a subset of some well-known set.
5
Some authors write all vectors bold, but I will only do it when disambiguation is needed. More often than
not the choice of letters suffices, u, v, w, x, y, z for vectors and a, b, c or Greek letters for scalars.
138
This is a monumental definition, and it’s not even the most general definition (see the
Chapter Notes for the most rigorous definition). There are a few things I want to remark
about why the definition is what it is, but before we do let’s write down some examples.
The simplest vector space is R, with R also being the scalars. In this case vectors are
just numbers, + is addition of real numbers, and · is multiplication of real numbers. The
number zero is the identity and the zero vector. Nothing about this should be surprising.
A more interesting example is one we’re familiar with from Chapter 2, polynomials.
Call V the set of all polynomials of a single variable. If t is our variable then 1 + t ∈ V
as well as 7 (a degree-zero polynomial) and πt + 700t99 . The operation + is defined by
adding coefficients term-wise, and c · p(t) by scaling each coefficient of p by c. The zero
polynomial, p(t) = 0 for every t, is the zero vector.6 As an aside, the secret sharing ap-
plication from Chapter 2 can also be understood and proved by appealing to polynomials
as a vector space; the evaluation-at-a-point function evala (p) defined by p 7→ p(a) is a
linear map. See the exercises for an exploration of this.
Even more general is the vector space of all functions f : X → R for any set X. As
an exercise to the reader: go through the conditions from Definition 10.1 and figure out
what + and · could mean. There should only be one natural and simplest option. As a
sepcific example, the space of all differentiable functions f : R → R is a vector space, and
the derivative operation f 7→ f ′ is a distinguished linear map for that space.
The final example is Rn = R × R × · · · × R, the set of all tuples of length n of real
numbers. The elements of Rn are vectors in the sense the reader is probably used to. A
vector is just a list of numbers, or in many programming languages a list whose elements
have the same type. But for us vectors need these extra operations + and ·, so let’s define
them now. The operation + on tuples is entry-wise addition. This means
Similarly, c · (a1 , . . . , an ) = (ca1 , . . . , can ), where on the right hand side the multipli-
cation happening in each coordinate is the usual product of real numbers. The zero vector
is (0, 0, . . . , 0), and the inverse of (a1 , . . . , an ) is −1 · (a1 , . . . , an ) = (−a1 , . . . , −an ).
All of the operations behave nicely because they’re applied independently to each entry,
and each entry is just arithmetic in R, which has all of the desired properties.
As with all of the examples of vector spaces, the definition of the specific vector space
is entirely contained in the implementation of the operations + and ·. The miniature
proofs that +, · have the needed properties constitute a proof that the chosen implemen-
tation is a vector space. This proof is rarely a challenge, as Rn is our main workspace for
applications, so we’ll brush over most of those details.
6
Readers who know a bit about abstract algebra and number theory will protest: this is only true when
the set of coefficients of the polynomial has certain properties. One could, for instance, define a family of
polynomials where the coefficients are binary and addition behaves like binary XOR on each term. In this
case there are polynomials with nonzero coefficients that are zero on all (binary) inputs. No such stumbling
block exists for real numbers.
139
(1, 2)
(-2, 1)
(1, -1)
With a few examples of vector spaces in our minds, let’s return to why Definition 10.1
is the way it is. The reason is that it’s the simplest way to define what addition means in
a context that is useful for geometry (defining an “algebra” for geometric objects). The
first thing a geometry needs is a set of points in space. Note that I’m using the words
“points” and “space” informally, to appeal to your intuition that a grain of dirt “occupies
a point” in the real world. A vector space is meant to appeal to that intuition, but with
“point” replaced by “vector,” and “space” just meaning the set of all vectors we allow.
Returning to our vector space, points are indeed simply vectors in Rn . In Figure 10.1,
we draw some vectors in R2 for the ease of visualization. For a reason we’ll explain
shortly, we also draw these points as arrows from the zero vector (the zero vector is
called the “origin,” in graphical parlance).
The “position” of a point specified by such an arrow is at the non-origin end of the
drawn line segment. This choice of drawing from the origin also implies that every vector
has a direction, which further implies there will be distances, angles, etc. More immedi-
ately, we can add two vectors by adding their coordinates. Geometrically this involves
moving the tail of one arrow to the head of the other and drawing an arrow from the
origin to the end of the resulting path. In Figure 10.2, we can add the two solid vectors
to get the dashed vector. The transparent dotted vector shows this geometric motion of
“moving the tail to the head.”
In geometry we like lines, and scaling vectors allows us to have them here too. A line
is the set of all ways to scale a single nonzero vector. In symbols, a line through the origin
and v is the set Lv = {c · v : c ∈ R}. For example, drawn in Figure 10.3 you can scale
v = (1, 2) by a factor of 2 to get (2, 4), shrink it down to (0.5, 1), or scale it negatively
to (−2, −4). The set of all possible ways to do this gives you all the points on the line
140
Figure 10.2: An example of vector addition. The dashed vector is the sum of the two solid
vectors.
v = (1, 2)
Lv = {c·v : c ∈ R }
{w + c·v : c ∈ R }
Figure 10.4: An example of a line as the span of a vector, shifted away from the origin by
a second vector.
complete the geometric picture by the end of the chapter, but for now we see that connec-
tions between vectors and geometry make sense. We’ll keep this geometric foundation
in mind while dealing with linear maps more abstractly (which, to be frank, is the hard
part of linear algebra). Our task for now is to study where Definition 10.1 takes us.
Definition 10.2. Let X, Y be vector spaces with +X , ·X being the operations in X and
+Y , ·Y in Y . A function f : X → Y is called a linear map if the following two identities
hold for every v, w ∈ X and every scalar c ∈ R:
1. f (v +X w) = f (v) +Y f (w)
2. f (c ·X v) = c ·Y f (v)
This notation +X , ·X burns my eyes, so we’ll drop it and understand that when I say
f (v + w) = f (v) + f (w), I mean that the + on the left hand side is happening in X and
the + on the right hand side is happening in Y . Likewise for scaling, f (cv) = cf (v). Any
other interpretation would be a fatal type error. Moreover, as we go on I’ll begin to drop
the · in favor of “juxtaposition”, so that if a is a scalar and v is a vector, it’s understood
that av = a · v. I will use the dot only when disambiguation is needed.
142
Here’s a simple example of a linear map. Let X be the vector space of polynomials,
and Y = R. Define the evaluation at 7 function, which I’ll denote by eval7 : X → R, as
eval7 (p) = p(7). Let’s check the two conditions hold. If p, q are two polynomials, then
And we can distribute and rearrange all these terms to get exactly p(7)+q(7). Likewise,
eval7 (c · p) = c · p(7). Since the number 7 was arbitrary, the same logic shows that evala
for any scalar a ∈ R is a linear map.
A second and completely arbitrary example is the map f : R3 → R2 defined by
(a, b, c) 7→ (−2a + 3b, c). Verify as an exercise that this is a linear map.
For the rest of the chapter, linear maps are the only kind of function we care about for
vector spaces. The reason, which we’ll spend the rest of the chapter trying to understand,
is that linear maps are the maps which preserve the structure of a vector space. Indeed,
we defined them to preserve the two operations that define a vector space! But as we’ll
see this covers all the bases. For example, the following fact is true for any vector space:
linear maps preserve the zero vector.
Proposition 10.3. If X, Y are vector spaces and f : X → Y is a linear map, then f (0) =
0.
As I did with + and ·, I’m using the same symbol 0 for the additive identity in both
vector spaces. In light of this fact it’s not so surprising: if there’s a “zero” in every vector
space, and every linear map (the only maps we care about) preserve the “zero,” then we
can really call it “the” zero.
The proof of this fact is direct. That is, we’ll directly use the definition of a linear map
and a vector space, and the proof will just “fall out” from the definitions. In fact, I’ll give
two proofs. To distinguish 0 the vector from 0 the scalar, I’ll make the vector bold, like 0.
Proof 1. Let’s use the fact that · is preserved by a linear map. First, f (0) is the same as
f (0 · 0). Since f is linear, this is the same as 0 · f (0). But 0 · v = 0 no matter what v is.
Putting these two together,
f (0) = f (0 · 0) = 0 · f (0) = 0,
which is what we wanted to prove.
Proof 2. This proof is similar, but uses the fact that 0 + 0 = 0. Indeed,
143
expressed in terms of your basis, you can get any vector by using (c, d) = c(1, 0)+d(0, 1).
Convince yourself of this by expressing (2, −1) in terms of our example basis. By the way,
I calculated the fractions 5/11 and 4/11, by writing down the equation
3a − b = 1
4a − 5b = 0
Solving for a and b gives a = 5/11 and b = 4/11. The fact that this works for most
pairs of vectors you can think of is no coincidence, but we’ll return to that later in the
chapter. The point for now is that there are many possible bases (“BAY-sees”, the plural
of basis) of a vector space, and each basis allows you to write any vector in the vector
space by summing and scaling the vectors in the basis.
The second note is that a basis can be thought of as an alternative coordinate system for
a vector space. In R2 we usually think of coordinates for points by specifying their x- and
144
v = e1 + 2e 2
e 2 = (0, 1)
e 1 = (1, 0)
Figure 10.5: Assembling a point (1, 2) as the linear combination of basis vectors repre-
senting x and y coordinates.
v = (–1/3)v1 + (–5/3)v2
v 1 = (2, -1)
v 2 = (-1, -1)
Figure 10.6: Assembling a point (1, 2) as a linear combination of two new basis vectors.
y-coordinates (i.e., using the standard basis, e1 , e2 ). However, once we’re fluid with linear
algebra we realize that saying “the x- and y-coordinate” is an arbitrary choice, and one
could just as easily have chosen v1 = (2, −1), v2 = (−1, 1) as a basis and expressed the
same points by their v1 -coordinate and v2 -coordinate, the coefficients needed to write a
point using sums-and-scales of v1 , v2 . In this case, the vector in the diagram in Figure 10.6
is represented as (− 31 , − 53 ).
145
∑
n
x = a1 v1 + · · · + an vn = ai vi
i=1
In particular, any way one could “add and scale” vectors reduces to this form, provided
one is willing to distribute scalar multiplication over addition, expand, and group all the
terms. This is the standardized way to express the existential claim that x can be “built”
up from the vi , like how a polynomial has a regularized form, even though polynomials
generically encode all ways to add and multiply a number.
A bit of common terminology is the span of a set B of vectors, which is the set of all
linear combinations of those vectors. That is,
span(v1 , . . . , vk ) = {a1 v1 + · · · + ak vk : ai ∈ R}
When we said informally that a basis is a set of vectors from which you can “get all
vectors in V ,” we really meant that a basis is a set of vectors whose span is V . This is
almost complete, but we need minimality.
This definition makes it clear why we don’t say things like “{(1, 0), (2, 0), (3, 0), (0, 1)}
is a basis for R2 .” Because while it does span R2 , it includes superfluous information. We
want our definitions to capture a notion as efficiently as possible.
We will have a lot more to say about vector space bases. Many insights and applications
of linear algebra revolve around computing a clever basis of a vector space. But first we
need a few more tools. One of the most important definitions in elementary linear algebra
is related to the existence and uniqueness of linear combinations.
Definition 10.6. Let V be a vector space, and v1 , . . . , vn ∈ V be nonzero vectors. The set
{v1 , . . . , vn } is said to be linearly independent if no vi is in the span of the other vectors
{vj : j ̸= i}. Informally we will also say the list v1 , . . . , vn is linearly independent,
though the ordering of the vectors has no consequence.
146
Another, equivalent definition of linear independence, and one that’s easier to work
with in proofs, is that the only way to write the zero vector as a linear combination of
v1 , . . . , vn is if all the coefficients ai are zero. In other words, there is no nontrivial way
to write zero as a linear combination.
0 = a1 x1 + · · · + an xn ⇒ ai = 0 for all i
Another equivalent (but seemingly more restrictive) way to express linear indepen-
dence is to say that B is linearly independent if every vector in span(B) has a unique
expression as a ∑ linear combination
∑ of vectors in B. Indeed, ∑ if some vector x could be
written as both ni=1 ai vi and ni=1 bi vi , then the difference ni=1 (ai − bi )vi would be
a nontrivial way to write the zero vector! It’s nontrivial because some ai and bi have to
be different, by our assumption that x has two different representations.
For example, in R2 the set {(1, 0), (0, 1)} is linearly independent, as is the set
{(3, 4), (−1, −5)}. However, {(1, 0), (3, 4), (−1, −5)} is not linearly independent (we
call it linearly dependent to avoid the double-negative) because, as we saw, (1, 0) is a
linear combination of the other two vectors.
Linear independence provides a different perspective on the concept of a basis, which
will lead us to Theorem 10.8 and allow us to have a coherent definition of a vector space’s
dimension.
Theorem 10.7. Let V be a vector space. Let B = {v1 , . . . , vn } be a set of linearly indepen-
dent vectors in V , and suppose it’s maximal in the sense that if you add any new vector to
B, then the resulting set is linearly dependent. Then B is a basis for V .
0 = a0 x + a1 v1 + · · · + an vn ,
and not all the ai are zero. Note a0 is the cofficient of x, the newly added vector.
Moreover, a0 ̸= 0 since, if it were, that would provide a nontrivial linear combination
of 0 using only the vectors in B, which contradicts the assumption that B is linearly
independent.
We can then safely rearrange to solve for x:
1
x=− (a1 v1 + · · · + an vn )
a0
147
This proves that x ∈ span(B). Beacuse x was chosen arbitrarily from V , this proves
that V ⊂ span(B). Since span(B) ⊂ V by definition of a vector space,7 we’ve shown
span(B) = V (cf. Definition 4.2 for a reminder on using subsets to prove set equality).
Second, we need to show that B is minimal with respect to spanning V . Indeed, you
cannot write v1 as a linear combination of v2 , . . . , vn , because v1 , . . . , vn form a linearly
independent set! Hence, removing v1 from B would make the resulting set not span V ;
(v1 ̸∈ span{v2 , . . . , vn }). The same goes for removing any vi .
The above proof makes it clear that for any x ̸∈ B, the statements “x ∈ span(B)”
and “B ∪ {x} is a linearly dependent set” are logically equivalent. This theorem also
provides a simple algorithm to construct a basis (though it’s not quite concrete enough
to implement). Start with B = {}. While there exists some vector not in span(B), find
such a vector and add it to B. When this loop terminates, B is a basis.
With linear independence, spanning, and bases in hand, we can define dimension and
finally the matrix.
10.4 Dimension
While the concept of a basis seems relatively underwhelming at first, it unlocks a world
of use. The first thing it allows us to do is measure the size of a vector space. We can do
this because of the following fact:
Theorem 10.8. Let V be a vector space. Then every basis of V has the same size.
Proof. This proof hinges on the claim that if U = {u1 , . . . , un } is a list of n linearly
independent vectors in V (perhaps not maximal), and W = {w1 , . . . , wm } is a list of m
vectors which span V (perhaps not minimally), then n ≤ m. The theorem follows because
if U and W are both bases, then they are both independent and spanning, meaning both
n ≤ m and m ≤ n, so n = m. To prove the claim, we use an iterative algorithm that
transforms W into U as much as possible.8 This will work by replacing each item from
W by one from U until we run out of vectors from U . Connecting to the fancy and useful
terminology from Section 4.1, we’re building an injection U → W one element at a time,
and the existence of an injection U → W implies |U | ≤ |W |.
Start by taking u1 , removing it from U , and adding it to W . By the fact that W spans
V , we can write u1 as a linear combination of the wi in which some coefficient, say a1
for w1 , is nonzero.9
7
B ⊂ V is a set of vectors, and the closure properties of a vector space ensure they stay in V .
8
The only other proof of this theorem I’m aware of uses all kinds of needless machinery regarding homoge-
neous systems of linear equations. Algorithms save the day!
9
This is another example of the mathematical sleight of hand called “without loss of generality.” What we
really mean is: take whichever wi has a nonzero coefficient, and use that going forward. However, since
we’re planning to do this step iteratively, if we wanted to be precise we’d have to keep track of which indices
were selected, and writing that down is painful (with a sub-index like wi1 , wi2 , . . . ). Instead we say, “let’s
148
u1 = a1 w1 + a2 w2 + · · · + am wm
This means we can rearrange the above to solve for w1 in terms of u1 , w2 , w3 , . . . , wm ,
and hence we can remove w1 from W ∪{u1 } without changing the fact that what remains
spans V . Call this resulting set W1 = {u1 , w2 , w3 , . . . , wm }, and call U1 = V − {u1 }.
Repeat this process with u2 , forming W2 , U2 , and keep doing it until you get to Un = {},
and Wn . In each step we can always remove a new wi —that is, we can find a wi with
a nonzero coefficient—because all of the u’s that we’re adding are linearly independent,
while Wi is still spanning. So the algorithm will reach the n-th step, at which point either
all of W is replaced by all of U (i.e. n = m), or there are some wi left over (n < m).
Definition 10.9. The dimension of a vector space V is the size of a basis. Denote the
dimension of V by dim(V ).
Theorem 10.8 hence provides well-definition for the notion of the dimension of a vector
space. Dimension is an invariant, because it does not depend on which basis you choose.
This reinforces our intuitive understanding of what dimension should be for Rn , i.e., how
many coordinates are needed to uniquely specify a point. So R is one-dimensional, the
plane R2 is two-dimensional, physical space at a fixed instant in time is 3-dimensional,
etc. The dimension of the space doesn’t (and shouldn’t) depend on the perspective, and
for linear algebra the perspective is the choice of a basis.
We end this section with the notion of a subspace.
n-dimensional vector space V , you can iteratively add vectors not in the span of A until
the resulting set spans all of V . This process, though not well-defined algorithmically, is
existentially possible, and it’s called extending A to a basis of V . In Chapter 12 we’ll see
a concrete algorithm for it called the Gram-Schmidt process, which produces extra useful
properties of the resulting basis.
10.5 Matrices
Now we can finally get to the heart of linear algebra.
Linear maps seem relatively complicated at first glance, but in fact they have a rigid
structure uniquely determined once you fix a basis in the domain and codomain. Let’s
draw this out and discover what that structure is. In this section English letters v, w, x,
and y will always be vectors, while Greek letters α, β, and γ will be scalars.
Start with a linear map f : V → W , maybe given by some formula. We want to
compute f on an input x. You choose a basis {v1 , . . . , vn } and a basis {w1 , . . . , wm } for
V and W , respectively.10 Now fix x ∈ V . Since the vi form a basis, there is some way to
write x as a linear combination of the vi , say
x = α1 v1 + α2 v2 + · · · + αn vn
Crucially, f is a linear map, so we can break f (x) up across the input.
I’m using familiar array-index notation to hint at where we’re going. The structure of
the matrix will fall out from our analysis. The point of the notation is that the first index,
the i in β[i, j], tells you which basis vector vi of V you’re mapping through f to get yi ,
and the second index j identifies the coefficient of the basis of W in the output (that of
wj ).
To write f (x) in terms∑ of the basis for W , we substitute the expansion of each f (vi )
into the formula f (x) = i αi f (vi ).
correspond to the basis of V , and the rows correspond to basis vectors of W . We call this
construction M (f ), and the mapping f 7→ M (f ) will be a bijection from the set of linear
maps (all using the same fixed basis) to the set of matrices. The underscores denote the
part of the construction I haven’t specified yet.
v1 v2 ··· vn
w1 _ _ ··· _
w2 _ _ ··· _
M (f ) = . . .. .. ..
.. .. . . .
wm _ _ ··· _
The entries of a column i are defined as the expansion of f (vi ) in terms of the wj . That
is, take the basis vector vi for that column, and expand f (vi ) in terms of the wj , getting
f (vi ) = β[i, 1]w1 + · · · + β[i, m]wm . The numbers β[i, j] (where j ranges from 1 to m)
form the i-th column of M (f ).
v1 v2 ··· vn
w1 β[1, 1] β[2, 1] ··· β[n, 1]
w2 β[1, 2] β[2, 2] ··· β[n, 2]
M (f ) = . . .. .. ..
.. .. . . .
wm β[1, m] β[2, m] ··· β[n, m]
You will have noticed that we’ve flipped the indices β[i, j] from their normal orienta-
tion so that i is the column instead of the row. This is an occupational hazard, but we
trust the competent programmer can handle index wizardry. One clever way to express
the construction of M (f ) with fewer indices is like this:
v1 ··· vn
w1 | |
..
M (f ) = . f (v1 ) ··· f (v ) n
wm | |
The vertical lines signal that f (vi ) is “spread out” over column i by its expansion in
terms of {wj }.
The computational process of mapping an input vector x to f (x) is called a matrix-
vector product, and it works as follows. First, write x in terms of the basis for V as before,
x = α1 v1 + · · · + αn vn , this time writing the coefficients in a column:
α1
α2
x= .
..
αn
152
Sometimes people call this a “column vector” to distinguish it from the obvious ana-
logue of writing the entries in a row. Let’s just call it a vector. Now to compute f (x) using
M = M (f ), you write M and x side by side (as if the operation were multiplication of
integers).
v1 v2 ··· vn
w1 β[1, 1] β[2, 1] ··· β[n, 1] α1
w2 β[1, 2] β[2, 2] ··· β[n, 2]
α2
Mx = . . .. ..
.. ..
.. .. . . . .
wm β[1, m] β[2, m] ··· β[n, m] αn
Recall, the output is a vector f (x) = z ∈ W , which, if written in the same col-
umn style as x, would have m entries. We’ll denote these entries by the Greek gamma
(γ1 , . . . , γm ) = z.
v1 v2 ··· vn
w1 β[1, 1] β[2, 1] ··· β[n, 1] α1 γ1
w2 β[1, 2] β[2, 2] ···
β[n, 2] 2 γ2
α
Mx = . . .. .. .. .. = .. = z
.. .. . . . . .
wm β[1, m] β[2, m] ··· β[n, m] αn γm
The computation to get from the left-hand side of this equation to the right is the
same as how we grouped terms to get the coefficient of wi earlier. Take the row of M
corresponding to wi , compute an entrywise product with x, and sum the result.11
The second:
( )
3 7 3
−1 −−−→ −2 −1
7 −2 0
4 0 4
−−−→ b = 7 · 3 + (−2) · (−1) + 0 · 4 = 23
It’s easy to get lost in the notation and miss the bigger picture. We’ve defined a me-
chanical algebraic process for computing the output f (x) ∈ W from the input x ∈ V ,
provided we have chosen a basis for V and W and provided we can express vectors in
terms of a given basis. This is a new type of “multiplication” operator that has very nice
properties. For example:
Theorem 10.13. Let V, W be vector spaces and f, g : V → W two linear maps. The
mapping f 7→ M (f ) is linear. That is, if f + g is the function x 7→ f (x) + g(x), then
M (f + g) = M (f ) + M (g), and likewise M (cf ) = cM (f ) for every scalar c.
Beyond being linear, the mapping f 7→ M (f ) is a bijection (again, for a fixed choice of
a basis). Injectivity: every f maps to a different M (f ), since f is completely determined
by how it acts on the basis, and two matrices M (f ) and M (g) with the same entries act
the same on a basis. If that’s not convincing enough, consider M (f − g) = M (f ) +
(−1)M (g). If that’s the matrix of all zeroes, then, because linear maps preserve zero,
f − g must be the zero map. Surjectivity: if you specify a matrix A, the f mapping to A
is the one with f (vi ) equal to the linear combination defined by the i-th column of A.
This bijection allows us to say that linear maps and matrices are “the same thing” with-
out angry mathematicians throwing chalkboard erasers at us.13 The matrix representa-
tion of a linear map is unique, so we can freely switch back and forth between a linear
map and its matrix, provided the basis does not change.
12
This generally means the proof is not complicated, but it may contain a mess of notation required to write it
out properly and doesn’t make for good reading. In any event, the statement of the theorem is the enlight-
ening part, while the proof is purely mechanical.
13
This actually happened to a friend of mine, and there’s an apocryphal tale of the irascible wunderkind
Évariste Galois, who, during an admittance exam to a prestigious French universty, was so frustrated by
the examiner’s inability to recognize his genius that he threw a chalkboard eraser at him. Needless to say,
Galois was not admitted.
154
M (g ◦ f ) = M (g)M (f ),
where g ◦ f denotes the function composition x 7→ g(f (x)), and M (g)M (f ) denotes
matrix multiplication.
And we aren’t yet done doing this with linear algebra! Because while we have said how
to compute once you have chosen a basis, we haven’t discussed the means of actually
finding such bases. Many applications of linear algebra are based on computing a useful
basis, and that will be the subject of both this chapter’s application and the next. As such,
it behooves us to dive deeper.
3a − b = 1
4a − 5b = 0
Here v1 = (3, 4) and v2 = (−1, −5) were the two vectors acting as our basis, and
we wanted to express the vector x = (1, 0) in terms of them. The variables a, b are the
unknown coefficients of v1 , v2 we solved for.
One important thing to point out: even though we want to write x = (1, 0) in terms
of v1 , v2 , we actually had a representation of x in terms of a basis already! To even
write x down in this coordinate-form, we implicitly used the standard basis for R2 , e1 =
(1, 0), e2 = (0, 1). In the example above x = 1e1 + 0e2 . In order to express x in terms of
a given basis, you have to have already expressed it in terms of some (maybe easy) basis.
This strategy generalizes. Let’s say we have an n-dimensional vector space V with two
bases:
E = {e1 , e2 , . . . , en }
B = {v1 , v2 , . . . , vn }
Say E is the “easy” basis, often the standard basis in Rn , and B is the target basis we
wish to express some vector x = (α1 , . . . , αn ) in. Write down a system of n equations
with n unknowns, as follows. I’m going to use the notation (e.g.) v2,4 to denote the 4th
entry of v2 , which is the standard way to do double-indexing in mathematics. Note that
all symbols here represent numbers in R.
β1 v1,1 + · · · + βn vn,1 = α1
β1 v1,2 + · · · + βn vn,2 = α2
..
.
β1 v1,n + · · · + βn vn,n = αn
We can rewrite the system of equations as a single matrix equation.
156
v1,1 · · · vn,1 β1 α1
v1,2 · · · vn,2 β2 α2
.. .. .. .. = ..
. . . . .
v1,n · · · vn,n βn αn
This makes it clear that expressing a vector in terms of a basis, while originally posed
as solving a system of equations, really is computing the unknown input of a linear map,
y = (β1 , . . . , βn ), given a specified output x = (α1 , . . . , αn ). It’s worthwhile to break
this down a bit further.
The matrix A = (vi,j ) defined above converts a vector from the domain basis to the
codomain basis. The domain basis—which indexes the columns of A—is the target basis.
It’s the one we want to express x in terms of. The codomain basis—indexing the rows—is
the “easy” basis E, the basis used to write x = (α1 , . . . , αn ). Finally, y is the vector of
coefficients (β1 , . . . , βn ) that expresses x in terms of v1 , . . . , vn , which is what we want.
This entire matrix-vector equation Ay = x expresses the conversion of a vector in the
hard basis to a vector in the easy basis. This is mildly strange, since if we think of A as
the matrix of a linear map, that linear map is x 7→ x, a no-op! Much like a change of a
number basis from binary to decimal or hexadecimal, the semantic meaning of the input is
unchanged by the operation, just its data representation and interpretation. Linear maps
are semantic, matrices are data interpretations. Nevertheless, these so-called change of
basis matrices are crucial to every computational endeavor. In particular, to write an
expression for x expressed in the basis (v1 , . . . , vn ), we simply form the change of basis
matrix P whose columns are the vi , and write y = P −1 x.
As an aside, it should be intuitively clear that P has an inverse as a function: every
vector has exactly one representation in terms of a basis. Even if we didn’t know how
the conversion works computationally, it must be a bijection. More usefully, and not that
we have a matrix multiplication operation, the inverse of a matrix A is defined in terms
of an identity. The identity matrix, denoted In or 1n , is the square n × n matrix defined
by having 1’s on the diagonal and zeros elsewhere.
1 0 0 ··· 0
0 1 0 · · · 0
0
In = 0 0 1 · · ·
.. .. .. . . ..
. . . . .
0 0 0 ··· 1
The matrix multiplication operation ensures that In A = AIn = A for any matrix A.
Then the inverse A−1 , if it exists, is defined as the matrix B for which AB = BA = In .
As an exercise, prove that if a linear map is a bijection, then its inverse is also a linear
map, and the linear-map-to-matrix correspondence preserves inverses.
More generally, a pattern used everywhere in mathematics is to change basis for a
limited-scope operation. In other words, given a change of basis matrix P which changes
157
from basis B to basis E, and some linear map A expressed in terms of E, you can apply
A to a vector w expressed in B-coordinates as
P −1 AP w
This expression works in sequence: express w in basis E, apply A, and convert the
result back to B. The matrix P −1 AP is exactly the linear map for A expressed in terms
of the B basis. It’s also true that any invertible map is a change of basis to some basis
(the basis formed by the columns of the inverse).
This general pattern of doing P −1 AP is called conjugation of A by P . If two matrices
can be equated by conjugation, they are often called similar. I personally hate the term
“similar” because we’re really saying they’re identical. If you look at a laptop on your desk
and then pick it up and hold it sideways above your head, it’s not “similar” to the laptop
on your desk, it’s the same thing from two different perspectives! That’s exactly what
happens when you conjugate a matrix; it may not be ergonomic to type that way, but
it’s the same machine. Taking a cue from Chapter 9, matrix similarity is an equivalence
relation, and the equivalence classes correspond to linear maps.
Now to actually compute P −1 x is a different pickle. From the perspective of a system
of n equations, the standard principle of solving the matrix-vector equation Ab = x by
isolating a single variable, substituting, and solving works, but it’s extremely tedious. To
help with the tedium, mathematicians came up an algorithm called Gaussian elimination
that formalizes the tedium and uses the matrix-form above to help organize. Gaussian
elimination is important, but it’s both inefficient15 and it computes a lot of extra informa-
tion.
Gaussian elimination is a general-purpose algorithm that works no matter what your
basis is. A shrewder approach, which many applications of linear algebra utilize, is to
think hard about the best basis for your intended application, and convert to that basis
once at the beginning of a computation. See the exercises for further references and point-
ers to industry-standard techniques for changing bases, and Chapter 12 for an extended
parable on the value of a good basis.
f −1 (x + y) = f −1 (f (x′ ) + f (y ′ )) = f −1 (f (x′ + y ′ )) = x′ + y ′ .
Proposition 10.16. Let Pm be the vector spaces of polynomials in one variable with degree
at most m. Then Rm+1 ∼
= Pm .
Proof. Let {1, t, t2 , . . . , tm } be the usual basis for Pm , and fix the standard basis of Rm+1 ,
i.e., {e1 , . . . , em+1 }. Define f : Pm → Rm+1 as
f (a0 + a1 t + · · · + am tm ) = (a0 , a1 , . . . , am )
16
In particular, without using the Axiom of Choice, a somewhat unintuitive postulate, one cannot even con-
clude that all infinite dimensional vector spaces have bases! This fact led to an amusing—if somewhat off-
color—t-shirt designed by my undergraduate math club, which emblazoned the slogan, “Pro Axiom of Choice:
because every vector space deserves a basis.”
159
First, f is a linear map: when you add polynomials you add their same-degree coeffi-
cients together, and scaling simply scales each coefficient. Second, f is a bijection: if two
polynomials are different, then they have at least one differing coefficient ∑m (injection); if
(b0 , b1 , . . . , bm ) is a vector in R m+1 , then it is the image of p(t) = k=0 bk tk under f .
This theorem isn’t meant to conclude that polynomials are the same as lists in every
respect. Quite the opposite, a polynomial comes with all kinds of extra interesting struc-
ture (as we saw in Chapter 2). Rather, to phrase polynomials as a vector space is to ignore
that additional structure. It says: if all you consider about polynomials is their linearity,
then they have the same linear structure as lists of numbers. At times it can be extremely
helpful to “ignore” certain unneeded aspects of a problem. As you’ll see in an exercise,
the polynomial interpolation problem relies only on the linear structure of polynoimals.
As such, it can inspire other (perhaps more efficient) techniques for doing secret sharing.
This exploration suggests that all data representations of finite-dimensional vector
spaces can be thought of as lists of numbers. Those numbers are the coefficients of the
basis vectors.
Proof. Let {v1 , . . . , vn } be a basis for an n-dimensional vector space V , and let
{e1 , . . . , en } be the standard basis for Rn . Define f : V → Rn as follows. Let x ∈ V be
the input, write x = α1 v1 + · · · + αn vn , and let f (x) = (α1 , . . . , αn ).
An analogous argument as in Proposition 10.16 shows f is a linear bijection.
∥v – w∥
∥v∥
θ
∥w∥
Figure 10.7: The lengths of the sides of the triangle satisfy the law of cosines.
∑
n
⟨v, w⟩ = α1 β1 + · · · + αn βn = αi βi .
i=1
This formula is special because it has a geometric interpretation. Indeed, it can even be
defined geometrically without any appeal to the basis, which we’ll do now. Note that to
understand this proof requires some “elementary” geometry which we haven’t covered
in this book, namely the idea of a cosine and the law of cosines. If you’re unfamiliar with
these topics, look them up online.
case of the inner product: the norm of a vector v, denoted ∥v∥, is de-
First, a special√
fined as ∥v∥ = √ ⟨v, v⟩. This quantity is the geometric length or magnitude of v. Its
formula, ∥v∥ = α12 + · · · + αn2 , is the generalization of the Pythagorean theorem to n
dimensions.
Theorem 10.19. The inner product ⟨v, w⟩ is equal to ∥v∥∥w∥ cos(θ), where θ is the angle
between the two vectors.18
Proof. If either v or w is zero, then both sides of the equation are zero and the theorem is
trivial, so we may assume both are nonzero. Label a triangle with sides v, w and the third
side v −w as in Figure 10.7. The length of each side is ∥v∥, ∥w∥, and ∥v −w∥, respectively.
Assume for the moment that θ is not 0 or 180 degrees, so that this triangle has nonzero
area.
The law of cosines states that
The left hand side is the inner product of v−w with itself, i.e. ∥v−w∥2 = ⟨v−w, v−w⟩.
We’ll expand ⟨v−w, v−w⟩ using two facts. The first is trivial from the formula, that inner
product is symmetric: ⟨v, w⟩ = ⟨w, v⟩. Second is that the inner product is linear in each
input. In particular for the first input: ⟨x + y, z⟩ = ⟨x, z⟩ + ⟨y, z⟩ and ⟨cx, z⟩ = c⟨x, z⟩.
The same holds for the second input by symmetry of the two inputs.19 Hence we can
split up ⟨v − w, v − w⟩ as follows.
⟨v − w, v − w⟩ = ⟨v, v − w⟩ − ⟨w, v − w⟩
= ⟨v, v⟩ − ⟨v, w⟩ − ⟨w, v⟩ + ⟨w, w⟩
= ∥v∥2 − 2⟨v, w⟩ + ∥w∥2
Combining our two offset equations, subtract ∥v∥2 + ∥w∥2 from each side and get
When I say, “P is true if and only if Q is true,” I am claiming that the two properties
are logically equivalent. In other words, you cannot have one without the other, nor can
you exclude one without excluding the other. Proving such an equivalence requires two
sub-proofs, that P implies Q and that Q implies P . Because logical implication is often
denoted using arrows—“P implies Q” being written P → Q, and “Q implies P ” being
written P ← Q—these sub-proofs are informally called “directions.” So one will prove
an if-and-only-if by saying, “For the forward direction, assume P …and hence Q”, and
“For the reverse/other direction, assume Q…and hence P .” Authors will also often mix in
proof by contradiction to complete the sub-proofs. The combined if-and-only-if is often
denoted with double-arrows: P ↔ Q, and when pressed for brevity, mathematicians
abbreviate “if and only if” with “iff” using two f’s. So “iff” is the mathematical cousin of
a classic Unix command: 2-3 letters and a long man page to explain it.
Let’s prove the if and only if for perpendicular vectors now.
19
We will see in Chapter 12 how these properties become a definition.
162
Proof. For the forward direction, assume v and w are perpendicular. By definition
the angle θ between them is 90 or 270 degrees, and cos(θ) = 0. Hence ⟨v, w⟩ =
∥v∥∥w∥ cos(θ) = 0. For the reverse direction, if ⟨v, w⟩ = 0 then so is ∥v∥∥w∥ cos(θ),
meaning one of ∥v∥, ∥w∥, or cos(θ) must be zero. Perpendicularity is not defined if one
of the two vectors is zero,20 so both vectors must be nonzero and have a nonzero norm.
This leaves cos(θ) = 0. The vectors are perpendicular.
As a side note, we’ll need the fact that two nonzero perpendicular vectors are linearly
independent. Suppose for contradiction that ⟨x, y⟩ = 0 but ax + by = 0 for some scalars
a, b. Suppose without loss of generality that b ̸= 0 (i.e., ax + by = 0 is a nontrivial linear
dependence). In this case, a is also nonzero, since a = 0 implies by = 0, which implies
y = 0, and y was assumed to be nonzero. Then
Definition 10.21. Let v, w be vectors in Rn . The projection of w onto v, denoted projv (w),
is defined as projv (w) = cv where c ∈ R is a scalar defined as21 c = ⟨v,w⟩
∥v∥ .
Let me depict this formula geometrically. Say that v, the vector being projected onto,
is special in that it has magnitude 1. Such a special vector is called a unit vector.22 In this
case the formula defined above for the projection is just ⟨v, w⟩v. Now (trivially) write
w – projv(w)
projv(w)
∑
k
projV (w) = projvi (w).
i=1
The codomain is R8 (if there are only 8 movies, as in this toy example), and the basis
vectors are yUp , ySkyfall , etc. By representing the ratings this way, we’re imposing the hy-
pothesis that the process of rating movies is linear in nature. That is, the map A computes
the decision making process from people to ratings. The coefficients of A(xAisha ) written
in terms of the basis of movies, forms the first column of the matrix in Figure 10.9. It is
also assumed to be one combined function, as opposed to different for each person.
These assumptions should give us pause. Beyond the sociological assumptions made
here, the linear model also grants us strange new mathematical abilities. We started with
a dataset of ratings, which is included in the linear-algebraic world as A(xAisha ), A(xBob ),
and A(xChandrika ). But since we represent movies and people as vectors, we may form
linear combinations. We may construct the movie 0.5yUp + 0.5ySnatch , which we might
think of as the abstract equivalent of a movie that is “half-way” between Up and Snatch.
We may also ask for a “person” whose movie-rating preferences are half-way in between
Aisha and Bob, and ask how this person would rate Amelie. Indeed, the fact that A is a
linear map provides an immediate answer to this question: average the ratings of Aisha
and Bob. The behavior of A on any vector is determined by its behavior on the basis.
We can also create nonsense when we subtract people, or scale them beyond reasonable
interpretations. What would the movie 75yGrease − 8yThor look like? You may conjure a
166
cohesive explanation, but you’d be straining logic to fit the image of gibberish. Very off
brand.
Of course, the goal of a rating system is to predict the ratings of people on movies they
have not seen, based on how two people’s ratings align. So a valid answer is, “we don’t
care about weird linear combinations.” That said, more likely than not your chosen linear
algebraic hammer relies on strange linear combinations. It’s worthwhile to illustrate the
necessary assumptions entailed by imposing linear algebra on a real world problem, and
the curious luggage this stranger brings along.
The central point is that we can represent a movie (or a person) formally as a linear
combination in some abstract vector space. But we don’t represent a movie in the sense
of its content, only those features of the movie that influence its rating. We don’t know
what those features are, but we can presumably access them indirectly through the data
of how people rate movies. We don’t have a legitimate mathematical way to understand
that process, so the linear model is a proxy. What’s amazing is how powerful a dumb
linear proxy can be.
It’s totally unclear what this means in terms of real life, except that you can hope
(or hypothesize, or verify), that if the process of rating movies is “linear” in nature then
this formal representation will accurately reflect the real world. It’s like how physicists
all secretly know that mathematics doesn’t literally dictate the laws of nature, because
humans made up math in their heads and if you poke nature too hard the math breaks
down. But math as a language is so convenient to describe hypotheses (and so accurate
in most cases!), that we can’t help but use it to design airplanes. We haven’t yet found a
better tool than math.
Likewise, movie ratings aren’t literally a linear map, but if we pretend they are we can
make algorithms that accurately predict how people rate movies. So if you know that
Skyfall gets ratings 1, 2, and 1 from Aisha, Bob, and Chandrika, respectively, then a new
person would rate Skyfall based on a linear combination of how well they align with
these three people on other ratings. In other words, up to a linear combination, in this
example Aisha, Bob, and Chandrika epitomize the process of rating movies.
The idea in SVD is to use a better choice of people than Aisha, Bob, and Chandrika,
and a better choice of movies, by isolating the “orthogonal” aspects of the process into
separate vectors in the basis. Concretely this means the following:
1. Choose a basis p1 , . . . , pn of the space of people. Every person in the database can
be written as a linear combination of the pi , and all the pi are perpendicular. This
is true of our starting basis, but (3) will clarify why this new basis is special.
3. Do (1) and (2) in such a way that the resulting representation of A only has entries
on the diagonal.24 I.e., A(p1 ) = c1 q1 for some constant c1 , likewise for p2 , p3 , etc.
24
Matrices with only nonzero entries on the diagonal are often called “diagonal” matrices, and if a matrix is
diagonal with respect to some choice of a basis, it’s called “diagonalizable.”
167
One might think of the pi as “idealized critics” and the qj as “idealized movies.” If the
world were unreasonably logical, then q1 might correspond to the “ideal action movie”
and p1 to the “idealized action movie lover.” The fact that A only has entries on the
diagonal means that p1 gives a nonzero rating to q1 and only q1 . A movie is represented
by how it decomposes (linearly) into “idealized” movies. To make up some arbitrary
numbers, maybe Skyfall is 2/3 action movie, 1/5 dystopian sci-fi, and −6/7 comedic
romance. A person would similarly be represented by how they decompose (via linear
combination) into a action movie lover, rom-com lover, etc.
To be completely clear, the singular value decomposition does not find the ideal action
movie. The “ideality” of the singular value decomposition is with respect to the inherent
linear structure of the rating data. In particular, the “idealized genres” are related to how
closely the data sits in relation to certain lines and planes. This is the crux of why the
SVD algorithm works, so we’ll explain it shortly. But nobody has a strong idea of how the
movie itself relates to the geometric structure of this abstraction. It almost certainly de-
pends on completely superficial aspects of the movie, such as how much it was advertised
or whether it’s a sequel. Indeed, one could add these features to a learning model! Nev-
ertheless, much of the usefulness of the abstraction relies on not being domain-specific.
The more a model encodes about movie-specific features, the less it applies to data of
other kinds. One sign of a deep mathematical insight is domain-agnosticism.
The takeaway is that this mental model of an idealized genre movie and an idealized
genre-lover grounds our understanding of the SVD. We want to find bases with special
structure related to the data. We know the analogy is wrong, but it’s a helpful analogy
nonetheless.
Earlier I said that the SVD is about finding a low-dimensional subspace that approxi-
mates the data well. It won’t be clear until we dive into the algorithm, but this is achieved
by taking our special basis of idealized people, p1 , . . . , pn (likewise for movies), and or-
dering them by how well they capture the data. There is a single best line, spanned by
one of these pi , that the points are collectively closest to. Once you’ve found that, there
is a second best vector which, when combined with the first, forms the best-fitting plane
(two-dimensional subspace), and so on.
The approximation aspect of the SVD is to stop at some step k, so that you have a
k-dimensional subspace that fits the data well. The matrix P whose rows are the chosen
p1 , . . . , pk is the linear map that projects the input vector x to the closest point in the sub-
space spanned by p1 , . . . , pk . This is simply because the matrix-vector multiplication P x
involves an inner product ⟨pi , x⟩—the projection formula onto a unit vector pi —between
each row of P and x.
Hopefully, k is much less than m or n, but still captures the “essence” of the data.25
Indeed, it turns out that if you define the special basis vectors in this way—spanning the
best-fitting subspaces in increasing order of dimension—you get everything you want.
And what’s astounding is that you can build these best-fitting subspaces recursively. The
25
One useful perspective is that the “truth” is a low-dimensional subspace, but the observations you see are
jostled off that subspace by noise in a predictable fashion. This is a modeling assumption.
168
best-fitting 2-dimensional subspace is formed by taking the best line and finding the next
best vector you could add. Likewise, the best 3-dimensional subspace is that best plane
coupled with the next best vector. We’re glomming on vectors greedily.
It should be shocking that this works. Why should the best 5-dimensional subspace be
at all related to the best 3-dimensional subspace? For most problems, in math and in life,
the greedy algorithm is far from optimal. When it happens, once in a blue moon, that
the greedy algorithm is the best solution to a natural problem—and not obviously so—it’s
our intellectual duty to stop what we’re doing, sit up straight, and really understand and
appreciate it.
Minimizing and Maximizing
First we’ll define what it means to be the “best-fitting” subspace to some data. Below, by
the “distance from a vector x to a subspace W ,” I mean the minimal distance between x
and any vector in W .
Next we study this definition to come up with a suitable quantity to optimize. Say I
have a set of m vectors w1 , . . . , wm in Rn , and I want to find the best approximating
1-dimensional subspace. Given a candidate line spanned by a unit vector v, measure
the quality of that line by adding the sum-of-squares distances from wi to v. Using the
projection function defined earlier,
∑
m
quality(v) = ∥wi − projv (wi )∥2
i=1
This formula, in a typical math writing fashion, exists only to help us understand what
we’re optimizing: squared distances of points from a line. To make it tractable, we convert
it back to the inner product. I’ll describe this process in a fine detail, with sidebars to
explain some notational choices.
We want to find the unit vector v that minimizes the quality function. We’d write the
goal of minimizing this expression as
∑
m
arg min ∥wi − projv (wi )∥2 .
v
i=1
min EXPR,
v∈Rn
∥v∥=1
which is the minimum value of EXPR considered over all possible unit vectors in Rn .
Just to drive the point home, this is existentially equivalent to the Python snippet:
The analogous expression which evaluates to the input vector v (instead of the ex-
pression being optimized) is called “arg min.” The arg prefix generally means, get the
“argument,” or input, to the optimized expression. Note that there can be multiple min-
imizers of an expression, so we are implicitly saying we don’t care which minimizer is
chosen. It’s a highly context-dependent bit of notation. If I replaced min with arg min in
the offset equation above, it would correspond to the following Python snippet.
I introduced the argmin because we actually want to find the minimizing vector. It’s
false to claim minx≥0 (x2 + 1) = minx≥0 x2 , even though the argmins are unique and
equal. So our line-of-best-fit problem is most rigorously written as:
∑
m
arg minn ∥wi − projv (wi )∥2
v∈R
∥v∥=1 i=1
Now we continue to convert it to the inner product. Since projv (wi ) and wi − projv (w)
are perpendicular, we can apply the Pythagorean theorem, in this case that ∥projv (w)∥2 +
∥wi − projv (w)∥2 = ∥w∥2 , rearranging to replace each term in the sum:
∑
m
( )
arg min ∥wi ∥2 − ∥projv (wi )∥2
v
i=1
Next, notice that the ∥wi don’t depend on the input v, meaning we can’t optimize
∥2
them and can remove them from the expression without changing the argument of the
minimum (it does change the value of the min). The minimization problem is now
( m )
∑
arg min − ∥projv (wi )∥2
v
i=1
And because minimizing something is the same as maximizing its opposite, we can
swap the optimization. Let’s also put in the inner product formula instead of the squared-
norm. We’ve reduced the best fitting line optimization to finding a unit vector v which
maximizes
170
∑
n
arg max ⟨wi , v⟩2
v
i=1
Maximizing the square of a non-negative value is the same as maximizing the non-
squared thing, so we can equivalently write: arg maxv ∥Av∥.
To summarize, we started with a dataset of m vectors wi which we interpreted as points
in Rn . These are the rows of the movie rating matrix, the vector of ratings per movie. We
saw that the best approximating line for the vectors {wi } is spanned by the unit vector
v ∈ Rn which maximizes ∥Av∥, where A is a matrix whose rows are the wi . This v will
end up being one of our “idealized people,” the so-called first singular vector of A.
There are many algorithms that solve this optimization problem. We’ll use a particu-
larly simple one, and defer implementing it until after we see how this problem can be
used as a subroutine to compute the full singular value decomposition.
Singular Values and Vectors
Here is the main theorem that makes the SVD work:
Theorem 10.23 (The SVD Theorem). Computing the best k-dimensional subspace fitting
a dataset reduces to k applications of the one-dimensional optimization problem.
This is so astounding and useful that the solutions to each one-dimensional problem
are given names: the singular vectors. I will define them recursively. Let A be an m × n
matrix (m rows for the movies, and n columns for the people) whose rows are the data
points wi . Let v1 be the solution to the one-dimensional problem
Call v1 the first singular vector of A. Call the value of the optimization problem, i.e.
∥Av1 ∥, the first singular value and denote it by σ1 (A), or just σ1 if A is understood from
context.
Informally, σ1 (A) is larger if we capture the data better by v1 . So as the points in A
move toward the line spanned by v1 , σ1 (A) increases. If all the data points lie on the line
spanned by v1 , then σ1 (A) is exactly the sum of squared-norms of the rows of A. Indeed,
if x ∈ span(v1 ) and v1 is a unit vector, then v1 = ±x/∥x∥ and projv1 (x) = ⟨x, v1 ⟩v1 = x.
Now we can move up in dimension. To find the best 2-dimensional subspace, you first
take the best line v1 , and you look for the next best line, ignoring all lines that are in
171
the span of v1 . That optimization problem is written as (assuming henceforth that the
domain is Rn )
The solution v2 is called the second singular vector, along with the second singular value
σ2 (A) = ∥Av2 ∥.
Often writers will use the binary operator ⊥ to denote perpendicularity of vectors
instead of the inner product. So v ⊥ v1 is the assertion that v and v1 are perpendicular.
The ⊥ symbol has many silly names (“up tack” on Wikipedia). In my experience most
people call it the “perp” symbol, since in mathematical typesetting it’s denoted by \perp.
Continuing with the recursion, the k-th singular vector vk is defined as the solution
to the optimization problem ∥Av∥ for unit vectors v perpendicular to every vector in
span{v1 , . . . , vk−1 }. The corresponding singular value is σk (A) = ∥Avk ∥. You can keep
going until either you reach k = n and you have a full basis, or else some σk (A) = 0, in
which case all the vectors in your data set lie in the span of {v1 , . . . , vk−1 }.
As a side note, by the way we defined the singular values and vectors,
This should be obvious, and if it’s not take a moment to do a spot check and see why.
Now we can prove the SVD Theorem.
Proof. Recall we’re trying to prove that the first k singular vectors are actually the k-
dimensional subspace of best fit for the vectors that are the rows of A. That is, they span
a linear subspace W which maximizes the squared-sum of the projections of the data onto
W . For k = 1 this is trivial, because we defined v1 to be the solution to that optimization
problem. The case of k = 2 contains all the important features of the general inductive
step. Let W be any best-approximating 2-dimensional linear subspace for the rows of
A. We’ll show that the subspace spanned by the two singular vectors v1 , v2 is at least as
good (and hence equally good as W ).
Let w1 , w2 be a basis of unit vectors of W , and require w1 ⊥ w2 . Note ∥Aw1 ∥2 +
∥Aw2 ∥2 is the quantity we need to maximize, and any unit-vector-basis of W maximizes
this quantity by assumption. Moreover, we’re going to pick w2 so that it’s perpendicular
to the first singular vector v1 . Justify this by considering two cases: either by happen-
stance v1 is already perpendicular to every vector in W , in which case any choice for
w1 , w2 will do, or else v1 isn’t perpendicular to W and you can choose w1 to be the unit
vector spanning projW (v1 ), with w2 being any unit vector in W perpendicular to w1 . The
resulting w2 is perpendicular to v1 . (If it’s hard to visualize that this can be done, draw a
picture in 3 dimensions.)
By definition v1 maximizes ∥Av∥, implying ∥Av1 ∥2 ≥ ∥Aw1 ∥2 . Moreover, since we
chose w2 to be perpendicular to v1 (and hence a possible candidate for the second singular
172
vector), the second singular value v2 satisfies ∥Av2 ∥2 ≥ ∥Aw2 ∥2 . Hence the objective by
{v1 , v2 } is at least as good as W :
The singular vectors vi are elements of the domain. In the context of the movie rating
example, the domain was people, and so the singular vectors in that case are “idealized
people.” As we said earlier, we also want the same thing for the codomain, the “idealized
movies,” in such a way that A is diagonal when represented with respect to these two
bases.
Say the singular vectors are v1 , . . . , vn , and the singular values are σ1 , . . . , σn . That
gives us two pieces of the puzzle: the diagonal representation Σ (the Greek capital letter
sigma, since its entries are the lower case singular values σi ) defined as follows:
σ1 0 · · · 0 0
0 σ2 · · · 0 0
.. .. . . .. ..
. . . . .
0 0 · · · σn−1 0
Σ=
0 0 · · · 0 σ
n
0 0 ··· 0 0
.. .. . . .. ..
. . . . .
0 0 ··· 0 0
And the domain basis: a matrix V whose columns are the vi , or equivalently V T whose
rows are the vi .27 If we want to write A in this diagonal way, we just have to fill in a
change of basis matrix U for the codomain.
A = U ΣV T
Indeed, there’s one obvious guess (which we’ll later scale to unit vectors): define ui =
Avi . Let’s verify the ui form a basis. Note they form a basis of the image of A (the set
{Av : v ∈ Rn }), since it can happen that m > n. To get a full basis, just extend the
27
Here the superscript T denotes the transpose of V ; that is, V T has as its i, j entry the j, i entry of V . It
swaps rows and columns but we’ll have much more to say in Chapter 12. For now, it’s enough to note (and
easy to verify) that if V has perpendicular unit vectors as columns, then V T = V −1 , so we can use V T as
a change of basis from the standard basis to the basis defined by V .
173
partial basis of ui ’s in any legal way to get a full basis. To show the ui form a basis, take
any vector w in the image of A, write it as w = Ax, and write x as a linear combination
of the vi :
w = A(c1 v1 + · · · + cn vn )
= c1 Av1 + · · · + cn Avn
= c1 u1 + · · · + cn un
It can be proved that the ui are perpendicular, but the only proof I have seen is some-
what technical and for brevity’s sake I will skip it. But taking this on faith, the ui form a
basis and one can express A = U ΣV T , as desired. The fact that A = U ΣV T is why SVD
is called a “decomposition.” The U, Σ, V are the components that A is broken into, and
each are particularly simple.
T
AT A = (U ΣV T ) (U ΣV T ) = V ΣU T U ΣV T = V Σ2 V T
We’re using Σ2 to denote ΣT Σ, which is a square matrix whose diagonals are the
squares of the singular values σi (A)2 . Also note that because the columns of U are per-
pendicular unit vectors, the product U T U is a matrix with 1’s on the diagonal and zeros
elsewhere; i.e., the identity matrix.
Using AT A isolates the V part of the decomposition. Now for the algorithm:
Theorem 10.24 (The Power Method). Let x be a unit vector that has a nonzero component
of v1 (a random unit vector has this property with high probability). Let B = AT A =
V Σ2 V T . Define xk = B k x, the result of k applications of B to x. Then as long as σ1 (A) >
σ2 (A), the limit limk→∞ ∥xxk ∥ = v1 .
k
∑
n
k
xk = B x = ci σi2k vi
i=1
Notice that, since σ1 is larger than σ2 (and hence all other singular values), the coef-
ficient for σ1 grows faster than the others. Normalizing xk causes the coefficient of σ1
tends to 1 while the rest tend to 0.
The intuition to glean from this proof is that B = AT A, when applied to a vector,
“pulls” that vector a little bit toward the top singular vector. If you normalize after each
step, then the magnitude of the vector doesn’t change, but the direction does.
The relevant quantity tracking the growth is the ratio between the two biggest singu-
lar values, (σ1 /σ2 )2n . Even if σ1 is only marginally bigger, say σ1 = (1 + ε)σ2 , the
resulting growth rate is exponential in the number of iterations. The growth rates will
be terrible, convergence will be swift. Most importantly, this lets us compute! Solving
the 1-dimensional optimization problem is now as simple as computing a matrix-vector
product and normalizing at each step.
Code It Up
Here’s the python code that solves the one-dimensional problem, using the numpy library
for matrix algebra. Note that numpy uses the dot method for all types of matrix-matrix
and matrix-vector and inner product operations.28 Also note the .T property returns the
transpose of a matrix or vector.
First, some setup and defining a function that produces a random unit vector.
def random_unit_vector(n):
unnormalized = [normalvariate(0, 1) for _ in range(n)]
the_norm = sqrt(sum(x * x for x in unnormalized))
return [x / the_norm for x in unnormalized]
And now the core subroutine for solving the one-dimensional problem.
28
They, along with most applied linear algebraists, view vectors as matrices with one column.
175
if n > m:
B = np.dot(A.T, A)
else:
B = np.dot(A, A.T) # spot check: why is this okay?
iterations = 0
while True:
iterations += 1
last_v = current_v
current_v = np.dot(B, last_v)
current_v = current_v / norm(current_v)
Since, as we saw in Chapter 8, the sequence will never quite achieve its limit, we stop
after xn changes its angle (as computed using the inner product) by less than some thresh-
old.
Now we can use the one-dimensional subroutine to compute the entire SVD. The helper
function we need for this is how to exclude vectors in the span of the singular vectors
you’ve already computed. Unfortunately, to solve this question opens up questions about
a new topic, namely the rank of a matrix, which I’ve found hard to fit into this already
very long chapter. As much as it hurts me to do so, we will save it for an exercise, and
present the formula here.29
The idea is this: to exclude vectors in the span of the first singular vector v1 with
corresponding u1 , subtract from the original input matrix A the rank 1 matrix B1 defined
by bi,j = u1,i v1,j (the product of the i-th and j-th entries of u1 , v1 , respectively). The
name for this matrix is the “outer product” of u1 and v1 , and it’s closely related to a
concept called the tensor product. Likewise, you can define Bi for each of the singular
∑ vi . To exclude all the vectors in the span of {v1 , . . . , vk }, you replace A with
vectors
A − ki=1 Bi .
In the following code snippet, we do this iteratively when we loop over svd_so_far
and subtract. The following assumes the case of n > m, with the other case handled
similarly in the complete program.30 The parameter k stores the number of singular
values to compute before stopping.
29
And, again, I would like to stress that this book is far too small to provide a complete linear algebra education.
The fantastic text “Linear Algebra Done Right” is an excellent such book for the aspiring mathematician. In
that I mean, they exhaustively prove every fact about linear algebra from the ground up.
30
See pimbook.org
176
for i in range(k):
matrix_for_1d = A.copy()
Let’s run this on some data. Specifically, we’ll analyze a corpus of news stories and
use SVD to find a small set of “category” vectors for the stories. These can be used, for
example, to suggest category labels for a new story not present in our data set. We’ll
sweep a lot of the data-munging details under the rug (see the Github repository for full
details), but here’s a summary:
1. Scrape a set of 1000 CNN stories, and a text file one-grams.txt containing a list
of the most common hundred-thousand English words. These files are in the data
directory of the Github repository.
2. Using the natural language processing library nltk, convert each CNN story into
a list of (possibly repeated) words, excluding all stop words and words that aren’t
in one-grams.txt. The output is the file all-stories.json.
3. Convert the set of all stories into a document-term matrix A, with m rows (one for
each word) and n columns (one for each document), where the ai,j entry is the
count of occurrences of word i in document j.
Then we run SVD on A to get a low-dimensional subspace of the vector space of words.
Indeed, if the above recipe is factored out into functions, then the entire routine is:
data = load(filename)
matrix, (index_to_word, index_to_document) = make_document_term_matrix(data)
matrix = normalize(matrix)
sigma, U, V = svd(matrix, k=10)
Here U is the basis for the subspace of documents, V for the words. However, these
basis vectors are very difficult to understand! If we go back to our interpretation of such
177
a word vector as an “idealized” word, then it’s a “word” that best describes some large set
of documents in our linear model. It’s represented as a linear combination of a hundred
thousand words!
To clarify, we can project the existing words onto the subspace, and then we can cluster
those vectors into groups and look at the groups. Here we use a black-box clustering
algorithm called kmeans2, provided by the scipy library.
projectedDocuments = np.dot(matrix.T, U)
projectedWords = np.dot(matrix, V.T)
Once we’ve clustered, we can look at the output clusters and see what words are
grouped together. As it turns out, such clusters often form topics. For example, after
one run the clusters have size:
>>> Counter(wordClustering)
Counter({1: 9689, 2: 1051, 8: 680, 5: 557, 3: 321,
7: 225, 4: 174, 6: 124, 9: 123})
The first cluster, as it turns out, contains all the words that don’t fit neatly in other
clusters—such as “skunk,” “pope,” and “vegan”—which explains why it’s so big.31 The
other clusters have more reasonable interpretations. For example, after one run the sec-
ond largest cluster contained primarily words related to crime:
>> print(wordClusters[1])
['accuse', 'act', 'affiliate', 'allegation', 'allege', 'altercation',
... 'dead', 'deadly', 'death', 'defense', 'department', 'describe',
... 'investigator', 'involve', 'judge', 'jury', 'justice', 'kid', 'killing', ...]
This is just as we’d expect, because crime is one of the largest news beats. Other clusters
include business, politics, and entertainment. We encourage the reader to run the code
themselves and inspect the output.
A natural question to ask is why not just cluster to begin with? Efficiency! In this
model, each word is a vector of length 1000 (one entry for each story), and each document
has length 100,000! Clustering on such large vectors is slow. But after we compute the
SVD and project, we get clusters of length k = 10. We trade off accuracy for efficiency,
and the SVD guarantees us that it’s extracting the most important (linear) features of
the data. Because of this, SVD is often called a “dimensionality reduction” algorithm: it
reduces the dimension of the data from their natural dimension to a small dimension,
without losing too much information.
31
It could also occur like this because we chose too few clusters: we have to pick ahead of time how many
clusters we want kmeans2 to attempt to find, which I omitted from the simplified code above.
178
But there’s more to the story. Recall our modeling assumption, that word meanings
“have the structure of” a low-dimensional vector space, but the values we see are per-
turbed by some noise. A crime story might use the word “baseball” for idiosyncratic rea-
sons, but most crime stories do not. The low-dimensional subspace captures the “essence”
of the data, ignoring noise, and the projection of the input word vectors onto the SVD
subspace provide a “smoothed” representation of the data. This new representation has
some strikingly useful properties, which are a direct consequence of the linear model
doing its job well in representing the most influential aspects of the English language.
Before I explain what that means, I need a caveat. What I’m about to describe doesn’t
strictly work for the code presented in this chapter. Since I wrote this code with the goal
to group news articles by topic, I counted frequency of terms occurring in documents
(and the dataset I used is quite small!). If you want to reproduce the behavior below, you
need a larger dataset and a different preprocessing technique, which is basically to count
how often word pairs co-occur in a document. Check out Chris Moody’s lda2vec,32 which
does this.
Now the fun stuff. The vector representation of words produced by the SVD has a
semantic linear structure. For example, if you take the vector for the word “king,” sub-
tract the vector for “man” and add the vector for “woman,” the result approximates the
vector for “queen.” Indeed, the SVD representation has reproduced the gender aspect
of language. This occurs for all kinds of other properties of words that fit into typical
word-association style tests like “Paris is to France as Berlin is to…”
This is surprising, and it tells us that some aspect of this SVD representation of words is
much better than the original input of raw word counts. It’s surprising because we think
of language as a highly quirky, strange, perhaps nonlinear thing. But when it comes
to the relationships between words, or the semantic meaning of document topics, these
linear methods work well. One might argue that the core insight behind this is that for
language, context is linear in nature. And then it’s immediately clear why this works: if
you see a document with “child” and “she” in it, and those words occur close together,
you intuitively know, that you’re more likely to be talking about a daughter than a son.
Replace the “she” with a “he” and you expect to see the word son instead. The SVD
captures this.
This fascinates me philosophically. Because while I certainly unconsciously under-
stood that semantic meaning is roughly additive, I never consciously knew it until I saw
these linear models and asked why they work. Math imitates life, but it can also teach
us about life as it drives us to explore, refine, and build. In fact, I was confused for a
long time because the original “additive word vector” ideas came from neural network
research, which typically involves models that are highly nonlinear. It wasn’t until I
talked with some experts in natural language processing that the additive roots of the
model became apparent.
32
https://github.com/cemoody/lda2vec, forked at https://github.com/pim-book/lda2vec just in case the original
is removed. Also note that these techniques can also be produced by neural networks, the application of
Chapter 14.
179
2. Coordinate systems are arbitrary, and linear algebra gives you the power to change
coordinate systems—change the basis of the vector space—at will. A useful basis is
a treasure.
3. The matrix representation hides the difficult notation of working with linear maps,
reducing the cognitive burden of the mathematician.
4. The linear model is a powerful abstraction for working with real-world data, and
understanding linear algebra allows us to pinpoint the assumptions of this model,
and in particular where those assumptions might break down or limit the applica-
bility of the model.
10.11 Exercises
10.1 Prove the 0 (the zero vector) is unique; that is, if there are two vectors v, w both
having the properties of the zero vector, then they are equal.
10.2 Prove that the composition of two linear maps is linear. I.e., the map x 7→ g(f (x))
is linear if g and f are linear.
10.3 Prove that the image of a linear map f : V → W is a subspace of the codomain of
W . Prove that the subset {v ∈ V : f (v) = 0} is a subspace of V .
10.4 Let V, W be two vector spaces. Show that the direct product V × W is also a vector
space by defining the two operations + and ·. How does the dimension of V ×W compare
to the dimensions of V and W ?
10.5 In R2 we have colorful names for special classes of linear maps that correspond to
geometric transformations. Look up defintions and pictures to understand matrices that
perform rotation, shearing, and reflection through a line.
10.6 Research definitions and write down examples for the following concepts:
1.The column space and row space of a matrix.
2.The rank of a matrix.
3.The rank-nullity theorem.
4.The outer product of two vectors.
180
10.7 Prove that the standard inner product on Rn (Definition 10.18) is linear in the first
input. I.e., if you fix y ∈ Rn , then ⟨x, y⟩ : Rn → R is a linear map. Argue by symmetry
that the same is true of the second coordinate.
fa,b (0) = a
fa,b (1) = b
fa,b (n) = fa,b (n − 1) + fa,b (n − 2) for n > 1
Prove that the set of all Fibonacci-type sequences form a vector space (under what oper-
ations?). Find a basis, and thus compute its dimension.
10.11 The Bernstein basis is a basis of the vector space of polynomials of degree at most
n. In an exercise from Chapter 2, you explored this basis in terms of Bezier curves. Like
Taylor polynomials, Bernstein polynomials can be used to approximate functions R → R
to arbitrary accuracy. Look up the definition of the Bernstein basis, and read a theorem
that proves they can be used to approximate functions arbitrarily well.
10.12 Look up the process of Gaussian Elimination, and specifically pay attention to the
so-called elementary row operations. Each of these operations corresponds to a change of
basis, and is hence a matrix. Write down what these matrices are for R3 , and realize that
every change of basis matrix is a product of some number of these elementary matrices.
10.14 Look up the definition of an inner product space (a vector space equipped with
an inner product), and the definition of an isometry between two inner product spaces.
181
Find, or discover yourself, the aforementioned proof that all n-dimensional inner product
spaces are isometric.
10.15 Linear independence has applications and generalizations all over mathematics.
One fruitful area is the concept of a matroid. Matroids have a special place in computer
science, because they are the setting in which one studies greedy algorithms in general.
That is, every problem that can be solved optimally with a greedy algorithm corresponds
to some matroid, and every matroid can be optimized using the greedy algorithm. Look
up an exposition on matroids and understand this correspondence. Apply this to the
problem of finding a minimum spanning tree in a weighted graph. See Chapter 6, Exer-
cise 6.11 for an introduction to weighted graphs.
10.16 The k-means clustering algorithm is an algorithm for splitting a set of n vectors
{x1 , . . . , xn } ⊂ Rd into k < n sets. The algorithm works as follows: choose k random
input vectors that are considered as “centers” of their clusters. Then repeat the following:
label each vector xi with its closest center (“assign” the vector to that cluster). Then
compute a new center for each cluster as the center of all the vectors in the cluster (add
up all the vectors and divide by the number of vectors added). Repeat this until there is
a round in which the centers don’t change, or you exceed a predetermined number of
rounds. Look up this algorithm and read about what goal it’s trying to achieve, and how
it can fail.
10.17 The singular value decomposition code in this chapter has at least one undesirable
property: numerical instability. In general, numerical instability is when an algorithm is
highly sensitive to small perturbations in the input. The SVD of a matrix which is not
full rank (Cf. Exercise 10.6) contains values that are zero. The algorithm in this chapter
does not output these properly, and instead produces non-deterministic mumbo-jumbo.
Audit the algorithm to verify this undesirable behavior occurs, and research a fix.
The field is the triple (K, +, ·), or just K if the operations are clear from context.
Good mathematicians see analogies between theorems or theories. The very best ones see
analogies between analogies.
– Stephen Banach
During my PhD studies, my thesis advisor Lev and I would occasionally talk about
teaching. Among others, he tought algorithms and I taught calculus and intro Python.
One algorithms topic he covered was the Fast Fourier Transform.
For those who don’t know (and apropos to an essay between two linear algebra chap-
ters) the Fourier Transform is a linear map that takes an input function f : R → R
and outputs the coefficients for a representation of f with respect to a special basis of
sine and cosine functions.1 The input functions are often thought of as “signals,” such as
sound waves, and the output representation is thought of as tonal frequencies. The Fast
Fourier Transform, or FFT for short, is a particularly efficient algorithm for writing (finite
approximations of) signals in this special basis. It’s fast because it takes advantage of the
symmetries in sines and cosines. The discovery of this algorithm has been described as
the beginning of the information age.
Lev was well familiar with the FFT, as the insights from the algorithm relate to deep
and important advances in theoretical computer science, his field of expertise. FFT is a
cornerstone of electrical engineering, but the technique is much deeper than simply inter-
preting electrical signals. For example, FFT can be used to multiply large integers much
faster than the usual algorithm. He was frustrated by students who didn’t understand
the basic FFT, and who didn’t care that they didn’t get it. It’s boring to teach people who
don’t care. I can sympathize.
But then he excitedly explained a new insight! It was something he learned about the
FFT while preparing his lecture notes. The details are irrelevant, but my advisor also
attempted to explain this new insight to his students. This was probably not helpful for
them. Instead of focusing on basic syntax and properties of the Fourier Transform, Lev
tried to convey insights he had learned over his career. This would have been great for
a graduate seminar, but unfortunately it was levels above his students ability to compre-
hend. They were still missing the foundational tools needed to express these thoughts.
1
This is nontrivial because the vector spaces involved are infinite dimensional.
185
186
Lev was tapping the beat of a song that played clearly in his head, but which his students
had never heard before.
Pedagogical critiques aside,2 after that conversation I synthesized what felt like an
obvious truth in hindsight, about math, programming, and surely all endeavors worth
pursuing. Understanding comes in levels of insight. And as you learn—but more impor-
tantly as you re-learn—you gain meta insights. Insights about insights. You learn what
parts of a thing to appreciate and what parts are cruft.
Most experienced programmers understand these levels well. You start with the basic
syntax and semantics of a given programming language. You move up to the basic tenets
of designing and maintaining software, such as how to extract and organize functions for
reuse, proper testing and documentation, and the role of various protocols interfacing
with your system. From there it grows to insights about a particular area of specialization,
such as how the choice of database affects the performance of a web application, how to
manage an ecosystem of interdependent services, or the tradeoffs between development
speed, maintainability, and extensibility.
When you switch to a new language, syntactic scaffolding can initially mask the core
idea of a program as you become acquainted with the basic paradigms. This can be com-
plex type declarations, or a strange new package management style, or the orthodoxy
of a particular pattern (promises, streams, coroutines, etc.), which are foundationally im-
portant, but mostly orthogonal to the core logic of a program. Over time—and with
experience, an improved mental model, and useful tooling—the cruft becomes invisible.
You see a program for its core logic while still taking advantage of the features of the
language.
In software, once an engineer is experienced in the lower levels of the hierarchy, for
the most part they’re not encouraged to relearn them. There are exceptions to this, for
example, when one learns a new programming language or is submitted to code review
by senior engineers with too much time on their hands. But usually one doesn’t spend a
lot of time revisiting the foundations of programming language design to pick up Go, nor
dive deep into the design of a database when deciding what to use for a new app. You
learn SQL once, and don’t revisit the technicalities of relational algebra unless absolutely
necessary.
In mathematics, relearning one’s field is routine. The prevalence of teaching in the re-
search mathematician’s profession has a large impact on this. Mathematicians spend an
unusual amount of time learning and relearning the basics of their field because they pre-
pare lectures for undergraduates, run seminars and reading groups, and induct clueless
graduate students into the world of their research. It’s an entrenched part of the culture,
and perhaps it explains why so many advanced math books have “Introduction” in their
title.
2
Collegiate education at research institutions is a snake’s nest of competing incentives and demands on one’s
time. Having been on the academic job market and seen what constitutes success in research, I can under-
stand the need to conduct teaching as Lev did even if I want the world to be better.
187
Terry Tao summarizes it well in his essay3 “There’s more to mathematics than rigour
and proofs.”
The point of rigour is not to destroy all intuition; instead, it should be used to destroy bad
intuition while clarifying and elevating good intuition. It is only with a combination of
both rigorous formalism and good intuition that one can tackle complex mathematical
problems; one needs the former to correctly deal with the fine details, and the latter to
correctly deal with the big picture. Without one or the other, you will spend a lot of time
blundering around in the dark (which can be instructive, but is highly inefficient). So once
you are fully comfortable with rigorous mathematical thinking, you should revisit your
intuitions on the subject and use your new thinking skills to test and refine these intuitions
rather than discard them. One way to do this is to ask yourself dumb questions; another
is to relearn your field.
This is a worthwhile endeavor for anyone who wants to understand mathematics more
deeply than copying a formula from a book or paper. One aspect of this is that it’s diffi-
cult to fully appreciate a definition or theorem the first time around. Veterans of college
calculus will appreciate our discussion of the motivation for the “right” definition of a
limit in Chapter 8, because typical calculus courses are more about the mechanics—the
syntax and basic semantics—of limits and derivatives. A deep understanding of the ele-
gance and necessity of the “supporting” definitions, and how they generalize to ideas all
across mathematics, is nowhere to be found. To do so requires equal parts elementary
proofs and sufficient time to discuss counterexamples, neither of which are present for
college freshmen in computer science and engineering.
Another aspect is that mathematical definitions and theorems create a complex web
of generalization, specialization, and adaptation that is too vast to keep in your head at
once. As one traverses a career, and studies some topics in more detail, reevaluating the
same ideas can produce new inspiration. While gnawing on a tough problem, returning to
teach basic calculus and thinking about limits might spur you to frame the problem in the
light of successively better approximations, providing a new avenue for progress. While
many researchers may find this more grueling than it’s worth—dealing with the added
distractions of grading, course design, and cheating students—in theory it has benefits
beyond the education of the pupils. My advisor’s foray into Fourier Analysis is another
example. He may not have found that insight were he not required to prepare a lecture
on the topic.
Linear algebra, even the basic stuff, is a perfect example of the web of variation and
generalization. One can take the idea of linear independence of vectors, and generalize
it to the theory of matroids, which turns out is a cozy place to study greedy algorithms
(Cf. Chatper 10, Exercise 10.15). Or, if one is interested in number theory, you have the
idea of transcendental numbers, those numbers like e and π which can’t be represented
as the root of a polynomial with rational coefficients. Independence plays an analogous
3
https://terrytao.wordpress.com/career-advice/
188
role via the idea of a transcendence basis, since R is a vector space over Q (cf. Chapter 2,
Exercise 2.4). In fields like algebraic geometry or dynamical systems, a central tool is
to take a complicated object and “linearize” it, via a transformation that, say, adds new
variables and equations, so that techniques from linear algebra can be applied. The form
and function of the applications and generalizations shapes one’s understanding of the
underlying theory.
Linear algebra has higher levels of abstraction as well. We spent time, and will continue
to spend time, discussing how to cleverly choose a basis. But there is a whole other side
of linear algebra that builds up the entire theory basis-free. As we discussed about the
definition of the limit, the “right” definition of a concept shouldn’t depend on arbitrary
choices. But almost everything we’ve seen about linear algebra depends on the choice of
a basis! Recreating linear algebra without a basis requires more complicated and nuanced
definitions, but often results in more enlightening proofs that generalize well to harder
problems. As the mathematician Emil Artin once said, “Proofs involving matrices can be
shortened by 50% if one throws the matrices out.” Though we don’t have the bandwidth
in this book to cover this perspective, it’s clearly a higher rung on the ladder.
One might expect such an elegant theory could completely replace linear algebra with
their messy basis choices and matrix algebra. It could hardly be further from the truth.
There is a famous quote of Irving Kaplansky, an influential 20th century mathematician
who worked in abstract algebra (among other topics), discussing how he and his col-
leagues approach problems that use linear algebra.
We share a philosophy about linear algebra: we think basis-free, we write basis-free, but
when the chips are down we close the office door and compute with matrices like fury.
That humorous scene is a microcosm of mathematical attitudes toward the various lev-
els of abstraction. When it comes down to it, mathematicians will pick the most effective
tool for the job, despite any additional mess or a high-horse preference for elegance. Or,
as my father-in-law likes to say, “Sometimes you gotta stick your hand in the toilet.” Ka-
plansky understands the depth and limitations of “thinking basis-free,” and part of the
meta-insight is to know which situations call for which tools, and why. One nice feature
of matrices (and most computationally-friendly representations) is you can let the syntax
bear the weight of most of the cognition. Fluency with notation and mechanics lets you
write a thing down (be certain it was correct when you wrote it) and forget about it until
you need it again.
In that respect, “cumbersome” syntax is like the manuals, READMEs, and automated
scripts that you write for yourself and refer to every time you forget how to configure
your web server. Writing things down in a precise, computational syntax also has the
benefit of isolating and clarifying the nuance and essential characteristics of difficult ex-
amples. It’s much easier to focus on the bigger picture, to look at a mess and point to
the interesting core—as one would with a large program—once one can freely create and
manipulate the atomic units. It’s the same reason I say (fully aware of the irony) that the
primary goal of a calculus class is to learn algebra.
189
You don’t learn calculus until you do differential equations. And then you don’t learn
calculus until you study smooth manifolds. And then you don’t learn calculus until you
write programs that do calculus. And then you don’t learn calculus until you teach cal-
culus. You basically never learn calculus, and every time you use it in a new setting you
get new insights about it. I learned calculus while writing this book! As you mature,
those insights become more nuanced, and your continued appreciation for that nuance is
what keeps mathematics fresh and enjoyable. This isn’t a unique feature to mathematics
(appreciation for nuance is as important over a long career in politics or tennis as it is
in mathematics), but the layman’s attitude toward mathematics is that of stark facts. In
reality, theories evolve and take on new colors over time.
Learning and re-learning is continuous in mathematics. When you return to an old
subject, you must repeat the useful mechanism I’ve been touting throughout this book:
to write down characteristic examples that serve as your mental model for a general
pattern. Keeping examples in mind—picturesque examples with enough detail that you
can descend the ladder of abstraction to compute if necessary—is what fortifies an idea
and fertilizes the orchard from which you can pick ripe analogies.
The final aspect is that relearning one’s field allows one to revisit the proofs of the
central theorems of that subject. The maturity afforded by not spending most of one’s
effort trying to understand the proof allows one to then judge the proof on its merits.
It’s like reading the code for a system you designed, long after you’ve implemented and
maintained it. You have a much better understanding of the real requirements and failures
of the system. Such considerations often result in alternative proofs, which generalize and
adapt in new and novel ways. Or one can gain a deeper understanding of the benefits
and limitations of a proof technique, and how they apply (or don’t) to a problem in the
back of one’s head.
Back down to earth, this book is roughly a second or third level of insight. The first
level would be functional fluency with symbol manipulation. Though it sounds like it’s
quite basic, most of college mathematics education for engineers does not tread far off
this path. This includes even differential equations, statistics, and linear algebra, often
considered the terminal math courses for future software engineers.
The second level is largely about proof. Can you logically prove that the symbolic ma-
nipulations in the first level are correct? It’s a meta level of insight, but in another sense
it’s still a kind of basic fluency. For many undergraduate mathematics majors, becoming
fluent in the language of proof is the central goal of their studies. This is why almost all
advanced math courses are proof-based courses, and why we’ve spent so much time in
this book proving and discussing methods for proof.
The next level of insight, usually which comes after being able to prove the basic facts
about an object, are the insights about why the existence and prevalence of that object
makes sense. This occurs often through proof, but also through a non-rigorous hodge-
podge of examples, discussion, connections to other objects, and the consideration of
alternatives by which one becomes accommodated with a thing.
Further tiers revolve around new research. Understanding what questions are interest-
ing, sketching why a theorem should be true before a proof is found, generalizing families
190
of proofs into a theory that makes all those proofs trivial. And all the while one traverses
the ladder of abstraction as needed, sometimes diving into the muddy waters to crack a
tough integral, other times honing in on the importance of one particular property of an
object.
It sounds negligent to speak about math in such an imprecise manner, and mathemati-
cians like to poke fun at themselves. John von Neumann (of computer architecture fame)
once told a physicist colleague, “In mathematics you don’t understand things. You just
get used to them.” How deliciously blasphemous! More seriously, my interpretation is
that this quote continues, “…until you find that next level of insight.” It’s true, at least,
in my experience, that one must gain sufficient comfort in mechanics before one can at-
tempt proof, and one must gain some level of comfort with proof before the next-level
insights about definitions can be appreciated.
It’s not just professional mathematicians who experience this. This happens at every
level of the hierarchy. My wife is a math professor at a community college, and despite
having spent years of her undergraduate career doing proofs by induction, it was not
until she taught it a few times that the deeper understanding of why it worked dawned
on her. She had a similar experience re-learning algebraic topology for a qualifying exam,
and I distinctly recall her gleeful yelp when she realized that the intimately understood
what she was doing and why it worked.
The cognitive scientist Douglas Hofstadter asserts that analogies are the core mecha-
nism of human cognition. Part of his evidence is the wealth of analogies that surround
us in every day life: the commonplace concept of an airport “hub” relies on analogies
between the spokes of a bicycle wheel and notions of centrality in a network, each of
which rely on lower-level analogies of position and motion. These ideas are paired with
ideas about corporations, a brand, and not to mention the myriad web of analogies that
go into human conceptions of airplane flight. This is all summarized by the single word
“hub.”
The quote at the beginning of this interlude suggests that mathematics is no different.
Mathematical cognition is also largely built on analogies between analogies built on top
of lower level analogies. And just like humans understand the concepts of motion or a
wheel long before we’re able to understand the concept of an airport hub, we’re able to
understand the lower levels of mathematical abstraction (and must become comfortable
with them) before we can draw the analogies necessary to make use of the more complex
and nuanced abstractions. And then, much later, we can look back at the bicycle wheel
with a new appreciation for its purpose and use. Mathematical intuition in particular is
the graduation from purely analytical and mechanical analysis to a more visceral feeling
of why a thing should behave the way it does.
No matter where you currently stand, there are insights to be found and analogies to
draw. Don’t underestimate their value, even if they lie among “simple” things that you
think you should have mastered years ago.
Chapter 12
The notion of eigenvalue is one of the most important in linear algebra, if not in algebra,
if not in mathematics, if not in the whole of science.
– Paolo Aluffi
If you polled mathematicians on what the “most interesting” topic in linear algebra
was, they’d probably agree on eigenvalues. The definition of an eigenvalue is so simple
that I can state it now without further ado.
Definition 12.1. Let V be a vector space and let f : V → V be a linear map. A scalar λ
is called an eigenvalue for f if there is a nonzero vector v ∈ V such that f (v) = λv. The
associated vector v is called an eigenvector of f with the corresponding eigenvalue λ.
A more concise, less precise rephrasing is to find a “nontrivial” solution1 to f (v) = λv.
Note that λ = 0 is a valid choice of f (v) = 0, so long as v is nonzero. As you would infer
from our discussion in Chapter 10, the same definition holds for a matrix A, where the
condition is equivalently written Av = λv.
The question of why eigenvalues are so central to linear algebra and its applications is a
deep one, and there is no easy answer. In a vague sense, the eigenvectors and eigenvalues
of a linear map encode the most important data about that map in a natural, efficient
way. More concretely, in the scope of this chapter eigenvectors provide the “right” basis
in which to study a linear map V → V . They transform our perspective so that the
important features of a map can be studied in isolation. If you accept that premise, it’s no
surprise that eigenvalues are useful for computation. But to say anything more concrete
than that, to explain the universality of eigenvalues, is difficult.
The application for this chapter is a deep dive into how eigenvectors and eigenvalues
explain the dynamics of a particular physical system describing one-dimensional waves.
In no uncertain terms, eigenvalues are the scientific theory that reveals the inner nature
1
“Trivial” gets new meaning in this context that is partially subjective. To conjure “the nontrivial solutions”
means to ignore the obvious counterexamples. For eigenvalues and eigenvectors, if 0 denotes the zero vector,
it’s clear that f (0) = λ · 0 for every λ. It would make the definition useless if we included these “trivial”
solutions. In this book we will state explicitly what the “trivial” solutions are, but elsewhere you may have
to infer.
191
192
So while (the coordinates of) eigenvectors are not preserved across different bases, the
eigenvalues are. A technical way to say this is that eigenvalues of a linear map f are
invariant properties of f . Invariance means that the property doesn’t change under some
prespecified family of transformations. In this case, eigenvalues are invariant under the
2
I hopefully assured you in Chapter 10 that basic algebra operations such as regrouping parentheses are legal
in matrix algebra, without requiring a detailed and painful derivation of that fact. Such work belongs in
textbooks, and we have more exciting things to do here.
193
Definition 12.3. Let G = (V, E) be a graph and V = {v1 , . . . , vn } (i.e., pick an ordering
of the n vertices of G). Define the adjacency matrix of G, denoted A(G), as the n × n
matrix whose i, j entry is 1 if (vi , vj ) ∈ E and 0 otherwise.
In the exercises, you will write down a description of this matrix as a linear map and
interpret what it means in graph-theoretic terms. In particular, each of the standard basis
vectors ei = (0, . . . , 0, 1, 0, . . . , 0) can be thought of as identifying the i-th vertex vi of
G. Figure 12.1 is an example graph and its adjacency matrix. We call a graph bipartite if
194
G A(G)
e1 e2 e3 e4 e5
e1
( (
1 2 0 1 0 1 1
e2 1 0 0 0 0
3 e3 0 0 0 1 0
e4 1 0 1 0 0
5 4
e5 1 0 0 0 0
its vertices can be partitioned into two parts in such a way that all edges cross from one
part to the other. The graph G in Figure 12.1 is bipartite because it can be partitioned into
{1, 3} and {2, 4, 5}, and no edges go between these two sets.
Bipartite graphs are common in applications, because they naturally encode networks
in which there are two classes of things, where things within a class don’t relate to each
other. For example: students and teachers, with edges being class membership; wholesale
factories and distributors, with edges being shipments; or files and users, with edges being
access logs. Problems that can be intractable on general graphs can turn out to be easy
to solve on bipartite graphs, which is a compelling reason to study them.
Now here is a fantastic theorem that we won’t prove. Let A(G) be the adjacency matrix
of a (not-necessarily bipartite) graph G. Let λ1 be the largest eigenvalue, λ2 the second
largest, etc., so that λn is the smallest. Note that these eigenvalues may be negative. Also
note that, while it is true that adjacency matrices have n eigenvalues, to see why we’ll
need the theory built up in this chapter (Propositions 12.11 and 12.14).
Theorem 12.4. Let G be a connected graph. Then G is bipartite if and only if λ1 = −λn .
This is just one of the many ways that the eigenvalues of the adjacency matrix of G
encode information about G. In hindsight, it’s obvious that some relationship should
exist: there is a systematic way to get from the graph G to the eigenvalues. What’s
surprising is that they encode such natural and useful information about G, which might
otherwise require designing an algorithm to discover.
Here is another theorem, which I will paraphrase slightly to hide the nitty-gritty details.
It says that the eigenvector for the second-largest eigenvalue of the adjacency matrix
encodes information about tightly-knit clusters of vertices in a graph. In fact, it encodes
this information better than statistics the following concrete setting.
Let G = (V, E) be a graph constructed by the following process: for each pair of
vertices vi , vj ∈ V , flip a fair coin. If heads, make (vi , vj ) an edge of E. Otherwise,
195
skip that edge. You can prove that this process produces all possible graphs with equal
likelihood, so the output is simply called a random graph.3
One can show (though we will not) that for a random graph, with overwhelming prob-
ability the densest cluster of vertices will have almost exactly 2 log(n) vertices in it. It’s
also widely believed that no efficient algorithm can reliably find the densest cluster.
So to make this cluster-finding problem easier, after creating the graph in this random
√
way, pick a random subset of vertices of size n, and connect all remaining edges among
those vertices. We’ll call the chosen subset a planted clique. In general, a clique is a
subset of vertices with a complete set of edges among them. It’s a subgraph that forms
the complete graph Km for some m. You might expect that such a dense cluster of vertices
would be detectable, simply by being a statistical anomaly. Maybe you could just count up
how many edges are on each vertex, looking at the ones that are unusually large, to find
the planted clique. I won’t√ prove so here, but this method provably fails. It requires the
planted clique to have size n log n or bigger. Instead, the following algorithm succeeds:
Theorem 12.5. Let v be an eigenvector for λ2 , the second largest eigenvalue of the adjacency
√
matrix of G, a random graph on n vertices with a planted clique of size n. The following
algorithm recovers the vertices of the planted clique with high probability:
√
1. Recall that the indices of v correspond to vertices of G, and select n such vertices
whose corresponding entries in v are the largest in absolute value. Call this set T .
2. Output the set of vertices of G that are adjacent to at least 3/4 of the vertices in T .
This is a result that is quite recent by mathematics standards. It was proved in 1998 by
Alon et al. No method is known to exist that can reliably find a smaller planted clique, and
moreover it can be proved that methods that only use statistics about the graph cannot
find a smaller clique.4 All of this is to say, eigenvalues of the adjacency matrix don’t just
encode information about G, in certain settings they do so in an optimal way. The specific
area of math studying how and when eigenvalues are useful in encapsulating information
about graphs is called spectral graph theory. The general idea of using eigenvalues and
eigenvectors of matrices derived from a graph to find dense clusters is called spectral
clustering, and there are many variations.
However, we still have little understanding about why eigensystems reveal such valu-
able information. The briefest possible answer might be formulated as “eigenvectors,
scaled by their eigenvalues, provide the most natural coordinate system in which to view
a linear map.”
A stronger intuition is difficult to explain without a longer expedition into the theory
than we have time in these pages. One reason it’s hard is that a linear map f on Rn
might have eigenvalues that are complex numbers instead of real numbers, and eigen-
vectors with complex entries. Much like the possibly complex roots of a single-variable
polynomial, having complex eigenvalues means you have fewer real eigenvalues. More
importantly, if you’re not comfortable with the geometry of complex numbers, you will
have difficulty interpreting how they relate to a linear map for vectors of real numbers.
This book skips complex numbers, so we will not be able to give a complete picture.
The second reason is that eigenvalues can occur with multiplicity in two different ways.
The differences, and how they manifest in the behavior of the map, are nuanced and not
needed for our application, though we do mention some pointers in Section 12.5.
Luckily, there is a nice way to avoid dealing with complex numbers and multiplicity
while still seeing the lion’s share of eigenvalue power in practice. That is the following
theorem:
Theorem 12.6. Let f : Rn → Rn be a linear map and let A be its associated matrix. If
A is symmetric, meaning A[i, j] = A[j, i] for every i, j, then A has n real eigenvalues and
eigenvectors.
A useful notation when working with symmetric matrices is that of the transpose. De-
fine by AT the matrix whose i, j entry is A[j, i]. That is, you take A, and flip it along
the top-left-to-bottom-right diagonal, and you get AT . With this notation, saying A is
symmetric is saying that A = AT . Here’s an example of a symmetric matrix.
1 2 3 4
2 5 6 7
3 6 8 9
4 7 9 −1
In Chapter 10 I promised you that every operation on a matrix corresponds to an oper-
ation on a linear map. This is also true for the matrix transpose. If f is a linear map and
A is a matrix representation, then AT corresponds to some linear map f T that’s related
to f . However, the operation itself is difficult to describe without a lot of extra notation
and definitions. We’ll revisit those ideas in the Chapter Notes, but here we’ll directly
prove the important takeaway of that discussion: symmetric matrices play nicely with
the inner product.
First, one can verify that the standard inner product definition results in ⟨Ax, y⟩ =
⟨x, AT y⟩ for all x, y. This is often written as ⟨Ax, y⟩ = xT AT y. One considers vectors
“single-column matrices,” notes that in this perspective ⟨x, y⟩ = xT y, and then you get
And so with symmetry you get a simplified formula ⟨Ax, y⟩ = ⟨x, Ay⟩. What’s special
is that symmetric matrices can be defined by this property.
Theorem 12.7. Let A be a symmetric real-valued n × n matrix, and let ⟨−, −⟩ denote5 the
standard dot product of real vectors. Then A is symmetric if and only if ⟨Ax, y⟩ = ⟨x, Ay⟩
for every pair of vectors x, y ∈ Rn .
Proof. Symmetry gives the forward direction of the “if and only if,” since ⟨x, AT y⟩ =
⟨x, Ay⟩. For the reverse direction, suppose that ⟨Ax, y⟩ = ⟨x, Ay⟩ for all x, y. Let
a1 , . . . , an be the columns of A, and apply this fact to the vectors x = ei , y = ej (the
standard basis vectors with a 1 in positions i and j, respectively). We have
And we can do the same thing with A on the other side, by assumption:
Since ⟨Aei , ej ⟩ = ⟨ei , Aej ⟩, we get A[i, j] = A[j, i], implying A is symmetric.
We will use symmetry to prove that every symmetric matrix with real-valued entries
has a real eigenvalue. This is the central lemma needed to prove Theorem 12.6. Funnily,
we’ve spent so long preaching the virtues of eigenvalues, we haven’t even considered the
basic question of their existence!
Lemma 12.8. Let A be a symmetric real-valued matrix. Then A has a real eigenvalue.
Proof. Let x be a unit vector which maximizes the norm ∥Ax∥, and let c = ∥Ax∥. Then
Ax = cy for some unit vector y. By the maximality of x we know that ∥Ay∥ ≤ c. If
y = x then we are done (in real life, this happens most of the time).
If y ̸= x then we can show that x + y is an eigenvector with eigenvalue c. After the
proof we’ll explain as a side note why it makes sense in hindsight to consider x + y. Now
notice that
The first equality is due to Theorem 12.7, the second is the definition of y, and the third
is because the inner product is linear and y is a unit vector.
The crucial observation is that ⟨x, Ay⟩ is the (signed) length of the projection of Ay
onto the unit vector x. Projecting a vector onto a unit vector can only make the first
5
The notation ⟨−, −⟩ is used to signify that the function will be expressed in this nonstandard “pairing”
notation. If the inputs are v, w ∈ V × V , the interpretation is to substitute the dashes with the inputs in
order, i.e. ⟨v, w⟩.
198
vector shorter. You should have some intuitive sense that this is true after our analysis—
particularly the pictures—in Chapter 10. We leave a rigorous proof for the exercises. As
a consequence, c = ⟨x, Ay⟩ ≤ ∥Ay∥.
Note that ∥Ay∥ ≤ ∥Ax∥ ≤ c, since x maximizes ∥Ax∥. This, combined with the fact
that ⟨x, Ay⟩ = c, gives us
∑
n
⟨x, y⟩ = xi yi .
i=1
This formula is intimately connected with geometry. It can be used to compute the
angle between two vectors (via cos θ = ⟨x, y⟩/(|x| · |y|)), and its value is the signed
length of the projection of one argument onto the other (scaled by the lengths of the
vectors).
199
Definition 12.9. Let V be a vector space with scalars in R. An inner product for V is a
function ⟨−, −⟩ : V × V → R with the following properties:
1. Symmetric: For every v, w ∈ V swapping the order of the inputs doesn’t change
the inner product, i.e. ⟨v, w⟩ = ⟨w, v⟩.
2. Bi-linear: If you fix any input to a constant v ∈ V then the restricted function,
considered as a map V → R, is linear. I.e., if we fix the second input ⟨−, w⟩, then
⟨cv, w⟩ = c⟨v, w⟩ for all c ∈ R, and likewise ⟨u+v, w⟩ = ⟨u, w⟩+⟨v, w⟩. Likewise
for fixing the first input.
3. Nonnegative norms: For every v ∈ V , the inner product with itself is nonnegative,
i.e. ⟨v, v⟩ ≥ 0. This is called the squared norm of v. Moreover, we require that the
only vector with norm zero is the zero vector.
A vector space V and a specific inner product ⟨−, −⟩ are together called an inner prod-
uct space.
A typical example of where these functions occur in real life is as sound waves. L2
forms a vector space. Addition is the point-wise addition of functions (f + g)(x) =
6
We won’t cover integration in this book, but you don’t need∫ ∞ to know (or remember) how to integrate func-
tions to follow along. In all of what follows, the integral −∞ f (x)dx is a number that represents the signed
area in between f (x) and the x-axis. The meta-motivation for inner products is well-worth any notational
discomfort.
200
f (x) + g(x), and with the requisite calculus one can prove that the sum of two square-
integrable functions is square-integrable. The case is similar for the other required vector
space properties. And finally, the jewel in the crown, the inner product is
∫ ∞
⟨f, g⟩ = f (x)g(x)dx.
−∞
This inner product space—which actually satisfies some additional properties that make
it into a so-called Hilbert space—is different from vector spaces we’ve seen so far. In par-
ticular, in Rn there’s a “default” basis in which we express vectors without realizing it:
the standard basis. L2 has no obvious basis. From on our discussion of Taylor series
in Chapter 8, we know that polynomials can approximate functions in the limit. One
might hope that polynomials form a basis of this space, perhaps {1, x, x2 . . . }. But actu-
ally none of these functions are even in L2 ! And moreover, many functions in L2 aren’t
differentiable everywhere, so Taylor series can run into trouble.
As it happens, there are many interesting and useful bases for this space. For example,
the following basis is called the Hermite basis:7
But proving this is a basis is not trivial! There are other useful bases as well. The
Fourier basis, a staple of the signal-processing world and electrical engineering, is the set
of complex exponentials {e2πikx : k ∈ Z}. Since we’re not officially covering complex
numbers in this book, think of this basis as the set of all sine and cosine functions with
all possible periods.
These bases are difficult to discover. But even when we have one, how in the name of
Grace Hopper can one even write a function in such a basis? You can’t set up a system
of equations because there’s no decent starting basis! Not to mention it’d be an infinite
system of infinitely long equations.
Using the inner product, and some work to modify the basis to make it geometrically
amenable, the process of writing a function with respect to one of these (modified) bases
reduces to computing an inner product. Once again, we translate an intuitive but hard
mathematical concept into a more computationally friendly language. This should im-
press upon you the importance of the inner product. Not only does it endow a vector
space with new, geometric measurements; it also makes computing basis representations
possible where it might otherwise not be. A powerful revelation indeed.
In the rest this chapter, except for the application, the inner product will be considered
abstractly, as we study its generic properties and how it relates to eigenvectors. We’ll also
see how the inner product relates to simplifying the computation of expressing a vector
in terms of a basis.
7
More specifically, the Hermite basis is what happens when you apply Gram-Schmidt to orthogonalize and
normalize this basis, which we’ll see later in this chapter.
201
In the exercises you will prove some other basic facts about inner products, but here is
one too important to relegate to the end of the chapter.
Proposition 12.11. Let A be real-valued symmetric matrix. Let v, w be eigenvectors of A
with corresponding eigenvalues λ ̸= µ, respectively. Then ⟨v, w⟩ = 0.
Proof. By the symmetry of A:
Another way to say it is that if two eigenvectors are not orthogonal, then they must
have the same corresponding eigenvalue (this is the contrapositive statement8 ).
As we proved in Chapter 10, the standard inner product on Rn allows one to compute
angles, and more specifically to determine when two vectors are perpendicular to each
other. In a generic inner product space, perpendicularity is undefined, and so we define
it by generalizing what we proved in Rn . Perpendicularity and length get new names.
Definition 12.12. Two vectors u, v ∈ V in an inner product space are called orthogonal
if ⟨u, v⟩ = 0.
√
Definition 12.13. The norm of a vector v ∈ V is the quantity ∥v∥ = ⟨v, v⟩. Without
a square root, it’s called the square norm.
Vectors with norm 1 are called unit vectors.
Most of the facts about perpendicularity and projection we proved for Rn actually
don’t depend on the definition of the standard inner product. They can be re-proved
using any inner product, because the key ingredients from those proofs were extracted
into the definition of an inner product. Next we’ll show that orthogonal vectors can be
used to build up a basis.
8
If “p implies q” is true, then it is equivalently true that “not q implies not p.” The latter is called the contra-
positive form of the former.
202
Proposition 12.14. Any set of nonzero vectors {v1 , . . . , vk } which are pairwise orthogonal
are linearly independent.
This explains part of a comment we made earlier about adjacency matrices. We said
that an adjacency matrix has n eigenvalues and eigenvectors. It has at most n distinct
eigenvalues because each one corresponds to an eigenvector, and together these eigen-
vectors would form a basis. Such a basis can be at most as big as the dimension of the
space, which for adjacency matrices was n, the number of vertices. The reason it’s exactly
n is because an adjacency matrix is symmetric, which is the hypothesis for this chapter’s
crowning result, Theorem 12.24.
Proposition 12.15. Let {v1 , . . . , vn } be an orthonormal basis for V , and let x ∈ V . Then
x can be written as
Proof. Fix any basis vector vi and let x = c1 v1 + · · · + cn vn where cj are the (unknown)
coefficients of x’s representation with respect to the basis. Then
⟨x, vi ⟩ = ⟨c1 v1 + · · · + cn vn , vi ⟩
= c1 ⟨v1 , vi ⟩ + · · · + cn ⟨vn , vi ⟩
= c1 · 0 + · · · + ci−1 · 0 + ci · 1 +ci+1 · 0 + · · · + cn · 0
|{z}
i-th term
= ci .
203
As we’ve discussed, the naive approach to computing the basis representation of a vec-
tor x ∈ Rn with respect to a basis {vi } would be to set up the system of linear equations
Ay = x, where the columns of A are the vi , and solve for y using a technique like Gaus-
sian elimination. As it turns out, Gaussian elimination takes cubic runtime in the worst
case (cubic in n, the dimension of the vector space).
However, with an orthonormal basis all you need to do is compute n inner products.
The standard inner product only takes n multiplications and n additions, meaning the en-
tire decomposition only takes time n2 . This is a huge improvement if, suppose, you could
compute an orthonormal basis once and use it to compute basis representations many
more times, as opposed to doing Gaussian elimination for each vector you wanted to rep-
resent in the target basis. It’s also worth noting that in practice there’s often a natural
ordering on a basis, so that the first vectors in the basis contribute “most significantly” to
the space, and one can approximate a basis representation using a constant-sized subset
of the basis. For our physics application the eigenvalues will determine the ordering.
But beyond that, in a space like L2 where there’s no natural starting basis, this gives
us a feasible way to compute basis representations: just compute the inner product! In
L2 you simply integrate.9
Going back to finite dimensions, the next important property of an orthonormal basis
is that the change of basis matrix (the matrix with the basis vectors as columns) is easy
to invert.
Proposition 12.16. Let {v1 , . . . , vn } be an orthonormal basis for V . Let B be the change
of basis matrix, with the vi as columns. Then B T = B −1 .
Proof. We can prove this directly by showing that B T B is the identity matrix, i.e., the
matrix 1n with 1s on the diagonal and zeros elsewhere. Indeed, the entries of B T B encode
all pairwise inner products of the vectors in the basis. The i, j entry of B T B is the inner
product ⟨vi , vj ⟩, which is 1 if and only if i = j, and zero otherwise.
One may wonder if it’s also necessary to show BB T = 1n in order to conclude that
BT is a proper inverse of B. A direct proof hits an immediate barrier, because the inner
products don’t line up as they did above. It turns out this barrier is a mirage. By pure
set theory, namely Proposition 4.12 from Chapter 4, a one-sided inverse of a bijection
is automatically a two-sided inverse. And, of course, all change of basis matrices are
bijections.
This has an almost startling consequence:
Proposition 12.17. If the columns of A form an orthonormal basis, then so do the rows of
A.
9
Integration is not always computationally easy, but you choose the orthonormal basis so that it is.
204
Proof. Let B = AT then B satisfies B T B = 1n , which as we saw above encodes all the
pairwise inner products of columns of B, i.e., rows of A. Since orthogonal vectors are
linearly independent (Proposition 12.14), the columns of B form a basis.
However, if we wanted to prove this without all the set theory hijinks, we could have
−1
done so by proving (AT ) = (A−1 ) . You will do this in the exercises.
T
One natural question you might ask is how to find an orthonormal basis. For finite
dimensional inner product spaces there’s an algorithmic method, and the method is called
the Gram-Schmidt process. It falls short of an algorithm by not defining how to do one
important step. First, a definition:
Definition 12.18. Let V be an inner product space and W ⊂ V a subspace with a basis
B = {w1 , . . . , wk }. Let v be a vector, and define the projection of v onto the subspace W ,
denoted by projW (v), as follows:
∑
projW (v) = projwi (v)
wi ∈B
1. Let S0 = {} be the empty set. Si will contain the basis built up so far at step i.
2. For i = 1, . . . , n:
3. Output Sn .
The Gram-Schmidt process doesn’t dictate how to find a vector not in the span of a
given set, but using that as a subroutine, the rest is well-defined arithmetic. The proof that
the result is an orthonormal basis is a simple exercise in induction. The same algorithm
allows one to start from a given basis (possibly of a subspace), and transform it into an
orthonormal basis with the same span. For this variant, if you have a subspace basis
{v1 , . . . , vk }, and you want to know what new vector to choose at step i, you can simply
choose vi .
205
As a side note, this algorithm is generally not considered “production ready,” because
it suffers from numerical instability. Most industry-strength linear algebra libraries use
one of a few different techniques based on linear algebra primitives (such as Householder
reflections and the famed Cholesky decomposition) that have been fine-tuned and opti-
mized for speed and stability.
If you believe that finding roots of single-variable polynomials is hard, you might also
be convinced that finding “roots” of linear maps is hard. In fact, you’ll prove in an exer-
cise that computing eigenvalues of linear maps is at least as hard as computing roots of
polynomials. And as we’ll see below, all eigenvalues can be expressed in terms of kernels.
As a quick exercise, prove that the kernel of a linear map is a subspace of V . Rephrasing
the above, the eigenvalues of f corresponding to the eigenvalue λ = 0 are exactly the
kernel of f . Also recall that I denotes the identity map I(x) = x, with corresponding
matrix In for n-dimensions.
1 0 0
A = 0 1 0
0 0 0
And we can inspect the matrix A − λI3 to compute the remaining eigenvalues.
1−λ 0 0
A − λI3 = 0 1−λ 0
0 0 −λ
A vector (a, b, c) in the kernel of this map (for some unknown λ) must satisfy a(1 −
λ) = 0 and b(1 − λ) = 0 and −λc = 0. The third equality implies either λ = 0 or c = 0.
In the former case, a = b = 0 and we get (0, 0, 1) as an eigenvector for λ = 0. In the
latter case, we’re left with the same two-dimensional eigenspace for λ = 1.
Here’s a more interesting example, the matrix for the map (a, b, c) 7→ (a, a+b, a+b+c).
1 1 1
B = 0 1 1
0 0 1
This matrix clearly has one eigenvector, (1, 0, 0) for the eigenvalue λ = 1. But what
about other potential eigenvectors? Indeed, we’re looking for the kernel of B −I3 , which
is
0 1 1
B − I3 = 0 0 1
0 0 0
Aside from the span of (1, 0, 0), there are no zeroes. And moreover, B − λI3 has only
the trivial kernel {0} (set up the system of three equations and verify this).
When an eigenvalue has multiple independent eigenvectors, we get a viscerally inter-
pretable kind of “multiplicity,” which goes by the name geometric multiplicity.
For for the matrix A above, the eigenvalue 1 has geometric multiplicity 2, but for B
the multiplicity is only 1.
There’s another, more subtle kind of multiplicity called algebraic multiplicity, which
I personally don’t know how to motivate from “first principles.” Specifically, the most
common definition uses the definition of the determinant of a matrix (as a polynomial).
An alternative way to define it is as follows.
Definition 12.22. The algebraic multiplicity of an eigenvalue λ for f is the largest integer
m for which ker((f − λI)m ) is strictly larger than ker((f − λI)m−1 ).
207
From this definition, we can see that the algebraic multiplicities of λ = 1 are different
for A and B above. Taking successive powers of B − I3 gives first (0, 1, 0) and then
(0, 0, 1) in the kernels, while the algebraic multiplicity for A is just 1.
These two types of multiplicity work together to give a characterization of any linear
map in terms of so-called Jordan blocks. These are square sub-matrices with λ on the
diagonal and 1’s on the adjacent diagonal. For example for n = 3:
λ 1 0
Jλ,3 = 0 λ 1
0 0 λ
The Jordan canonical form theorem states that for any linear map V → V there is a ba-
sis for V , for which the matrix of that linear map consists entirely of Jordan blocks along
the diagonal. There may be more than one Jordan block for a given eigenvalue, but the
size and number of blocks are determined by the algebraic and geometric multiplicities
of that eigenvalue, respectively.
All of this is to note two things: it’s possible to compute all of the eigenvalues and
eigenvectors for a linear map, and these, along with some auxiliary data (some of which
I’ve left out from this text), do in fact give a complete characterization of the map. How-
ever, it’s a more nuanced characterization, and one whose benefits are not as easily dis-
played as when you have an orthonormal basis of eigenvectors. The Jordan canonical
form is an important theorem that has generalizations and adaptations in other fields of
mathematics. You will explore the Jordan canonical form more formally in the exercises.
Finally, as a quick aside, the set of all eigenvalues together with their geometric multi-
plicities is called the spectrum of a linear map.
Definition 12.23. Let f : V → W be a linear map between vector spaces. Define the
spectrum of f as the set
It is interesting to note that most scientific uses of the word “spectrum” refer to this
mathematical idea, for example the spectrum of wavelengths of light or the spectrum of
an atom.
λ1 0 · · · 0
0 λ2 · · · 0
A= . .. . . ..
.. . . .
0 0 · · · λn
And the reverse holds too: if a linear map can be written in this diagonal form, then the
basis vectors used must be orthogonal eigenvectors. A linear map that can be written this
way for some basis is called diagonalizable. What’s astounding is that every symmetric
matrix has an orthonormal basis of eigenvectors. This is the centerpiece theorem of this
chapter and the secret ingredient in the physics application to follow.
Theorem 12.24 (The Spectral Theorem). A real-valued matrix A is symmetric if and only
if it has eigenvectors that form an orthonormal basis (i.e., is diagonalizable).
This theorem requires some nontrivial amount of work, pieces of which we have al-
ready proved in this chapter. The easy part, however, is the reverse direction. It uses the
fact that (AB)T = B T AT .
Proof. There is a change of basis matrix U , whose columns are the orthonormal basis, for
which A = U T DU , for D a diagonal matrix. A diagonal matrix is clearly symmetric, so
T T
AT = (U T DU ) = U T DT (U T ) = U T DU = A, implying A is symmetric.
The strategy for the other half of the proof will be by induction on the dimension of
the vector space. That is, given the fact that every (n − 1) × (n − 1) symmetric matrix
has an orthonormal basis of eigenvectors, we’ll show that every n × n symmetric matrix
does as well.
Induction suggests we should find one way to “peel off” one dimension in a way that’s
independent of the rest of the argument. Given A, we’ll find an eigenvector v with cor-
responding eigenvalue λ that will be the first vector in the basis. Then we’ll decompose
Rn into two subspaces, a one-dimensional space spanning v, and an (n − 1)-dimensional
space, which we’ll apply induction on. In particular, we will be able to rewrite A in a
“block” form like so:
( )
λ 0
A→
0 A′
In the above, the boldface 0 are to denote that zeroes take up the entire “area” implied
by the dimensions. If A is an n × n matrix, and λ is a scalar, then A′ is (n − 1) × (n − 1)
and each boldface zero represents n − 1 zeroes in the only allowable shape.
Intuitively, what we’re doing here is partially rewriting the basis in terms of one known
eigenvector. Indeed, we have to describe a full basis to get a block decomposition, but as
209
long as whatever process we use to make the basis maintains the symmetry of A′ , we win.
We’ll be able to combine the orthonormal basis of A′ with v to get a full orthonormal basis
for A. The remaining details relate to the algebra of a precise proof, which we’ll exhibit
now.
compute Ax for any vector x, we write it with respect to the basis, and apply A to each
piece. In this case that’s Aui = ⟨ui , v⟩v + A′ ui , and ⟨ui , v⟩ = 0. So if ui is an eigenvector
for eigenvalue λ, then Aui = A′ ui = λui .11
The Setup
Consider the system depicted in Figure 12.2 in which a string is pulled tight through five
equally spaced beads. If you pluck the string, it naturally creates a wave that propagates
through the string from end to end. Or, if you pluck the middle bead, the string oscillates
in a symmetric fashion.
First, we need to write down a formal mathematical model in which we can describe
the motion of a bead. We start by defining a function of time that represents an object’s
position. Ultimately, we’ll only care about the vertical motion of the beads, but a priori
we’ll need two dimensions to describe the forces involved.
Let x : R → R2 be a function describing the position of an object at a given time t. In
particular, we choose a reference point in the universe to be (0, 0) and a basis {e1 , e2 }
of R2 for measurement. Then the components of x(t) = (x1 (t), x2 (t)) represent the
11
A different argument is to introduce the notion of a direct sum of vector spaces. To write a vector space in
terms of the direct sum of subspaces (which is what we did here) means that a vector can be written uniquely
as a sum of vectors in each subspace. Orthogonal complements always form a direct sum.
12
pimbook.org
211
Figure 12.2: A system in which five beads are equidistantly spaced on a taut string.
position of the object, in e1 , e2 units, respectively, relative to (0, 0). The obvious choices
of coordinates are the standard basis vectors (1, 0) and (0, 1) representing horizontal and
vertical, as aligned with the picture.
Model 12.26. Let x(t) = (x1 (t), x2 (t)) be the position of an object at time t. Then its
derivative, x′ (t) = (x′1 (t), x′2 (t)), describes the object’s velocity at time t, and the second
derivative x′′ (t) = (x′′1 (t), x′′2 (t)) describes its acceleration at time t.
These should intuitively make sense when thinking of the derivative as a rate of change.
Velocity is the rate of change of position, acceleration the rate of change of velocity. As
an aside, this kind of vector-valued function that has a 1-dimensional input and a multi-
dimensional output is often called a parametric function. We’ll cover derivatives in more
generality in Chapter 14.
We must also describe a mathematical model (one that will suffice for our purposes)
for a physical force. Note that while we’re doing everything here in two dimensions, the
same principles apply to three or more dimensions.
In the formulas below, we’re concerned with the force in a particular direction. Indeed,
given a force vector F (t) at a specific time t, projecting F (t) onto the appropriate unit
vector v gives the component of F in the direction of v. If we choose the basis to align
with the vertical direction, the projection is trivial: just look at the second entry of the
force vector. But in general you can use projections to get the component of a force in
any direction.
In a sense that is not rigorous but part of the mathematical model, forces “act” on
objects. By that I mean they are applied to objects and influence their motion. If you
212
Figure 12.3: A simpler system that has only one bead, displaced from its equilibrium and
released.
Model 12.28 (Newton’s n-th law for some n). If F1 , . . . , Fn are forces acting on an object
with mass m whose position is described by x(t), then
∑
n
F (i) = mx′′ (t)
i=1
In other words, the sum of the forces applied to an object determines the acceleration
of that object. More massive objects need larger forces to move them.
One Bead
Now let’s inspect our beaded string in the special case of a single bead in the middle of a
string. The bead has been plucked and released, as in Figure 12.3.
Our goal is to model the dynamics of this system as a linear system. At any given
time t, we should be able to calculate the acceleration x′′ (t) of the bead as linear function
of its current position. As we’ll see that’s enough to compute the position x(t) at any
time. When we extend the model to include all five beads, it will depend linearly on the
positions of multiple beads.
We’ll make a whole host of unrealistic assumptions to aid us. Let’s pretend the string
has no mass, the bead has no width, there is no friction or air resistance, and let’s do
away with gravity. More generously, we assume that all of these values are “negligibly
small” compared to the forces we care about. These kinds of simplifying assumptions are
the physics analogue of what mathematicians do when they encounter a hard problem:
keep stripping out the difficult parts until you can solve it. If you simplify the problem
in the right way, you’ll be analyzing just the aspects of the problem that you really care
about. After solving it, having hopefully gained useful intuition in the process, you can
replace each removed bit and use your newfound intuition to find a solution of the harder
213
F1+F2
F2 F1
Figure 12.4: The forces pull in opposite directions toward the wall, and together sum to
a vertical force.
problem. Or, if you cannot, you can see how the simpler solution breaks with the new
assumption, and thus understand why the full problem is hard to solve. This process is
by no means as easy as it sounds, but it’s a powerful guide.
The above assumptions are minor, but there are two crucial assumptions that we have
to discuss in more detail. First, we assume the string is not stretched too far. This allows
us to use a Taylor series approximation for the sine and tangent of a small angle. Second,
assume the string is already stretched tightly when the beads are plucked. This is what
allows us to ignore the horizontal motion of the bead. We’ll discuss these in more detail
when we employ them.
Once we’ve eliminated gravity and its cohort, there are only two forces acting on the
bead: the force of tension in the string on the left and right sides of the bead. When the
bead is pulled downward, the string is stretched longer than its resting length, and the
bonds between the string’s atoms create a force that “pulls” the string back to its normal
length. Luckily, tension is well understood. The standard model is Hooke’s law.
Model 12.29 (Hooke’s law). The force of tension in an elastic string that has been
stretched from its resting length by a distance d ≥ 0 is −T d, where T is a constant
depending on the material of the string. This model only applies for a sufficiently small
d that does not exceed a limit (which again depends on the material in the string).
If the string is tied to a surface and you pull away from the surface, even at an angle,
the force is directed back along the string toward the surface. This gives our bead two
forces as in Figure 12.4.
Since we assumed the bead has no width (or, if you will, the forces act on the center of
mass of the bead), the tails of these vectors are the same point, and when we sum them
we get the net force pulling the bead upward.
In our system the string is taut, and we’ll suppose it’s stretched to begin with. Call 2l
the natural length of the string (so that l is the length of one of the two halves), T the
214
F2 F1
tension constant, and 2linit the length the string is initially pulled to when the system is
at rest. In that case, the two forces on the bead have magnitude T (linit − l) and face in
opposite directions. The bead does not move.
Let’s focus on the right hand side of the bead (the left side is symmetric) in Figure 12.6.
Choose the resting point of the bead, when the string is completely straight, to be (0, 0).
Use the standard basis {(1, 0), (0, 1)} and let x(t) = (x1 (t), x2 (t)) be the displacement
of the bead at time t (some arbitrary but small vertical position not at rest). Call d(t) the
length of the right string segment at time t, and F1 (t) the force pulling on the bead by
the string. The diagram in Figure 12.6 labels these values.
√Now we compute. Our choice of basis and the Pythagorean theorem give d(t) =
2 + x (t)2 . We construct F (t) first by finding a unit vector in the correct direction,
linit 2 1
then scaling it so its length is the magnitude of the force. That magnitude is T (d(t) − l),
according to Hooke’s law. The force vector starts at x(t) and points toward (linit , 0), so
we can take (linit , 0) − x(t) = (linit , −x2 (t)) and normalize it by dividing by d(t). So far
we have
Conveniently, the right hand term is the magnitude of tension when the system is at
rest. For the left hand term, we can use a Taylor series approximation. First we do some
simplification.
215
l init
(0, 0)
F1
d(t)
(x 1(t), x 2(t))
Figure 12.6: The force pulling the bead rightward when the bead is displaced.
√
2 + x (t)2
d(t) =
linit 2
√ ( )
x2 (t) 2
= linit 1 +
linit
√
Next we compute the Taylor series for 1 + z 2 , substituting z = x2 (t)/linit at the end.
Indeed, the Taylor series is
√ z2 z4 z6
1 + z2 = 1 + − + − ···
2 8 16
2
Using the first two terms to approximate, we get d(t) ≈ linit (1 + x2l
2 (t)
2 ). If we wanted
init
to be more rigorous, we could hide the lower order terms in a big-O notation, but we’ll
save that for Chapter 15.
2
Returning to the force of tension, minor algebra gives T (d(t) − linit ) = T x2l 2 (t)
init
. In
other words the magnitude of the force of tension in the string is the initial tension, plus
a small factor proportional to the square of the deviation.
x2 (t)2
T + T (linit − l)
2linit
The formula above is why we can assume, as most physics texts do without nearly
as much fuss as we have displayed here, that the magnitude of tension in the string is
216
constant. This Taylor series approximation is the first assumption showing up in the
math: if the initial deviation x2 (t) is small, say much less than 1 unit of measurement,
then x2 (t)2 is even smaller and can be ignored, as can all higher powers of x2 (t). Our
computation shows that the first power x2 (t) does not show up anywhere in the Taylor
series, so if we’re committed to simplifying everything to be linear, the Taylor series
assures us we’re not accidentally ignoring terms we want to preserve.
I personally feel it’s important to see how the math justifies the assumptions rather
than relying entirely on “physical intuition.” Once you state which forces you want to
consider—and once you’ve formalized the mathematical rules governing those forces—
the mathematics should stand on its own. In particular, many physics books say that
the constant tension assumption rests on the fact that the bead is not displaced very far
from rest. Strictly speaking, this is not enough information. What also matters is the
relationship between the displacement of the bead and the initial stretch that holds the
string taut at rest. The former must contribute an order of magnitude smaller force than
the latter to be negligible. The Taylor series revealed this nuance, and further allows us
to measure how big a displacement is too big to ignore.13
We continue with the assumption, then, that the magnitude of the force of tension in
the string is constant over the entire evolution of the system. From this point on we’ll
use T in place of T (linit − l) to simplify the formulas (it’s all just a constant anyway).
Recalling that we formed the unit vector by scaling by d(t), the force on the right string
is the vector
Note that while we ignored the x2 (t)2 factor in the magnitude, we haven’t yet ignored
it in the scaling of the unit vector. That begins now: since the two forces F1 (t) and F2 (t)
are symmetric, we only need the components of F1 (t) in the vertical direction. That
means we can project F1 (t) onto the vector (0, 1), i.e., isolate the second entry of the
vector.
Now that all our forces are vertical, we can just work with the 1-dimensional pic-
ture and see that the sum of the forces on the bead in the vertical direction is F (t) =
−2T x2 (t)/linit . By Newton’s law, this dictates the acceleration of the bead, giving
b3
b2 b5
b1
b4
√
motion: x2 (t) = x2 (0) cos(t 2T ). It’s a smooth cosine with a constant frequency deter-
mined by the tension in the string. This is exactly what we expect from a single bead.
Multiple Beads
Now we graduate to multiple beads, shown in Figure 12.7.
Horizontal forces are a new concern. We want to retain our assumption of constant
tension in the string. But because the angles are different on different sides of a bead, the
fraction of that constant tension pulling the bead left and right can be different, resulting
in horizontal motion. We know that the tension in the string will eventually pull the bead
back to the center, but we want to feel secure that these violations of our assumptions are
minor enough that we can justify ignoring them. We leave it as an exercise to the reader to
adapt the setup for a single bead to this scenario, and to use Taylor series approximations
to argue that horizontal motion can be ignored.
Since we are ignoring horizontal motion, we’ll simplify the notation so that the forces,
displacements, velocities, and accelerations are 1-dimensional vectors, i.e., scalars repre-
senting vectors pointing in the vertical direction. Let b1 , . . . , b5 be the beads of mass mi ,
and let yi be the displacement of bi , with yi′ and yi′′ the velocity and acceleration, as be-
fore. The natural resting point of the beads is zero. If we just think about position—and
as we saw this completely determines the forces and the acceleration—then the state of
this system is a vector y = (y1 , y2 , y3 , y4 , y5 ) ∈ R5 . The forces we’re about to compute
will form a linear map A mapping y 7→ y ′′ .
Let’s now focus on bead b2 as a generic example, shown in Figure 12.8. In the figure,
the gap between b1 and b2 is y2 − y1 , and the angle θ1 is the angle between the string and
the horizontal. Likewise for the corresponding data on right hand side of the bead. The
tension is a constant T . The projected tension in the vertical direction is −T sin(θ1 ) +
T sin(θ2 ), with the sign flip because the first is pulling the bead down.15
Now we’ll use two Taylor series approximations:
15
When b1 is above b2 , the angle is negative and that reverses the sign: sin(−θ) = − sin(θ). So the orientations
work out nicely.
219
b3
b2
b1 ϑ2
ϑ1 y3
y2
y1
θ3 θ5
sin(θ) = θ − + + ···
3! 5!
θ3 2θ5
tan(θ) = θ + + + ···
3 15
Because the first two terms are equal, and for θ small enough to ignore θ3 and higher,
we can replace sin(θ) with tan(θ) wherever it occurs. This is the same reasoning as before,
because we want to extract the linear aspects of the model. The force on bead b2 is
y2′′ m2 = F2 (t)
= −T sin(θ1 ) + T sin(θ2 )
= −T tan(θ1 ) + T tan(θ2 )
y2 − y1 y3 − y2
= −T +T
linit linit
m2 linit ′′
y2 = y1 − 2y2 + y3
T
Simplify the equation by setting m2 = linit = T = 1. The forces for the other beads
are analogous, with the beads on the end having slightly different formulas as they’re
attached to the wall on one side. As a whole, the equations are
220
y1′′ = −2y1 + y2
y2′′ = y1 − 2y2 + y3
y3′′ = y2 − 2y3 + y4
y4′′ = y3 − 2y4 + y5
y5′′ = y4 − 2y5
At last, we turn to eigenvalues. This matrix is symmetric and real valued, and so by
Theorem 12.24 it has an orthonormal basis of eigenvectors which A is diagonal with
respect to. Let’s compute them for this matrix using the Python scientific computing
library numpy. Along with Fortran eigenvector computations, numpy wraps fast vector
operations for Python.
After defining a helper function that shifts a list to the right or left (omitted for brevity),
we define a function that constructs the bead matrix, foreseeing our eventual desire to
increase the number of beads.
def bead_matrix(dimension=5):
base = [1, -2, 1] + [0] * (dimension - 3)
return numpy.array([shift(base, i) for i in range(-1, dimension - 1)])
Next we invoke the numpy routine to compute eigenvalues and eigenvectors, and sort
the eigenvectors in order of decreasing eigenvalues. For those unfamiliar with numpy,
the library uses an internal representation of a matrix with an overloaded index/slicing
operator [ ] that accepts tuples as input to select rows, columns, and index subsets in
tricky ways.
Eigenvalue Eigenvector
λ y1 y2 y3 y4 y5
-0.27 0.29 0.50 0.58 0.50 0.29
-1.00 -0.50 -0.50 -0.00 0.50 0.50
-2.00 0.58 -0.00 -0.58 0.00 0.58
-3.00 -0.50 0.50 -0.00 -0.50 0.50
-3.73 -0.29 0.50 -0.58 0.50 -0.29
0.6
0.4
0.2
= -0.267949
= -1
0.0 = -2
= -3
= -3.73205
0.2
0.4
0.6
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0
Figure 12.9: The rounded entries of the eigenvectors of the 5-bead system (top) and their
plots (bottom).
And, finally, a simple use of the matplotlib library for plotting the eigenvectors. Here
our x-axis is the index of the eigenvector being plotted, and the y-axis is the entry at that
index. Plotting with five beads gives the plot in Figure 12.9.
In case it’s hard to see (there will be a clearer, more obvious diagram at the end of the
section), let’s inspect it in detail. The top eigenvalue, λ = −0.267 . . . , corresponds to
the eigenvector in the chart above with circular markers. The eigenvector entry starts at
0.29, increases gradually to 0.58, and then back down to 0.29, a sort of quarter-period of
a full sine curve. The second largest eigenvalue, λ = −1 with triangular markers, has an
eigenvector starting at −0.5 and increasing up to 0.5, performing a half-period of sorts.
The next eigenvector for λ = −2 performs a single full period, and so on.
Now this is something to behold! The eigenvectors have a structure that mirrors the
waves in the vibrating string, and as the corresponding eigenvalue decreases, the “fre-
quency” of the wave plotted by the eigenvector increases. That is, the wave exhibits
faster oscillations.
This wave is not a metaphor. If you simulate the beaded string with initial position set
222
to one of these eigenvectors, you’d see a standing wave whose shape is exactly the plot
of that eigenvector. In fact, I implemented a demo of this in Javascript, which you can
explore for yourself at pimbook.org.16 The demo is a first-principles simulation of the
system, so horizontal forces are not ignored, nor are Taylor series approximations used.
Because of this, if you set the initial positions of the beads to be quite large, you’ll see
irregularities caused by horizontal motion. These are highligthed by how the demo draws
the force vector acting on each bead at every instant. It’s fun to watch, and it provides
a hint as to what assumption allows one to ignore horizontal motion. Indeed, if you set
the position to the top eigenvector 100v1 (scaled to account for the units being pixels),
you can see the same shape as v1 in the plot above. If you scale it even larger, you can
see the horizontal forces come into play. For example, try setting the initial positions to
300v1 = (87, 150, 174, 150, 87).
Let’s witness how the formulas work out for the first eigenvector v1 , when the posi-
tions start as that eigenvector y = v1 ≈ (0.29, 0.5, 0.58, 0.5, 0.29). In that case each
bead’s trajectory can be computed independently according to y ′′ = Ay = −0.27y. So
the second bead, say, evolves as y ′′ = −0.27y with initial position y2 = 0.5. This is
identical to the single-bead system we solved earlier, and the result is a simple cosine
wave with a fixed period and amplitude. The same holds for each bead. The beads in the
middle have longer periods and higher amplitudes, as expected.
We have the tools to understand this eigenvector phenomenon beyond concrete com-
putations. As we saw, the eigenvectors of the bead system form an orthonormal basis.
The basis vectors are the independent components of the joint forces acting on all the
beads. What’s more, the proof of the Spectral Theorem explains why the eigenvectors
have a natural ordering. The way we choose an eigenvector at each step is, according to
Lemma 12.8, by maximizing ∥Av∥ over unit vectors v. In the proof of the Spectral Theo-
rem we then removed that vector, and its span, from consideration for the next vector.17
So the largest magnitude eigenvalue (in this case the most negative one) is the first one
extracted, and that corresponds to the highest frequency. The next eigenvector chosen
corresponds to the second largest magnitude eigenvalue, and so on, each having a smaller
frequency than the last.
But wait, there’s more! Because it’s an orthonormal basis of eigenvectors, we can
express any evolution of this system in terms of the eigenvectors, and do it as simply as
taking inner products.
Take, for example, the complex evolution that occurs when you pluck the second bead.
Say y(0) = (0, 0.5, 0, 0, 0). The individual beads don’t evolve according to a single co-
sine wave. They jostle in a more haphazard manner. Nevertheless, we can express their
trajectory as a sum of five simple cosine waves, one for each eigenvector. Indeed, the
following Python snippet performs the decomposition of y (for a concrete, fixed time) in
16
Note the demo is written in ES6 using d3.js, and the implementation is available in the Github repository
linked at pimbook.org.
17
This is suspiciously similar to the singular value decomposition in Chapter 10, though there we focused on
the geometric perspective.
223
return coefficients
With results printed below rounded for legibility, the coefficients for our chosen y can
be computed and used to reconstruct the original vector.
>>> A = bead_matrix(5)
>>> eigensystem = sorted_eigensystem(A)
>>> eigenvalues, eigenvectors = eigensystem
>>> w = [0, 0.5, 0, 0, 0]
>>> coeffs = decompose(eigensystem, w)
>>> print(coeffs)
{0: 0.25, 1: -0.25, 2: 0, 3: 0.25, 4: 0.25}
>>> numpy.sum([coeffs[i] * eigensystem[1][i] for i in range(5)], axis=0)
array([ 0, 5.0e-01, 0, 0, 0])
So y(0) = 0.25v1 + −0.25v2 + 0v3 + 0.25v4 + 0.25v5 , and we can compute this sum
and pick out any coordinate we want to get the initial position of a particular bead.
Now, in the basis of eigenvectors, we define a new set of variables z(t) =
(z1 (t), . . . , z5 (t)). Let zi (t) be the coefficient of vi for the representation of y(t) in the
basis of eigenvectors. In words, before we were tracking the position of the beads as they
evolve over time, and now we’re tracking the coefficients of the eigenvectors as they evolve
over time. This is the whole point of the change of basis. In this new representation the
differential equation changes to
y ′′ = Ay =⇒ z ′′ = Dz
Where D is the diagonal matrix of eigenvalues λ1 , . . . , λn (in any order we please, let’s
say in decreasing order). Then each coordinate is just like our single-bead case. For ex-
ample z1′′ = λ1 z1 , along with an initial condition z1 (0) = 0.25 (as per the decomposition
of y(0) above).
We can solve each of these differential equations separately, just as we solved the single-
bead equation, and then combine them by converting back to the standard basis of bead
positions. The result will give us the trajectory of each bead expressed as a sum of simple
cosine waves.
The equations, with initial conditions placed adjacent, are (with some rounding to sim-
plify):
224
Fantastic! We started with a tightly coupled system, in which the position and motion
of the different beads seem to depend heavily on each other. They do, it’s true, but this
eigensystem provides a perspective in which their motions can be computed indepen-
dently! You don’t have to know where bead 3 is to compute the future position of bead
2. That’s the promise fulfilled by eigenvectors.
Finally, as you may have guessed from the arbitrary choice of five beads, we can gen-
eralize this system to any number of beads. If we take even just a hundred beads, and
plot the eigenvectors for the top few eigenvalues as we did above, we see smoother, more
obvious waves. Figure 12.10 shows this. With such natural shapes of increasing complex-
ity, it makes sense to give a name to these eigenvectors. They’re called the fundamental
modes of the system, and the frequencies of the “sinusoidal curve” of each eigenvector18
are called the resonant frequencies of the system.
18
Or rather, the curves implied to underlie these discrete points.
225
0.15
0.10
0.05
= -0.000967435
= -0.00386881
0.00 = -0.0087013
= -0.0154603
= -0.0241391
0.05
0.10
0.15
0 20 40 60 80 100
Figure 12.10: The plot of the top five eigenvectors for a hundred-bead system.
If one decreases the distance between beads and increases the number of beads in
the limit, the result is the wave equation. This is a differential equation (in both time and
position along the string) that one can use to track the motion of a traveling wave through
a string. See the exercises for more on that. But more importantly for us, the vector space
for that continuous model has infinite dimension, it still has a basis of eigenvectors, and
they correspond to proper sine curves instead of discrete approximations. In this case,
since the “zero-width” beads are now at every position of the string, you can think of them
as cross sections of molecules that make up the string itself, with atomic forces playing
the role of Hooke’s law. These eigenvectors then describe the intrinsic properties of the
string itself.
So there you have it. Eigenvectors have revealed the secrets of waves on a string.
12.9 Exercises
12.1 Let V be an n-dimensional inner product space, whose norm is given by the inner
product. Prove the following.
1.The only vector with norm zero is the zero vector.
2.The distance function induced by an inner product is nonnegative and symmetric.
3.The distance function induced by an inner product satisfies the triangle inequality.
That is, d(x, y) + d(y, z) ≤ d(x, z) for all x, y, z ∈ V .
12.2 Prove that a linear map f : Rn → Rn preserves the standard inner product—
i.e. ⟨x, y⟩ = ⟨f (x), f (y)⟩ for all x, y—if and only if its matrix representation A has
orthonormal columns with respect to the standard basis. Hint: use the fact that ⟨x, y⟩ =
xT y.
12.3 Let A be a square matrix with an inverse. Using only the fact that (BC)T = C T B T
−1
for two square matrices B, C, prove that (AT ) = (A−1 ) .
T
12.4 Prove the following basic facts about eigenvalues, eigenvectors, and inner products.
1.Fix a vector y and let fy (x) = ⟨x, y⟩. Prove that if x is restricted to be a unit vector,
then fy (x) is maximized when x = y.
2.Let V, W be two n-dimensional inner product spaces with inner products ⟨−, −⟩V
and ⟨−, −⟩W . Define a bijective linear map f : V → W that is an isomorphism
of vector spaces and also satisfies ⟨x, y⟩V = ⟨f (x), f (y)⟩W for all x, y ∈ V . Such
a map is called an isometry. Hint: start by using Gram-Schmidt to choose an or-
thonormal basis of each vector space.
3.Fix the inner product space Rn with the standard inner product. Let A : Rn → Rn
be a change of basis matrix. Find an example of A for which ⟨x, y⟩ ̸= ⟨Ax, Ay⟩.
In other words, an arbitrary change of basis does not preserve the formula for the
standard inner product. As we saw in the chapter, only an orthogonal change of
basis does this. Determine a formula (that depends on the data of A), that shows
how to convert inner product calculations in one basis to inner product calculations
in another.
12.5 Look up a proof of Theorem 12.30, on the uniqueness of the sine function, that uses
Taylor series. The analytical tool required to understand the standard proof is the concept
227
12.6 In Definition 12.3 we defined the adjacency matrix A(G) of a graph G = (V, E).
This matrix corresponds to some linear map f : Rn → Rn , where n = |V |. How would
you interpret the vector space V ? What is a natural description of the basis of V that
we’re using to represent A(G)? What is a natural (English) description of the linear
map f , if you restrict to input vectors whose entries are either 0 or 1? If this is hard to
formulate abstractly, write down an example graph on 5 vertices. What happens to your
description of f when you allow for non-binary inputs?
12.7 Prove that a connected graph G is bipartite if and only if it contains no cycles of
odd length. Write a program to find cycles of odd length, and hence to decide whether a
given graph is bipartite.
12.8 Implement the algorithm presented in the chapter to generate a random graph on
n vertices with edge
√ probability 1/2, and a planted clique of size k. For the rest of this
exercise fix k = n log n. Determine the average degree of a vertex that is in the plant,
and the average degree of a vertex that is not in the plant, and use that to determine a rule
for deciding √if a vertex is in the clique. Implement this rule for finding planted cliques of
size at least n log n with high probability, where n = 1000.
12.9 As in the previous problem, implement the algorithm in this chapter for finding
√
planted cliques of size k = 10 n in random graphs with n = 1000. Use a library such
as numpy to compute eigenvalues and eigenvectors for you.
12.11 We proved that symmetric matrices have a full set of eigenvectors and eigenvalues.
In this exercise we will see that to understand eigenvalues of non-symmetric matrices,
we must necessarily prove the Fundamental Theorem of Algebra, which we remarked
in Exercise 2.12 is quite hard. First prove that r is a root of the polynomial p(x) =
xn + an−1 xn−1 + · · · + a1 x + a0 if and only if r is an eigenvalue of the matrix
228
0 1 0 ··· 0 0
0 0 1 ··· 0 0
.. .. .. .. .. ..
. . . . . .
Ap =
0 0 0 ··· 1 0
0 0 0 ··· 0 1
−a0 −a1 −a2 · · · −an−2 −an−1
Notice that this matrix is not symmetric, and that because the roots of a polynomial might
necessarily be complex numbers, this implies the eigenvalues of a matrix might also be
complex. Walk away from this exercise with a new appreciation for the convenience of
symmetric matrices, and the inherent difficulty of writing a generic eigenvalue solver.
12.12 Look up a proof of the theorem that every square matrix can be written in the
so-called Jordan canonical form.
12.13 Implement the Gram-Schmidt algorithm using the following method for finding
vectors not in the span of a partial basis: choose a vector with random entries between
zero and one, repeating until you find one that works. How often does it happen that you
have to repeat? Can you give an explanation for this?
12.14 Look up the derivation of the wave equation from Hooke’s law for a beaded string
(or equivalently, beads on springs) as the distance between adjacent beads tends to zero.
12.15 Look up a proof that the singular values of a non-square real matrix A are the
square roots of the eigenvalues of the matrix AT A.
12.16 Generate a “random” symmetric 2000 × 2000 matrix via the following scheme:
pick a random distribution (say, Gaussian normal with a given mean and variance), and
let the i, j entry with i ≥ j be an independent draw from this distribution. Let the
remaining i < j entries be the symmetric mirror. Compute the eigenvalues of this matrix
(which are all real) and plot them in a histogram. What does the result look like? How
does this shape depend on the parameters of the distribution? On the qualitative choice
of distribution?
12.17 At the end of the chapter we converted the eigenvector-coefficient solution to
z ′′ = Dz back to the bead basis by hand. Write a program that, given the initial position
of the beads, sets up the independent differential equations in the eigenvector basis, solves
those equations, and converts them back to the bead position basis.
12.18 Using Taylor series, find appropriate conditions under which horizontal motion
in the 5-bead system can be ignored.
12.19 Generalize our one-dimensional bead system to a two dimensional lattice. That
is, fix n and put a bead at each (i, j) ∈ {1, 2, . . . , n}2 , with strings connecting adjacent
229
beads, with fixed walls on each boundary side. Pluck the beads perpendicularly to the
lattice. Can you design a symmetryic linear model for this system? If so, what do the
eigenvectors look like? If not, what step of the modeling process breaks? What is the
fundamental obstacle?
12.20 Consider a one-dimensional “bead system” where instead of the beads physically
moving, they are given some initial heat. Adjacent beads transfer heat between them
according to a discrete version of the so-called heat equation. Find an exposition of the
discrete heat equation online that allows you to set up a linear system and solve it for 10
beads. What do the eigenvalues of this system look like?
12.21 PageRank is a ranking algorithm that was a major factor in the Google search
engine’s domination of the internet search market. The algorithm involves setting up a
linear system based on links between webpages, and computing the eigenvector for the
largest eigenvalue. Find an exposition of this algorithm and implement it in code. Can
you visualize or interpret the eigenvector in a meaningful way?
Mathematics as we practice it is much more formally complete and precise than other
sciences, but it is much less formally complete and precise for its content than computer
programs. The difference has to do not just with the amount of effort: the kind of ef-
fort is qualitatively different. In large computer programs, a tremendous proportion of
effort must be spent on myriad compatibility issues: making sure that all definitions
are consistent, developing good data structures that have useful but not cumbersome
generality, deciding on the right generality for functions, etc. The proportion of energy
spent on the working part of a large program, as distinguished from the bookkeeping
part, is surprisingly small. Because of compatibility issues that almost inevitably es-
calate out of hand because the right definitions change as generality and functionality
are added, computer programs usually need to be rewritten frequently, often from scratch.
Programmers who brave mathematical topics often come away wondering why math-
ematics isn’t more like programming. We’ve discussed some of the issues surrounding
this question already in this book, like why mathematicians tend to use brief variable
names, and how conventions will differ from source to source. Beneath these relatively
superficial concerns is a question about rigor.
Thurston’s observations above were as true in the mid 90’s as they are over twenty
years later. Software is far more rigorous than mathematics, and most of the work in
software is about interface and data compatibility—“bookkeeping,” as Thurston calls it.
This is the kind of work required by the rigor of software. You need to care whether
your strings are in ASCII or Unicode, that data is sanitized, that dependent systems are
synchronized, because ignoring this will make everything fall apart.
I once took a course on compiler design. The lectures were taught in the architec-
ture building on campus. One day, the architecture students were having a project fair
in the building, marveling over their structures and designs. In a lightly mocking tone,
the professor observed that software architecture was much more impressive than build-
ing architecture. Their buildings wouldn’t fall over if they forgot a few nails or slightly
changed the materials. But a few misplaced characters in software has caused destruction,
financial disaster, and death.
231
232
My professor had a point. Regular mayhem is caused by software security lapses, with
root causes often related to improper string validation or bad uses of memory copying.
Single improperly set bits can cause troves of private data to become public. Financial
insecurity is almost synonymous with digital currencies, one particularly relevant exam-
ple being the 2016 hack of the “Decentralized Autonomous Organization,” a sort of hedge
fund governed by an Ethereum contract that contained a bug allowing a hacker to with-
draw 50 million USD before it was mitigated. The root cause was a bug in the contract
allowing an infinite recursion. Multiple (unmanned) space probes, costing hundreds of
millions of dollars each, have been destroyed shortly after launch due to coding errors.
The Ariane 5 crashed in 1996 because of a bug with integer overflow. The Mariner 1 in
1962 because of a missing hyphen. Finally, in 1991, a bug in the Patriot missile defense
system resulted in the death of 28 soldiers at a military base in Saudi Arabia. The bug was
an inaccurate calculation of wall-clock time due to a poor choice of rounding. I have lit-
tle doubt there will be additional deaths1 caused by lapses and insecurities in self-driving
car software, in addition to the damage already caused by accidents (many of which went
unreported, according to some 2018 reporting).
These sorts of bugs cause internal debacles at every company with alarming regularity.
One consequence is a general feeling among many engineers that “all software is shit.”
More optimistically, the best engineers work very hard to design interfaces and abstrac-
tions that, to the best of software’s ability, prevent mistakes. Those who design aircraft
control systems do this quite well. Once you’ve made enough mistakes of your own, you
learn a certain air of humility. No matter how smart, even the best engineers get tired,
grumpy, overworked, or forgetful—each of which is liable to make them forget a hyphen.
Good tools make forgetting the hyphen impossible.
In the subfield of computer science dealing with distributed systems, these issues are
exacerbated by the extreme difficulty of even telling whether a system satisfies the guar-
antees you need it to. A titan of this area is mathematician turned computer scientist
Leslie Lamport. Through his work, Lamport essentially defined distributed computing
as a field of study. Many of the concepts you have heard of in this area—synchronized
clocks, Paxos consensus, mutexes—were invented by Lamport.
Lamport has no particular love of mathematical discourse. In his 1994 essay, “How to
Write a Proof,” he admits, “Mathematical notation has improved over the past few cen-
turies,” but goes on to claim that the style of mathematical proof employed by most of
mathematics (including in this book)—mixing prose and formulas in a web of proposi-
tions, lemmas, and theorems—is wholly inadequate.
Much of Lamport’s seminal work in the last few decades grew out of his frustration
with errors in distributed systems papers. As he attests, some researcher would propose
(say) a consensus algorithm. It might seem correct at first glance, but inevitably it would
contain mistakes—if not be wrong outright. Lamport concludes that guarantees about the
behavior of distributed systems are particularly hard to establish with the rigor that is
1
I personally attribute the 2018 death of Elaine Herzberg to engineers intentionally disabling safety features
and cutting personnel costs than to software bugs.
233
needed for practical considerations. If you’re going to design a new distributed database,
you want a much stronger assurance than the assent of some overworked journal referees.
Lamport writes,
These proofs are seldom deep, but usually have considerable detail. Structured proofs
provided a way of coping with this detail. The style was first applied to proofs of ordi-
nary theorems in a paper I wrote with Martín Abadi. He had already written conven-
tional proofs—proofs that were good enough to convince us and, presumably, the referees.
Rewriting the proofs in a structured style, we discovered that almost every one had serious
mistakes, though the theorems were correct. Any hope that incorrect proofs might not
lead to incorrect theorems was destroyed in our next collaboration. Time and again, we
would make a conjecture and write a proof sketch on the blackboard—a sketch that could
easily have been turned into a convincing conventional proof—only to discover, by trying
to write a structured proof, that the conjecture was false. Since then, I have never believed
a result without a careful, structured proof. My skepticism has helped avoid numerous
errors.
This is coming from a Turing Award winner, a man considered a luminary of computer
science. Even the smartest theorem provers among us make ample mistakes.
Consequently, Lamport designed a proof assistant called TLA+, which he has used to
check the correctness of various claims about distributed systems.2 TLA+ is supposed to
prevent you from shooting your own mathematical foot. TLA+ falls in step with a body
of work related to automated proof systems. Some systems you may have heard include
Coq and Isabelle. Some of these systems claim the ability to prove your theorems for you,
but I’ll instead focus just on the correctness checking aspects.
So computer scientists like Lamport and software engineers are perturbed by the lack of
rigor in mathematics. Each remembers the fresh wounds of catastrophes due to avoidable
mistakes. Meanwhile, Lamport and others provide systems like TLA+ that would allow
mathematician to achieve much higher certainty in their own results. This raises the
question, why don’t all mathematicians use automated proof assistants like TLA+? This
is a detailed and complex question. I will not be able to answer it justly, but I can provide
perspectives built up throughout this book.
We have argued that the elegance of a proof is important. Mathematicians work hard
to be able to summarize the core idea of a proof in a few words or a representative pic-
ture. Full rigor as the standard for all proofs would arguably strip many proofs of their
elegance, increasing the burden of transmitting intuition and insight between humans.
The work you put into making an argument automatable is work you could have spent
on making math accessible to humans (via additional papers, talks, and working with stu-
dents). These extra activities already serve as correctness checks, so is there significant
added benefit to a formal specification? Lamport’s counter is that making it accessible
2
I particularly enjoyed his tutorial video course, which you can find at https://lamport.azurewebsites.net/
video/videos.html.
234
to humans is counterproductive when the result is incorrect. He would also argue that a
structured proof is easier to understand. One underlying issue Lamport’s riposte ignores
is that mathematics is a social activity, and formal proof specifications are decidedly an-
tisocial. Good for those who want to ensure planes don’t crash, bad for those who want
to do mathematics.
Another aspect concerns the priorities and preferences of the subcultures of mathe-
matics. Theory builders might argue that if your proof is too complicated to keep track
of—which is why you would want TLA+—it’s because your theory has not been built
well enough to make the proof trivial. Conversely, problem solvers might complain that
proof assistants limit their ability to employ clever constructions. Being able to invoke
a result from a disconnected area of math requires you to re-implement that entire field
in your new context. Dependency management would turn few-page arguments into
thousand-line software libraries.
Both of these attitudes reconverge on Thurston’s observation, that the kind of effort
that goes into math is categorically different from software. Mathematicians don’t want
to nitpick type errors and missing parentheses. They want to think about ideas at a higher
level. Mathematicians have built up so many abstractions over the years specifically
to avoid the mundane details that can muddle an idea. One explanation for why TLA+
work so well for distributed systems theorems is that those theorems have relatively few
layers of indirection. A handful of bits might represent consensus. On the other hand,
in geometry you might think the thought, “this space is very flat, and that should have
such-and-such effect.” An automated proof assistant will be of no use there, nor will it
help you refine the degree to which your hypothesized effect is present. You must lay
everything out perfectly formally, even if your definitions haven’t been finalized. Then
too often you resort to writing and rewriting, and before long you’ve stopped doing math
entirely. Just as Michael Atiyah argues that the proof is the very last step of mathematical
inquiry, which implies a proof assistant is useless for the majority of your work.
As most engineers can understand, the degree of rigor to require is a tradeoff with
tangible benefits on both sides. Mathematicians opt to let some errors slip through. Over
time these errors will eventually be found and reverted or fixed. Since technology rarely
goes straight from mathematical publication to space probe control software, the world
has enough headway to accommodate it.
Thurston also questions the two assumptions underlying this discussion:
1. that there is uniform, objective and firmly established theory and practice of math-
ematical proof, and
2. that progress made by mathematicians consists of proving theorems.
Thurston instead prefers a question more leading to what he feels is the correct an-
swer: “How do mathematicians advance human understanding of mathematics?” Many
mathematicians feel unsatisfied by computer-aided proofs because they don’t help them
personally understand the proof. If the core insight can’t fit in a single human’s head, it
might as well be unproved. This is still the attitude of many toward the famous four-color
235
theorem, the shortest proof of which to date involves much brute force case checking by
computer. As much as rigor helps one establish correctness, it does not guarantee syn-
thesis and understanding.
Thurston continues,
I think that mathematics is one of the most intellectually gratifying of human activities.
Because we have a high standard for clear and convincing thinking and because we place
a high value on listening to and trying to understand each other, we don’t engage in
interminable arguments and endless redoing of our mathematics. We are prepared to be
convinced by others. Intellectually, mathematics moves very quickly. Entire mathematical
landscapes change and change again in amazing ways during a single career. When one
considers how hard it is to write a computer program even approaching the intellectual
scope of a good mathematical paper, and how much greater time and effort have to be put
into it to make it “almost” formally correct, it is preposterous to claim that mathematics
as we practice it is anywhere near formally correct.
Rather, Thurston claims that reliability of mathematical ideas “does not primarily come
from mathematicians formally checking formal arguments; it comes from mathemati-
cians thinking carefully and critically about mathematical ideas.”
Chapter 14
—David Mumford
f (x) − f (c)
f ′ (c) = lim
x→c x−c
On the real line, we defined the symbolic abstraction x → c to mean “any sequence
xn that converges to c,” where we declared the derivative only exists if the limit doesn’t
depend on the choice of sequence. When we work in Rn (which, among many other
properties, has a nice measure of distance for vectors d(x, y) = ∥x − y∥) the notion of
a convergent sequence generalizes seamlessly. A sequence of vectors x1 , x2 , · · · ∈ Rn
converges to c ∈ Rn if the sequence dn = ∥xn − x∥ of real numbers converges to
zero. Our deeper problem, however, is that despite sequence convergence generalizing,
the obvious first attempt to adapt the derivative violates well-definition. Ignoring the
obvious type error—one cannot divide a scalar by a vector—the “value” of the derivative
would depend on the sequence chosen. That’s the “smell” we pointed out in Chapter 8
that makes for a useless definition.
There are many easy examples to demonstrate. For instance: the function f (x1 , x2 ) =
−x22 , and the two sequences xn = (1 + n1 , 1) and x′n = (1, 1 + n1 ). Both sequences
converge to (1, 1), but because f depends on the second coordinate quadratically, (and
doesn’t depend on the first coordinate at all!) the direction along which x′n approaches is
steeper than that of xn . Using the former for “the derivative” would result in something
−1−(2/n)−(1/n2 )+1
like limn→∞ −1+1
(1/n)
= 0, while the latter would be lim n→∞ (1/n)
= −2. This
is illustrated in Figure 14.1.
In this brave new world, the underlying idea of “steepness” now inherently depends on
direction. This is something one intuitively understands from the natural world; a hiker
traverses switchbacks to avoid walking straight up a hill, and a skier skis in an S shape to
slow down their descent.1 In fact, for f (x1 , x2 ) = −x22 , and standing at the point (1, 1),
every direction provides a slightly different slope.
This suggests one intuitive way to generalize the one-dimensional definition of the
derivative.
f (c + tv) − f (c)
Dir(f, c, v) = lim
t→0 t
1
2
3
f(x1, x2)
4
5
6
7
8
9
1.0
1.0 0.5 0.0
0.5 1.0 1.5 1.5 1.0 0.5 0.0 0.5
x2 2.0 2.5 3.0 3.0 2.5 2.0 x1
(1 + 1/n, 1)
(1, 1 + 1/n)
Figure 14.1: The steepness of a surface depends on the direction you look.
So instead of allowing a sequence to approach the point of interest from any direction,
we restrict it to the line through the direction v we’re interested in. Here we’re using
t → 0 to denote any sequence tn ∈ R which converges to zero.
This definition has two serious problems. The first is that it’s hard to compute. It’s not
that any individual limit is particularly hard to compute on its own, but that on its face
this definition requires us to recompute limits for every direction. With single-variable
derivatives, we developed efficient techniques for computing a formulaic derivative. We
want a similar mechanism for multivariable derivatives at any point and in any direc-
tion. We want to compute a formula once, and use that to enable many easy relevant
computations later.
The second problem is that it’s not strong enough to capture what we really want out
of a derivative definition. And when I say “we” I mean “mathematicians with centuries
of hindsight.” This is a bit subtle, but a corkscrew surface shown in Figure 14.2 illustrates
the problem.
On this surface at c = (0, 0), the directional derivative exists in every direction, but
jumps sharply as the direction rotates past the negative x1 axis. In a mildly technical
parlance we have avoided making precise in this book, the directional derivative isn’t
continuous with respect to direction. If I stand at the origin and look directly in the
direction of the jump (a ray down the negative x1 -axis), then as my gaze perturbs left
and right by any infinitesimally small amount, my view of the steepness of the surface
jumps drastically from very steeply negative to very steeply positive. This is bad because
it destroys the possibility that a derivative based on the directional derivative can serve
240
6
4
2
f(x1, x2)
0
2
4
6
2.0
2.0 1.5
1.5 1.0
1.0 0.5
0.5 0.0
0.0 0.5 x2
x1 0.5 1.0
1.0 1.5
1.5 2.0
2.0
Figure 14.2: A corkscrew function, demonstrating that directional derivatives need not
be continuous as the direction changes.
as a global approximation to f near (0, 0). It will err egregiously in the vicinity of the
jump.
As we’ll see soon, a stronger derivative definition avoids these issues. The definition
will only apply if the function can be usefully approximated by a linear function. It will
provide a linear map representing the whole function, and applying simple linear algebra
will produce the one-dimensional derviative in any direction. Since it’s linear algebra, we
even get the benefit of being able to choose a useful basis, though I haven’t yet made it
clear what the vector space in question is. That will come as we refine what the right
definition of “the” derivative should be.
f f
Figure 14.3: Left: a linear approximation without shifting f . Right shifted so that (c, f (c))
is at the origin.
one wants to travel away from c. The output is the derivative’s approximation of how
much f will change as a result. The matrix for Lf,c is the single-entry matrix [f ′ (c)].
Moreover, Lf,c (z) is exactly the first-degree Taylor polynomial for the version of f that
gets translated so that (c, f (c)) is at the origin. Figure 14.3 shows the difference.
If you don’t like shifting f to the origin, we can define the affine linear map (affine
just means a translation of a linear map away from the origin), which we’ll call a linear
approximation to f .
The linear approximator has the following obvious property, which is a restatement of
the limit definition of the derivative.
Proposition 14.4. For any differentiable f : R → R and its linear approximation L(c, x),
f (x) − L(c, x)
lim =0
x→c x−c
Proof. Split the limit into two pieces:
I spell this out in such detail because the existence of a linear approximator (an affine
linear function satisfying 14.4) becomes a definition for functions Rn → R.
242
2. The affine linear function defined by L(c, x) = A(x − c) + f (c) (which depends
on A) satisfies
f (x) − L(c, x)
lim =0
x→c ∥x − c∥
If the above both exist, we call L a linear approximation of f at c and A a total derivative
of f at c.
f(x1, x2) 5
10
15 1.0
0.00.5
0.5
1.0
1.5 x1
1.0 0.5 0.0 0.5 1.0 1.5 2.0
2.5
2.0 2.5 3.0 3.0
x2
Figure 14.4: The linear subspace defined by the total derivative of f sits tangent to the
surface of f at the point the total derivative is evaluated at.
It’s worthwhile to do some concrete examples. First in one dimension, then in three.
For single-variable functions f : R → R, at every point c the tangent space is a one-
dimensional vector space. The vectors in the vector space represent left/right deviations
of the input of f from c, and the linear map A √
describes the approximate change in f due
to this deviation. As an example, let
√ f (x) = x + 2 and consider the point (c, f (c)) =
(2, 4). The derivative of f is 1/(2 x + 2), which evaluates to 1/4 at c = 2. Thus, the
tangent space Tf (4) is a copy of R, and the total derivative at c = 2 is A(x) = 41 x. The
affine linear map is L(x) = 14 (x − 2) + 4.
In three dimensions, let f (x, y, z) = x2 + (y − 1)3 + (z − 2)4 and let c = (3, 2, 1).
The tangent space Tf (c) = R3 , and so the total derivative A : R3 → R has three-
dimensional inputs. We won’t learn how to compute this map from the definition of
f until Section 14.4, so for now we give the answer magically; it’s the following 1 × 3
matrix:
( )
A = 6 3 −4 .
And as a result
f (x) − LB (c, x)
lim ̸= 0
x→c ∥x − c∥
Proof. Let B be an arbitrary linear map different from A. We use the trick of writing
B = A + (B − A), which allows us to split the limit above in a useful way:
This validates us calling the total derivative the total derivative. There is no other linear
map that can satisfy the defining property. As such, we can define a more convenient
notation for the total derivative.
Definition 14.7. Define the notation Df (c) to mean the total derivative matrix A of f
at the point c.
A quick note on notation, D is a mapping from functions to functions, but the way it’s
written it looks like c is an argument to a function called “Df”. To be formal one might
attempt to curry arguments. D(f )(c) is a concrete matrix of real numbers, and D(f ) is
a function that takes as input a point c and produces a matrix as output. Mathematicians
often drop the parentheses to reduce clutter, and even the evaluation at c if this is clear
from context. One might also subscript the c as in Dfc , or use a pipe that usually means
“evaluated at,” as in Df |x=c . We will stick to Df (c), as it achieves a happy middle: just
think of the total derivative of f as being named Df .
245
Now that we’ve established some consistency, and understand that the total derivative
is a linear map at its core, we can dive into the details of how we compute. To make this
process cleaner, we first deviate to generalize the derivative to functions Rn → Rm .
2. The affine linear function defined by L(c, x) = A(x − c) + f (c) (which depends
on A) satisfies
Proposition 14.6 on uniqueness can be rewritten almost verbatim for Definition 14.8.
In most of the rest of this chapter, we’ll restrict to the special case m = 1. However,
the chain rule—a singularly powerful and beautiful tool that will guide our proofs and
application—shines most brightly in arbitrary dimensions. It says that the derivative of
a function composition is the product of their total derivative matrices.
Proof. The proof is a clever use of the chain rule. We prove it first for v1 and the first
component, but the same proof will hold if v1 is replaced with any vi . Fix a small ε > 0.
Define by g : [−ε, ε] → Rn the map t 7→ c + tv1 . Then define h(t) = f (g(t)). We chose
a “small ε” to ensure h is defined, which it only is if t is sufficiently close to c, but the
proof doesn’t depend crucially on the value of ε.
t7→c+tv f
h : R −−−−−→
1
Rn −−−−→ R
Note that h is a single-variable function R → R, and that h′ (0) = Dh(0) is3 exactly
Dir(f, c, v1 ), by definition of the directional derivative. Now we apply the chain rule to
h, and we get that Dh(0) = Df (c)Dg(0). As Df (c) is a 1 × n matrix, call z1 , . . . , zn the
unknown entries of Df (c), written with respect to the basis {v1 , . . . , vn }. Also note that
Dg(0) can be written as an n × 1 matrix with respect to the same basis (for the codomain
of g):
3
I’m implicitly identifying h′ (0) with the 1 × 1 matrix Dh(0).
247
1
0
Dg(0) = .
..
0
The form of Dg(0) is trivial: t 7→ tv1 has no coefficient of any other vi but v1 . Com-
bining these, Dh(0) = Df (c) · Dg(0) is the 1 × 1 matrix [z1 ], proving z1 = Dir(f, c, v1 ).
Doing this for each vi instead of v1 establishes the theorem.
The same proof can be adapted for functions Rn → Rm , which we will explore in the
exercises.
Theorem 14.10 provides two pieces of insight. The first is that the directional derivative
wasn’t so far off from the “right” definition. For “nicely behaved” functions, the total
derivative and the directional derivatives agree. There’s even a theorem that relates the
two: if the directional derivative is continuous with respect to the choice of direction,
then the directional derivative matrix from Theorem 14.10 is the total derivative.4 That
theorem implies that our initial counterexample (with a jump as the direction rotates)
is the only serious obstacle to exclusively using directional derivatives for computation.
This theorem is important enough that it deserves offsetting, despite our negligence in
providing a proof, as one can say something slightly stronger.
The standard basis is so useful because it allows one to define an easy computational
rule of thumb. For a directional derivative for basis vector dx2 , you may consider all
variables except x2 to be constants, and then apply the same rules for single-variable
derivatives the function considered just as a function of x2 . If it helps, you can imagine
a “curried” function f (x1 , x2 , x3 ) = f (x1 , x3 )(x2 ), the former part of which closes over
the fixed choices of values for x1 , x3 . The values of x1 , x3 are fixed, but unknown at the
time of derivative computation, and what’s left is a single-variable function of x2 . As an
example with f (x1 , x2 , x3 ) = x21 x2 + cos(x3 ), we have Dir(f, c, dx1 ) = 2c1 c2 + cos(c3 ).
You will prove the mathematical validity of this rule in the exercises, but I suspect most
readers have seen it and used it before.
The directional derivative along a standard basis vector—i.e., with respect to a single
variable—has a special name: the partial derivative with respect to that variable. This
is denoted using the ∂ sign (which I have always spoken “partial,” but it sometimes
called “boundary”) as ∂f /∂x2 , which is read, “the partial derivative of f with respect
to x2 .” In the same way that single variable derivatives f ′ are typically written in the
same variables as f (i.e., using x instead of c), the example above can be written as
∂f /∂x1 = 2x1 x2 + cos(x3 ). One refers to the operation of taking a partial derivative
∂
with respect to x by the function named ∂x , with the juxtaposition of the f in the nu-
merator taking place of the standard parenthetical function application. Mathematicians
have built up a hodgepodge of notations throughout history for this. In part, it’s be-
cause parentheses are slow to write on a chalkboard—though they are easy for computers
to parse, every new lisp programmer discovers they’re hard for humans to read unless
formatted just so. In part, it’s because mathematicians don’t always want to think of
derivatives as functions. Sometimes they want to highlight a different aspect, such as the
vector structure. A mess of lisp-y parentheses would not fit nicely in an inner product or
summation.
When your chosen basis is the standard basis for each variable, the resulting total
derivative matrix Df is called the gradient of f , and it’s denoted ∇f . The symbol ∇ is
often spoken “grad,” and officially called a “nabla.” We’ll discuss the gradient in more
detail below, because the gradient has some nice geometric properties that help in doing
optimization.
An example gradient for the function f (x1 , x2 , x3 ) = x21 x2 + cos(x3 ) is as follows.
Below I will write the matrix generically in the sense that it works for any choice of
c = (x1 , x2 , x3 ), in the same way that when writing a single-variable derivative one uses
the same variable before and after taking the derivative.
( )
∇f = 2x1 x2 x21 − sin(x3 )
With this, we can compute the directional derivative in the direction of a vector v =
(1, −1, 2) by applying the linear map ∇f .
249
Any way you slice it, the value we want is just one inner product away!
Many authors don’t write the gradient as a vector in this way. Instead, they denote the
basis vectors as dxi , and the gradient is written as a single linear combination of these
basis vectors. For the example f we’ve been using, it would be
This notation has the advantage that you can use it while still hating linear algebra:
this is just the inner product written out before choosing values for v1 , v2 , v3 , i.e., the
coefficients of dx1 , dx2 , dx3 in the vector v to evaluate. It also helps you keep in mind
that dxi are meant to represent deviations of xi from the point being evaluated. Some-
times they’re written as a “delta”, ∆xi or δxi , since delta is commonly used to represent a
change.5 On the other hand, since it uses the symbols dxi , it’s easy to confuse the mean-
ing with d/dxi . We learned to love linear algebra. We’ll stick to the vector notation.
Looking back, we now have exactly what we wanted: a way to compute directional
derivatives as easily as taking single-variable derivatives. And now that we have a handle
5
I find it curious how “delta” is used as a synonym for “difference” or “change” by executives in discussions
that otherwise lack precision. Perhaps they studied math and incorporated that into their natural speech, or
perhaps their faux-technical jargon impresses and confounds their enemies. I have certainly seen instances
of both.
250
on the basic definition, we can study the geometry of the gradient to see how it enables
optimization.
Henceforth, when we say “differentiable function” we mean a function with a total
derivative, we’ll assume all functions are differentiable, and we’ll seamlessly swap be-
tween total derivatives, directional derivatives, linear maps, and matrices.
1. The standard inner product ⟨w, v⟩ is the signed length of projv (w). The sign is
positive if the result of the projection points in the same direction as v and negative
if it points opposite to v.
2. If you project w onto v, and v is not on the same line as w, then ∥projv (w)∥ < ∥w∥.
3. An alternate formula for ⟨v, w⟩ is ∥v∥∥w∥ cos(θ), where θ is the angle between v
and w. In the case that ∥v∥ = 1, the formula is ∥w∥ cos(θ).
All of these point to the same general insight, which is a theorem with a famous name.
The Cauchy-Schwarz inequality has many, many proofs. I’ll just share one that uses
the cosine formula above to emphasize the geometry. You’ll do a different proof in the
exercises, and I’ll gush over it in the Chapter Notes.
Proof. From ⟨v, w⟩ = ∥v∥∥w∥ cos(θ), and since −1 ≤ cos(θ) ≤ 1, it follows that
|⟨v, w⟩| ≤ ∥v∥∥w∥.
Because cos(θ) repeats after θ = 2π, we can restrict our attention to 0 ≤ θ < 2π.
For this range, cos(θ) = 1 if and only if θ = 0, and cos(θ) = −1 if and only if θ = π.
For all other values, cos(θ) < 1. This proves the “if and only if” part of the theorem,
251
because when cos(θ) = ±1, the two vectors lie on the same line, and hence are linearly
dependent.
The details of this proof show more than the statement. Since the directional derivative
is a projection of the gradient ∇f onto a unit vector v—i.e., ⟨(∇f )(x), v⟩—if you want
to maximize the directional derivative, v should point in the same direction as (∇f )(x).
Said a different way, the gradient (∇f )(x) points in the steepest possible direction.
One is tempted to think this theorem is amazing (it is), but in fact it is not. With
linear algebra we’ve created the perfect conditions for this theorem to be not only true,
but trivial. We get further splendors for free: the direction of steepest ascent at c and
the level curve of f (the set of constant-height inputs {(x, f (x)) : f (x) = f (c)}, like
the topographic altitude lines on a map) are perpendicular to each other. This is simply
because if v is a direction on the level curve, then the height of f doesn’t change in that
direction, so 0 = D(f, c, v) = ⟨∇f, v⟩, and such inner products occur when two vectors
are perpendicular.
Since many things in life and science can be modeled using functions Rn → R, a com-
mon desire is to find an input x ∈ Rn which maximizes or minimizes such a function. For
the sake of discussion, let’s suppose we’re looking for minima. Even when a mathemati-
cal model f exists for a phenomenon, minimizing it might be algebraically intractable for
a variety of reasons. For example, it might involve functions that are difficult to separate,
such as trigonometric functions and threshold functions. Alternatively, it might simply
be so large as to avoid any human analysis whatsoever, as is often the case with a neural
network that has millions of parameters related to labeled data. The rest of this chapter
is devoted to understanding how to tackle such situations, and the core idea is to “follow”
the direction indicated by the gradient.
• Define your function f : R → R whose input x you control, and whose output
you’d like to minimize. Select a range of interest a ≤ x ≤ b.
• The optimal input x is the minimum value of f (x) where x is among the critical
points, or x = a or x = b.
For multivariable inputs, you might reasonably expect an analogous technique to work:
look at all the points x for which (∇f )(x) is the zero vector, and check them all for
optimality. Unfortunately the story is more complicated. There are still critical points—
those values x for which (∇f )(x) is the zero vector or undefined—but it’s not as simple
to enumerate them all and check which is the largest.
Take, for example, the function f (x, y) = x2 + y 2 + 2xy. Its gradient is (2x + 2y, 2y +
2x). Equating this to the zero vector results in an infinite family of solutions given by
x + y = 0. In other words, while one-dimensional functions can be reduced to a discrete
set of points to check, the solution to ∇f = 0 can be a complicated surface. Even if
you restrict just to polynomial equations life is still hard. There is an entire field of math,
called algebraic geometry, dedicated to understanding the geometry of so-called varieties.
A variety is the formal term for the space of solutions to a set of polynomial equations.
The study of varieties is interesting and nuanced, beyond what can fit in this humble
volume. Suffice it to say that understanding the shape of varieties from their defining
formulas is not trivial, so we generally shouldn’t expect to enumerate the zeros of the
gradient.
If the equations are simple enough, one can apply a classical technique called Lagrange
multipliers to compute optima. This was a central workhorse of a lot of pre-computer-
era optimization. In general, Lagrange multipliers fail to help in almost every modern
application, so we relegate it to the exercises. We’ll instead focus on a more general
algorithmic technique that works best when the function you’re optimizing is intractable
for pen-and-paper analysis. The technique is called gradient descent, and in modern times
it has grown into a huge field of study.
Gradient descent (or gradient ascent, if you’re maximizing) works as follows. Given
f , start at a random point x0 . Iteratively evaluate the gradient (∇f )(xi ), which points
in the direction of steepest ascent of f , and set xi+1 = xi − ε(∇f )(xi ), where ε is some
small scalar. The subtraction is the focus: you “take a small step” in the opposite direction
of the gradient to get closer to a minimum of f . So long as the gradient is a reasonable
enough approximator of f at each xi , each f (xi+1 ) is smaller than the f (xi ) before it.
Repeat this over and over again, and you should find a minimum of some sort.6
Gradient ascent intuitively makes sense, but there are a few confounding details that
trick this algorithm into stopping before it reaches a minimum. The devil lies in the details
of the stopping condition: if we’re at a minimum, the gradient should definitely be the
zero vector (there’s no direction of ascent at all, so there’s no “steepest” direction), but
does it work the other way as well?
Definitely not. However, to get a useful feel for why, we have to correct an injustice
from Chapter 8: we never discussed the geometry of the second derivative.
6
Or decrease without bound, but in our application zero will be an absolute lower bound by design.
253
Proof. The Taylor series is our hammer. Since f ′ (c) = 0, near c we can expand f (x)
using a Taylor series that primarily depends on f ′′ (x).
7
As we saw in Chapter 8, there are nonzero functions so flat at a point that all of their derivatives are zero!
254
10
4
y = x2
y = 0.5x2
2 y = 5x2
0
4 2 0 2 4
2
15
10
0
3 2 1 0 1 2 3 4 5
5
10
15
1 (x 1)2(x 4)(x + 2)
2
20
f ′′ (c)
f (x) = f (c) + (x − c)2 + r(x)
2
Here r(x) is the remainder term of the Taylor Theorem (Theorem 8.14). It’s a degree-
3 polynomial in x − c whose coefficient depends on an evaluation of f (3) (z) at some
unknown point z ∈ (c, x). The most important detail of this is that it’s a degree-3 poly-
nomial, but in complete detail, it’s
f (3) (z)
r(x) = (x − c)3 for some unknown z between c and x.
6
We need to argue that because x − c is very small when x is close to c, the value
of (x − c)3 is dwarfed by the value of (x − c)2 , so that the min/max behavior of f is
determined solely by the (x − c)2 term.
Indeed, if you could informally argue that—say, by erasing r(x) with reckless
abandon—then f (x) would be a simple, shifted parabola. The sign of f ′′ (x) would dictate
whether the curve is concave up or concave down, and the peak would obviously be a
min or a max (respectively). To make it more rigorous, we restrict ourselves to a small
interval.
Let’s suppose that f ′′ (x) > 0, so that we need to show f (c) is a local min. In this
case we want an interval (a, b) on which f (c) ≤ f (x) for all x. Rearranging the formula
above,
f ′′ (c)
f (c) = f (x) − (x − c)2 − r(x).
2
′′
If the term [− f 2(c) (x − c)2 − r(x)] is not positive on (a, b), then f (c) ≤ f (x). So
the theorem will be proved if we can find an interval on which that term is at most zero.
Rearranging, we need the following inequality to hold:
2f (3) (z)
(x − c)2 ≥ (x − c)3
6f ′′ (c)
Since the value of r(x) depends on z (which can be different for different values of
x), we can’t proceed unless we eliminate the dependence on z. We’ll do that by esti-
mating, i.e., replacing f (3) (z) with the max of f (3) over an interval. So start with some
fixed interval around c, say (c − 0.01, c + 0.01),8 and let M be the maximum value of
f (3) (z)/(3f ′′ (c)) on that interval. I.e., M is the largest value of the coefficient of (x − c)3
in the above inequality that can occur close to c. Then we need to find an interval, per-
haps smaller than (c − 0.01, c + 0.01), for which the following (simplified) inequality is
true for all x in that interval.
8
All we need is any interval on which f is defined and has no pathological or discontinuous behavior. This is
guaranteed to exist because f is differentiable at c. To be completely rigorous one should use (c − ε, c + ε)
and argue existence of such by continuity/differentiability, but you get the point.
256
(x − c)2 ≥ M (x − c)3
But this is easy! So long as x ̸= c we can simplify to see we just need a small enough
interval that ensures (x − c) ≤ 1/M . This will be true of either (c − 1/M, c + 1/M ) or
(c − 0.01, c + 0.01), whichever is smaller.
That was a lot of work to achieve a proof. Recalling our discussion of waves in Chap-
ter 12, the reader might begin to understand why a working physicist would rather erase
terms with reckless abandon than wade through the strange existential z’s that plague
Taylor series. However, as was the case with matrix algebra providing an elegant (though
intentionally leaky) abstraction for linear maps, mathematical analyses like these have
their own abstractions to aid computation while maintaining rigor. In this case, most
programmers are aware of it: big-O notation. We’ll display its use in Chapter 15.
When f ′′ (x) = 0, we can’t conclude anything. f might have a max/min, or it might
have neither. One example of having neither is f (x) = x3 at x = 0. The function
switches concavity from concave down to concave up, but f has no local max or min.
The idea of “local” behavior is a powerful one across mathematics. It is almost always
easier to talk about local properties of an object rather than the global structure. A lot of
time is spent investigating how a collection of unrelated bits of local information affect
a global property. For single variable functions, one incarnation of this is that the local
mins and maxes of f —along with a slight amount of extra information—determines the
global min/max of f .
One can also think of a directional derivative as a sort of “local” property. It’s the
derivative when one “only looks” in a certain window, while the total derivative is global.
If you can show that each directional derivative is continuous—or even just that the par-
tial derivatives are continuous—then you automatically get the global (total) derivative.
You have built global structure out of local pieces. Of course, the total derivative at a
point is also a local construct from a different perspective. The total derivative describes
the approximate structure of f at a point, and with enough information about the total
derivative at every point of f (and a few bits of extra information), you can completely
reconstruct f . So there are multiple scales of locality that allow one to discuss local and
global properties, and how they relate to each other.
The Hessian
For multivariable functions, locality replaces an interval with an “open ball,” i.e., a set
Br (c){x : ∥x − c∥ < r}, which consists of all the points within a given radius of the
point in question. The radius takes the place of the length of the interval to say “how
local” you’re looking.
While there are still local maxes and mins of the obvious sort, there are many ways
a local min/max can fail to exist. These are called saddle points. The shape of these is
quite literal: the surface looks like the saddle of a horse, or the shape of a potato chip, in
257
3
2
1
f(x1, x2)
0
1
2
3
2.0
2.0 1.5 1.0 0.5 0.51.01.5
0.0 0.5 1.0 1.5 1.51.00.50.0 x2
x1 2.0 2.0
which the curvature goes up along one direction and down along another. A prototypical
example of a curve with a saddle point is f (x, y) = x2 − y 2 , pictured in Figure 14.7.
With many variables comes many different directions along which curvature can dif-
fer. You might imagine a function with 5 variables, each axis giving two choices of up-
curvature or down-curvature, for a total of 25 = 32 different kinds of saddles (including
the normal max/min). The way to get a handle on these forms is to look at the matrix of
all ways to take second derivatives. First we define notation for second derivatives.
Definition 14.15. Let f : Rn → R be a function which has first partial derivatives
for every variable (recall, denoted ∂f /∂xi ). The second (or mixed) partial derivative with
respect to xi and xj is the partial derivative of the partial derivative. A compact notation
for this is
( )
∂2f ∂ ∂f
=
∂xi ∂xj ∂xj ∂xi
If i ̸= j, the derivative is called a mixed partial. If i = j we write ∂ 2 f /∂x2i .
Personally I hate this notation, particularly how arbitrarily it’s defined so that the “nu-
merator” of the variable names are smushed together. My inner programmer cries out in
anguish, because it’s breaking algebra and functional notation at the same time by pre-
tending they’re the same. Are we taking the squared derivative with respect to a squared
variable? Multiplying the top and bottom of a function name separately? Your syntactic
sugar is rotting my brain! Alas, the notation is widespread, and the only alternative I
2f
know of, fxi xj (x) = ∂x∂i ∂xj
, is not all that much better.
258
One might expect the mixed partials with respect to xi , xj and xj , xi to be different
due to the order of the computation. When f has a total derivative, they turn out to be
the same.
We quote this theorem without proof, but notice that, in addition to reducing our com-
putation duties by a half, it gives a hindsight rationalization for the fraction notation. If
the order of partial derivatives doesn’t matter, then we need not bother with the func-
tional notation that emphasizes order precedence.
Next we define the Hessian, which is the matrix of mixed partial derivatives of a func-
tion.
Just like the gradient, H(f ) is really a function whose input is a point x in the domain
of f , and the output is the matrix H(f )(x). The notation gets even hairier since H(f )(x)
is itself a linear map Rn → Rn . In an exercise you’ll interpret this linear map to make
more sense of it.
Because of Schwarz’s theorem, any point x we use to make H(f ) concrete produces
a real symmetric matrix. As we know from Chapter 12, symmetric matrices have an or-
thonormal basis of real eigenvectors with real eigenvalues, and so we can ask what these
eigenvalues tell us about the structure of f local to x. The theorem is a nice generalization
of the min/max structure for single variable functions.
We’ll skip the proof for brevity, but our understanding of eigenvalues and eigenvectors
provides a tidy interpretation. The eigenvectors of nonzero eigenvalues correspond to
the directions (when looking from x) in which the curvature of f is purely upward or
259
3
2
1
f(x1, x2)
0
1
2
3
2.0
0.51.01.5
2.0 1.5 0.0
1.0 0.5 0.5 x2
0.0 0.5 1.0
x1 1.0 1.5 1.5
2.0 2.0
Figure 14.8: A function with a saddle point. The eigenvectors of the Hessian at the saddle
point are shown as arrows, and represent the maximally positive and negative curvatures
at the saddle point.
downward, and maximally so. In a sense that can be made rigorous, because H has an
orthonormal basis of eigenvectors, these curvatures “don’t interfere” with each other. If
one has an ellipsoidal bowl, the eigenvectors correspond to the “axes” of the bowl. For
a saddle point, the eigenvectors are the directions of the saddle that are parallel and
perpendicular to the imagined horse’s body. This is shown in Figure 14.8.
Of course, all of this breaks down if the sort of curvature we’re looking at can’t be
captured by second derivatives. There might be an eigenvalue of zero, in which case you
can’t tell if the curvature is positive, negative, or even completely flat.
But this raises a natural question: if the gradient gives you first derivative information,
and the Hessian gives you second derivative information, can we get third derivative
information and higher? Yes! And can we use these to form a sort of “Taylor series”
for multivariable functions? More yes! One difficulty with this topic is the mess of nota-
tion. A fourth-derivative-Hessian analogue is a four-dimensional array of numbers. With
more dimensions comes more difficulty of notation (or the need for a better abstraction).
Nevertheless, we can at least provide the analogue of the Taylor series for the first two
terms:
f1 f2 ... fm
x1 x2 ... xn
Figure 14.9: The dependence of g ◦ f on each xi contains paths through each of the fj .
Let’s first think about why this should be harder in principle than the single variable
case. Call x = (x1 , . . . , xn ) the variables input to f = (f1 , . . . , fm ), a function Rn → Rm .
The derivative of g ◦ f measures how much g depends on changes to each xi . But while
f depends on an input xi in a straightforward way, g depends on xi transitively through
the possibly many outputs of f . Computing ∂g/∂xi should require one to combine the
knowledge of ∂fj /∂xi for each j, and that combination might be strange. The function
g ◦ f is mapped out by a dependency graph like in Figure 14.9, where the arrows a → b
indicate that b depends on a. A similar dependence describes dependence among the
partial derivatives.
Luckily the relationship is quite elegant: for one dependent variable you multiply along
each branch and sum the results. Doing this for every input variable produces exactly the
matrix multiplication that makes up the chain rule. We’ll prove a slightly simpler version
of the chain rule where g has only one output, which has all the necessary features of the
more general proof where g = (g1 , . . . , gk ) is vector-valued.
261
∂g ∑ ∂g
m
∂hi
(c) = (h(c)) · (c)
∂x1 ∂hi ∂x1
i=1
Proof. For clarity, in this proof the boldface v will denote a vector of numbers or func-
tions (a function with multiple outputs). Denote by h(x) = (h1 (x), . . . , hm (x)), so that
we can conveniently abbreviate g(h1 (x), . . . , hm (x)) as g(h(x)). Let H be the matrix
representation of the total derivative of h,
H1
H2
H= ..
.
Hm
Let G be the matrix representation of the total derivative of g (i.e., ∇g). The claimed
total derivative matrix for g(h(x)) is the matrix multiplication GH. This results in the
formula claimed by the theorem. We need to show that GH satisfies the linear approxi-
mation condition for g(h(x)), i.e., that
Define s = H(t) + errH (t), so that we can substitute g(h(c) + s) using a rewriting of
the definition of errG .
G(errH (t))
lim .
t→0 ∥t∥
Note that because G is a gradient, G(errH (t)) is an inner product—the projection of
errH (t) onto a fixed vector, ∇g(c). The Cauchy-Schwarz inequality informs us that the
norm of G(errH (t)) is bounded from above by CerrH (t), where C = ∥∇g(c)∥ is con-
stant. So the limit above is
errH (t)
C lim = 0.
t→0 ∥t∥
This goes to zero because (by the definition of errH ) it’s the defining property of the
total derivative of H. It remains to show the second part is zero:
errG (s)
lim
t→0 ∥t∥
We would like to bound this limit from above by a different limit we can more easily
prove goes to zero. Indeed, if there were a constant B for which
The quantity on the right hand side is a directional derivative of h (rather, a vector of
directional derivatives), and for sufficiently small t, the quantity is no larger than twice
the largest possible directional derivative, i.e., 2∥(∇h1 (c), . . . , ∇hm (c))∥. Choose B so
that 1/B is larger than this quantity, and the proof is complete.
263
This was the most difficult proof in this book. And it’s easy to get lost in it. We started
from a relatable premise: find a formula for the chain rule for multivariable functions.
To prove our formula worked, we reduced progressively trickier and more specialized
arguments, boiling down to an arbitrary-seeming upper bound of a haphazard limit of an
error term of a linear approximation.
To be sure, the steps in this proof were not obvious. One has to take a bit of a leap of
faith to guess that GH was the right formula (though it is the simplest and most elegant
option), and then jump from an obtuse limit to the realization that, if one writes every-
thing in terms of error terms, the hard parts (g composed with h) will cancel out. Suffice
it to say that this proof was distilled from hard work and many examples, and it leaves
a taste of mystery in the mouth. Until, that is, one dives deeper into the general subfield
of mathematics known as “analysis,” where arguments like this one are practiced until
they become relatively routine. One gains the nose for what sorts of quantities should
yield their secrets to a well-chosen upper bound. Contrast this to subjects like linear al-
gebra and abstract algebra (Chapter 16), in which pieces largely tend to fit together in a
structured manner that—in my opinion—tends to appeal to programmers in a way that
analysis doesn’t. Another demonstration of subcultures in mathematics.
3. Output x.
This algorithm can be fast or slow depending on the choice of the starting point and
the smoothness of f . If x lands in a bowl, it will quickly find the bottom. If x starts on
a plateau of f , it will never improve. For this reason, one often runs multiple copies of
this loop, and outputs the most optimal run. If the inputs are chosen randomly, there’s a
good chance one avoids the avoidable plateaus.
264
∂g ∑ ∂g ∂hi
k
= .
∂x1 ∂hi ∂x1
i=1
In more generality, one will often have a function which depends on some input pa-
rameter transitively through many layers of functions. To compute these often requires
a long “chain” of partial derivatives. Ignoring that there are sums involved (or assuming
only one dependency branch), you’d get chains like this:
∂f ∂f ∂g ∂h ∂i ∂j
= .
∂x ∂g ∂h ∂i ∂j ∂x
So if you were doing an on-paper analysis of some complex function, you’d generally
break it up into parts. More useful for software is to observe that the terms in such a
big product can be grouped and re-grouped arbitrarily. For example, if you’ve already
computed ∂g ∂f
∂j , then to get ∂x you need only compute the missing terms
∂f ∂f ∂g ∂j
= .
∂x ∂g ∂j ∂x
This allows one to use caching to avoid recomputing derivatives over and over again.
That’s especially useful when there are many dependency branches. In fact, as we’ll real-
ize concretely when we build a neural network, the concept of derivatives with branching
dependencies is core to training neural networks. To prepare for that, we’ll describe the
abstract idea of a computation graph and reiterate how the chain rule is computed reur-
sively through such a network.
265
x1 —
+
x2
*
x3 log
4. There is exactly one vertex v ∈ V with no outgoing edges designated as the output
vertex.
If there’s an edge (v, w), we say that v is an argument to w and that w depends on v.
a1 h1
a2 f h2
... ...
an hk
Figure 14.11: A generic node of a computation graph. Node f has many inputs, its output
feeds into many nodes, and each of its inputs and outputs may also have many inputs
and outputs.
of G with respect to an input variable that may be even deeper than f . Using the chain
rule, we’ll describe the algorithm for computing the derivative generically at any vertex
and then apply induction/recursion. More specifically, at vertex f we’ll compute ∂G/∂f
and multiply it by ∂f /∂ai to get ∂G/∂ai .
So given a vertex with operation f (a1 , . . . , an ), argument vertices a1 , . . . , an , and
whose output is depended on by vertices h1 , . . . , hk . We’re interested in computing
∂G/∂a1 (with the other arguments ai to f being analogous). This is illustrated in Fig-
ure 14.11.
We know ∂f /∂a1 by assumption, having designed the graph so the gradient ∇fv of
each vertex v is easy to compute. By induction, for each output vertex hj we can compute
∂G/∂hj . Then apply the chain rule:
∑
k
∂G ∂hi
∂G/∂f = · .
∂hi ∂f
i=1
Once we have that, each ∂G/∂ai = (∂G/∂f ) · (∂f /∂ai ), as desired. Note that if ai
has another path to G, they need to be summed.
Because we use the vertices that depend on f as the inductive step, the base case is the
output vertex, and there ∂G/∂G = 1. Likewise, the top of the recursive stack are the
input vertices, and at the end we’ll have ∂G/∂xi for all inputs xi .
As one can easily see, a network with heavily interdependent vertices requires one
to cache the intermediate values to avoid recomputing derivatives everywhere. That’s
exactly the strategy we’ll take with our neural network.
267
1. Collect a large sample of handwritten digits, and clean them up (as all programmers
know, we must sanitize our inputs!).
2. Get humans to provide labels for which pictures correspond to which digits.
3. Run a machine learning training algorithm on the labeled data, and get as output
a classifier that can be used to label new, unseen data.
One usually defines an allowed universe of possible classifiers—say, the class of deci-
sion trees that make decisions based on individual pixels—and the training algorithm uses
the data to select a decision tree. An example decision tree might ask yes/no questions
like, “does pixel (12, 25) have intensity higher than 128?” The answer determines the
next question to ask, and eventually the final classification.
A slow, brutish training algorithm might be: generate all possible decision trees in
increasing order of size, and select the first one that’s consistent with the data.
To get a more pungent whiff, let’s jump right into the handwritten digit dataset we’ll
use in the remainder of this chapter. The dataset is a famous one that goes by the irrele-
vant acronym MNIST (Modified National Institute of Standards and Technology referring
to the institution that created the original dataset). The database consists of 70,000 data
268
It's a 7!
yes
yes yes
no
start pixel (12, 15) > 128? pixel (6, 20) > 0?
no no
... It's a 0!
Figure 14.12: An example decision tree classifying an image by looking at specific pixels.
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 115 121 162 253 253 213 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 63 107 170 251 252 252 252 252 250 214 0 0 0 0 0 0
0 0 0 0 0 0 0 0 25 192 226 226 241 252 253 202 252 252 252 252 252 225 0 0 0 0 0 0
0 0 0 0 0 0 0 68 223 252 252 252 252 252 39 19 39 65 224 252 252 183 0 0 0 0 0 0
0 0 0 0 0 0 0 186 252 252 252 245 108 53 0 0 0 150 252 252 220 20 0 0 0 0 0 0
0 0 0 0 0 0 70 242 252 252 222 59 0 0 0 0 0 178 252 252 141 0 0 0 0 0 0 0
0 0 0 0 0 0 185 252 252 194 67 0 0 0 0 17 90 240 252 194 67 0 0 0 0 0 0 0
0 0 0 0 0 0 83 205 190 24 0 0 0 0 0 121 252 252 209 24 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 77 247 252 248 106 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 253 252 252 102 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 134 255 253 253 39 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 6 183 253 252 107 2 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 102 252 253 163 16 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 13 168 252 252 110 2 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 41 252 252 217 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 40 155 252 214 31 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 165 252 252 106 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 43 179 252 150 39 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 137 252 221 39 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 67 252 79 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Figure 14.13: A training point for a digit 7 (aligned to make it easier to see).
points, each of which is a 28-by-28 pixel black and white image of a handwritten digit.
The digits have been preprocessed in various ways, including resizing, centering, and anti-
aliasing. The raw dataset was originally created around 1995, and since 1998 the machine
learning researchers Yan LeCun, Corinna Cortes, and Christopher Burges have provided
the cleaned copy on LeCun’s website.10 We also include a copy in the code samples for
this book, since their version of the dataset has a non-standard encoding scheme.
MNIST is the Petersen graph of machine learning: every technique should first be
tested on it as a sanity check. Figure 14.13 shows an example of a training point with
label 7, pretty-printed from its raw format as a flat list of 784 ints.
10
http://yann.lecun.com/exdb/mnist/
269
The data is split into a training set and a test set, the former having 60,000 examples
and the latter 10,000, which are stored in separate files. The separation exists to give a
simulation of how well a classifier trained on the training data would perform on “new”
data. As such, to get a good quality estimate, it’s crucial that the training algorithm uses
no information in the test set. We load the data using a helper function, which scales the
pixel values from [0, 255] to [0, 1]. For our application, we’ll simplify the problem a bit to
distinguishing between two digits: is it a 1 or a 7? The digit 1 corresponds to a label of 0,
and a digit 7 corresponds to a label of 1.
def load_1s_and_7s(filename):
print('Loading data {}...'.format(filename))
examples = []
with open(filename, 'r') as infile:
for line in infile:
if line[0] in ['1', '7']:
tokens = [int(x) for x in line.split(',')]
label = tokens[0]
example = [x / 255 for x in tokens[1:]] # scale to [0,1]
if label == 1:
examples.append([example, 0])
elif label == 7:
examples.append([example, 1])
print('Data loaded.')
return examples
But before we go on, I must emphasize that the first two steps in the “machine learning
recipe,” collecting and cleaning data, are much harder than they appear. A misstep in any
part of these processes can cause wild swings in the quality of the output classifier, and
getting it right requires clear and strict procedures. We were fortunate enough to have
LeCun and his colleagues vet MNIST for us. These prepared datasets are like goods in
supermarkets. A shopper doesn’t see, appreciate, or viscerally comprehend the amount of
work and resources required to rear the cow and grow the almonds, nor even the general
form of the pipeline. A common refrain among data scientists and machine learning
practitioners is that machine learning is 10% machine learning, and 90% data pipelines.
For example, deciding on the meaning of a label is no simple task. It seems easy for
problems like handwritten digits, because it’s mostly unambiguous what the true label
for a digit is. But for many interesting use cases—detecting fraud/spam, predicting what
video a user will enjoy, or determining whether a loan applicant should receive a loan—
determining what constitutes a positive or negative label requires serious thought, or
worse, the hindsight of a disaster caused by getting it wrong. Harder still are the system-
level implications of how a classifier will be used. If a video website deploys a system
that naively optimizes for a shallow metric like total time watched, creators will upload
superficially longer videos. This wastes everyone’s time and hurts the reputation of the
site.
Another concern is bias in the training data. Not just statistical bias, which can be a
result of errors in data collection on the part of the process designer, but human bias be-
yond one’s control. When you collect data on human preferences, it’s easy for population
270
majorities to overwhelm less prevalent signals. This happens roughly because machine
learning algorithms tend to look for the statistically dominant trends first, and only cap-
ture disagreeing trends if the model is complex enough to have both coexist. Think of
Chapter 12 in which we studied a physical model by throwing out small order terms. In
this context, if those terms corresponded to a coherent group of users, those users would
be ignored or actively harmed by the mathematical model.
Even worse, active discrimination can be encoded into training labels. If one trains
an algorithm to predict job fitness on a dataset of hiring information, incorporating the
reviews of human interviewers can muddy the dataset. You have to be aware that hu-
mans, and especially humans in a position of power, can exhibit bias for any number of
superficial characteristics that are unrelated to job fitness, most notably that an applicant
looks and behaves like the people currently employed. An algorithm trained on this data
will learn to mimic the human preferences, which may be unrelated to one’s goal.
While mathematics and engineering do weigh in on these problems, it’s extremely im-
portant to realize that the transition to numbers and equations doesn’t magically avoid
problems like bias and bad process. If anything it obfuscates them from those who aren’t
fluent in the language. All the user sees is the biscuit that the algorithm decided was
appropriate for them to eat. When math is applied to the real world, it serves as a model
with assumptions as a foundation. If the assumptions disagree with reality, the levee will
break. Riots can literally ensue. We acutely understand this in software: most systems
rely on a mess of consistency constraints, some validated explicitly and others not, and
when you put garbage data into a software system, you’ll get garbage results. So it is
for machine learning, which is why it’s sometimes called the “high interest credit card
of technical debt.” These sorts of problems, though interesting and important, are be-
yond the scope of this book. Instead we’ll focus on the “easy” part, actually training an
algorithm and producing a classifier.
Linear threshold functions have n + 1 parameters: the n weights w and the bias b. The
linear threshold function lives up to its name, thanks to the geometry of the inner product.
In particular, w defines an (n − 1)-dimensional vector space w⊥ = {v : ⟨w, v⟩ = 0},
which splits Rn into two halves.11 If b = 0, then w⊥ passes through the origin, and the
inner product ⟨w, x⟩ is positive or negative depending on whether x is on the same side
of w⊥ as w or the opposite side (respectively). If b ̸= 0, then w⊥ is shifted away from the
origin by a distance of b in the direction of −w.
One must also decide how to measure the quality of a proposed classifier. Measures
vary depending on the learning model, but in practice it usually boils down to: does the
classifier accurately classify the slice of data that has been cordoned off solely for the
purpose of evaluation? This special slice of data is the test set. In the exercises, we’ll
explore a handful of theoretical learning models that give provable guarantees. Though
these models are theoretical—for example, they assume the true labels have a particular
structure—they serve as the foundation for all principled machine learning models. In
these models, if a classifier is accurate on a test set, it will provably generalize to accu-
rately classify new data.
A simple example learning model and problem, which is a building block for many
other learning problems,12 is the following. Given labeled data points chosen randomly
from a distribution over Rn that can be separated by a linear threshold function, design
an algorithm that finds a “good” threshold function, i.e., one that will generalize well to
new examples drawn from the same distribution. We’ll explore this more in the exercises.
Summarizing, given a hypothesis class H and a dataset S, a learning algorithm takes as
input S and produces as output a hypothesis h ∈ H. We want training algorithms to be
efficient and classification to be “correct,” where correct means that h should accurately
classify the test data.13
1.0
y = ex/(1 + ex)
0.8
0.6
0.4
0.2
0.0
4 2 0 2 4
The most important component operation that is used to build up a neural network is
the linear halfspace, the same Lw,b of Definition 14.22. We’ll call a vertex of the compu-
tation graph corresponding to a linear halfspace a linear node, and each linear node will
have its own distinctly tunable set of parameters, the choice of w and b.
However, there must be more to a neural network than linear nodes. As we know
well from linear algebra, a composition of linear functions is still linear. The geometry
of the space of handwritten digits is probably more complicated than a linear function
can model. That is to say, we need to include operations in our computation graph that
transform the input examples in nonlinear ways.
A historically prevalent operation is the sigmoid function, that is, the single-variable
function defined by σ(x) = ex /(1 + ex ), with the graph depicted in Figure 14.14.
The sigmoid is clearly nonlinear, nice and differentiable, and its output is confined to
[0, 1]. You may hear of this operation being compared to the “impulse” of a neuron in
a brain, which is why the sigmoid is often called an activation function. Though neural
14
The full program is available in the repository linked at pimbook.org.
273
x1
linear → sigmoid
. . .
. . .
. . .
x784
networks are called “neural,” the name is mostly an inspiration. Simply put, sigmoids and
other activation functions introduce nonlinearity in a useful way.
Typically, one applies the single-input activation function to the output of every linear
node. Occasionally, the combined pair of a linear node and its activation function are
called a neuron. Activation functions usually do not have tunable parameters.
Another important activation function, which is particularly popular in deep learning,
is the rectified linear unit.
Definition 14.23. The ReLU function is the function
{
x if x ≥ 0
ReLU(x) =
0 otherwise
Equivalently, it can be defined as ReLU(x) = max(0, x).
A ReLU needs no plot, as it’s simply the function: truncate negative values to zero.
The ReLU is particularly interesting because it is not differentiable! However, it’s only
fails to have a derivative at x = 0, and in practice one can simply ignore the problem.
One nice thing about the ReLU, which is particularly nice when you need lightning-fast
computations for training massive networks, is that its evaluation and derivative require
only branching comparisons and constants. No exponential math is required.
The network we’ll build is architected (quite arbitrarily, as it happens) as depicted in
Figure 14.15. The leftmost layer consists of the 784 input nodes, which are inputs to each
node of the first layer of 10 linear nodes, each of which has a ReLU activation function.
The outputs of the first-layer ReLUs feed as input to a second layer of 10 linear nodes,
again with ReLUs, and the output of those goes into a final single linear node with a
sigmoid activation.
274
def build_network():
input_nodes = InputNode.make_input_nodes(28*28)
linear_output = LinearNode(second_layer_relu)
output = SigmoidNode(linear_output)
error_node = L2ErrorNode(output)
network = NeuralNetwork(output, input_nodes, error_node=error_node)
return network
The final output of the network is a real number in [0, 1]. Labels are binary {0, 1}, and
so we interpret the output as a probability of the label being 1. Then we can say that the
label predicted by a network is 1 if the output is at least 1/2, and 0 otherwise.
You might be wondering how someone comes up with the architecture of a neural
network. The answer is that there are some decent heuristics, but in the end its an en-
gineering problem with no clear answers. Our network is quite small, only about 7,500
tunable parameters in all (because it’s written in pure Python, training a large network
would be prohibitively slow). In real production systems, networks have upwards of
millions of parameters, and the process of determining an architecture is more alchemy
than science. There is a now-famous 2017 talk by Ali Rahimi in which he criticized what
he argued was a loss of rigor in the field. He quoted, for example, how a change to
the default rounding mechanism in a popular deep learning library (from “truncate” to
“round”) caused many researcher’s models to break completely, and nobody knew why.
The networks still trained, but suddenly failed to learn anything. Rahimi argues that brit-
tle optimization techniques (gradient descent) applied to massively complex and opaque
networks create a house of cards, and that theory and rigor can alleviate these problems.
Brittle or not, gradient descent on neural networks has proved to be remarkably useful,
making some learning problems tractable despite the failure of decades of research into
other techniques. So let’s continue.
Once we’ve specified a neural network as a computation graph and obtained a dataset
S of labeled examples (x, l), we need to choose a function to optimize. This is often
called a loss function. For a single labeled example (x, l), it’s not so hard to come up with
a reasonable loss function. Let fw be the function computed by the neural network and
w the combined vector of all of its parameters. Then define E(w) = (fw (x) − l)2 as
the “error” of a single example. This is just the squared distance of the output of f on
an example from that example’s label. Note we’re not doing any rounding here, so that
f (x) ∈ [0, 1].
If we wanted to convert this to a loss function for an entire training dataset, we could,
1 ∑ 2
as Etotal (w) = |S| (x,l)∈S (fw (x) − l) . Then the natural method is to use gradient
descent to minimize Etotal . However, this loss function requires us to loop over the entire
275
training dataset for each step of gradient descent. That is prohibitively slow. Instead, one
rather applies what’s called stochastic gradient descent. In stochastic gradient descent,
one chooses an example (x, l) at random, and applies a gradient descent step update to
E(w) = (fw (x) − l)2 . Each subsequent gradient step update uses a different, randomly
chosen example. The fact that this usually produces a good result is not obvious.15
There are many different loss functions, and the loss function we chose above is called
the L2 -loss. The name L2 comes from mathematics, and the number 2 describes the 2’s
∑ 1/2
that occur in the formula for the norm: ∥x∥2 = ( i x2i ) . Changing the 2 to, say, a
3 results in an L3 norm, and for a general p these are called Lp norms. You will explore
different loss functions in the exercises.
As we outlined in Section 14.9, each vertex of our computation graph needs to know
about various derivatives related to the operation computed at that node, and that these
values need to be cached to compute a gradient efficiently. Now we’ll see one way to
manifest that in code. Let’s start by defining a generic base node class, representing
a generic operation in a computation graph. We’ll call the operation computed at that
node f , which has arguments z1 , . . . , zm , and possibly tunable parameters w1 , . . . , wk .
f = f (w1 , . . . , wk , z1 , . . . , zm )
Call the function computed by the entire graph E. The inputs to E are both the normal
inputs and all of the tunable parameters at every node. For the sake of having good
names, we’ll define the global derivative of some quantity x to mean ∂E/∂x, while the
local derivative is ∂f /∂x (it’s local to the node we’re currently operating with). These
are not standard terms.
Now we define a cache to attach to each node, whose lifetime will be a single step of
the gradient descent algorithm.
class CachedNodeData(object):
def __init__(self):
self.output = None
self.global_gradient = None
self.local_gradient = None
self.local_parameter_gradient = None
self.global_parameter_gradient = None
The attributes are as follows, with each expression evaluated at the current input x and
the current choice of tunable parameters.
Now we define a base class Node for the vertices of the computation graph. Its
children are InputNode, ConstantNode, LinearNode, ReluNode, SigmoidNode, and
L2ErrorNode. Here’s an example of how the subclasses of Node are used to build a
computation graph:
class Node(object):
def __init__(self, *arguments):
# if has_parameters is True, child class must set self.parameters
self.has_parameters = False
self.parameters = []
self.arguments = arguments
self.successors = []
self.cache = CachedNodeData()
'''Argument nodes z_i will query this node f(z_1, ..., z_k) for �f�/z_i,
so we need to keep track of the index for each argument node.'''
self.argument_to_index = {node: index for (index, node) in enumerate(arguments)}
The list of arguments is ordered, so that all inputs and gradients correspond index-wise.
We’ll define the core methods in Node that perform gradient descent training momentar-
ily, but first we have to define what functions the subclasses need to implement. They
are:
the output of this node, by recursively calling output on the argument nodes and
performing an operation to produce an output.
The example of the linear node illustrates each of these pieces. Let
f (w, b, x) = ⟨w, x⟩ + b
∑n
=b+ wi , x i
i=1
We model the bias term b by adding an extra input as a ConstantNode. We also have
a simple InputNode for the input to the whole graph.
class ConstantNode(Node):
def compute_output(self, inputs):
return 1
class InputNode(Node):
def __init__(self, input_index):
super().__init__()
self.input_index = input_index
@staticmethod
def make_input_nodes(count):
'''A helper function so the user doesn't have to keep track of
the input indexes.
'''
return [InputNode(i) for i in range(count)]
Now we can define LinearNode. First, we initialize the weights and add a constant
node for the bias. In this way, the bias is treated the same as any other input, which
makes the formulas convenient.
278
class LinearNode(Node):
def __init__(self, arguments):
super().__init__(ConstantNode(), *arguments) # first arg is bias
self.initialize_weights()
self.has_parameters = True
self.parameters = self.weights # name alias
def initialize_weights(self):
arglen = len(self.arguments)
# set the initial weights randomly, according to a heuristic distribution
weight_bound = 1.0 / math.sqrt(arglen)
self.weights = [random.uniform(-weight_bound, weight_bound) for _ in
range(arglen)]
∂f ∂f ∂E ∂E ∂f
= wi , = xi , =
∂xi ∂wi ∂wi ∂f ∂wi
This turns into code as follows:
class LinearNode(Node):
[...]
def compute_local_gradient(self):
return self.weights
def compute_local_parameter_gradient(self):
return [arg.output for arg in self.arguments]
def compute_global_parameter_gradient(self):
return [
self.global_gradient * self.local_parameter_gradient_for_argument(argument)
for argument in self.arguments
]
The other nodes are defined similarly, with the parameter functions returning empty
lists as the LinearNode is the only node with tunable parameters. For each of the four
279
compute_ methods defined on each child class, we define corresponding methods on the
parent class that check the cache and call the subclass methods on cache miss. They all
look more or less like this:
class Node:
[...]
@property
def local_gradient(self):
if self.cache.local_gradient is None:
self.cache.local_gradient = self.compute_local_gradient()
return self.cache.local_gradient
The methods in the child classes use these properties when referring to their arguments,
so the values will be lazily evaluated and then cached as needed. Finally, the computation
of the global gradient for a node doesn’t depend on the formula for that node, so it can
be defined in the parent class.
class Node:
[...]
def compute_global_gradient(self):
return sum(
successor.global_gradient * successor.local_gradient_for_argument(self)
for successor in self.successors)
At this point we’ve enabled the computation of all the gradients we need to do a step
of gradient descent.
class Node:
[...]
Recall, each subclass defines its vector of parameters, and the global_parameter_gradient
has to line up index by index. Also recall that we’re subtracting because we want to
minimize the error function E, and ∇E points in the direction of steepest increase of E.
The very last node of the computation graph, which computes the error for a training
example, has some extra methods that depend on a training example’s label. For the L2
error, the entire class is:
280
class L2ErrorNode(Node):
def compute_error(self, inputs, label):
argument_value = self.arguments[0].evaluate(inputs)
self.label = label # cache the label
return (argument_value - label) ** 2
def compute_local_gradient(self):
last_input = self.arguments[0].output
return [2 * (last_input - self.label)]
def compute_global_gradient(self):
return 1
Now we define a wrapper class NeuralNetwork that keeps track of the input and
terminal nodes of the computation graph, resets caches, and controls the training of the
network. We start with a self-explanatory constructor, and a helper function for applying
some function to each node of the computation graph exactly once.
class NeuralNetwork(object):
def __init__(self, terminal_node, input_nodes, error_node=None, step_size=None):
self.terminal_node = terminal_node
self.input_nodes = input_nodes
self.error_node = error_node or L2ErrorNode(self.terminal_node)
self.step_size = step_size or 1e-2
while nodes_to_process:
node = nodes_to_process.pop()
func(node)
processed.add(node)
nodes_to_process |= set(node.arguments) - processed
The for_each function performs a classic graph traversal (whether it’s depth-first or
breadth-first depends on the semantics of pop and add, but we only care that each node
is visited exactly once). We can use it to reset the caches at every node. We can also
trivially define the evaluate function and compute_error functions as wrappers.
281
class NeuralNetwork(object):
[...]
def reset(self):
def reset_one(node):
node.cache = CachedNodeData()
self.for_each(reset_one)
Finally, the training loop. It’s as simple as randomly choosing an example, computing
the output error for that example, and then calling do_gradient_descent_step on
each node.
class NeuralNetwork(object):
[...]
Now let’s apply this to the MNIST dataset. First we build our network, with two
fully connected layers of LinearNodes and ReluNodes, with a final LinearNode with a
SigmoidNode output.
def build_network():
input_nodes = InputNode.make_input_nodes(28*28)
first_layer = [LinearNode(input_nodes) for i in range(10)]
first_layer_relu = [ReluNode(L) for L in first_layer]
second_layer = [LinearNode(first_layer_relu) for i in range(10)]
second_layer_relu = [ReluNode(L) for L in second_layer]
linear_output = LinearNode(second_layer_relu)
output = SigmoidNode(linear_output)
error_node = L2ErrorNode(output)
return NeuralNetwork(output, input_nodes, error_node=error_node, step_size=0.05)
Then we split the training set into batches, separating from each batch a so-called
validation set, which we use to measure the quality of the training as it progresses. At
282
train = load_1s_and_7s('mnist/mnist_train.csv')
test = load_1s_and_7s('mnist/mnist_test.csv')
network = build_network()
n, epoch_size = len(train), int(len(train) / 10)
for i in range(5):
shuffle(train)
validation, train_piece = train[:epoch_size], train[epoch_size:2*epoch_size]
print("Starting epoch of {} examples with {} validation".format(
len(train_piece), len(validation)))
network.train(train_piece, max_steps=len(train_piece))
print("Finished epoch. Validation error={:.3f}".format(
network.error_on_dataset(validation)))
print("Test error={:.3f}".format(network.error_on_dataset(test)))
Which is about 1.1% error. Figure 14.16 shows some examples of classifications of digits
after training. To make it easier to display in the book, I’ve rounded any nonzero values
to 0 and 1, though in the full code we provide a helper function show_random_examples
that shows the raw pixel values. As you can see, the first two are correct, and the third
is incorrect (though the correct classification of that digit is hardly obvious).
Looking closely at the validation error as training progresses, the validation error pro-
gressively decreases, but at the end increases from 0.6% to 1%. One possible explanation
for this is the phenomenon of overfitting. We’ll explore it more in the exercises, but a cur-
sory explanation is that as a sufficiently expressive machine learning model continues
to be trained, it can learn to encode specific features of the dataset. That is, the longer
one trains on the same data, the more the trained model resembles a lookup table. We’ll
explore this more in the exercises.
So there we have it! A functioning neural network, built as a computational graph of
arbitrary operations, with automatic gradient computations.
283
True label 0, predicted 0.00011 True label 1, predicted 0.99661 True label 1, predicted 0.00529
• Local properties—those properties which hold only in a narrow slice around a point
of interest—tend to be easier to reason about and compute, and they often inform
one about the global properties of an object.
14.12 Exercises
14.2 Prove the analogue of Theorem 14.10 for functions Rn → Rm . In that case, if
f = (f1 , . . . , fm ), the total derivative matrix should be:
Df1 (c, v1 ) Df1 (c, v2 ) · · · Df1 (c, vn )
Df2 (c, v1 ) Df2 (c, v2 ) · · · Df2 (c, vn )
.. .. .. ..
. . . .
Dfm (c, v1 ) Dfm (c, v2 ) · · · Dfm (c, vn )
284
Hint: the same proof works, but the construction of the single-variable function to apply
the chain rule to is slightly different.
14.4 Find and study a proof of Schwarz’s theorem, that mixed partial derivatives of suf-
ficiently nice functions don’t depend on the order you take them in. The proof is gritty,
but enlightening.
14.5 Prove the first part of the Cauchy-Schwarz inequality for real vectors, that
|⟨v, w⟩| ≤ ∥v∥∥w∥, using basic algebra.
14.6 Prove that the rule for computing partial derivatives by assuming other variables
are constant is valid.
14.9 Find and understand a statement of Taylor’s theorem for two-dimensional functions
(with an arbitrary number of approximation terms).
14.10 Perhaps the most famous theoretical machine learning model is called the Probably
Approximately Correct model (abbreviated PAC). This model formalizes much of modern
machine learning. Given a finite set X (the universe of possible inputs), the PAC model
involves a probability distribution D over X used both for generating data and evaluat-
ing the quality of a hypothesis. A machine learning algorithm gets as input the ability to
sample as much data as it wants from D, and its output hypothesis h must have high ac-
curacy on D (hence the name “approximately” in PAC). Since the sampled data is random,
the learning algorithm may fail to produce an accurate classifier with small probability.
However—and this is the most stringent qualification—in order for a learning algorithm
to be considered successful in the PAC model, it must provably succeed for any distribu-
tion on the data. If the distribution is uniformly random or focused on just a small set of
screwy points, a valid “PAC learner” must be able to adapt. Look up the formal definition
of the PAC model, find a simple example of a problem that can be PAC-learned, and read
a proof that a successful algorithm does the trick.
285
14.11 Another important learning model involves an algorithm that, rather than pas-
sively analyzing data that’s given to it (as in the PAC model of the previous exercise),
is allowed to formulate queries of a certain type, an “oracle” (a human) answers those
queries, and then eventually the algorithm produces a hypothesis. Such a model is often
called an “active learning” model. Perhaps the most famous example is exact learning
with membership and equivalence queries. Look up a formal definition of this model, and
learn about its main results and variations.
14.12 Write a program that uses gradient descent to solve the linear threshold function
problem from the end of Section 14.10. That is, determine what the appropriate loss
function should be, determine a formula for the gradient, and enshrine it in code.
14.13 In this chapter, our gradient descent used a fixed ε as the step size. However, it
can often make sense to adjust the rate of descent as the optimization progresses. At
the beginning of the descent, larger steps can provide quicker gains toward an optimum.
Later, smaller steps help refine a close-to-optimal solution. A popular way to do this due
to Yurii Nesterov involves keeping track of a so-called momentum term, and adding both
the normal gradient descent step plus the momentum term. Research Nesterov’s method
(Under what conditions does it work? Do these reasonably apply to neural networks?)
and adapt the program in this chapter to use it. Measure the improvement in training
time.
14.14 Another popular technique for training neural networks is the so-called minibatch,
where instead of a stochastic update for each example, one groups the examples into
batches and computes the average loss for the batch. Research why minibatch is con-
sidered a good idea, and augment the program in this chapter to incorporate it. Does it
improve the error rate of the learned hypothesis?
14.15 There are many different loss functions for a neural network. Look up a list of the
most widely used loss functions, and research their properties.
14.16 One particularly relevant loss function is called softmax, because it applies to a
vector-valued input. Softmax is typically used to represent the loss of a categorical (1
out of N options) labeling, and it’s particularly useful to adapt MNIST from a binary
two-digit discriminator to a full ten-digit classifier. Augment the code in this chapter to
incorporate softmax, and use this to implement a classifier for the full MNIST dataset.
too long on the same set of examples. Explore the degree to which overfitting occurs in
the neural network in this chapter for MNIST by running the training loop for a long
time. Try decreasing the size of the training set, and observe the overfitting get worse.
14.18 Space and orientation is particularly useful to computer vision applications. One
industry-standard “feature” used in deep neural networks for computer vision is a primi-
tive called convolution. Research this new operation, and implement a 4 × 4 convolution
node in the neural network from this chapter. Design an architecture that incorporates
convolution, and train MNIST on it. Does the quality improve?
However, fancy hardware won’t fix issues like overfitting, where a model with bil-
lions of parameters essentially becomes a lookup table for the training data and doesn’t
generalize to new data. To avoid this, experts employ a handful of engineering and ar-
chitectural tricks. For example, between each layer of linear nodes, one can employ a
technique called dropout, in which the outputs of random nodes are set to zero. This
prevents nodes in subsequent layers from depending on specific arguments in a fragile
way. In other words, it promotes redundancy. Such techniques fall under the umbrella
of regularization methods.
Other techniques are specific to certain application domains. For example, the concept
of convolution is used widely in networks that process image data. While convolution
has a mathematically precise definition, we’ll suffice to describe it as applying a “filter”
to every 4 × 4 pixel window of an image. Such techniques allow individual neurons to
encode edge detectors. When combined in layers—filters of filters, and so on—the results
are nodes that act as quite sophisticated texture and shape detectors.
The individual computational nodes also get much consideration. Historically, the orig-
inal nonlinear activation node for a linear node was the sigmoid function. However, be-
cause the function plateaus for large positive and negative values, training a network
that solely uses sigmoid activations can result in prohibitively slow learning. The ReLU
function avoids this, but brings its own problems. In particular, when linear weights
are randomly initialized as we did, ReLU nodes have an equal chance of being zero or
nonzero. When a ReLU activation is zero, that neuron (and all the input work to get to
that neuron) is essentially dead. Even if the neuron should contribute to the output of
an example, the gradient is zero and so gradient descent can’t update it. Other activation
functions have been defined and studied to try to get the best of both worlds.
For the reader eager to dive deeper into production-quality neural networks, check out
the Keras library. Keras is a layer on top of Google’s TensorFlow library that makes im-
plementing neural networks in Python as straightforward as in this post. The designer of
Keras also wrote a book, “Deep Learning with Python,” which—beyond including a mul-
titude of examples—covers the nitty-gritty engineering details with plenty of references.
Chapter 15
– Donald Knuth
useful for mathematics, and it was a particularly popular notation in number theory. It
was not until mid-century 1900’s that big-O found its way to computer science. Donald
Knuth opens a 1976 essay with, “Most of us have gotten accustomed to [big-O notation],”
and goes on to formalize it and introduce lower-bound analogues.
For understanding function approximations, big-O is relevant to Taylor series . In the
language of big-O, sin(x) being well approximated by x near x = 0 is phrased as
sin(x) = x + O(x3 )
To explain what this means, recall that the Taylor series for sin(x) at x = 0 is
x3 x5 (−1)n x2n
sin(x) = x − + − ··· +
3! 5! (2n)!
Big-O says the x3 terms and smaller are dominated by the x term. What’s unspoken
here is what “dominates” means. In the analysis of algorithms, “dominates” usually means
an upper bound as the size of the input grows larger. But here nothing is growing! Instead,
here the big-O notation implies a limit x → 0. I.e., when x shrinks, x3 vanishes much
faster than x. The formal definition is as a limit.
The limit notation needs a disambiguation. We’re not saying that the limit has to exist.
Indeed, sin(x) does not have a limit, but sin(x) = O(1). Rather, we simply need that the
limit does not grow without bound.
So when we say f = O(g), we mean that g is a sort of upper bound on f under some
limit. Usually the limit point a is established once at the beginning of a discussion, or
obvious from context (e.g., you’re doing a Taylor series at a). In the rare cases one needs
to disambiguate, one can use Ox→a (g(x)).
Unpacking this definition a bit, consider the special case when the limit exists and is
finite. Then there is some constant C for which
f (x)
lim = C,
x→a g(x)
and so there is some interval around a so that |f (x)| ≤ (C +1)|g(x)|. Indeed, |f (x)| ≤
Dg(x) for some constant D, so long as x is near the point of interest.
This notation satisfies some straightforward properties that allows one to do algebra
with big-O quantities. Their proofs are straightforward from Definition 15.1 and standard
properties of limits.
292
Take care, because when we say f = O(g), the symbol = doesn’t mean = in the usual
sense. For example, it’s not symmetric or transitive; x = O(x3 ) and x2 = O(x3 ) as
x → 0, but x ̸= x2 . When someone uses big-O notation like f = O(g), it’s best to read
= as “is,” and then the sentence makes sense: “f is (at most) order of g.” Moreover, when
we include O(g(x)) in the context of some larger expression, like sin(x) = x + O(x3 ),
what we mean is that sin(x) = x + f (x) for some f (x) = O(x3 ). Fluent use of big-O
involves “native support” for this implicit association in your head, which can take some
time to get used to. √
Continuing with the example of sin(x), say we √ wanted an estimate of sin(x) 1 − x2 .
Recall from Section 12.7 that the Taylor series for 1 − x2 is
√ x2 x4 x6
1 + x2 = 1 + − + − ···
2 8 16
√
The generic n-th term of 1 + x2 is not that easy to write down, √ so we won’t. But
we just want to compute an approximation of the product sin(x) 1 + x2 near zero. One
thing we could do is compute the Taylor series of the entire thing by hand, computing
derivatives for every term. Quite laborious! Another thing we could do is try to reason
about the infinite product of their Taylor series. That would still be a lot of work, and
without extra prior knowledge, we might question whether it’s valid to take a term-by-
term product of two infinite series.
Big-O can help. If we decide in advance how many terms we care about, then we can
truncate the two series with big-O and we’re left with a finite product. Note that if these
next computations look strange, it’s probably because you’re used to seeing big-O as an
infinite limit, whereas the big-O used here is a limit as x → 0. In this context, x5 = O(x3 ).
We’ll see the “usual” version of big-O shortly.
sin(x) = x + O(x3 ),
√
1 + x2 = 1 + O(x2 ),
√
sin(x) 1 + x2 = (x + O(x3 ))(1 + O(x2 ))
= x + O(x3 ) + x · O(x2 ) + O(x3 )O(x2 )
= x + O(x3 ) + O(x3 ) + O(x3 )
= x + O(x3 )
In particular, this makes rigorous the idea that “(x+ something small), multiplied by
(1+ something small), is still (x+ something small).” It’s the kind of reasoning that one
293
sees in physics books all the time, but instead of using the mathematically valid big-O,
they say “we’ll ignore this term” or “assume this term is zero.” However, being sloppy
in this uncontrolled way can result in unforeseeable errors. Missing error terms can get
combined in ways that the combination of the error is of the same order of magnitude as
the term you care about. With big-O, error terms are still present, but they’re present in a
way that doesn’t complicate calculations. When two terms get combined, you’re forced
to ask if the combined error is too big. The interface helps prevent careless mistakes.
Following one of the major themes of this book, it reduces both the cognitive load of
doing algebra, and the cognitive load of keeping track of error terms.
We can extend this notation to infinite limits:
Definition 15.2. Let f, g : R → R be two functions with g(x) ̸= 0 for all sufficiently
large x. We say f (x) = O(g(x)) as x → ∞ if the limit of their ratios does not diverge.
f (x)
lim <∞
x→∞ g(x)
With the infinite limit, we’re saying |f (x)| ≤ D|g(x)| for all sufficiently large x
and some constant D. Here and elsewhere in math, “sufficiently large” abbreviates the
limx→∞ . Some N exists, above which (x > N ) the property is always true.
Definitions 15.1 and 15.2 have the same name because they satisfy the same properties.
However, the hypotheses of these properties are different. For example, x2 = Ox→0 (x)
and x3 = Ox→0 (x), implying x2 + x3 = Ox→0 (x), but for infinite limits the part that
fails is x2 ̸= Ox→∞ (x). Instead, x2 = Ox→∞ (x3 ), x = Ox→∞ (x3 ), and so x + x2 =
Ox → ∞(x3 ).
Little-o, Omega, and Theta
There is one other important asymptotic notation known as little-o notation. If big-O is
phrased as “less than or equal to,” then little-o is “much less than.” Formally, instead of
the defining limit being finite, for little-o the defining limit is zero.
Definition 15.3. Let f, g : R → R be two functions with g(x) ̸= 0 for all x sufficiently
close to a ∈ R. We say f (x) = O(g(x)) as x → a if the limit of their ratios is zero.
f (x)
lim =0
x→a g(x)
In other words, the function f (x) vanishes compared to g(x). So while 2x3 = O(x3 )
as x → ∞, it’s not o(x3 ). Little-o requires something smaller, for example x2 = o(x3 ).
Shaving off any sufficiently large-growing function can also be the difference between
big-O and little-o. In particular, as x → ∞ it’s true that x = o(x log x) and even x =
o(x log(log(log(x)))).
The rest of the asymptotic notation family is defined by relation to big-O and little-o.
294
Little-o in particular has some nice uses simplifying calculus. In particular, we can
define the derivative entirely in terms of O notation. Donald Knuth is a champion of this
approach.
Definition 15.5. Let f (x) be a function. We say that f ′ (x) is the derivative of f if (for
a parameter ε → 0)
As an exercise, prove that this is a restatement of the usual definition of the derivative
as a limit.
Part of what makes this version of the derivative definition so elegant is that it puts the
core idea of multivariable derivatives—that we care about a linear approximator—front
and center. The function f is literally approximated by a linear map ε 7→ f ′ (x)ε in the
formula. All of the cruft about limits is now hidden by the O notation. As an example of
its usage, the derivative of x2 is computed to be 2x:
Note that f ′ (g(x)) and g ′ (x) are constants relative to the little-o, so the bracketed
terms simplify to o(ε). What’s left is the coefficient of ε, which is f ′ (g(x))g ′ (x).
295
Algorithm Analysis
Infinite limit big-O notation is a hallmark of algorithm runtime and space analysis. One
cares about the runtime of an algorithm as the input size scales. The prototypical example
is sorting. If an input list has n fixed-length integers, then BubbleSort has O(n2 ) worst-
case runtime, while MergeSort has O(n log n) worst-case runtime. For this essay we
ignore the worst-case/best-case/average-case distinction.
To say anything meaningful about which algorithm is better, we want big-O for two
reasons. First, just as the interface for a software system shouldn’t depend on the im-
plementation, our analysis of the quality of an algorithm shouldn’t depend on the fine-
grained details of the implementation. If one decides to structure the algorithm as three
functions instead of four, the raw runtime will change; extra steps are taken to push stack
frames and handle return values! Of course, many engineers spend a lot of important and
valuable time studying the fine-grained runtime of time-critical algorithms. There are ex-
perts in loop-unrolling, after all. But big-O isn’t meant for those situations; rather, it’s
meant for the life of the system that comes before fine-tuning. Big-O is a first-responder
to the scene. By the time you’re fine-tuning, big-O’s job is done.
Second, and closely related, the analysis of the quality of the algorithm shouldn’t de-
pend on features of the system the code is being run on that are beyond the programmer’s
control. If you’re sensitive to whether your C compiler is run with aggressive or extremely
aggressive optimization flags, then big-O will not help. But most systems don’t ever reach
that level of care in their entire lifetime. Big-O allows you to ignore it.
And so we package those details up into a “constant factor” of overhead, which we
accept as the penalty for being able to make decisions on principle. As such, given two
algorithms with different big-O runtime, the order of magnitude change inside the big-O
is our main focus. When we ask, “can this algorithm be solved any faster?” we don’t mean
can the constant be improved. Rather, we mean can it be solved an order of magnitude
faster, ignoring constants and runtime for small inputs.
I often hear the complaint, “But what if the constant factor is a billion! Then it’s com-
pletely useless to use big-O!” Computer scientists are well aware of the possibility that
the hidden constant might be absurd. A witty meme, whose origin I can’t recall and
failed to hunt down, involves the Black Knight of Monty Python and the Holy Grail. This
character famously loses his limbs in a sword fight, but refuses to surrender, exclaiming,
“It’s just a flesh wound!” On this image, the meme superimposes the quote, “It’s just a
constant factor!” Joking aside, more often than not the constant factors are mere flesh
wounds. Constants dominating runtime—i.e., when big-O misleads—is the exception to
the rule, and usually a sign of recent, or purely theoretical research. A famous example is
the linear-time algorithm for polygon triangulation. This algorithm has a large constant
factor, and is so tricky to implement that it has been called “hopeless” by Steve Skiena,
the author of “The Algorithm Design Manual.”
296
We’ve established that big-O can be used to measure things beyond algorithm runtime
and space usage, like the quality of an approximation. Indeed, big-O can be used to
discuss the usage of any constrained resource. For Taylor series the resource is “deviation
from the truth,” but in computer science there are a whole host of other things that big-O
is used to analyze.
• Collisions: Load balancers have to assign jobs to servers with an extremely high
rate of jobs assigned per second. In particular, they almost never have enough time
to ask a server how many jobs its processing. Instead, load balancing algorithms
use randomness and reason about the expected worst-case load of a server. One
can think of collisions of job assignments as a constrained resource a load balancer
wants to minimize for the most impacted server.
• Errors: In systems where data integrity is important, expensive, and bits are often
lost or flipped (such as data being transmitted through space, or on a scratched up
disc), one often employs redundancy schemes called error-correcting codes that al-
low one to recover from these errors. Such schemes require one to store additional
bits, and so there’s a tradeoff between how many additional bits one needs to store
and the error tolerance of the scheme.
• Labeled examples: Most machine learning systems require labeled training data
to produce a classifier. Since compute power is generally cheaper than getting
humans to label examples, one major bottleneck on the efficiency of a learning sys-
tem is access to clean data. Many learning systems are studied under the lens of so-
called query complexity, which measures access to data. A popular topic these days
is also interactive learning, in which a learning system has a “human in the loop”
that helps the machine with difficult examples. A human doing work is clearly a
bottleneck to an automated system.
Each of these topics has a rich history of design and analysis, and for each the principles
of the discussion revolve around asymptotic analysis. An interactive learning system that
takes n pieces of input data but requires Ω(n) queries to a human to learn can already be
determined unscalable, but one that only needs O(log(n)) might work. A load balancer
√
that spreads m jobs over n servers and causes the worst server to have Θ(m/n + m)
jobs is almost certain to crash servers during peak hours compared to one that guarantees
O(m/n + log n).
Big-O is a cognitive tool that allows a human to organize and make sense of a mess
of details in a rigorous fashion. It’s a tool for high level thinking. Software is full of
constrained resources, tradeoffs, and the desire for principled decision making. Fluency
in asymptotic language will help you navigate these decisions efficiently and formulate
hypotheses that can then be backed up by data.
Chapter 16
Groups
In Chapter 10 we briefly discussed the shift in mathematics from thinking about objects
to thinking about transformations between objects. This shift was radical for mathemat-
ics and much of physics. It has been less dramatic for programmers, because many ideas
that brewed in mathematics for centuries have commonplace analogues in programming.
That, and that software matured as a discipline largely after these mathematical revolu-
tions took hold.
Embodying part of this novelty are ideas like programs that transform other programs.
You write programs. Compilers are programs that turn your programs into other pro-
grams. A program analyzes the quality of a compiler. Programs test the correctness of
the compiler analyzer. Software automates the running of the tests of the correctness
of the compiler analyzer. And, of course, you use a program to help refactor the pro-
grams that automate the running of the tests of the correctness of compiler analyzer. It’s
programs all the way down.
What’s less obvious to a programmer is that studying the class of transformations of
an object provides insight into that object. It’s as if studying a refactoring tool not only
taught you how to write easily-refactorable programs, but also gave you a shortcut to
understanding an unfamiliar program! Building up a theory based on transformations is
like a slick development framework, which you later learn applies to programs you never
anticipated writing. Group theory is a fantastic example of this.
Group theory is the mathematical study of symmetry. As we’ll see in this chapter,
symmetry has algebraic structure. We can work with symmetry in much the same way
we do algebra with numbers or matrices. This is why group theory is part of a general
area of mathematics called abstract algebra.
The original insight of group theory,1 bringing us full circle to Chapter 2, is that the
roots of a single-variable polynomial have symmetric structure. Such structure can be for-
1
By many accounts attributed to a Frenchman named Évariste Galois in the early 1800’s
299
300
mulated as a group, and used to analyze the properties of a polynomial. Or, as the case
may have it, to make general statements about all polynomials. Indeed, as we mentioned
in Chapter 8, it can be hard to analytically find the roots of a polynomial of large degree.
By “analytically” I mean in the sense of the quadratic formula: a single algebraic expres-
sion using elementary operations, involving the coefficients of the polynomial, which one
could use to find all the roots. The difficulty of this motivated us to derive and implement
Newton’s method for numerically finding approximate roots.
We have group theory in part to thank for not wasting our time on the analytical ap-
proach. Using group theory one can prove that it’s not merely difficult to find an algebraic
formula for the roots of a generic degree-5 polynomial. It’s impossible. We foreshadowed
this in Chapter 2 when we discussed existence and uniqueness. This theorem—known as
the Abel-Ruffini theorem—is a crown jewel of mathematics. And though this book is too
short to do the theorem justice, the modern proof relies heavily on the shift in thought
from objects to transformations.
A second perspective on groups is understood easily, almost trivially, from program-
ming. One beautiful aspect of group theory is how it allows one to cleanly compartmen-
talize the difference between a mathematical object and its representation. The defini-
tion of a group serves as an interface or a template class—in the sense of object-oriented
programming—and concrete groups are semantically equivalent implementations of this
interface in different contexts. True surprises occur when a family of objects that has
been studied for a long time is discovered to implement the group interface. Such is the
case with elliptic curves of cryptography fame. Any time a field of mathematics has the
word “algebraic” prepended to it—such as algebraic geometry or algebraic topology—you
automatically know the subject is about finding algebraic structures like groups hidden
among seemingly non-algebraic company. When such miracles occur, you can leverage
the power of algebra to compute in the cleaner, abstract setting of the algebraic structure.
Michael Atiyah, a famous geometer, once quipped,
Algebra is the offer made by the devil to the mathematician. The devil says: “I will give
you this powerful machine, it will answer any question you like. All you need to do is give
me your soul: give up geometry and you will have this marvellous machine.”
Hermann Weyl echoed a similar idea seventy years earlier: “In these days the angel of
topology and the devil of abstract algebra fight for the soul of each individual mathemati-
cal domain.” While these seem like superstitious warnings to the unsuspecting apprentice
of mathematics, the utility of algebra for computation is undeniable. If there’s anything
to read from these quotes, it’s that geometric arguments are considered fashionable, pure,
and beautiful by a certain group of influential mathematicians. Subcultures abound.
But you, dear programmer, would never patronize computation as mere contentedness.
We know deep in our hearts that computation is beautiful. It deserves to be cherished as
an equal to geometry, analysis, logic, and the rest. Algebra deserves our special attention
in that, to the extent it destroys geometry, it enables computation.
301
A B
D C
Figure 16.1: A square with each of its corners labeled.
The most common example of a group—and its raison d’etre—is the set of symmetries
of some object. That is to say, a group is nothing if it does not “act” on some set by trans-
forming it in a composable, reversible way. You use groups to elucidate the symmetry in
objects of interest. In this final chapter we’ll see how the concept manifests itself in Eu-
clidean and hyperbolic geometry, and in the exercises we’ll explore groups as they show
up in number theory, cryptography, polynomials, graphs, and others.
We’ll finish off the chapter, and the book, with a dive into hyperbolic geometry. We’ll
see how of geometry can be studied via the groups that transform geometric space. Fi-
nally, we’ll apply what we learned to draw hyperbolic tessellations, of the same sort that
M.C. Escher studied to create his art.
and call f (x, y) one of the rigid motions described above. Then f has the property
that for every pair of points (x1 , y1 ), (x2 , y2 ), the distance between (x1 , y1 ) and (x2 , y2 )
302
These properties make an arbitrary function sensible enough that one could reasonably
call it a “distance” function. Of particular interest in the triangle inequality, which says
that taking a direct path from x to y is never worse than taking an indirect path through
z.
In Chapters 10 and 12 we discussed how the Euclidean inner product gives rise to a
distance metric
√
d(x, y) = ∥x − y∥ = ⟨x − y, x − y⟩.
This metric is the same metric for Euclidean geometry. However, not all metrics arise
from an inner product. Our study of hyperbolic geometry will produce a highly non-
linear metric, so it’s worth teasing apart the two concepts.
Back to our example of the square. Since we labeled the corners, we can track how
an isometry affects the corners. And in a sense that will become clear shortly, we only
care about how it affects the corners. If we denote a counterclockwise quarter-turn by ρ
(the Greek lower-case rho) and a flip across the AC diagonal2 by σ (the Greek lower-case
sigma), we can write down a sequence of these operations like
ρρσρ,
where we apply the operations in order from right to left. That is, the above operation
is “rotate a quarter turn, then flip, then rotate twice more.” Figure 16.2 shows how the
symmetries transform the square.
To emphasize that we’re talking about isometries that fix the square (as it might be
viewed inside an ambient space like R2 ), we call these isometries symmetries of the square.
This gives rise to the natural question: what are all of the different symmetries of the
2
This flip is specific to the initial position of A and C. As A and C move around, the flip operation is still
top-left-corner to bottom-right-corner.
303
B C A D D C
A D B C A B
ρ σ ρρσρ
square? There are infinitely many ways to compose symmetries on paper, but two sym-
metries created via different methods can result in the same operation.
To study this, we identify some core properties of symmetries.
• The operation where we “do nothing” (the identity function f (x, y) = (x, y)) is a
symmetry.
• Every symmetry has an opposite symmetry. This follows from isometries being
bijections.
Two different ways to compose symmetries can result in the same symmetry. Indeed,
flipping across the same diagonal twice is the same thing as doing nothing, and rotating
four times in the same direction is also the same thing as doing nothing. Note we only
consider the relative change of the square compared to how it started. To apply the next
rigid motion in a sequence, you need not know how it was previously transformed.
Moreover, a symmetry of the square is completely determined by how it acts on the
corners. We sketch a proof. By our requirement that distances are preserved, the cor-
ners must also go to corners. Specifically, opposite diagonal corners have a maximal
distance between any two points in the square. Their distance can’t be achieved except
by opposite-corner points. Once the corners are chosen every other point in the square
is required to be a certain distance from each corner. And there is a short but not com-
pletely trivial proof that three or more circles (whose centers don’t form a line) that have
a simultaneous intersection point must have exactly one such point. Figure 16.3 shows
an example.
As an exercise, flesh out this proof sketch in more detail. However, be warned that
not all possible labelings of the corners arise from symmetries of the square. Opposite
corners of the square cannot be mapped by an isometry to neighboring corners.
304
Figure 16.3: The position of a point is uniquely determined by its distance from the three
corners.
With a handful of symmetries, such as our ρ and σ from earlier, we can write down
compositions of those symmetries, and make equations of symmetries. The following
three are some particularly simple ones:
ρ4 = 1
σ2 = 1
ρσρ = σ
Where 1 is a placeholder for the identity symmetry. The suggestive algebraic notation
hints at our goal: 1 is the multiplicative identity satisfying, e.g., ρ · 1 = ρ. We even write
ρ−1 as the quarter-turn in the reverse direction.
These three identities allow us to reduce complicated expressions, such as σρ9 σρ−3 σ,
to a more tractable form. The geometric picture of applying symmetries give way to
mechanized computation. The notation bears the burden of the mental picture. Note
that below we mostly use ρσρ = σ to reduce the large powers of ρ.
305
People often say that a set G is a group “under” an operation instead of “paired with.”
There are a few issues we need to tackle regarding this definition and the notation asso-
ciated with it, but first let’s see some trivial examples.
3
Copyright restrictions prevent me from including a photograph.
4
Since most of our groups will be numbers, matrices, or functions, this axiom will naturally hold. We will
ignore it for brevity.
306
The singleton set {e} with the binary operation · defined by asserting e · e = e is a
group. And there was much rejoicing. The set of integers Z forms a group under the
operation of addition. It is common knowledge that zero fits the definition of the identity
element, that the sum of two integers is an integer, that addition on integers is associative,
and that every integer x has an additive inverse −x.
Likewise, all of the number systems in this book are groups under addition: rational
numbers, real numbers, complex numbers, etc. If we want to work with multiplication,
it is not hard to see that R − {0} is a group, since every nonzero real number has a
multiplicative inverse, and 1 is the multiplicative identity. Vector spaces are groups under
vector addition; indeed, the group axioms are a subset of the vector space axioms.
An important example comes from our discussion in Chapter 9, the set of integers
modulo n, denoted Z/nZ, under the operation of addition modulo n. For example,
Z/4Z = {0, 1, 2, 3}.
A few basic propositions clear up the ambiguities in Definition 16.3. For instance, the
uniqueness of the identity element follows from the other axioms of a group. Here’s a
proof by contradiction: if there were two identity elements e ̸= e′ , then by the following
logic they must be equal:
e = e · e′ = e′
The first equality holds because e′ is an identity element, and the second because e is.
A similar proof shows that the inverse of an element is unique. These facts justify the fol-
lowing notation: we call the identity element 1, and use subscripts 1G , 1H to distinguish
between identity elements in different groups G, H. We also replace the explicit · opera-
tion with an invisible operation (juxtaposition). So that xyz replaces x · y · z. Moreover,
we emulate repeated applications of the operation by saying xn to mean x · x · · · · · x
multiplying n times.
One more caveat to support “legacy” math. If we’re talking about the integers Z under
addition, the juxtaposition operation (which implies multiplication) feels unsanitary. It
simply won’t do. In this case, and whenever we have a group of numbers with a + symbol
as the operation, we’ll use +. And instead of xn we’ll use nx to mean x + x + · · · + x
adding n times. Here n is not considered an element of Z as a group, but just the number
of additions. Likewise, −x is the inverse of x, while in a multiplicative group the inverse
is x−1 . This is purely syntactic sugar.
Now we demonstrate how two drastically different sets can have the same underlying
group structure, which will inform our dive into structure-preserving mappings between
groups. The first group we understand well: R under addition. For the second, consider
the set of 2 × 2 matrices of the following form, under the operation of matrix multiplica-
tion.
{( ) }
1 a
G= :a∈R
0 1
The identity matrix is the identity element. Notice G has some familiar structure.
307
( )( ) ( )
1 a 1 b 1 a+b
= .
0 1 0 1 0 1
Indeed, matrix multiplication in G corresponds to addition of the top-right entry of
the matrix. This suggests the natural bijection f : R → G defined by
( )
1 x
x 7→ .
0 1
And indeed, addition of the inputs corresponds exactly to multiplication of the cor-
responding matrices! The fact that these particular groups have the same underlying
structure isn’t all that shocking. What’s deep is that we have two different concrete repre-
sentations for the same abstract algebraic structure. Not only are the elements in bijective
correspondence, but the operations are as well! Just as juicy, any concrete representation
of the abstract group R can be identified by finding this sort of operation-correspondence
with the set R and the usual operation +.
A mathematician might see all of this as a challenge: can we classify all the different
kinds of group structures? Could we get a new perspective on the symmetry group of
the square by turning it into a suitable group of matrices?
Before we get ahead of ourselves, let’s make these structure-preserving maps precise.
The left hand side is equal to 1H , and the right hand side is equal to f (1G ), so f (1G ) =
1H , as desired.
Proof. The same idea as the last proof, but more briefly: If x ∈ G and f : G → H is a
homomorphism, then
The extent to which a homomorphism degrades the structure of the input group is
tracked by what elements are mapped to the identity.
ker f = {x : f (x) = 1H }
An example: G = Z under addition and H = Z/10Z under addition modulo 10. Let
f : G → H mapping n 7→ 2n mod 10. The kernel of f is {0, ±5, ±10, ±15, . . . }.
Despite losing the multiples of 5, the image f (G) still has a group structure inside H.
Note f (G) = {0, 2, 4, 6, 8}, and the group operation in H—applied only to elements of
f (G)—maintains the property of being in f (G). In other words, part of the structure of
G is embedded inside H using the operation of H, but not all of it.
A group that sits inside another group (and shares the containing group’s operation)
is called a subgroup.
Definition 16.8. Let H ⊂ G be two sets and let G be a group under the operation ·.
Then H is called a subgroup of G if:
• 1 ∈ H.
• If x ∈ H, then so is x−1 .
Another term for the above conditions is that H is “closed” under · and the inverse-
taking operation (−)−1 .
Proof. First we prove that ker f is a subgroup of G. We’ll prove this directly, by assuming
x, y are arbitrary elements of ker f , and showing that xy ∈ ker f and x−1 ∈ ker f . These
are the second two conditions required of a subgroup by Definition 16.8, and the first
condition, 1G ∈ ker f , is implied by Proposition 16.5.
If x, y ∈ ker f , then f (xy) = f (x)f (y) = 1 · 1 = 1, so xy ∈ ker f . Likewise, since
group homomorphisms preserve inverses, f (x−1 ) = f (x)−1 = 1−1 = 1.
Next we’ll prove im f is a subgroup of H. Let x, y ∈ im f . By the definition of
the image, there are two elements a, b ∈ G for which f (a) = x, f (b) = y. Then
f (ab) = f (a)f (b) = xy, which by definition means that xy is in the image of f . Like-
wise, f (a−1 ) = f (a)−1 = x−1 , so x−1 is in the image of f . Again, Proposition 16.5
implies 1H ∈ im f .
Lemma 16.10. For any homomorphism f , the quotient set G/ ker f forms a group under
the operation [a][b] = [ab].
If G and H are isomorphic, they have identical group structure, and H is simply a
relabeling of the elements of G. The boolean comparison (or assertion) that two groups
G, H are isomorphic is denoted G ∼ = H. And in words, we say that two groups are
the same “up to isomorphism,” meaning only their representations are different. For our
“suspicious” example above, Z/ ker f ∼
= Z/5Z.
A simple theorem relates the groups defined by a homomorphism.
Figure 16.4: Because 5 is odd, the lines of symmetry of the regular pentagon each pass
through a side and a vertex.
Next we have the symmetry groups of regular convex polygons5 in the plane, such
as the square we started this chapter with. The group corresponding to the polygon
with n ≥ 3 sides is called the dihedral group and is denoted D2n . It has 2n elements,
corresponding to the n rotations by an angle of 2π/n and the n reflections across lines
passing through the vertices and sides. These lines of symmetry depend on the parity of n,
as is made clear by the lines of symmetry in the pentagon and the hexagon in Figure 16.4.
Dihedral groups are not cyclic. Each D2n is generated by ρ and σ, where ρ is a rotation
by 2π/n and σ is a reflection across some axis of symmetry. Because two elements gen-
erate the entire group, you might guess D2n to be isomorphic to a product of two cyclic
groups, Z/2Z × Z/nZ, with σ generating the former and ρ the latter. You might guess,
and you’d be wrong. These are subgroups, but dihedral groups have extra structure be-
cause the interaction between ρ and σ is not independent. If it were, σρσ would equal
σ 2 ρ = ρ, but in fact σρσ = ρ−1 . The extra structure is more precisely described by a
semi-direct product, which you will see in the exercises.
Next we have matrix groups. Given any number system that has addition and multi-
plication, say R for example, we can form a group of square matrices under matrix mul-
tiplication, which is often called the general linear group. Define by GLn (R) the set of
invertible n × n matrices with real entries. As we saw in Section 16.2, asserting some spe-
cific structure on the groups often leads to an interesting subgroup. One famous subgroup
of the general linear group is called the orthogonal group, denoted On (R), consisting of
matrices whose columns form orthonormal bases.
in Section 16.5. Another interesting facet of groups of matrices is that they have enough
structure that one can do calculus on them as if it were a geometric space. In the formal
jargon, the general linear group is a smooth manifold. This is far beyond the scope of this
book, but at least explains why the general linear group gets such a special name.
The last example is called the symmetric group. Really, it should be called the permu-
tation group, since it is the set of all bijections of a fixed set to itself. Let A be a set, and
define the symmetric group S(A) to be the set of all bijections A → A. It is easy to see
that if A, B are both finite sets of size n, then S(A) ∼ = S(B). In that case, denote S(A)
by Sn . In the exercises you will study the structure of finite permutation groups, and a
useful data representation for computation.
As it turns out, every group is a subgroup of a symmetry group. The proof is simple:
every group G has a group homomorphism f : G → S(G), where a ∈ G defines
the bijection x 7→ ax (the inverse is x 7→ a−1 x). Since im f is a subgroup of S(G) and
ker f ∼= {1}, we have that G ∼ = im f . One takeaway is that if you want to write programs
that do computations on finite groups, it’s enough to write programs that work with finite
permutation groups. Indeed, most useful group-theoretic algorithms are algorithms on
finite permutation groups.
• A quantity of interest that you want to study. For example, you may want to mea-
sure length. In that case, you need a metric d : X × X → R.
With these in hand, the symmetry group of the space is the set of bijections X → X
that preserve the quantity of interest. In Euclidean geometry, points and lines are the
usual points and lines in Rn , and distance is the quantity of interest. Such “quantities of
interest” are called invariants. A different type of geometry might only wish to preserve
area of figures, or preserve the property of similarity (invariance under scaling).
Klein’s view was that a geometry should be studied via its group of symmetries. The
classical concepts like angles, areas, and lengths are seen as measures that may or may not
be invariant under the application of a symmetry. Thus, geometry has two approaches:
given a group of symmetries, study the interesting quantities invariant to those transfor-
mations; and given a quantity you think is important, find the group of symmetries that
313
preserves that quantity. Every geometry has a group. Every group corresponds to some
geometry.
Klein called his view the Erlangen Program.6 One striking result7 was that all geome-
tries are a special case of projective geometry—a geometry that allows projections to a
possibly infinite horizon. In particular, even though different geometries might have dif-
ferent axioms (regarding, say, configurations of parallel lines), every geometry can be
modeled inside of a projective geometry. For example, hyperbolic geometry is projective
geometry restricted to a particular surface inside a larger projective space. Moreover, the
group corresponding to this model of hyperbolic geometry is a subgroup of the symme-
try group of the projective geometry. We get containments of the spaces as sets, and of
the groups as subgroups.
We won’t study this particular relationship in this book, but it shows how Klein’s desire
fits into the larger mathematical goal: to connect and unify disparate geometries into a
single theory. I encourage the reader interested in cryptography to learn about projective
geometry, in part because it’s the correct setting for studying elliptic curves. It’s also a
great way to exercise your linear algebra muscles, as projective geometry is simply a
quotient of the vector space Rn by a suitable equivalence relation.8
We now turn to Euclidean geometry, and study it through the lens of groups.
Euclidean Geometry
Euclidean geometry is the study of isometries of Rn with the usual distance metric
d(x, y) = ∥x − y∥. Recalling Definition 16.2, f : Rn → Rn is an isometry if
d(x, y) = d(f (x), f (y)) for all x, y ∈ Rn . Because isometries preserve distance, and
angle measure is determined by the lengths of the sides of triangles, isometries also pre-
serve angle measure.
With a few moments of thought, it’s easy to come up with examples of Euclidean
isometries for the plane:
Remember that rotations, projections, and reflections are examples of linear maps. Ig-
noring translations for a moment, it’s natural to wonder which linear maps double as
isometries.
Theorem 16.13. The isometries of Rn that fix the origin are exactly the linear maps whose
columns form an orthonormal basis.
Proof. In Chapter 12, we observed that matrices with orthonormal columns preserve the
inner product. Let A be such a matrix. In Rn , squared distance is d(x, y)2 = ⟨x−y, x−y⟩.
As a consequence,
Since distances are non-negative, the square roots are also equal.
To show that any isometry fixing distance is a linear map with orthonormal columns,
we first show it is linear. We will use slick geometric arguments, but one can prove it just
as well with formulas involving inner products (which the reader is encouraged to try).
Let f be an isometry; we need to show f (x + y) = f (x) + f (y) and f (ax) = af (x) for
any vectors x, y and any scalar a.
First, f (ax) = af (x). To prove this we first prove that any Euclidean isometry maps
lines to lines. We will use the fact that in Euclidean geometry a straight line is the shortest
path between any two points. In particular, we if x lies on the shortest path from 0 to ax,
then f (x) lies on the shortest path from 0 to f (ax): letting c = d(0, x), then x minimizes
the following:
+ w
w v
v
By our arguments above (isometries preserve length, angle measure, and parallelism of
lines), isometries map parallelograms to parallelograms. But a parallelogram is precisely
how we define addition of two vectors! The sum of the vectors representing the sides is
the diagonal vector drawn from the origin to the opposite vertex.
Now that we’ve established isometries that fix the origin are linear maps, we already
know from linear algebra that a linear map preserves distance if and only if it preserves
the inner product (d(x, y) = ∥x − y∥ is defined in terms of the inner product), which
happens if and only if its columns are orthonormal. (Cf. Chapter 12, Exercise 12.2)
This proof puts into practice Klein’s idea to study invariants preserved by isometries.
The invariants that can be derived from distance preservation are highly structured, al-
lowing one to explicitly limit an isometry’s shenanigans. As an added benefit, thinking
in terms of invariants removes the need to rephrase geometric concepts in symbolic lan-
guage. If you found the epsilon-delta proofs of calculus tedious, you might just be a
geometer.
The group of n × n matrices with orthogonal unit vector columns is called the orthog-
onal group O(n).10 Recall it has the following characterization.
10
Not to be confused with big-O notation.
316
O(n) = {A : AT A = In }.
We’ve already shown that this set forms a group under matrix multiplication. Still,
it’s worthwhile to check again in purely linear algebraic terms. Each matrix represents a
change of basis, and composing two basis-changes is again a change of basis. The identity
is a no-op basis change, and every basis change has an inverse. Finally, orthogonality is
preserved: if AT A = In , B T B = In , then (AB)T (AB) = B T (AT A)B = B T B = In .
Likewise for A−1 .
Because these isometries are linear maps, we can also infer that the complete behavior
of the isometry is determined by its behavior on n linearly independent points. This is
another example of local information being used to infer global structure.
Now the classification theorem: every isometry is a composition of an orthogonal map
with a single translation.
Proof. First, we prove that E(n) is a group. The identity is in E(n) if we set v = 0, A = In .
Given f (x) = Ax+v, the inverse is f −1 (x) = A−1 (x−v). Given f (x) = Ax+v, g(x) =
Bx + w, the composition is B(Ax + v) + w = BAx + Bv + w. Since O(n) is a group,
BA ∈ O(n), and the translation vector is Bv + w.
Next, fix an isometry f that does not necessarily preserve the origin. Let v = f (0),
and define f ′ (x) = f (x)−v, effectively translating v to the origin. f ′ (0) = f (0)−v = 0,
so f ′ ∈ O(n) and can be written as a matrix A by Theorem 16.13. Rewrite f as f (x) =
Ax + v, which has the form required to be a member of E(n). This maps an isometry
f to a member of E(n). This mapping is a homomorphism by repeating the argument
from the last paragraph: if f = x 7→ Ax + v with v = f (0) and g = y 7→ By + w
with w = g(0), then gf = x 7→ BAx + Bv + w, where Bv + w is precisely g(f (0)).
This mapping is also a bijection: if f and g differ, f (0) = g(0) = v, then f (x) − v and
g(x)−v must differ on some basis vector, and hence have different matrix representations.
Coupling this with the one-sided inverse (Ax + v is an isometry for any choice of A, v),
we get our bijection.
Hyperbolic Geometry
In antiquity, the Greek mathematician Euclid laid out a grand vision of geometry in which
every theorem can be proved from a core set of axioms. The axioms, one of which was
“any two points can be connected by a straight line,” cannot be proved and must be taken
as a truism.
Euclid’s 5 axioms, published in his magnum opus, The Elements, were:
3. For any straight line segment, there is a circle with that line as its radius and one
endpoint as its center.
5. Given any straight line and a point not on that line, there is a unique
straight line that passes through the point and never intersects the first
line.
The fifth axiom, commonly called “the parallel postulate,” nagged mathematicians for
centuries. It always seemed possible that it could be converted from an axiom to a theo-
rem by deducing it from the other four axioms.
These efforts were sadly in vain. As is often the case, the more failed attempts at
proving a theorem, the more it seems the theorem might be false. Indeed, the parallel
postulate can broken in a few ways. There are geometries that satisfy the first four axioms,
but no parallel lines exist (all possible lines intersect the first). There are also geometries in
which multiple parallel lines exist. Projective geometry is an instance of the first breakage,
and hyperbolic geometry the second.
Let’s now define a model of the hyperbolic plane and classify its symmetries. I say “a”
because there are many models of the hyperbolic plane. The connections between them
are interesting and useful, but for this chapter we’ll work entirely in the model called the
Poincaré disk. The Chapter Notes contain more historical details.
The universe of points for the Poincaré disk is the interior of the unit disk,11 D =
{(x, y) ∈ R2 | x2 + y 2 < 1}. One is supposed to colorfully imagine the boundary of the
unit circle as a “line at infinity,” a sort of horizon that lines can approach without ever
reaching. To us—we omniscient beings viewing this universe from the outside—a point
moving at unit speed along such a line simply appears to slow down. As we’ll make clear
with a distance formula, points close to the boundary grow exponentially farther away
from each other compared to points near the origin.
In the Poincaré disk, there are two kinds of lines. The first is one which includes the
origin, and these lines are simply diameters of the unit circle (not including the endpoints).
Otherwise, a line is a segment of a circle perpendicular to the unit circle. More formally,
define the angle made by two distinct, intersecting circles to be the angles made by the
tangent lines to those circles at their intersection points. Because the line through the
circles’ centers is a line of symmetry, the angle will be the same regardless of which
intersection point is chosen. A circle is perpendicular (or orthogonal) to another circle
if they form right angles at their intersection points. The types of lines are displayed in
Figure 16.5
11
We will refer to a “circle” strictly as the boundary of a circle, and the disk as the circle jointly with its interior.
This is standard mathematical parlance.
318
Figure 16.5: Lines in the Poincaré disk. The solid black line is the boundary of the disk.
The dashed diameters are one type of line. The arcs of the dashed circles are another. The
circles must intersect the boundary of the disk at perpendicular angles.
Figure 16.6: Given the dotted Poincaré line and the indicated point, all three dashed lines
pass through the point without ever intersecting the dotted line. The parallel postulate
fails.
319
C
q
p'
x p
Now we can immediately see why the parallel postulate fails: parallel lines are just
circles that don’t intersect! We can easily draw many of them through a fixed point. This
is pictured in Figure 16.6.
There is a bit of work to do to establish the axioms of geometry for this model. We need
to be able to draw a line between any two points, and to draw a circle with a segment
as its radius. We also need to define the angle between two hyperbolic lines, and verify
that right angles are all congruent. For each of these it helps to have our first hyperbolic
symmetry in hand: inversion in a circle.
Definition 16.15. Let C be a circle with center x and radius r. Let p be a point different
from x. Define the inverse of p with respect to C as the point p′ along the ray from x
through p that satisfies:
d(p, x)d(p′ , x) = r2 .
The verb for computing the inverse with respect to C is “inverting in C.” For the
classical geometric construction of the inverse of p in C: suppose p is in the interior of
C. Draw a ray from x through p, as in Figure 16.7. Then draw a perpendicular segment
from p to C to get a point q. Then the inverse p′ is the intersection of the tangent to C at
q with the ray x → p.
If p is outside the circle, one can perform these steps backward: compute a tangent to
C through p to get q, then p′ is the intersection of the altitude of the triangle ∆xqp with
the ray x → p. If p lies on the circle, then p is its own inverse.
To see why this has the property required by Definition 16.15, look again at Figure 16.7.
Triangles ∆xp′ q and ∆xqp are similar (a general truth about altitudes of right triangles),
meaning d(x, p′ )/r = r/d(x, p).
Another way to construct the inverse is to “just do it.” You want a point along the ray
from the center x through p compatible with its defining property. Simply compute
320
p′ = x + r2 (p − x)/∥p − x∥2 .
Proposition 16.16. Let C and D be circles. If C and D are orthogonal, then every point
in D is fixed by inversions in C, and vice versa. Moreover, if x, y are two points that are
inverses with respect to C, and D passes through both of them, then C and D are orthogonal.
Proof. The proof is left as an exercise in geometry. As George Pólya said, geometry is the
science of correct reasoning on incorrect figures. Take this to heart and make lots of bad
drawings.
Next, we construct a Poincaré line as the arc of a circle orthogonal to the boundary of
the unit disk. We ignore some special cases made precise in code in Section 16.8. Given
two points p, q, pick one that’s not the center and invert it in the unit circle to get a third
point s. By Proposition 16.16, the unique circle through these three points is orthogonal
to the unit circle, as desired. The arc of that segment that is between p and q and lies
inside the unit circle is defined to be the line segment between p and q, as well as the
shortest path between them.
Second, the drawing of a circle. We take a cue from Euclidean geometry, where a circle
has the property that it is perpendicular to every line through its center. Again we use
the inversion: fix a line segment between p and q, and say we want to draw the circle
with center p. Pick any two hyperbolic lines L1 , L2 that pass through p but not q. Invert
q in both of these lines to get q ′ , q ′′ . Then the Euclidean circle passing through q, q ′ , q ′′
is the hyperbolic circle centered at p with radius pq. Curiously, a hyperbolic circle in the
Poincaré disk is a Euclidean circle, but its center is not the same point as the Euclidean
center.
Finally, define the angle between hyperbolic lines as the usual Euclidean angle between
their tangents at their point of intersection. Since hyperbolic lines are orthogonal to the
unit circle, their center necessarily lies outside of the Poincaré disk. Hence, if two lines
intersect they intersect at a single point. Since the angles are defined in terms of Euclidean
angles, all right angles are congruent.
Together, these facts establish the axioms of a geometry for the Poincaré disk.
321
p q''
q'
L1
L2
Figure 16.8: The construction of a Poincaré circle with center p and radius pq.
Definition 16.17. Let w, x, y, z be four points (in a specific order). The cross ratio of
w, x, y, z, denoted [wx; yz] is defined as
/
∥w − y∥ ∥x − y∥ ∥w − y∥∥x − z∥
= .
∥w − z∥ ∥x − z∥ ∥w − z∥∥x − y∥
The cross ratio holds the distinguished position of being the invariant quantity of pro-
jective geometry. Since all geometries are special cases of projective geometry, an ap-
propriately contextualized version of the cross ratio should be invariant for hyperbolic
geometry as well.
To show this, first we need a lemma.
322
C
x'
x
y
z
y''
Figure 16.9: The image of two points uniquely determines the circle of inversion (the easy
case).
Lemma 16.18. Two hyperbolic reflections agreeing on two distinct points are equal. That
is, the circle defining an inversion operation is uniquely determined by how that operation
behaves on two points with distinct images.
Proof. When the hyperbolic reflection in question is a diameter of D2 , the lemma is true
because reflection in a Euclidean line is uniquely determined by its behavior on two points
(prove this as an exercise). The paragraphs to follow will heavily use Definition 16.15.
Let x, y be points and x′ , y ′ be their inversions, with respect to an unknown circle
C with center z and radius r. The simple case is when x, y, z are not on a common
Then z is the intersection of the line through x, x′ and the line through y, y ′ , and
line. √
r = ∥z − x∥∥z − x′ ∥ (Definition 16.15). This is depicted in Figure 16.9.
If x, y, z lie on a common line, then we may assume without loss of generality that
x, y, z lie on the horizontal axis—otherwise we may make this true via a rotation about
the origin of D2 , and the uniqueness will still be determined.12 With this, we may set
x = (a, 0), x′ = (a′ , 0), y = (b, 0), y ′ = (b′ , 0), and we need to find z = (c, 0) and r > 0
such that (a′ − c)(a − c) = r2 and (b′ − c)(b − c) = r2 (i.e. Definition 16.15), where c, r
are variables. Subtracting the two equations gives aa′ − bb′ + c(b + b′ − a − a′ ) = 0,
which can be solved for c as long as a ̸= b and a′ ̸= b′ .
Lemma 16.18 fails in the case that the two points are exchanged by the inversion. It
simplifies the above pair of equations to (a − c)(b − c) = r2 . If you arbitrarily choose a
position for c to the
√right of both a and b or to the left of both a and b, then you can always
find a radius r = (a − c)(b − c) that works. Hence, an extra condition is required for
uniqueness, and the condition relevant to the upcoming Lemma 16.21 is that the inverting
circle is orthogonal to the unit disk.
12
Equivalently, one could parameterize the line by picking a unit vector v = (x − z)/∥x − z∥ and letting
x = z + av, y = z + bv.
323
Next, we show that the cross ratio is preserved by hyperbolic reflections. The proof
is trivial for reflection in a diameter of the Poincaré disk, so we focus on the case of
inversion in a circle.
Theorem 16.19. Let f (x) be inversion in a circle with center c and radius r. Let w, x, y, z ∈
R2 be any four distinct points. Then [wx; yz] = [f (w)f (x); f (y)f (z)].
Proof. For ease of notation, let w′ = f (w) (similarly for x, y, z), and let (ab) denote
∥a − b∥, the length of the line segment between two vectors a and b. We’ll use · for
multiplication to disambiguate. Then we must prove
can show this, then (note the second equality is where we apply the claim, and the rest
is grouping):
/
(wy) · (xz) (w′ y ′ ) · (x′ z ′ ) (wy) · (xz) · (w′ z ′ ) · (x′ y ′ )
=
(wz) · (xy) (w′ z ′ ) · (x′ y ′ ) (w′ y ′ ) · (x′ z ′ ) · (wz) · (xy)
(cw) · (cx) · (cw′ ) · (cx′ )
=
(cy ′ ) · (cz ′ ) · (cz) · (cy)
(cw) · (cw′ ) · (cx) · (cx′ )
=
(cy) · (cy ′ ) · (cz) · (cz ′ )
r4
= 4 = 1,
r
which proves the theorem.
(wy) (cw)
To prove that (w ′ y ′ ) = (cy ′ ) , we split into two cases depending on whether c, w, y are
collinear. If they are not, then this follows from the similarity of the triangles ∆cwy ∼
∆cy ′ w′ : they share the angle with c and the defining property of circle inversion implies
(cw) (cy)
(cy ′ ) = (cw′ ) . If they are collinear, consider the diagram in Figure 16.10. If w, y are on
different sides of c, then
which was our goal. If w and y are on the same side of c, then replacing the sum (wy) =
(cy) + (cw) with (wy) = (cy) − (cw), or (cw) − (cy), as the case may be, yields the same
result.
Though we leave out a coherent explanation of why this ultimately works as a distance
function, the following construction provides the “correct” metric on the Poincaré disk.
324
w' w y y'
c
(wy) (cw)
Figure 16.10: The central claim is that (w′ y ′ ) = (cy ′ ) .
Definition 16.20. Let p, q ∈ D2 be two distinct points. Form the hyperbolic line through
those points, and let x, y be the intersection of the hyperbolic line with the boundary of
D2 , so that x is closest to p and y to q. Define the distance between p and q to be:
1 1
(x − q)(y − p)
d(x, y) = log[xy; qp] = log
2 2 (x − p)(y − q)
where logs are taken with base e.
Admittedly vaguely, the choice of these two special points used to compute the cross
ratio results in a “canonical” choice that allows different distances to be compared with
respect to the same reference scale. As p and q near the boundary of the circle, the
denominators involved in the cross ratio tend to zero and the cross ratio increases. See
the exercises for more.
The hyperbolic distance function satisfies the properties of a metric from Defini-
tion 16.1 (proof omitted). If a metric is defined on a geometric space that has unique
shortest line segments between points, then we get an additional property: d(x, y) =
d(x, z) + d(z, y) if and only if z lies on the shortest path between x and y. We will use
this in the proof of Lemma 16.21.
Due to Theorem 16.19, we automatically know that hyperbolic distance is an invariant
of a hyperbolic reflection. Moreover, a rotation tθ of D2 by θ radians around the origin
is also an isometry of the Poincaré disk: such rotations preserve the unit circle and are
Euclidean isometries. These two facts together allow us to analyze the structure of all
hyperbolic isometries.
First we prove an important lemma.
Lemma 16.21. The set of points equidistant from two distinct points x, y is a hyperbolic
line, and a hyperbolic reflection in this line exchanges x and y.
Proof. First, we establish that for any two points x, y, there is a unique hyperbolic reflec-
tion f : D2 → D2 that exchanges x and y. Then we prove that a point is fixed by f if
and only if it is equidistant to x and y. Since we know that a point is fixed by a circle
inversion if and only if it lies on that circle,13 this completes the proof.
13
The same holds for reflections by Euclidean lines.
325
z
f (z)
y
x
Figure 16.11: The line between x and f (z) is mapped to the line between y and z by
reflection, and the intersection of these points is w.
The existence of f : if x and y both have the same Euclidean distance from the origin,
then one can use the diameter of D2 that bisects the angle between x, y, and the center of
D2 . Otherwise, as per the postscript of Lemma 16.18 we follow the steps of Lemma 16.18
with the added condition that the inverting circle is orthogonal to the unit circle.
Rotate the center of the (unknown) circle of inversion so it, x, and y all lie on the same
horizontal line, which we may suppose without loss of generality is the horizontal axis.
Let x = (a, 0), y = (b, 0), and the center be (c, 0). The condition that x, y are exchanged
is (a − c)(b − c) = r2 . Via the Pythagorean theorem, being orthogonal to D2 adds the
constraint 1 + r2 = ∥d − (c, 0)∥2 , where d = (d1 , d2 ) is a fixed vector.14 Combining
these two equations and rearranging we get
This has a unique solution for c if and only if d1 ̸= (a + b)/2, i.e., if d does not lie on
the (Euclidean) perpendicular bisector of the line segment between x, y. This exceptional
case is exactly when we use a reflection in a diameter of D2 , i.e., the first case above.
Next, we show a point z is fixed by f if and only if z is equidistant to x, y. For the
forward implication, suppose f exchanges x and y, and let z = f (z). Then d(x, z) =
d(f (x), f (z)) = d(y, f (z)) = d(y, z). For the converse, let z be a point with d(x, z) =
d(y, z), and suppose to the contrary that z ≠ f (z). Let L be the hyperbolic line defined
by f ; by swapping z and f (z) we may assume z is on the same side of L as x. In this case
note that z, f (z) are exchanged by f , since f is a reflection. This implies that any point
w fixed by f is also equidistant to z and f (z). We have the picture in Figure 16.11.
Now d(x, z) = d(y, z) by hypothesis, and chaining this with d(y, z) =
d(f (y), f (z)) = d(x, f (z)) (since f preserves distance and f (y) = x) we get d(x, z) =
d(x, f (z)). Now consider the hyperbolic line segment between x, f (z), which intersects
L (the hyperbolic line defining f ) at a point w. This w is on the shortest path between
14
Here d is the translation of the center of D2 (the origin) that is required to shift rotate and shift our picture
to the horizontal axis.
326
x and f (z), meaning d(x, f (z)) = d(x, w) + d(w, f (z)), and note that w is fixed by f .
Finally,
And now the finale: all isometries of the Poincaré disk are a composition of reflections.
Proof. First, we claim that any isometry is determined by its effect on three non-collinear
points x, y, z (not on any Poincaré line). Suppose to the contrary there were two isome-
tries f, g with f (x) = g(x), f (y) = g(y), f (z) = g(z), but for which some p ̸∈ {x, y, z}
satisfies f (p) ̸= g(p). Since f and g are isometries, each of the points {f (x), f (y), f (z)}
is equidistant to f (p), g(p). By Lemma 16.21, {f (x), f (y), f (z)} must lie on a hyperbolic
line. But this contradicts the fact that isometries map lines to lines, since {x, y, z} are
not collinear.
To show three reflections are enough to express any isometry f : D2 → D2 , choose
any x, y, z not on a line. In the special case that x = f (x) and y = f (y), then reflection
in the hyperbolic line through x, y must map z to f (z). Indeed, z has the same distance to
x = f (x) and y = f (y) as f (z), so Lemma 16.21 applies. In this case f is just a reflection.
In the slightly less special case that only one of the three points equals its image under
f , say x = f (x), then map y to f (y) via reflection in the unique hyperbolic line consisting
of equidistant points to y and f (y) provided by Lemma 16.21. Again, since y and f (y) are
equidistant from x = f (x), the line being reflected must pass through x, meaning x is
fixed by this reflection. With one reflection we’ve reduced to the case x = f (x), y = f (y);
the first case adds one more reflection to get f .
Finally, in the least special case that all three points are different from their images, we
can apply any reflection mapping x 7→ f (x), reducing to the second case. This results in
a simple algorithm:
2. If g1 (y) ̸= f (y), then map g1 (y) to f (y) using a second reflection. This is guaran-
teed to leave g1 (x) fixed. Call that reflection g2 (or the identity if g1 (y) = f (y)).
3. Do the same for g2 (g1 (z)) and f (z), provided they are not equal, and call the re-
sulting reflection g3 . This reflection fixes both g2 (g1 (x)) = g1 (x) and g2 (g1 (y)).
327
+ az + b
fa,b (z) =
bz + a
− az + b
fa,b (z) =
bz + a
Also force a, b to satisfy |a|2 − |b|2 = 1. These are the isometries of the Poincaré disk.
−
Theorem 16.23. The isometries of D2 are of the form fa,b
+
or fa,b .
Proof. The proof is left in the exercises for those who feel comfortable with complex
numbers.
15
I like complex numbers, but I thought the book was getting too long to fit a full chapter. The topic deserves
nothing less, and I’m aware of the irony of this section.
328
+
The functions fa,b are “orientation preserving” isometries of D2 , meaning they are a
product of an even number of reflections.16 Each one can be identified with a matrix
( )
a b
+
fa,b 7 →
b a
And if you multiply the matrices, you get the composition of the two maps.
−
Likewise, the functions fa,b form orientation reversing isometries (the product of an
odd number of reflections). It is tedious, but elementary, to show that a product of the
− −
form fc,d ◦fa,b +
has the form fac+db,bc+ad , which is exactly what you get when you multiply
their corresponding matrices. Two orientation reversing isometries compose to get an
orientation preserving isometry (if not, it would be hard to speak of “orientation” in good
faith). One must be a little careful here, because the matrix representations of orientation
reversing and orientation preserving isometries are not trivially compatible. The same
matrix A is interpreted in two ways depending on whether you conjugate the input. This
is one of the deficiencies of the Poincaré disk model, which is not present in some other
models of hyperbolic geometry (see Exercise 16.24).
( )
a b
Finally, a complete description of the group. Let G = { : a, b ∈ C} be the set
b a
of orientation preserving isometries under matrix multiplication. Augment this group by
− −
adding a single fa,b , say f1,0 (z) = z (a reflection across the horizontal axis), to get the set
−
G ∪ f1,0 G. This is the isometry group of the Poincaré disk. Another way to describe it is
that G, the orientation preserving isometries, is the quotient of the full isometry group
by the subgroup consisting of the identity and a single reflection.
Figure 16.16: Cloth, Hawaii. From The Grammar of the Orient. A pattern which has two
linearly independent directions of translational symmetry.
such as in Figure 16.16. The groups that describe such patterns—which include the tes-
sellations used in much of historical decorations—have a complete known classification.
They are called wallpaper groups, and there are exactly 17 of them, up to isomorphism.
Wikipedia contains a complete classification of the wallpaper groups, and examples of
each occurring in actual decorations from cultures all around the world. One example
is in Figure 16.17, the group called “p4.” It’s characterized by its core pattern providing
two quarter-turn centers of rotation (the corner diamond and the center square), one 180-
degree center of rotation (the thin diamonds bisecting each side), translation along two
independent dimensions, and no other isometries.
Simpler than classifying all wallpaper patterns, we can ask what are the possible tessel-
lations of the Euclidean plane by a convex polygon? For example, regular squares (each
interior angle having the same measure, and each side being the same length) tile a plane
via a group of translations isomorphic to Z × Z, a fact familiar to anyone who has seen
a chess or checkers board. And while regular pentagons don’t tile the plane, irregular
pentagons do, as depicted in Figure 16.18.
To reiterate, a tessellation transforms a single base shape via a fixed group of isometries.
The shapes we’re narrowing down to study are convex, possibly irregular polygons. Out
of curiosity, if you try to tessellate the plane using an 8-sided convex polygon, you will
struggle. Your struggle is true: it’s impossible. The proof we’ll see is quite interesting—it
uses graph theory, aided by asymptotic notation, to double-count angles in a hypothetical
tessellation. A veritable capstone for the techniques in this book!
Theorem 16.24. There is no tessellation of the Euclidean plane by a single n-sided convex
polygon for any n > 6.
331
Proof. Suppose for contradiction that there is an n-sided convex polygon P , scaled to
area 1, that tessellates the plane, and fix the set T of all polygons in such a tessellation.
Our proof will have two steps: first, we will fix a bounded piece of the tessellation of area
A. Then we’ll count the number of angles of polygons contained in that piece in two
different ways, and arrive at an inequality of A in terms of A. This inequality will be a
contradiction for a sufficiently large A.
Fix a circle C of area A, and let S ⊂ T be the polygons in T that contain at least one
point within C. This finite set of polygons forms a graph G = (V, E), where V is the set
of vertices of polygons in S, and E is the (possibly subdivided17 ) set of polygon edges.
Moreover, this graph is planar since the tessellation S provides a literal drawing in the
plane. Call F the set of faces of G (i.e., the polygons plus the outside face, as we did in
Chapter 16). We summarize in Figure 16.19.
First, split each of V, E, F into “interior” and “exterior” subsets. The exterior subsets
correspond to those vertices, edges, and faces that are adjacent to the outside of the graph.
I.e., these came from the polygons that are only partially in the circle C. The interior
vertices, edges, and faces are those that come from polygons entirely inside C. Subscript
V, E, F with “int” for interior and “ext” for exterior, like Vext .
We will use the Euler characteristic formula from our chapter on graphs, Theorem 6.5,
which says that for a planar graph |V | − |E| + |F | = 2. We first claim two facts which
imply the formula |V | = (n/2 − 1)A + O(A1/2 ), which is attained by substituting these
two facts into Euler’s formula and combining.
17
Two polygons in the tessellation can touch so that the vertex of one lies partway along the edge of another.
The graph would then split this edge into two.
332
exterior vertex
interior vertex
exterior edge
...
interior edge ...
P
interior angles
of polygons
...
Figure 16.19: The setup for a hypothetical tilling of the Euclidean plane by a convex 7-gon.
The bold circle has area A, and we include any polygon having at least one point inside
the disk with boundary C.
You will prove these facts in the exercises, but they can be thought of intuitively: the
interior faces Fint (each of area 1) fill up a total area roughly equal to the area of the circle
C, and the exterior faces are a thin band surrounding C, providing area proportional to
the circumference of C times some constant width. The big-O hides both the deviation
of the area covered by Fint from being exactly A, and the entire area of Fext ; both are
O(A1/2 ).
Now we will count the number of interior angles of polygons in S in two different
ways. What I mean by “interior angle” is an angle at a vertex inside a face. The first way
is obvious, n(|F | − 1) = n|S| ≤ n|F |, because each polygon has n interior angles by
definition (ignoring the exterior face). Second, we count by vertex, splitting into interior
and exterior cases. Call av the number of interior angles meeting at a vertex v ∈ V .
∑ ∑
# interior angles = av + av .
v∈Vint v∈Vext
333
For Vint , there must be at least three interior angles at each vertex (one of these an-
gles may be part of an edge of some polygon, thus having measure π). This bounds the
first sum from below by 3|Vint |. The second sum is O(A1/2 ) because every exterior ver-
tex touches an exterior edge, and fact (2) above shows the number of exterior edges is
O(A1/2 ). This gives (# interior angles) ≥ 3|Vint | + O(A1/2 ). Since |Vint | = |V | − |Vext |,
we have |Vint | = (n/2 − 1)A + O(A1/2 ) as well.18
Combining these formulas and bounds gives
The right hand side is approximately 32 nA, and the left hand side is nA, hinting at the
contradiction. More precisely, this inequality fails as A → ∞ if and only if 1 > 3(n/2−1)
n
,
which happens if and only if n > 6.
While this may disappoint hopeful weavers of the next great tapestry, one can tessellate
the hyperbolic plane with a 7-gon. Not only that, but there are infinitely many ways to
do it! Figure 16.20 shows two ways produced by the program in this section.19
In the figure, a regular 7-gon tessellates the Poincaré disk, with 3 polygons meeting at
each vertex. The two parameters implied by (7, 3) provide an infinite family of tessella-
tions by regular, convex p-gons.20 Given a convex, regular, hyperbolic p-gon, let [p, q]
denote the configuration of a tessellation by that polygon in which q copies of the poly-
gon meet at each vertex. The example above has configuration [7, 3]. This configuration
is sometimes called the Schläfli symbol.
Theorem 16.25. Let p, q be integers. A regular, convex, hyperbolic p-gon tessellates the
plane with q copies of the polygon meeting at each vertex if and only if (p − 2)(q − 2) > 4.
The artist M.C. Escher used a [6, 4] tessellation to construct his Circle Limit IV, displayed
in Figure 16.21 with additional lines showing the hyperbolic lines used in its design. The
remainder of this chapter is devoted to drawing the outlines of hyperbolic tessellations.
18
Here we used |Vext | = O(A1/2 ), which is true because every exterior exterior edge touches at most two
exterior vertices, and the number of exterior edges is O(A1/2 ).
19
The intrepid reader will revisit the proof of Theorem 16.24 and determine where it fails for hyperbolic geom-
etry.
20
To be sure, there is a cornucopia of interesting hyperbolic tilings beyond regular convex p-gons. The en-
gineer/artist/mathematician Roice Nelson runs a fantastic Twitter account called @TilingBot that displays
many pretty pictures and animations.
334
Figure 16.20: Left: a tiling of the hyperbolic plane by 7-gons with 3 meeting per vertex.
Right: with 4 meeting per vertex.
In an exercise you’ll extend the program to input a pattern (like the angel/devil motif in
Figure 16.21) and output an Escher-style drawing.
The core of these kinds of hyperbolic tessellations is the fundamental region, which
is the smallest subset of the tessellation which, when all symmetries in the tessellation
group are applied, tile the plane. In the case of Escher’s angels, the fundamental region
is the region shown in Figure 16.22. Since we’re just drawing the outline of a tessellation,
we only need a single triangle.
Definition 16.26. The fundamental triangle for a [p, q] tessellation of the hyperbolic
plane is a hyperbolic triangle with angle measures πp , πq , π2 .
If such a triangle has its π/p vertex centered at the origin, then Figure 16.23 shows why
it produces a hyperbolic p-gon that tessellates the plane. In Figure 16.23, the fundamental
triangle is the thick solid shape, and it’s been repeatedly reflected along the edges incident
to the origin. Recall from Theorem 16.22 that all isometries are products of reflections,
and here we’re expressing rotations of 2π/p by two reflections. The result is that the
triangle and its mirror are rotated to produce a hyperbolic p-gon centered at the origin.
Likewise, the vertex with an angle of π/q allows one to rotate around an exterior vertex
by an angle of 2π/q, forming a piece of each of the q distinct polygons at each vertex.
Thus, if we can draw a fundamental triangle and reflect a set of points across a hyper-
bolic line, we’ll be able to draw regular convex tessellations.
Computing Orthogonal Circles and Reflections
Recall that a hyperbolic line between two points in the Poincaré disk is represented by the
circle passing through those two points orthogonal the unit circle. Moreover, reflection
in that line is inversion in the circle.
To compute these quantities, we start by defining geometric classes for a (Euclidean)
point, circle, and line. These classes are largely not interesting. So I will just outline
335
Figure 16.21: Left: Circle Limit IV, M.C. Escher, 1960. Right: annotated showing the center
6-gon that is tessellated.
their method signatures in Figure 16.24. One notable part is that all equality comparisons
have to be converted to “closeness” comparisons, up to some arbitrary but fixed tolerance
ε ≈ 10−8 . The reflection across a Euclidean line is also relevant because some hyperbolic
lines—those that are diameters of the Poincaré disk—are also Euclidean lines.
It gets interesting with the circle class, which has a few key methods. The first is
computing the inversion of a point. We saw the construction for this in Section 16.5,
which included a formula implemented below. Error handling is omitted for brevity.
With these basic objects and operations, we can define the core method for finding the
hyperbolic line passing through two points. In terms of the function signature, the input
is two points which the hyperbolic line must pass through, along with a circle it must be
orthogonal to. The orthogonal circle argument happens to be the boundary of D2 , but
the implementation does not depend on this.
There is one simple case to start: when both points are already on the orthogonal
circle. In this case, the output hyperbolic line is the Euclidean circle whose center is the
intersection of the two tangent lines at the points, depicted in Figure 16.25. This results
in the following edge case in code.
If there is at least one point not on the circle, then the output is computed as follows.
Invert the non-circle point in the circle, and the result is a set of three points, which
uniquely determine the equation of a circle. The reason we invert is because Proposi-
tion 16.16 guarantees orthogonality.
The equation for the center of the circle passing through three given points can be
computed by setting up three equations and solving. The equations being solved are
337
class Line:
def __init__(self, point, slope):
@staticmethod
def through(p1, p2): """Return a Line through the two given points."""
def intersect_with(self, line): """Compute the intersection of two lines."""
def y_value(self, x_value): """Compute the y value of this line at x."""
def contains(self, point): """Return True if the point is on this line."""
def __eq__(self, other): """Return True if two lines are equal."""
class VerticalLine(Line):
[override some methods from Line]
built by substituting our known points into the equation of a circle. Here the unknowns
are cx , cy , and r.
(x1 − cx )2 + (y1 − cy )2 = r2
(x2 − cx )2 + (y2 − cy )2 = r2
(x3 − cx )2 + (y3 − cy )2 = r2
A succinct way to express the solution to these equations is in terms of the ratios of
determinants of a cleverly chosen matrix. We haven’t talked about the determinant in
this book, but in addition to being a deeply meaningful quantity in its own right, it shows
up frequently in computational geometry. More about the determinant in the Chapter
Notes. In this case, the solution is summarized by ratios of determinants of sub-matrices
of the following matrix:
2
x + y2 x y 1
x21 + y12 x1 y1 1
x22 + y22 x2 y2 1
x23 + y32 x3 y3 1
Computing a determinant reduces to repeatedly removing a (row, column) pair and
computing the determinant of the smaller matrix, called a minor. Once the recursion
reduces to determinants of 3-dimensional matrices, we can easily hard-code a formula.
You’ll read about the correctness of this function in an Exercise.
def row(point):
(x, y) = point
return [x ** 2 + y ** 2, x, y, 1]
This allows us to define relevant abstractions for a hyperbolic line and the hyperbolic
plane. An instance of the Poincaré disk is a circle, with methods to compute a line through
339
two given points. A hyperbolic line is a circle, which happens to be orthogonal to the
unit circle forming the boundary of the Poincaré disk.
class PoincareDiskModel(Circle):
def line_through(self, p1, p2):
"""Return a PoincareDiskLine through the two given points."""
if orientation(p1, p2, self.center) == 'collinear':
return Line.through(p1, p2)
else:
circle = circle_through_points_perpendicular_to_circle(p1, p2, self)
return PoincareDiskLine(circle.center, circle.radius)
class PoincareDiskLine(Circle):
def reflect(self, point):
"""Reflect a point across this line."""
return self.invert_point(point)
To determine if three points are collinear, we again employ the determinant. More
generally, if you provide three points A = (ax , ay ), B = (bx , by ), C = (cx , cy ) in se-
quence, one can determine via the sign of a determinant whether visiting the points in
order results in a clockwise turn, a counterclockwise turn, or a straight line. The relevant
matrix is
1 ax ay
1 bx by
1 cx cy
The determinant, which can be thought of as computing a signed area of a particular
triangle built from the rows of the matrix, will produce zero if the points all lie on a
common line. For a 3 × 3 matrix the determinant formula is simple enough to inline.
their tangent lines at the point of intersection. To simplify the description of our funda-
mental triangle, we require that A is the origin and D lies on the horizontal-axis. Thus,
computing our desired triangle can be summarized by identifying the coordinates of B
and D in Figure 16.26.
The requirement of the three angle measures, paired with the side AD lying on the
horizontal axis, uniquely determines the positions of B and D. Let’s derive this now.
( ) ( )
Lemma 16.27. Define the constant Z = tan πp + πq tan πp . The coordinates of the
point B = (bx , by ) are given by
√
1
bx =
1 + 2Z − (tan(π/p))2
by = bx tan(π/p),
and the x-coordinate of D = (dx , 0) is given by
r2 = b2y + (bx − gx )2
dx = gx − r,
where G = (gx , 0) = (bx (Z +1), 0) is the x-coordinate of the center of the circle defining
the hyperbolic line passing through B and D.
Proof. The point B = (bx , by ) is defined to be on the line which makes an angle of π/p
with the horizontal, i.e., y = tan(π/p)x. Since A is the origin, hyperbolic lines through
A are the same as Euclidean lines. This gives the formula for by . B also lies on a circle
orthogonal to the unit circle that passes through D. Call this unknown circle C, and
suppose it has center G = (gx , 0). Note that the y-coordinate of G must be zero in order
for C to make a right angle with D = (dx , 0). Refer to Figure 16.26.
We’re asking for an angle of π/q between the line y = tan(π/p)x and the tangent to
this unknown circle C at B. Stare at the diagram in Figure 16.27 to convince yourself
that the desired tangent line must have an angle of πp + πq with the horizontal, implying
the slope of this tangent line is tan( πp + πq ).
The equation of the unknown circle (in terms of our unknown quantities) is (x − gx )2 +
y 2 = r2 , where r2 = (bx − gx )2 + b2y . When y > 0, the derivative of the circle is given
by C ′ (bx , by ) = −(bx − gx )/by , and setting C ′ = tan( πp + πq ), we solve for gx in terms
of bx as
( ) ( )
π π π
bx (Z + 1) = gx , where Z = tan + tan
p q p
If we can get another independent equation relating bx and gx , we can eliminate one
variable and solve the entire system. The fact we have yet to use is that C and the unit
circle are orthogonal. This gives a relationship between their radii, which form the legs
of a right triangle: 12 + r2 = gx2 , where r2 = (bx − gx )2 + tan (π/p)2 b2x . Solving this
341
B
π/q 2 2
r = (bx– gx ) + by
π/p
A D G
Figure 16.26: The unknown points computed in Lemma 16.27 are B, D, and G, which is
the center of the orthogonal circle C passing through B, D, that makes the desired angle
of π/q with the top edge of the fundamental triangle.
π/q
B π/p
π/q
A π/p D
/p)x
(π
tan
y= C
Figure 16.27: By symmetry, the angle of the tangent line to C at B with the horizontal is
π/p + π/q.
342
equation for bx gives the formula stated in the theorem, and substitution provides the
rest.
This results in the following code, whose documentation is far more tedious than its
implementation:
def compute_fundamental_triangle(tessellation_configuration):
p = tessellation_configuration.numPolygonSides
q = tessellation_configuration.numPolygonsPerVertex
tan_p = math.tan(math.pi / p)
Z = math.tan(math.pi / p + math.pi / q) * tan_p
A = Point(0, 0)
B = Point(b_x, b_y)
D = Point(d_x, 0)
return [A, B, D]
class TessellationConfiguration(
namedtuple('TessellationConfiguration',
['numPolygonSides', 'numPolygonsPerVertex'])):
def __init__(self, numPolygonSides, numPolygonsPerVertex):
if not self.is_hyperbolic():
raise Exception("Configuration {}, {} is not hyperbolic.".format(
(self.numPolygonSides, self.numPolygonsPerVertex)))
def is_hyperbolic(self):
return (self.numPolygonSides - 2) * (self.numPolygonsPerVertex - 2) > 4
class HyperbolicTessellation(object):
def __init__(self, configuration, min_area=6e-4):
self.configuration = configuration
self.disk_model = PoincareDiskModel(Point(0, 0), radius=1)
def compute_center_polygon(self):
center, top_vertex, x_axis_vertex = compute_fundamental_triangle(
self.configuration)
p = self.configuration.numPolygonSides
"""The center polygon's first vertex is the top vertex (the one that
makes an angle of pi / q), because the x_axis_vertex is the center of
an edge.
"""
polygon = [top_vertex]
return polygon
344
while queue:
polygon = queue.popleft()
if processed.contains_polygon(polygon):
continue
tessellated_polygons.append(polygon)
processed.add_polygon(polygon)
if len(processed) > max_polygon_count:
processed.add_polygon(polygon)
break
return tessellated_polygons
We close with some outputs for different configurations, shown in Figure 16.32.
16.10 Exercises
16.1 Recall the symmetric group Sn is the set of all bijections of a set of n elements.
Call the set being permuted {1, 2, 3, . . . , n}, and consider the following helpful notation
for a permutation: define a cycle notation whereby the tuple (1 3 4 2) represents the
permutation σ mapping 1 7→ 3, 3 7→ 4, 4 7→ 2, and 2 7→ 1. All other values are fixed
by σ. Define a product of cycles, such as (going right to left) (2 4)(1 2) = (1 4 2) as
345
16.3 Determine the subgroup of S4 that corresponds to D8 , the symmetry group of the
square. Express this in terms of elements of S4 and the building blocks of groups.
16.5 Prove that the property of being isomorphic is an equivalence relation on groups.
In particular, show that the inverse of an isomorphism is a homomorphism.
16.7 Let G be a group and H a subgroup. Prove |H| evenly divides |G|. Use this to prove
that for any a ∈ G, a|G| is the identity.
16.8 Define by φ(n) the size of the set {k ∈ N : k < n, gcd(n, k) = 1}. This function
is called the Euler totient function. Prove that for any integer a, aφ(n) ≡ 1 mod n. Hint:
use the previous exercise.
347
16.9 Let n ∈ N, and let G = (Z/nZ)× be the multiplicative group of integers (those
integers between 1 and n that have a greatest common divisor of 1 with n). When n is a
product of two large primes, this group is called the RSA group. Research the RSA public-
key cryptography protocol, and write a program that implements it for two hundred-digit
primes. Hint: you will need to find a fast way to generate hundred-digit primes.
16.10 Research and implement the ElGamal digital signature scheme using (Z/nZ)× .
16.11 Look up the definition of a semi-direct product of groups, and use this to under-
stand the characterization of the dihedral group D2n as a semi-direct product of Z/2Z
with Z/nZ, where the former acts on the latter by “conjugation.”
16.12 If you’re comfortable with complex numbers, find a source online that discusses
the symmetry groups of the roots of polynomials with coefficients in Q. At the risk of
referring to an interactive essay that has disappeared from the internet after this book is
published, see Fred Akalin’s essay, “Why is the Quintic Unsolvable?”23
16.13 ( Recall
) an undirected graph G = (V, E) is a set of vertices V and a set of edges
V
E ⊂ 2 that link pairs of vertices. A symmetry of G is a bijection f : V → V such that
(v, w) is an edge if and only if (f (v), f (w)) is an edge. In words, a symmetry permutes
the vertices of G in such a way that preserves adjacency and non-adjacency. Compute the
symmetry group of the Petersen graph. Hint: the size of this group is 120, so brute-force
will be difficult.
16.14 Two graphs are called isomorphic if there is a bijection between their vertex sets
having the same property as a symmetry: all adjacencies and non-adjacencies are pre-
served. The problem of efficiently computing whether two graphs are isomorphic is one
of the most famous open problems in computer science, called the graph isomorphism
problem. Prove that the graph isomorphism problem reduces to the problem of comput-
ing a generating set of the symmetry group of a single graph. A generating set of a group
G is a subset S ⊂ G from which every g ∈ G can be written as a product of elements
in S. (Groups can be much larger than the graphs they act on, but generating sets are
usually small)
16.15 Prove that any Euclidean isometry in E(n) can be written as the product of at
most n + 1 reflections.
16.16 Read about determinants and understand why the formula we presented in Sec-
tion 16.8 for the circle passing through three given points is correct.
16.17 Research the cross ratio in the context of projective geometry. How is it defined
there? What are the projective transformations? And why is it preserved by those trans-
formations?
23
https://www.akalin.com/quintic-unsolvability
348
16.20 We neglected to give a good intuition for why the hyperbolic distance function is
intuitively a good choice. The reason is that the morally acceptable way to think about
this function involves integral calculus, which we avoided in this book. To do this for-
mally, one defines a metric tensor or line element that describes the length of a curve via
an integral. Research these topics to understand how the hyperbolic metric is defined. Be
warned that many sources jump straight into advanced terminology and concepts. You’re
looking for an “introduction to tensor calculus” or an “introduction to Riemannian geom-
etry.” Because of the close relation to physics and general relativity, there are also many
sources explaining these concepts for physicists. Apply the usual caveats that come with
physicists explaining mathematics.
16.21 Extend the hyperbolic tessellation program in this chapter to one which, when
given an input motif (an image that replaces the fundamental triangle) draws a hyperbolic
polygon using that image and then tessellates the Poincaré disk.
16.22 Determine how to represent inversion in a circle with center c with radius r as a
complex 2 × 2 matrix.
16.23 Prove Theorem 16.23. Hint: an f + preserves orientation, and an f − reverses it.
16.24 A different model of hyperbolic geometry is the upper half-plane model. This
model has as points the complex numbers {a + bi : b > 0}, and as lines the half circles
orthogonal to the horizontal axis b = 0, along with vertical rays. The line b = 0 forms
the “boundary” analogous to the unit circle bounding the Poincaré disk. The isometries
of this model are the so-called Möbius transformations. Prove the following.
•The set of Möbius transformations, those mappings of the complex line defined by
z 7→ az+b
cz+d with ad − bc ̸= 0, form a group under function composition. This group
is called the Möbius group.
•Find a formula for inversion in a circle (reflection in an upper-half-plane-model line)
as a Möbius transformation.
•The Möbius group is isomorphic to the group of matrices P GL2 (R) = GL2 (R)/ ∼,
where ∼ is the equivalence relation defined by A ∼ λA for every nonzero λ ∈ R.
Why is this quotient necessary?
349
16.25 In this exercise we’ll explore the symmetry group of the hyperbolic tessellation of
a regular convex p-gon with configuration [p, q]. Fix the fundamental triangle of the con-
figuration, and consider the reflections α, β, γ across each edge. What are the algebraic
relations between these symmetries? Can you identify the resulting (infinite) group of
symmetries with a subgroup of a familiar group?
That is, for each permutation you take the products of the entries of A whose rows and
columns are input-output pairs of σ, scale by the parity of σ, and sum.
The definition shows up in so many geometric formulas because it computes the signed
volume of a particular solid based on the rows of A. This solid is called a parallelepiped,
the n-dimensional analogue of a parallelogram. For example, for the (signed) area of a
triangle T with vertices (ax , ay ), (bx , by ), (cx , cy ), we used
350
1 ax ay
det 1 bx by
1 cx cy
This embeds the triangle in the plane defined as {x ∈ R3 : x1 = 1}, and computes the
signed area of the triangular prism of height 1 whose apex is the origin and whose base is
T . This sets the height to 1, making the volume of the prism equal to the area of T . If the
points lie in a line, the volume is zero, and otherwise the sign is determined by whether
the vertices of T are visited in clockwise or counterclockwise order. It’s possible to see
this by noting that swapping two rows of a matrix multiplies the determinant by −1.
Chapter 17
A New Interface
We are no longer constrained by pencil and paper. The symbolic shuffle should no longer
be taken for granted as the fundamental mechanism for understanding quantity and
change. Math needs a new interface.
This book has been quite a journey. We laughed. We cried. We computed with matrices
like fury.
Math is a human activity. It’s messy and beautiful, complicated and elegant, useful
and bull-headedly frustrating. But in reading this book, dear reader, my dream is that
you have found the attitude, confidence, and enough prerequisite knowledge to continue
to engage with mathematics beyond these pages. I hope that you will find the same joy
in the combination of math and programming that I have.
You may be wondering what’s next. Each topic in this book was only covered lightly.
There’s a vast world of math out there, in the form of books, blog posts, video lectures,
and the questions from your own curiosity. So much to explore! Here are a few of my
favorite resources, which might spur your imagination.
• Linear algebra: Linear Algebra Done Right, by Sheldon Axler, and Thirty-Three
Miniatures, by Jiri Matousek.
• Differential equations and systems: Nonlinear Dynamics and Chaos, by Steven Stro-
gatz (with excellent online videos).
• Fourier analysis: The Fourier Transform and its Applications, by Brad Osgood (course
notes online, with excellent videos).
In these closing words, I’d like to explore a vision for how mathematics and software
can grow together. Much of our effort in this book involved understanding notation,
and using our imagination to picture arguments written on paper. In contrast, there’s a
growing movement that challenges mathematics to grow beyond its life on a chalkboard.
One of the most visible proponents of this view is Bret Victor. If you haven’t heard of
him or seen his fantastic talks, please stop reading now and go watch his talk, “Invent-
ing on Principle.” It’s worth every minute.1 Victor’s central thesis is that creators must
have an immediate connection to their work. As such, Victor finds it preposterous that
programmers often have to write code, compile, run, debug, and repeat every time they
make a change. Programmers shouldn’t need to simulate a machine inside their head
when designing a program—there’s a machine sitting right there that can perform the
logic perfectly!
Victor reinforces his grand, yet soft-spoken ideas with astounding prototypes. But his
ideas are deeper than a flashy user interface. Victor holds a deep reverence for ideas and
enabling creativity. He doesn’t want to fundamentally change the way people interact
with their music library. He wants to fundamentally change the way people create new
ideas. He wants to enable humans to think thoughts that could not previously have
been thought at all. You might wonder what one could possibly mean by “think new
thoughts,” but fifteen minutes of Victor’s talk will show you and make disbelieve how we
could have possibly made do without the typical software write-compile-run loop. His
demonstrations rival the elegance of the finest mathematical proofs.
Just as Lamport’s structured proof hierarchies and automated assistants are his key to
navigating complex proofs, and similarly to how Atiyah’s most effective tool is a tour
of ideas that pique his interest, Victor feels productive when he has an immediate con-
nection with his work. A large part of it is having the thing you’re creating react to
modifications in real time. Another aspect is simultaneously seeing all facets relevant to
your inquiry. Rather than watch a programmed car move over time, show the entire tra-
1
https://vimeo.com/36579366
353
jectory for a given control sequence, the view updating as the control sequence updates.
Victor demonstrates this to impressive effect.2
It should not surprise you, then, that Victor despises mathematical notation. In his
essay “Kill Math,” Victor argues that a pencil and paper is the most antiquated and un-
helpful medium for using mathematics. Victor opines on what a shame it is that so much
knowledge, and so many useful ideas, is only accessible to those who have the unnatural
ability to manipulate symbols on paper.
One obvious reason for the ubiquity of mathematical notation is an accident of his-
tory’s most efficient information distribution systems, the printing press and later the
text-based internet. But given our fantastic new technology—virtual reality, precise sen-
sors, machine learning algorithms, brain-computer interfaces—how is it that mathematics
is left in the dust? Victor asks all these questions and more.
I have to tread carefully here, because mathematics is a large part of my identity. When
I hear “kill math,” my lizard brain shoots sparks of anger. For me, this is a religious issue
deeper than my favorite text editor. Even as I try to remain objective and tactful, take
what I say with a grain of salt.
Overall, I agree with Victor’s underlying sentiment. Lots of people struggle with math,
and a better user interface for mathematics would immediately usher in a new age of en-
lightenment. This isn’t an idle speculation. It has happened time and time again through-
out history. The Persian mathematician Muhammad ibn Musa al-Khwarizmi invented
algebra (though without the symbols for it) which revolutionized mathematics, elevating
it above arithmetic and classical geometry, quickly scaling the globe. Make no mistake,
the invention of algebra literally enabled average people to do contemporarily advanced
mathematics.3 I’m a bit surprised I haven’t seen Victor use it as an example, because
algebra also allowed humans to think thoughts that could not have been thought before.
And it only gets better, deeper, and more nuanced. Shortly after the printing press was
invented French mathematicians invented modern symbolic notation for algebra, allow-
ing mathematics to scale up in complexity. Symbolic algebra was a new user interface
that birthed countless new thoughts. Without this, for example, mathematicians would
never have discovered the connections between algebra and geometry that are so preva-
lent in modern mathematics and which lay the foundation of modern physics. Later came
the invention of set theory, and shortly after category theory, which were each new and
improved user interfaces that allowed mathematicians to express deeper, more unified,
and more nuanced ideas than was previously possible.
Meanwhile, many of Victor’s examples of good use of his prototypes are “happy ac-
cidents.” By randomly fiddling with parameters (and immediately observing the effect),
Victor stumbles upon ideas that would never occur without the immediacy. To be sure,
2
It’s amusing to see an audience’s wild applause for this, when the same people might easily have groaned as
students being asked to sketch (or parse a plot of) the trajectories of a differential equation, despite the two
concepts being identical. No doubt it is related to the use of a video game.
3
See Keith Devlin’s essay for more on this: http://devlinsangle.blogspot.com/2016/04/algebraic-roots-part-
1.html
354
serendipity occurs in mathematics as well. Recall Andrew Wiles fumbling in his dark
room looking for a light switch. Many creative aspects of mathematics involve luck,
good fortune, and “eureka” moments, but there is nowhere near the same immediacy.
Immediacy makes it dreadfully easy to explore examples, which is one of the most
important techniques I hope you take away from this book! But what algebraic notation
and its successors bring to the table beyond happenstance is to scale in complexity beyond
the problem at hand. While algebra limits you in some ways—you can’t see the solutions
to the equations as you write them—it frees you in other ways. You need not know how to
find the roots of a polynomial before you can study them. You need not have a complete
description of a group before you start finding useful homomorphisms. We didn’t need
to understand precisely how matrices correspond to linear maps before studying them,
as might be required to provide a useful interface meeting Victor’s standards. Indeed, it
was algebraic grouping and rearranging (with cognitive load reduced by passing it off to
paper) that provided the derivation of matrices in the first place.
Then there are the many “interfaces” that we’ve even seen in this book: geometry and
the Cartesian plane, graphs with vertices and edges, pyramids of balls with arrows, draw-
ings of arcs that we assert are hyperbolic curves, etc. Mathematical notation goes beyond
“symbol manipulation,” because any picture you draw to reason about a mathematical ob-
ject is literally mathematical notation.
I see a few ways Victor’s work falls short of enabling new modes of thought, particu-
larly insofar as it aims to replace mathematical notation. I’ll outline the desiderata I think
a new interface for mathematics must support if it hopes to replace notation.
4. Incrementalism: Adapting the interface to study a topic must not require encod-
ing extensive prior knowledge about that topic.
The last two properties are of particular importance for any interface. Important inter-
faces throughout history satisfy the last two, including spoken language, writing, most
tools for making art and music, spreadsheets, touchscreens and computer mice, key-
boards,4 and even the classic text editors vim and emacs—anyone can use them in a basic
fashion, while experts dazzle us with them.
Let’s briefly explore each desired property.
4
Layouts of buttons and toggles in general, of which QWERTY is one
355
Counterfactual Reasoning
Because mathematical reasoning can be counterfactual, any system for doing mathemat-
ics must allow for the possibility that the object being reasoned about cannot logically
exist. We’ve seen this time and again in this book when we do proof by contradiction: we
assume to the contrary that some object A exists, and we conclude via logic that 1 = 2
or some other false statement, and then A, which we handled as concretely as we would
throw a ball, suddenly never existed to begin with. There is no largest prime, but I can
naively assume that there is and explore what happens when I square it. Importantly, the
interface need not encode counterfactual reasoning literally. It simply needs to support
the task of counterfactual reasoning by a human.
Lumped in with this is population reasoning. I need to be able to reason about the
entire class of all possible objects satisfying some properties. The set of all algorithms
that compute a function (even if no such algorithm exists), or the set of all distance-
preserving functions of an arbitrary space. These kinds of deductions are necessary to
organize and synthesize ideas from disparate areas of math together (connecting us to
“Flexible complexity” below).
A different view is that a useful interface for mathematics must necessarily allow the
mathematician to make mistakes. But part of the point of a new interface was to avoid the
mistakes and uncertainty that pencil and paper make frequent! It’s not entirely clear to
me whether counterfactual reasoning necessarily enables mistakes. It may benefit from
a tradeoff between the two extremes.
Meaning Assignment
One of the handiest parts of mathematical notation is being able to draw an arbitrary
symbol and imbue it with arbitrary semantic meaning. N is a natural number by fiat. I
can write f (ab) = f (a)f (b) and overload which multiplication means what. I can define
a new type of arrow ,→ on the fly and say “this means injective map.”
This concept is familiar in software, but the defining feature in mathematics is that
one need not know how to implement it to assert it and then study it. This ties in with
“Incrementalism” below. Anything I can draw, I can give logical meaning.
Ideally the interface also makes arbitrary meaning assignment easy. That is, if I’ve built
up an exploration of a problem involving pennies on a table, I should easily be able to
change those pennies to be coins of arbitrary unknown denomination. And then allow
them to be negative-valued coins. And then give them a color as an additional property. If
each change requires me to redo large swaths of work (as many programs built specifically
to explore such a problem would), the interface will limit me. With algebraic notation, I
could simply add another index, or pull out a colored pencil (or pretend it’s a color with
shading), and continue as before. In real life I just say the word, even if doing so makes
the problem drastically more difficult.
Flexible Complexity
Music is something that exhibits flexible complexity. A child raps the keys of a piano
and makes sounds. So too does Ray Charles, though his technique is multifaceted and
356
deliberate.
Mathematics has similar dynamic range that can accommodate the novice and the ex-
pert alike. Anyone can make basic sense of numbers and unknowns. Young children can
understand and generate simple proofs. With a decent grasp of algebra, one can compute
difficult sums. Experts use algebra to develop theories of physics, write computer pro-
grams with provable guarantees, and reallocate their investment portfolios for maximum
profit.
On the other hand, most visual explorations of mathematics—as impressive and fun as
they are—are single use. Their design focuses on a small universe of applicable ideas, and
the interface is more about guiding you toward a particular realization than providing a
tool. These are commendable, but when the experience is over one returns to pencil and
paper.
The closest example of an interface I’ve seen that meets the kind of flexible complexity
I ask of a replacement for mathematics is Ken Perlin’s Chalktalk.5 Pegged as a “digital
presentation and communication language,” the user may draw anything they wish. If
the drawing is recognized by the system, it becomes interactive according to some pre-
specified rules. For example, draw a circle at the end of a line, and it turns into a pendulum
you can draw to swing around. Different pieces are coupled together by drawing arrows;
one can plot the displacement of the pendulum by connecting it via an arrow to a plotting
widget. Perlin displays similar interactions between matrices, logical circuits, and various
sliders and dials.
Chalktalk falls short in that your ability to use it is limited by what has been explicitly
programmed into it as a behavior. If you don’t draw the pendulum just right, or you try
to connect a pendulum via an arrow to a component that doesn’t understand its output,
you hit a wall. To explain to the interface what you mean, you write a significant amount
of code. This isn’t a deal breaker, but rather where I personally found the interface strug-
gling to keep up with my desires and imagination. What’s so promising about Chalktalk
is that it allows one to offset the mental task of keeping track of interactions that algebraic
notation leaves to manual bookkeeping.
Incrementalism
Incrementalism means that if I want to repurpose a tool for a new task, I don’t already
need to be an expert in the target task to use the tool on it. If I’ve learned to use a
paintbrush to paint a flower, I need no woodworking expertise to paint a fence. Likewise,
if I want to use a new interface for math to study an optimization problem, using the
interface shouldn’t require me to solve the problem in advance. Algebra allows me to pose
and reason about an unknown optimum of a function; so must any potential replacement
for algebra.
Geometry provides an extended example. One could develop a system in which to
study classical geometry, and many such systems exist (Geogebra is a popular one, and
quite useful in its own right!). You could enable this system to draw and transform various
5
https://github.com/kenperlin/chalktalk
357
shapes on demand. You can phrase theorems from Euclidean geometry in it, and explore
examples with an immediate observation of the effect of any operation.
Now suppose we want to study parallel lines; it may be as clear as the day from simu-
lations that two parallel lines never intersect, but does this fact follow from the inherent
properties of a line? Or is it an artifact of the implementation of the simulation? As we
remember, efficient geometry algorithms can suffer from numerical instability or fail to
behave properly on certain edge cases. Perhaps parallel lines intersect, but simply very
far away and the interface doesn’t display it well? Or maybe an interface that does dis-
play far away things happens to make non-intersecting lines appear to intersect due to
the limitations of our human eyes and the resolution of the screen.
In this system, could one study the possibility of a geometry in which parallel lines
always intersect? With the hindsight of Chapter 16 we know such geometries exist (pro-
jective geometry has this property), but suppose this was an unknown conjecture. To
repurpose our conventional interface for studying geometry would seem to require defin-
ing a correct model for the alternative geometry in advance. Worse, it might require us to
spend weeks or months fretting over the computational details of that model. We might
hard-code an intersection point, effectively asserting that intersections exist. But then
we need to specify how two such hard-coded points interact in a compatible fashion, and
decide how to render them in a useful way. If it doesn’t work as expected, did we mess
up the implementation, or is it an interesting feature of the model? All this fuss before
we even know whether this model is worth studying!
This is mildly unfair, as the origins of hyperbolic geometry did, in fact, come from
concrete models. The point is that the inventors of this model were able to use the sorts of
indirect tools that precede computer-friendly representations. They didn’t need a whole
class of new insights to begin. If the model fails to meet expectations early on, they
can throw it out without expending the effort that would have gone into representing it
within our hypothetical interface.
an integer grid N × N, and in each grid cell (i, j) you can have a person or no person.
The people are called “clones” because they are allowed to take the following action: if
cells (i + 1, j) and (i, j + 1) are both empty, then the clone in cell (i, j) can split into two
clones, which now occupy spaces (i + 1, j), (i, j + 1), leaving space (i, j) vacant. You
start with three clones in “prison” cells (1, 1), (1, 2), (2, 1), and the goal is to determine
if there is a finite sequence of moves, after which all clones are outside the prison. For
this reason, Stankova calls the puzzle “Escape of the Clones.”
Left: An example move in “Escape of the Clones” whereby the solid-bordered clone trans-
forms into the two dotted-border clones. Right: the starting configuration for the puzzle.
Suppose that our dream interface is sufficiently expressive that it can encode the rules
of this puzzle, and even simulate attempts to solve it. If the interface is not explicitly pro-
grammed to do this, it would already be a heroic accomplishment of meaning assignment
and flexible complexity.
Now after playing with it for a long time, you start to get a feeling that it is impossible
to free the clones. We want to use the interface to prove this, and we can’t already know
the solution to do so. This is incrementalism.
If we were to follow in Stankova’s footsteps, we’d employ two of the mathematician’s
favorite tools: proof by contradiction and the concept of an invariant. The invariant
would be the sum of some weights assigned to the initial clones: the clone in cell (1, 1)
has weight 1, and the clone in cells (1, 2), (2, 1) each get weight 1/2. To be an invariant,
a clone’s splitting action needs to preserve weight. A simple way to do this is to simply
have the cloning operation split a clone’s current weight in half. So a clone in cell (2, 1)
with weight 1/2 splits into two clones in cells (2, 2), (3, 1) each of weight 1/4. We can
encode this in the interface, and the interface can verify for us that the invariant is indeed
an invariant. In particular, the weight of a clone depends only on its position, so that the
weight of a clone in position (i, j) is 2−(i+j−2) . The interface would determine this and
tell us. This is immediacy.
Then we can, with the aid of the interface, compute the weight-sum of any given con-
figuration. The starting region’s weight is 2, and it remains 2 after any sequence of op-
359
erations. It dawns on us to try filling the entire visible region outside the prison with
clones. We have assumed to the contrary that an escape sequence exists, in which the
worst case is that it fills up vast regions of the plane. The interface informs us that our
egregiously crowded region has weight 1.998283. We then ask the interface to fill the
entire complement of the prison with clones (even though that is illegal; the rules imply
you must have a finite sequence of moves!). It informs us that weight is also 2. We real-
ize that if any cell is cloneless, as must be true after a finite number of moves, will have
violated the invariant. This is counterfactual reasoning.
Frankly, an interface that isn’t explicitly programmed to explore this specific proof—
yet enables an exploration that can reveal it in a more profound way than paper, pencil,
and pondering could—sounds so intractable that I am tempted to scrap this entire es-
say in utter disbelief. How can an interface be so expressive without simply becoming
a general-purpose programming language? What would prevent it from displaying the
same problems that started this inquiry? What precisely is it about the nature of hu-
man conversation that makes it so difficult to explain the tweaks involved in exploring a
concept to a machine?
While we may never understand such deep questions, it’s clear that abstract logic puz-
zles and their proofs provide an excellent testbed for proposals. Mathematical puzzles
are limited, but rich enough to guide the design of a proposed interface. Games involve
simple explanations for humans with complex analyses (flexible complexity), drastically
different semantics for abstract objects like chessboards and clones (meaning assignment),
there are many games which to this day still have limited understanding by experts (in-
crementalism), and the insights in many games involve reasoning about hypothetical
solutions (counterfactual reasoning).
In his book “The Art of Doing Science and Engineering,” the mathematician and com-
puter scientist Richard Hamming put this difficulty into words quite nicely,
It has rarely proved practical to produce exactly the same product by machines as we
produced by hand. Indeed, one of the major items in the conversion from hand to machine
production is the imaginative redesign of an equivalent product. Thus in thinking of
mechanizing a large organization, it won’t work if you try to keep things in detail exactly
the same, rather there must be a larger give-and-take if there is to be a significant success.
You must get the essentials of the job in mind and then design the mechanization to do
that job rather than trying to mechanize the current version—if you want a significant
success in the long run.
We write programs to manage our files, and in doing so we lose much of the spatial
reasoning that helps people remember where things are. The equivalent product is that
the files are stored and retrievable. On the other hand, for mathematics the equivalent
product is human understanding. This should be no surprise by now, provided you’ve
come to understand the point of view espoused throughout this book. In this it deviates
from software. We don’t want to retrieve the files, we want to understand the meaning
behind their contents.
My imagination may thus defeat itself by failing to give any ground. If a new inter-
face is to replace pencil and paper mathematics, must I give up the ease of some routine
mathematical tasks? Or remove them from my thinking style entirely? Presuming I can
achieve the same sorts of understanding—though I couldn’t say how—the method of ar-
rival shouldn’t matter. And yet, this attitude ignores my experience entirely. The manner
of insight you gain when doing mathematics is deeply intertwined with the method of
inquiry. That’s precisely why Victor’s prototypes allow him to think new thoughts!
Mathematics succeeds only insofar as it advances human understanding. Pencil and
paper may be the wrong tool for the next generation of great thinkers. But if we hope
to enable future insights, we must understand how and why the existing tools facilitated
the great ideas of the past. We must imbue the best features of history into whatever we
build. If you, dear programmer, want to build those tools, I hope you will incorporate the
lessons and insights of mathematics.
Until then!
About the Author and Cover
Jeremy Kun is a software engineer at Google, as part of a team that plans and optimizes
Google’s “fleet” of datacenter machines. Born in 1989 in San Francisco, California, he
earned his undergraduate degree in mathematics from California Polytechnic State Uni-
versity at San Luis Obispo, and his doctorate in mathematics from the University of Illi-
nois at Chicago where he was advised by Lev Reyzin. Jeremy writes the blog Math ∩
Programming at jeremykun.com. He lives in Oakland, California with his wife, Erin.
I believe it is possible that, through horizontal and vertical lines constructed with aware-
ness, but not with calculation, led by high intuition, and brought to harmony and rhythm,
these basic forms of beauty, supplemented if necessary by other direct lines or curves, can
become a work of art, as strong as it is true.
I hope that you, dear reader, will discover and find meaning in mathematics. I believe
that the harmony and rhythm in these basic forms of beauty, supplemented if necessary
by programs, can become a work of art, even stronger than it is true.
361
Index
′
dihedral, 311, 346 manifold, 133 prime
∏n ( ), 79, 98, 107
examples, 310 matching markets, 39, 54, 60, i=1 , 18
generating set, 310 61 R, 14
product, 310 matrix, 150, 306, 327, 338 set-builder, 40
quotient, 309 diagonalizable, 208 ⊂,
∑n41
semi-direct product, 311 identity, 156, 173 i=1 , 18
subgroup, 308 inverse, 156 □, 21
symmetric, 312, 344, 349 symmetric, 196 ∀, 46, 105
transpose, 172, 174, 196, notationi
Haken, Wolfgang, 77 230 Z, 14
halfspace, 271 matroid, 181, 187 NP-hard, 74
Halmos, Paul, 37 metric, 32, 302, 324
Hamming, Richard, 359 MNIST data set, 267 optimization
Hessian, 258, 284 modular arithmetic, 26, 33, gradient descent, 252
Hilbert space, 200 133, 306 multiple variables, 251
Hofstadter, Douglas, 190 monotonic, 57 single variable, 129
homomorphism, 307 Mumford, David, 92, 237 orthogonal complement, 209
Hooke’s law, 213, 228 orthonormal basis, 202
hypothesis class, 270 Nelson, Roice, 333
Nesterov, Yurii, 285 partition, 72, 346
inner product, 160, 196, 198 Newton’s law, 212 perfect secrecy, 27
norm, 201 Newton’s method, 119 Perlin, Ken, 356
orthogonal, 201 Newton, Isaac, 102, 357 Picard, Émile, 66
invariant, 77, 96, 148, 192, 225, notation picture proofs, 50
312 ≈, 99 Poincaré, Henri, 63, 92
isometry, 159, 226, 302 arg, 327 polynomial, 84, 180, 227
isomorphism, 84, 158, 309, arg min, arg max, 169 definition, 6
346, 347 →, 13, 105 degree, 6, 9
big-O,
(n) 215, 290, 331 interpolation, 15, 22
Jordan canonical form, 207 k
, 49 product
C, 327 of groups, 310
Kaplansky, Irving, 188 i, 327 of sets, 42
kernel, 205, 308 a + bi, 327 semi-direct, 311, 347
Klein, Felix, 312 D, 244 projection, 162, 204, 245, 247
Kleinberg, Jon, 32 d
dx
, 110 proof techniques
Knuth, Donald, 289, 294 dx, 247 bijection, 48, 49, 77
equivalence class [·], contradiction, 53, 58, 78,
Lamport, Leslie, 232, 352 132 113, 146, 162, 331
LeCun, Yan, 268 ≡, 133 diagonalization, 59
Leibniz, Gottfried, 102 ∃, 44, 105 double-counting, 49, 60,
limit gradient∇, 248 77, 78, 331
definition, 105 ⟨−, −⟩, 160 if and only if, 161, 197,
of a function, 106 interval, 112 201
sequence, 103 little-o, 293 induction, 51, 76, 78, 209
linear approximation, 241 7→, 110 monotonicity, 57, 148
linear functional, 229 ∈, 14, 40 without loss of general-
linear map, 110 min, max, 168 ity, 147, 162
definition, 141 ∂ 2 f /∂x∂y, 257
linear threshold function, 271 mod, 133 quantifier
locality, 253, 256 N, 14 existential, 44, 103, 105
negation, 41 universal, 46, 103, 105
machine learning, 267 ∂, 248 quotient, 131
bias, 269 ⊥, 171, 271 definition, 132
365