A Programmers Introduction To Mathematics
A Programmers Introduction To Mathematics
A Programmers Introduction To Mathematics
Jeremy Kun
Copyright © 2020 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 Owen Jones’s design masterpiece, The Grammar of the Orna-
ment (1856), M.C. Escher’s Circle Limit IV (1960), 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.
An extra thanks to the readers who submitted errata at pimbook.org for the first
edition, including Abhinav Upadhyay, Abhishek Bhatia, Alejandro Baldominos, Andrei
Paleyes, Arman Yessenamanov, Arthur Allshire, Arunoda Susiripala, Bilal Karim Reffas,
Brian Cloutier, Brian van den Broek, Britton Winterrose, Cedric Bucerius, Changyoung
Koh, Charlie Mead, Chris G, Chrislain Razafimahefa, Darin Brezeale, David Bimmler,
David Furcy, David Shockley, David Wu, Devin Conathan, Don-Duong Quach, Fidel
Barrera-Cruz, Francis Huynh, Glen De Cauwsemaecker, Harry Altman, Ivan Katanic,
Jaime, Jan Moren, Jason Hooper, K. Alex Mills, Kenytt Avery, Konstantin Weitz, Lean-
dro Motta Barros, Luke A., Marco Craveiro, Matthijs, Maximilian Schlund, Meji Abidoye,
Michael Cohen, Michaël Defferrard, Nicolas Krause, Nikita V., Oliver Sampson, Ondrej
Slamecka, Patrick Stingley, Rich Yonts, Rodrigo Ariel Sota, Ryan Troxler, Seth Yastrov,
Simon Skrede, Sriram Srinivasan, Steve Dwyer, Steven D. Brown, Tim Wilkens, Timo
Vesalainen, Tyler Smith, Wojciech Kryscinski, and Zorro.
Special thanks to John Peloquin for his thorough technical review for the second edi-
tion, and to Devin Ivy for technical review of parts of the first edition.
Contents
Our Goal i
Chapter 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.8 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Chapter 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
Chapter 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 . . . . . . . . . . . . . . . . . . . . 78
6.6 Approximate Coloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
6.7 Cultural Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
6.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
6.9 Chapter Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Index 383
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 make progress toward understanding that topic. I will “teach”
you by introducing you to—or having you revisit—a broad foundation of topics and tech-
niques 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 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 incon-
ceivable!) without mathematics. That includes programs in this book about cryptogra-
phy, data science, and art, but also to many revolutionary technologies in industry, such
as signal processing, compression, ranking, optimization, and artificial intelligence. 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. Your comfort
1
Hopefully you’re a programmer; otherwise, the title of this book must have surely caused a panic attack.
i
ii
with functions, logic, and protocols gives you an intuitive familiarity with basic topics
such as boolean algebra, recursion, and abstraction. You can rely on this to make math-
ematics less foreign, progressing all the faster to more nuanced and stimulating topics.
By contrast, most educational math content is for students with no background. Such
content focuses on rote exercises and passing tests. This book will omit most of that.
Programming also allows me to provide immediate applications that ground the abstract
ideas in code. In each chapter, we’ll fashion our mathematical designs into a program you
couldn’t have written before. All programs are written in Python 3. 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 cover the basic methods of proof, and each chapter will
use proofs to build the subject matter. 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. Each proof contains layers of insight that are genuinely worthwhile, but few
gain a complete picture of a topic in a single sitting. As you grow into mathematics, the
act of reading even previously understood proofs provides both renewed and increased
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
through the chapters, we’ll gradually become more terse, and you’ll have many opportu-
2
pimbook.org
iii
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
explorations of concepts beyond these pages. Finally, 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. Learn the main definitions. See new notations, conventions,
and attitudes. Take the opportunity to explore topics that pique your interest.
A number of readers have reached out to me to describe their struggles with proofs.
They found it helpful to read a companion text on the side with extra guidance on sets,
functions, and methods of proof—particularly for the additional exercises and consis-
tently gradual pace. In this second edition, I added two appendices that may help with
readers struggling with the pace. Appendix B contains more detail about the formalities
underlying proofs, along with strategies for problem solving. Appendix C contains a list
of books, and specifically a section for books on “Fundamentals and Foundations” that
cover the basics of set theory, proofs, and problem solving strategies.
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. Chapter 12 on eigenvalues includes a miniature intro-
duction to differential equations. The notes for Chapter 16 on groups briefly summarizes
complex numbers. Probability underlies our discussion of random graphs in Chapter 6
and machine learning in Chapter 14. Moreover, many topics in this book are prerequi-
sites for these other areas. 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, but for all my shortcomings I con-
sidered 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
generate 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. Despite following test-driven development,
I saw those dreaded words: Segmentation fault. 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 program-
mers can relate, the most aggravating part was that changing four characters (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. For one, we were
forced to take math classes for many year in school. That forced investment shouldn’t
have been pointless. But the culture of mathematics and the culture of mathematics
education—elementary through lower-level college courses—are completely different.
Even college math majors have to reconcile this. I’ve had many conversations with
such students, including 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 reaches the point of a math major at which they must
read and write proofs in earnest. It requires an ambiguous, open-ended exploration that
they don’t enjoy. Despite being a stark departure from the rigid structure of high school
math, incoming students are not warned in advance. After coming to terms with their
unfortunate situation, they decide that their best option is to persist until they graduate,
at which point they return to the comfortable setting of pre-collegiate math, 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. The subject they once were good at is suddenly impenetrable. It’s
a negative feedback loop in the education system. Math takes the blame.
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
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. Why things work takes priority over practical implementation.
3
There’s nothing inherently wrong with using mathematics as a black box, especially
the sort of applied mathematics that comes with provable guarantees. But many pro-
grammers want to dive deeper. 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 foundations. We’re
naturally curious.
A second obstacle is that math writers are too terse. The purest fields of mathematics
take a sort of pretentious pride in how abstract and compact their work is. I can think
of a handful of famous books, for which my friends spent weeks or months on a single
chapter! This acts as a barrier to entry, especially since minute details matter for applica-
tions.
Yet another hindrance is that mathematics has no centralized documentation. Instead it
has a collection of books, papers, journals, and conferences, each with subtle differences,
citing each other in a haphazard manner. Dealing with this is not easy. One often needs
to translate between two different notations or jargons. Students of mathematics solve
these problems with knowledgeable teachers. Working mathematicians “just do it.” They
reconcile the differences themselves with coffee and contemplation.
What programmers consider “sloppy” notation is one symptom of the problem, but
there there are other expectations on the reader that, for better or worse, decelerate the
pace of reading. Unfortunately I have no solution here. Part of the power and expressive-
ness of mathematics is the ability for its practitioners to overload, redefine, 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 counter-
productive.
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 technolo-
gies. Each of these notations had a revolutionary effect on science, and also, to quote
Bret Victor, on our capacity to “think new thoughts.” One can draw a line from the pro-
liferation of algebraic notation to the invention of the computer.1 Borrowing software
terminology, algebraic notation is 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 sym-
metries in the formulas. This makes it easier for us to identify, analyze, and encapsulate
structure when it occurs.
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 sums of powers of integers. In the early 1900’s Hilbert posed
his Tenth Problem on algorithms for solving Diophantine equations, and later his Entscheidungsproblem,
which was solved concurrently by Church and Turing and directly led to Turing’s code-breaking computer.
4
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 assumes a lower bar. Code is encouraged to be simple enough for new
grads to understand, and heavily commented otherwise. Surprising behavior is consid-
ered harmful. As such, the uninitiated programmer often has a much larger cognitive
load when reading math than when reading a program.
There are good reasons why mathematics is the way it is, though the reasons may not
always be clear. I like to summarize the contrast by claiming that mathematical notation
is closer to spoken language than to code. There is a historical and cultural context miss-
ing from many criticisms of math. It’s a legacy system, yes, but a well-designed one. We
should understand it, learn from its advantages, and discard the obsolete parts. Those
obsolete parts are present, but rarer than they seem.
To fairly evaluate mathematics, we must first learn some mathematics. Only then can
we compare and contrast programming and mathematics in terms of their driving ques-
tions, their values, their methods, their measures of success, and their cultural expecta-
tions. Programming, at its core, focuses on how to instruct a computer to perform some
task. But the broader driving questions include how to design a flexible system, how to
efficiently store and retrieve data, how to design systems that can handle various modes
of failure, how to scale, and how to tame growth and complexity.
Contrast this with mathematics which, at its core, focuses on how to describe a math-
ematical object and how to prove theorems about its behavior. The broader driving ques-
tions include how to design a unified framework for related patterns, how to find compu-
tationally useful representations of an object, how to find interesting patterns to study,
and most importantly, how to think more clearly about mathematical subjects.
A large chunk of this book expands on this summary through interludes between each
chapter and digressions after introducing technical concepts. The rest covers the fun-
damental objects and methods of a typical mathematical education. So let’s begin our
journey into the mathematical mists with an open mind.
Read on, and welcome to the club.
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
The reason I’m so confident is that I’m certain you’ve overcome the same obstacle in
the context of programming. For example, my first programming language was Java.
And my first program, which I didn’t write but rather copied verbatim, was likely similar
to this monstrosity.
5
6
/******************************************
* 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. Computers don’t generally require you to understand a code
snippet to run. But at some point, we all 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. As we go, we’ll clarify some nuance to the
definition related to certain “edge cases.”
Syntax
A definition is an English sentence or paragraph in which italicized words refer to 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 comma-separate the indices in the subscript, i.e. ai,j is equivalent
to a[i][j]. Hence, the coefficients are an array of real numbers. Many mathematicians
index arrays from 1 instead of 0, and we will do both in this book.
We used a strange phrase in Definition 2.1, that “f has the form” of some expression.
This means that we’re choosing specific values for the data defining f . It’s making a
particular instance of the definition, as if it were a class definition in a program. In this
case the choices are:
1. The names for all the variables involved. The definition has chosen f for the func-
tion, x for the input variable name, a for the array of coefficients, and n for the
degree. One can choose other names as desired.
makes our internal concept of a polynomial more general than the letter of Definition 2.1.
A polynomial is any function of a single numeric input that can be expressed using only
addition and multiplication and constants, along with the input variable itself. So the
following is a polynomial:
You recover the precise form of Definition 2.1 by algebraically simplifying and group-
ing terms. The form described in Definition 2.1 is not ideal for every occasion! For
example, if you want to evaluate a polynomial quickly on a computer, you might rep-
resent the polynomial so that evaluating it doesn’t redundantly compute the powers
t1 , t2 , t3 , . . . , tn . One such scheme is called Horner’s method, which we’ll return to in
an Exercise. The form in Definition 2.1 might be called a “canonical” or “standard” form,
and it’s often useful for manipulation in proofs. As we’ll see later in this chapter, it’s easy
to express a generic sum or difference of two polynomials in the standard form.
Suffice it to say, there are many representations of the same abstract polynomial. You
can do arithmetic and renaming to get to a standard representation. f (x) = x + 1 is the
same polynomial as g(t) = 1 + t, though they differ syntactically.
There are other ways to think about polynomials, and we’ll return to polynomials in
future chapters with new and deeper ideas about them. Here are some previews of that.
The first is that a polynomial, as with any function, can be represented as a set of pairs
called points. That is, if you take each input t and pair it with its output f (t), you get a
set of tuples (t, f (t)), which can be analyzed from the perspective of set theory. We will
return to this perspective in Chapter 4.
Second, a polynomial’s graph can be plotted as a curve in space, so that the horizontal
direction represents the input and the vertical represents the output. Figure 2.1 shows a
plot of one part of the curve given by the polynomial f (x) = x5 − x − 1.
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.
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” between increasing
and decreasing. Making this mathematically rigorous requires more nuance—after all,
the degree five polynomial in Figure 2.1 only changes direction twice—but the pattern
suggested by Figure 2.2 is no coincidence.
Finally, polynomials can be thought of as “building blocks” for complicated structures.
That is, polynomials are a family 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 use Taylor polynomials to approximate things in
Chapters 8 and 14.
9
Polynomials occur with stunning ubiquity across mathematics. It makes one wonder
exactly why they are so central. It’s because 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, allow us to build
circuits that make a computer. Those three operations yield the full gamut of algorithms.
Polynomials fill a similar 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)
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 should 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.
Software testing provides a good analogy. 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
unavoidable occupational hazard—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, that intuition allows you to absorb new ideas faster.
So let’s write down some examples of polynomials according to Definition 2.1, starting
from the simplest. To make you pay attention, I’ll slip in some examples that are not
11
polynomials and your job is to check them against the definition. Take your time, and
you can check your answers in the Chapter Notes.
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
refer 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,” a sensible 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. What is
the degree of f ? On one hand, it makes sense to say that the zero polynomial 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) = 0 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. Note that every time a
definition includes the phrase “by convention,” a computer program gains an edge case.2
This edge case made us reconsider the right definition of a polynomial, 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 concept. It’s curious how
mathematical books tend to start with the final product, instead of the journey to the
2
You may wonder: is it possible to represent the same polynomial with two formulas that have different
degrees? Theorem 2.3 can be used to prove this is impossible. Exercise 4 asks you to prove it using elementary
means.
12
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.
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 used in
parsers to define 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.
While Definition 2.1 isn’t perfect, it signals that a polynomial is a function of a single input.
BNF only provides a sequence of named tokens. As a human, we want to understand that
a polynomial is a function with particular structure, and that’s not captured by a purely
syntactic definition. 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 and rigorous 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 computer-friendly representation. It’s why mathe-
maticians 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 steroids. In Java you have to separate an interface from the
class which implements it, and in C++ templates are distinct from their usage. In math, we
often have multiple equivalent definitions—some closer to an interface and some closer
to a syntactic representation—and we have to prove that they are equivalent to justify
switching between them. Once we’ve built up a collection of these definitions, we often
settle on one as the “clearest” and most generally useful definition that is presented first.
Underlying all the definitions is an abstract concept we keep in our minds. The definition
is one way to make that concept concrete while also expressing one particular facet of its
properties for the task at hand.
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. It also explains why understanding a
definition is such an important prerequisite to reading the mathematics that follows.
Polynomials may seem frivolous to illustrate the difference between an object-as-
abstract-concept and the representational choices that go into understanding a defini-
13
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
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 Chapter 10, our first exposure to linear algebra.
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. Each
new perspective and definition was driven by an additional need. As a consequence, 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 the fashionable
calculations of 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 inputs are integers, and the type of the output is also integer, but 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. The notation f : A → B
specifies the domain A and codomain B, while 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 before the function definition.
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.3 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 means “pairs” of real num-
bers. Likewise, Z3 would be triples of integers, and N10 length-10 tuples of natural num-
bers.
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.5 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.6
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:
a0 + a1 · 2 = 3
a0 + a1 · 7 = 4
5
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.
6
If you’re comfortable solving basic systems of equations, you may want to skip ahead to the next section.
16
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. Our example above reinforces 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,
“x1 < x2 < · · · < xn+1 ”. In our example, this means we require x1 < x2 . So this is
where we run a sanity check. What happens if x1 = x2 ? 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.
Existence of Polynomials Through Points
We will show existence by direct construction. That is, we’ll “be clever” and find a general
way to write down a polynomial that works. Being clever sounds scary, but the process
is actually quite natural, and it follows the same pattern as we did for reading and un-
derstanding definitions: you start with the simplest possible example (but this time the
17
example will be generic) and then you work up to more complicated examples. By the
time we get to n = 2 we will notice a pattern, that pattern will suggest a formula for the
general solution, and we will prove it’s correct. In fact, once we understand how to build
the general formula, the proof that it works will be trivial.
Let’s start with a single point (x1 , y1 ) and n = 0. I’m not specifying the values of x1
or y1 because I don’t want the construction to depend on my arbitrary specific choices. I
must ensure that f (x1 ) = y1 , and that f has degree zero. Simply enough, we set the first
coefficient of f to y1 , the rest zero.
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 (e.g., f (x1 ) = y1 ) could be isolated in its own term, while all the
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.
18
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. Then argue why f is degree 2.
The general formula for n points (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 j’s as the numerator. It works
for the same reason that our formula works for three terms above. By now, 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=1 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”
∑n and “pi”. They essentially
represent loops of arithmetic. That is, the statement i=1 (expr) is equivalent to the
following code snippet.
int i;
sometype theSum = defaultValue;
return theSum;
Note by indexing from 1 and including the upper limit of the for loop condition,
∑ we are
deviating from the standard programming style. Indexing from zero, like ni=0 , produces
n + 1 terms in the resulting sum.
I used the undefined tokens defaultValue and sometype to highlight that the mean-
ing of the sum depends on what the conventional ‘zero object’ is in that setting. For
adding numbers the zero object is zero, and for adding polynomials it’s the zero polyno-
mial. It gets exotic with more advanced
∑ mathematics, which we’ll see in Chapter 16 when
we study groups. The point is that does not imply a type. It’s merely a shorthand for
the symbol +.
∑ ∏
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-
19
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 1 to n, we’d expect j to have that range. Being so loose might seem
hazardous, but if mathematicians consider it “easy” to infer the intent of a notation, then
it is considered rigorous enough.7
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 you 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 often 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.
Finally, the j ̸= i part is an implied filter on the range of j. Inside the
∏ for loop you add
an extra if statement to skip that iteration if j = i. Read out loud, j̸=i would be “the
product over j not equal to i.” If we wanted to write out the product-nested-in-a-sum as
a nested loop, it would look like this:
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.
7
Another reason is that mathematicians get tired of writing these “obvious” details over and over again. Math-
ematicians don’t have text editor tools like programmers do.
20
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+1 , yn+1 ) be a list of n + 1 points with no two xi the same.
To show existence, construct f (x) as
∑
n+1 ∏ x − xj
f (x) = yi
xi − xj
i=1 j̸=i
Clearly the constructed polynomial f (x) has degree at most n because each term has
degree at most n. For each i, plugging in xi causes all but the i-th term in the sum to
vanish,8 and the i-th term evaluates to yi , as desired.
… Uniqueness part (we’ll complete this proof in the next section) …
The square □ is called a tombstone and marks the end of a proof. It’s a modern replace-
ment for QED borrowed from magazines.
The proof writer gives a relatively brief overview and you are expected to fill in the
details to your satisfaction. It sucks, but if you do what’s expected of you—that is, write
down examples of the construction before reading on—then you build up those neural
pathways, and eventually you realize that the explanation is as simple and clear as it can
be. Meanwhile, your job is to evaluate the statements made in the proof on your exam-
ples. Practice allows you to judge how much work you need to put into understanding a
construction or definition before continuing. And, more importantly, you’ll understand
it more thoroughly for all your testing.
Uniqueness of Polynomials Through Points
Now for the uniqueness part. This is a straightforward proof, but it relies on a special
fact about polynomials. We’ll state the fact as a theorem that we won’t prove. Some
terminology: a root of a polynomial f : R → R is a value z for which f (z) = 0.
Theorem 2.3. The zero polynomial is the only polynomial over R of degree at most n which
has more than n distinct roots.
8
To “vanish” means to evaluate to zero.
21
Clearly the constructed polynomial f (x) is degree ≤ n because each term has degree
at most n. For each i, plugging in xi causes all but the i-th term in the sum to vanish,
and the i-th term clearly evaluates to yi , as desired.
To show uniqueness, let g(x) be another polynomial that passes through the same set
of points given in the theorem. We will show that f = g. Examine f −g. It is a polynomial
with degree at most n which has all of the n + 1 values xi as roots. By Theorem 2.3, we
conclude that f − g is the zero polynomial, or equivalently that f = g.
22
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 at most 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
single_term 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
my_list] is a shorthand expression for the following.
output_list = []
for x in my_list:
output_list.append(EXPRESSION)
def interpolate(points):
""" Return the unique polynomial of degree at most n 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.
The last block creates a list of terms of the sum from the proof of Theorem 2.2. The return
statement sums all the terms, using the zero polynomial as the starting value. Now for
the single_term function.
Arguments:
- points: a list of (float, float)
- i: an integer indexing a specific point
"""
the_term = Polynomial([1.])
xi, yi = points[i]
for j, p in enumerate(points):
if j == i:
continue
xj = p[0]
the_term = the_term * Polynomial(
[-xj / (xi - xj), 1.0 / (xi - xj)]
)
2. If four of the daughters collude without the fifth, they cannot use their shares to
reconstruct the secret.
3. If all five of the daughters combine their shares, 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 ≤ n). So I might have 20 daughters, 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.
25
Polynomial interpolation gives us all of these guarantees. Here is the scheme. First
represent the secret s as an integer. 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 the coefficients of f . Set a0 = s and randomly pick the other coefficients,
while ensuring ad ̸= 0. 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? 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
interpolate g(x), they can appeal to Theorem 2.2 to be sure that g(x) = 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. Pick a polynomial f (x)
of degree d = k − 1 = 2. If the secret is 109, we generate f as
(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 two little claims 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. Due to Theorem 2.2, each choice of y gives a different interpolating
polynomial.
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
representation of s, and you have the shares f (1), . . . , f (10). Let y be 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 gives 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.”
Note that the coefficients of the fake secret polynomial are no longer integers, but this
problem is fixed when you do everything with modular arithmetic instead of floating
point numbers (again, see the Chapter Notes).
27
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.
The property of being able to “decode” to 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.7 Exercises
2.3. Verify the following theorem using the examples from the previous exercise. That
is, write down examples and check that the theorem works as stated. 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.5. A number x is called algebraic if it is the root of a polynomial whose coefficients are
rational
√ numbers (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.6. Prove the product of two algebraic numbers is algebraic. Similarly (but much
harder), prove the sum of two algebraic numbers is algebraic. Despite the fact that π
29
and e are not algebraic, it is not known whether π + e or πe are algebraic. Look up a
proof that they cannot both be algebraic. Note that many such proofs appeal to vector
spaces, the topic of Chapter 10.
∑
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 shows one way the coefficients of a polynomial encode information
about the roots.
2.8. 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.9. There are many ways to skin a cat. The polynomial interpolation construction from
this chapter is just one, often called Lagrange interpolation. Another is called Newton
interpolation. Find a source that explains what it is, try to understand how these two
interpolation methods differ, and implement Newton interpolation. Compare the two
interpolation methods in terms of efficiency.
2.10. Bézier curves are single-variable polynomials that draw a curve controlled by a
given set of “control points.” The polynomial separately controls the x and y coordinates
of the Bézier curve, allowing for complex shapes. Look up the definition of quadratic
and cubic Bézier curves, and understand how it works. Write a program that computes
a generic Bézier curve, and animates how the curve is traced out by the input. Bézier
curves are most commonly seen in vector graphics and design applications as the “pen
tool.”
2.11. 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 they
are
∏
20
w(x) = (x − i)
i=1
10
This also works for possibly complex roots.
30
The coefficient of x19 in w(x) is −210, and if it’s decreased by 2−23 the position of many
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.11
2.12. 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 with a 32-bit modulus p.
2.13. 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.14. The Chinese Remainder Theorem is stated as follows. Suppose M > 1 is an integer
and M = m1 · m2 · · · mk where each mi > 1 is an integer. Suppose further that for each
i, j, the greatest common divisor of mi and mj is 1. Let r1 , . . . , rk be integers such that
0 ≤ ri < mi (ri is considered a desired remainder when dividing by mi ). Then there is
a unique x with 0 ≤ x < M such that x = ri mod mi for all i. One can construct the
desired number directly, provided one knows how to find multiplicative inverses, and the
proof is identical to the polynomial interpolation theorem. Find a source that expands on
the details and try to understand them.
2.15. 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.12 Sadly, we avoid complex numbers in this book. Luckily, there is a
“baby” fundamental theorem, which says that every single-variable polynomial with real
coefficients can be factored into a product of linear and degree-2 terms
• 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,
13
Though he had a Ph.D, early in his career Zhang had been unable to find academic work, and had stints in
a motel, as a delivery driver, and at a Subway sandwich shop before he found a position as a lecturer at the
University of New Hampshire.
32
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.14
One can interpret this theorem as an explanation (in part) for why clustering is a hard
problem. While there are hundreds of clustering algorithms to choose from, none “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 mathematically incompatible.
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 say, this process is a superb
example of navigating the border separating impossibility, existence, and uniqueness in
mathematics.
More on Secret Sharing
The secret sharing scheme presented in this chapter was originally devised by Adi Shamir
(the same Shamir of RSA) in a two-page 1979 paper called “How to share a secret.” In this
paper, Shamir follows the terse style and does not remind the reader how the interpolating
polynomial is constructed.
14
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
He does, however, mention that in order to make this scheme secure, the coefficients
of the polynomial must be computed using modular arithmetic. Here’s what is meant
by that, and note that we’ll return to understand this in Chapter 16 from a much more
general perspective.
Given an integer n and a modulus p (in our case a prime integer), we represent n
“modulo” p by replacing it with its remainder when dividing by p. Most programming
languages use the % operator for this, so that a = n%p means a is the remainder of
n/p. Note that if n < p, then n%p = n is its own remainder. The standard notation in
mathematics is to use the word “mod” and the ≡ symbol (read “is equivalent to” or “is
congruent to”), as in
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 evaluating 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 has a bank of examples to bolster
her. She knows how to cycle through applicable proof techniques, and how to switch
between different representations to see if a different perspective helps. Some of these
techniques are described in Appendix B.
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.
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
bar | 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). The ∈
symbol denotes membership in a set, and the objects in a set are called elements.
Fans of functional programming are cheering as they read, because set-builder notation
exists in many programming languages as comprehension syntax. In a language with
infinite list comprehensions, say Haskell, the above would be implemented as follows:
Lists made with list comprehensions need not have unique elements, while mathemat-
ical sets must. In set-builder notation is also more expressive. Put 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
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 A set
with no elements is called the empty set, and it has cardinality zero.
Proving one set is a subset of another is usually easy, but not always. The standard
technique is to fix b to be an arbitrary element of B, and use whatever characteristic
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 divisible 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.
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 parentheses denote a tuple, i.e., an ordered list allowing repetition.
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}
and the second is
(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.
You should be writing down examples, but this one needs some help. We think of
functions computationally as mappings from inputs to outputs. So much so that the nouns
function and map are synonyms. But this definition of a function is a set. I’m going to
convince you that the distinction is merely a matter of notation. It exists to fill the role
of a “bare metal” implementation of a function in the modeling language of sets.
For the example, say F is the set of pairs of positive integers and their squares.
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 Definition 4.6 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.
Reiterating a note from Chapter 2, the codomain B is not strictly encoded in the data
of a function F : A → B. The codomain is the set of allowed outputs.
So why go through all the trouble of defining functions in terms of sets? Part of the
answer is historical. The concept of sets as a modeling tool has probably existed for as
long as mathematics, but it was primarily used in its language form (“I declare, consid-
ereth only those heavenly numbers whose factorisation into prymes containeth nary a
repeated factor!”). The notation y = f (x) was invented in the 1700’s by Leonhard Eu-
ler, and in those times most functions were only defined in terms of formulas that were
easy to write down. 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 integers and ordered tuples in terms of sets.
How tedious.
In this light, our initial definition of a set was completely imprecise. There is a more pre-
cise definition, but it is the sort that only a logician would love, called Zermelo-Fraenkel
set theory. In brief, its base concepts are the empty set, set membership, a notion of in-
finity, and a restricted choice of ways to build sets from other 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 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.
Few do it, but we all take comfort in the idea that we could peel back the layers to reveal
the raw assembly instructions. In reality, abstractions keep us productive. Likewise,
defining the entirety of mathematics in sets is like “bare metal” programming, but without
any of the speed benefits of the finished program. Someone ironed out set theory it once,
and we have a record of their work. Now we can get back to doing mathematics.
44
The special notation for functions highlights our conceptual emphasis. We think of
functions differently than regular sets, with a semantic input-output dependence that set
notation doesn’t natively convey.
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 signal 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 a 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
All bijections are invertible, and vice versa invertible functions must be bijections.
Computing the inverse function given only a description of a function can be notoriously
difficult. Indeed, most of cryptography rests on the assumption that some functions are
6
Injections are sometimes called one-to-one and surjections are sometimes called onto. I won’t use those terms
in this book, but they are common.
47
A B
The next proposition says that a “left-sided” inverse—satisfying just one of the two
requirements to be an inverse—that happens to be a bijection is automatically a two-sided
inverse.
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.
48
Let’s rephrase that elegant argument in the language of sets. Let X be the set of games
and Y the set of players. Define a function f : X → Y by calling f (x) the loser of
game x. This function is not a surjection. Rather, the image f (X) is the subset L ⊂ Y of
losers. However, f is an injection (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 (almost) exactly 2. “Almost,” because the winner
may have lost zero games or one game. This also means you can’t count the number of
games exactly, but will be forced to provide bounds.
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 US Census Bureau faces regularly. You might 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 , which doesn’t
depend on the particular elements in X, just its size. In words, n2 is the number of ways
to choose two objects from 8
(n) a set of n objects. The problem is, can we come up with an
arithmetic formula for 2 in terms of n? 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
you that two different choices of balls give you different diagonals, i.e., g is an injection.
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 when |X| = n. Each pair
of squares in the bottom row corresponds to a unique ball in the triangular arrangement
above it.
Likewise, given 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 balls,
and each row has one fewer ball than the( )row underneath
( ) it, so |Y | = 1 + 2 + · · · + n − 1.
Moreover, X has n squares in it, so | X2 | = n2 . The bijection tells us that these two
values must be equal.
You may wonder: 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 mathe-
matician would consider a picture as a rigorous proof in and 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 represent the balls as points inside R2 . 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 11
(Xselect
) two balls from
(n+1a) bin. For simplicity we’ll use the numbers
themselves. Now 2 is a set of size ( 2) and we want to express the size in terms of
our (inductively assumed) formula for n2 . 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 let’s split the elements of X2 into two parts: the part 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
(X ) between the two options, we can add the sizes of both
parts to count the size of 2 . ( )
( )
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 the two elements, then there are only n remaining choices for the
second element.12
Adding up the sizes of the two parts gives exactly
1 + 2 + · · · + (n − 1) + n,
which is what we set out to prove.
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.13
You’ll appreciate the answer to this problem best if you spend some time trying to
solve it first.
Back already? 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 is true, and using that as-
sumption 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 > 1 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: 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.14
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
13
Looking forward to Chapter 6 on graph theory, we’re saying that the social connections at our “party” form
a simple, undirected graph.
14
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
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.”
For those readers who are interested in a bit more details about what makes a math-
ematical proof, or how to approach proving things, in this second edition I added two
appendices that may help. Appendix B contains a bit more details about the formalities
underlying proofs, along with a section at the end called “How does one actually prove
things?” Appendix C contains a list of books under “Fundamentals and Foundations” that
cover the basics of set theory, proofs, and problem solving strategies. Readers of the first
edition have told me that following along with these books has helped immensely.
Before I state what “not cheating” means mathematically for the marriage problem, I
encourage you to write down a small example of sets M, W of size n = 4, rankings
prefw (m) for each w ∈ W and prefm (w) for each m ∈ M , and a candidate marriage
f : M → W . I’ll call the marriage from the women’s perspective f −1 : W → M .
What I mean by “no mutually desired cheating” is the following.
2. The pair m and w mutually prefer each other over their assigned matches.16 I.e.,
both prefm (w) < prefm (f (m)) and prefw (m) < prefw (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.17 In the interest
of generality, I’ve defined classes Suitor and Suited to differentiate: Suitors propose
to Suiteds.
16
Remember, a lower number in pref means a higher preference!
17
See pimbook.org
56
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
The Suitor class is simple. Instances are uniquely identified by an id, which I’m
defining to be the index in a global list of Suitors. A Suitor has a preference_list,
which is a list of Suited ids sorted from most preferred to least preferred. The
index_to_propose_to variable simultaneously 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 best in her preference_list, and returns all others 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.
We will argue that the algorithm terminates by monotonicity. Here’s what I mean by
that: say you have a sequence of integers a1 , a2 , . . . which is monotonically 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 itera-
tion t of the core loop, and show that at is monotonically 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.
Theorem 4.15. The deferred acceptance algorithm always terminates, and the bijection
produced at the end is stable.
Proof. For the deferred acceptance algorithm we have a nice monotonic 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.
58
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 Suited
w with mutual incentive to cheat, so for contradiction’s sake we’ll suppose that the f out-
put 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),
}
2. Bijections show up everywhere, and they’re a central tool for understanding the
same object from two different perspectives.
3. Mathematicians usually accept silent type conversions between sets when it makes
sense to do so, i.e., when there is a very clear and natural bijection between the two
59
sets.
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 it is empty, or 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.4. Look up a statement of the pigeonhole principle, and research how it is used in proofs.
4.6. For each n ∈ N, let An be a countably infinite set, such that all the An have empty
intersection. Prove that the union of all the An is countable. Hint: use the previous
problem.
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
Theorem 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
18
Note, this ambiguous notation conflicts with the previous exercise, and takes a different meaning here. ABC:
Always Be Contextualizing.
60
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 for a description of this principle (it is a family of theorems) and find ways it
is used to help count.
4.10. There is a large body of mathematics 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 S, say {1, . . . , n}, such that every size-t subset of S 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 an explicit description of a (7, 3, 2)-system.
4.11. Continuing 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 on n may a Steiner (n, 3, 2)-system exist.
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 deter-
mine how numbers and pairs are represented in this “bare metal” mathematics. Look up
Russell’s paradox, and understand why ZF-set theory avoids it.
61
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
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.
Roth gives a fascinating talk19 about the evolution of the medical residency market
before he stepped in, detailing how students and hospitals engaged in a maniacal day-
long sprint of telephone calls, and all the ways unethical actors would try to game the
protocol in their favor.
19
https://youtu.be/wvG5b2gmk70
62
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
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é
63
64
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 mathematical
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 money 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: 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. Programmers do this as well, but the conventions differ. Ev-
ery mathematician knows that n is a natural number, and that f is a function. Or at least,
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
they know that when they see these letters out of context, they should at least behave
like a natural number and a function, respectively. Seeing n(f ) out of context would
momentarily startle me, though I can imagine situations making it appropriate.2 Simi-
larly, 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 cheekily
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. You can 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,
you can 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 num-
ber.
3
For example. You don’t need to know what a homomorphism is.
66
Why not invent a better name? They do! Just later. In fact, because the expression
H 1 (X, O∗ ) is so important in the study of algebraic geometry, it was renamed 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! 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 it while drunk or asleep. 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. There are the standard inequalities and equali-
ties. Then there are operators that look like ∼ = or ≃ that represent equality “up to some
differences 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
version 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. The core properties of = are respected even if not
identity. 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 suggests 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—the commutative property—because this is true of addition. Many multiplications are
not commutative, such as matrix multiplication, and so a generic multiplication uses ×
or juxtaposition to signal this.
With this in mind it’s the mathematician’s turn to criticize programmers. For example,
67
reading programming style guides has always amused me. It makes sense for a company
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,
except 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 pro-
gram? Besides, if there were truly a good reason for the first option, why wouldn’t the
language designer just disallow the second option 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 dis-
coverable!) 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 variable,∑a minimum and maximum value for the index, and an expression being
summed. So 9i=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 b2 to
sum the squares of all elements of B.
But wait, there’s more! It often happens that B has an implicit, or previously defined
∑ 2 of the elements B = {b1 , . . . , bn }, in which case one takes the liberty of writing
order
i bi (“the sum over relevant i”) with no mention of the set in the (local) syntax at all!
As we saw in Chapter 2 with polynomials, one can additionally add conditions below
the index to filter only desired values, or even have the constraint implicitly define the
68
variable range! So you can say the following to sum all odd bi ∈ B
∑
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 of the abuse
when they find it genuinely useful.
What makes all of this okay is when the missing parts are fixed throughout the discus-
sion 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 use 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.
69
70
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.
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, typesetting braces requires an escape character. They’re
also visually harder to parse when nested. Finally, directed edges use the ordering of a tuple.
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 label ‘e’. Vertices 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” or “walk” to a path which does repeat vertices. For us, the difference
won’t matter. A cycle is a path that starts and ends at the same vertex.
A subgraph (H, F ) of a graph (G, E) if a choice of a subset of the vertices and edges of
G which also forms a valid graph. I.e., H ⊂ G and F ⊂ E. Crucially, this requires that
any edge e = (u, v) in H has both u and v in F . An induced subgraph has the additional
property that if two vertices are adjacent in G, they must also be adjacent in H. In that
way, the structure of an induced subgraph H is completely determined by the subset of
vertices, which is why the term “induce” is appropriate.
A graph is called connected if there is a path from each vertex to each other vertex, and
otherwise it is called disconnected. Equivalently (you will prove this in an exercise), G =
(V, E) is connected if it is impossible to split V into two nonempty 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 connected subgraph 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
72
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.3 The Petersen graph is 3-colorable (find a 3-coloring!) but not 2-
colorable.
Definition 6.3. The chromatic number of a graph G, denoted χ(G), is the minimum
integer k for which G is k-colorable.
Recall from Chapter 4 that mathematicians often define functions without knowing
how to compute them. The chromatic number is an excellent example. We define the
concept to clarify what it is we want to study, and the modeling language of sets allows
us to start to reason about it.
If you believe that the Petersen graph is not 2-colorable—or you do the exercise that
proves this—then we know 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.
3
Why? Part of it is that the Petersen graph is highly symmetric. We’ll revisit this in the exercises for Chap-
ter 16.
73
A simple graph meets this bound and has χ(G) = maxv∈V deg(v) + 1. See if you
can find it. On the other hand, this bound can be quite loose. Here “loose” means that
there are graphs which meet the conditions of the proposition, but the true χ(G) is much
smaller than the proposition enforces. Consider the “star” graph which has n vertices
and 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 proposition is effectively
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. Since φ is proper,
{ −1 there are no edges }
among these vertices. Moreover, since φ is a function, the set φ (j) : j = 1, . . . , k
partitions4 V into “color classes,” and all the edges of G go between the color classes.
Figure 6.5 shows a picture for the Petersen graph.
This perspective can be used to design coloring algorithms. Start 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.
frequencies. Radio towers pick frequencies to broadcast, but if nearby towers are broad-
casting on the same frequency, they will interfere. So the vertices of the graph are towers,
nearby towers are connected by an edge, and the colors are frequencies.
A more interesting and satisfying application is register allocation. That is, suppose
you’re writing a compiler for a programming language. Logically the programmer has
no bound on the number of variables used in a program, but on the physical machine
there is a constant number of CPU registers in which to store those variables. The register
allocation algorithm must decide (at compile time) which registers will store which logical
variables as the computation progresses, and which logical variables must be stored in
memory. The less often you need to shuffle data back and forth between memory and
CPU registers, the faster the program will run.
The connection to graph coloring is beginning to reveal itself: the vertices are the log-
ical variables and the colors are physical registers, but I haven’t yet said how to connect
two vertices by an edge. Intuitively, it depends on whether the logical variables “overlap”
in the scope of their use. The structure of scope overlap is destined to be studied with
graph theory.
To simplify things, we’ll do what a compiler designer might reasonably do, and compile
a program down to almost assembly code, where the only difference is that we allow
infinitely many “virtual” registers, which we’ll just call variables. So for a particular
program P , there is a nP ∈ N that is the number of distinct variable names used in the
program. Each of these integers is a vertex in G.
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 its 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
75
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.5
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 there
is no known provably correct (in the worst case) and provably efficient algorithm for
computing it. Moreover, if there were, the same algorithm could be adapted to solve a
whole class of problems that are also believed to be intrinsically hard to solve. The notion
of efficiency here is—as usual for algorithm analysis—in terms of the runtime compared
to the size of the input as the input grows. This is called “asymptotic analysis” or “big-O.”
See Chapter 15 for a longer discussion.
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 efficient and
provably correct algorithm for the following problem. Fix any c such that 0 < c < 1.
Given any graph G as input, if G has n vertices, output a number Z with the property
Z
that χ(G) < nc .
As mentioned, this is an asymptotic statement, meaning an algorithm that only works
for all graphs with fewer than a thousand nodes is not a solution. A lookup table, though
it would be massive, would solve this problem efficiently. No, a true solution must work
and must work efficiently for any arbitrarily large graph in principle, though working
on small graphs may be sufficient in practice.6 But to put the numbers in perspective
with an example, this theorem says that for graphs with n = 104 vertices and with
c = 1/2, algorithms will struggle to output a number guaranteed to be between χ(G)
and 100 · χ(G).
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.
Figure 6.6: An example of a planar graph which can be drawn with no edges crossing.
be surprised if you’re struggling to prove that a given graph is not planar. You personally
failing to draw a specific graph without edges crossing is not a proof that it is impossible
to do so. There is a nice rule that characterizes planar graphs, but it is not trivial. See the
chapter exercises for more.
Now that you’ve tried the exercise: Figure 6.7 depicts two important graphs that are
not planar. The left one is called the complete graph on 5 vertices, denoted K5 . The
word “complete” here just means that all possible edges between vertices are present.
The second graph is called the complete bipartite graph K3,3 . “Bipartite” means “two
parts,” and the completeness refers to all possible edges going between the two parts.
The subscript of Ka,b for a, b ∈ N means there are a vertices in one part and b in the
other.
We defined planar graphs informally in terms of drawings in the plane, which doesn’t
use sets, functions, or anything you’ve come to expect. Indeed, the hand-wavy definition
is the one that belongs in your head, but the official definition of a planar graph is one
which has an embedding into R2 . The problem is that defining an embedding requires
opening a big can of worms, because it applies to spaces more general than a graph. We’ll
give you a taste in the chapter notes.
One feature about planar graphs is that when you draw a planar graph in such a way
that no edges cross, you get a division of R2 into distinct regions called “faces.” Figure 6.8
shows a graph with four faces, noting that by convention 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 notice7 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) with at least one vertex, and
any drawing of G in the plane R2 defining a set F of faces, the quantity |V |−|E|+|F | = 2.
7
Why anyone would have reason to analyze this quantity is a historical curiosity; it was discovered by Euler
for certain geometric shapes in three dimensions called convex polyhedra. See the following for more: http:
//mathoverflow.net/q/154498/6429
77
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.
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). Adding a vertex by itself is not allowed because the graph must stay
connected at all times.
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
Notice how it does not matter how we drew the edge, so long as it doesn’t cross any
other edges to create more than one additional face. The second case is similar, except
adding an edge connected to a new vertex does not create any new faces. Convince
yourself that any vertex involved in a path that encloses a face has to have degree at least
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 or not it’s
planar) can be built up by a sequence of adding edges by these two cases. This completes
the proof.
note that the connectivity requirement is crucial for the theorem to hold, since a graph
with n vertices and no edges has |V | − |E| + |F | = n + 1.
This was proved by Kenneth Appel and Wolfgang 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).
Lemma 6.9. If a planar graph G has m ≥ 2 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.8 In other words, each face
is “counted” by each edge it touches, and each face has at least three edges counting it.
Hence 3f counts each edge at most twice, while 2m counts each face at least three times.
8
An edge incident to a vertex of degree 1 will touch the “outside” face twice, but this only counts as one face.
79
The requirement that m ≥ 2 is necessary, since if there is only one edge (or zero), then
the outside face is the only face. It only gets “counted” twice (or zero times) by the edges
it touches. Once we get to two edges, the outside face is counted twice (2m = 4). As
you add more edges, either you add dangling edges (or subdivide existing edges) which
increases 2m but not 3f , or you add edges that create new faces. In the case of a single
edge creating a single new face, the lower bound 3f increases by exactly 3, but the upper
bound 2m only increases by 2.
Despite having just read a proof, this may be surprising: can’t we keep adding face-
creating edges to make the lower bound of 3f exceed the upper bound of 2m? It’s in-
structive to take a moment and play with examples. You’ll eventually get to a situation
in which all interior faces are triangles, and the inequality is either an equality or very
close. Then the creation of new faces requires a sufficient number of non-face-creating
edges to be made first, which loosens the inequality. The proof above explains how this
loosening and tightening of the inequality corresponds to the geometry of a graph drawn
in the plane. It translates the geometry to algebra. When the algebra seems to misbehave,
we can call back to the geometry to understand.
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 from Lemma 6.9 into the Euler characteristic 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.
80
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. We want to extend or
modify this coloring and in doing so properly color v. This will finish the proof. When v
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, because the neighbors may need
all 5 colors a priori.
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 new graph G′ (“G prime”9 ) by deleting v and merging wi and wj ,
i.e., delete v, wi , wj and add a new vertex x which is adjacent to all the remaining vertices
in N (wi ) ∪ N (wj ). I claim that if G′ is planar then we’re done: G′ has |V | − 1 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 coloring is proper. 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 can 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. When
the two vertices merge, and “become” x, we get a planar drawing of G′ . 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.
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.
9
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.
81
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.
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. We use this to assign colors to the vertices, using [ ]. For
example, this is the base case of our induction: trivially color each vertex of a ≤ 5 vertex
graph with all different colors.
82
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
needed 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:
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 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 imple-
mentation is technical and not interesting. The official igraph function we used is called
contract_vertices. The remainder of the algorithm executes the recursive call, and
83
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.10 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
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.
√
If there are no vertices of degree ≥ n, then the maximum degree of a vertex is at
√
most ⌈ 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 at
√
most 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 breakthroughs using
a technique called semidefinite programming got the number of colors down to roughly
n0.2 . 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.
11
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.
12
The symbols ⌈−⌉ denotes the ceiling of the argument, which is the smallest integer greater than or equal to
the input. Similarly, ⌊−⌋ denotes the floor. These are mathematical ways to say round up or down.
85
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 from 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. In Section 6.1 we claimed that the following two definitions of a connected graph
are equivalent: (1) there is a path between every pair of vertices, (2) it is impossible to
split V into two nonempty subsets X, Y such that no edge e = (a, b) has a ∈ X and
b ∈ Y . Prove this.
6.4. 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 between 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 conse-
quence 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.
86
6.5. 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.7. 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.8. 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.
√
6.9. 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.10. 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
are like normal paths, 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.11. 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.12. 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 max flow problem.
6.13. A hypergraph generalizes the size of an edge to contain more than two vertices.
87
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 .
in the codomain. Without continuity, a “drawing” could break edges into disjoint pieces
and there would be chaos.
The real question is: what is the domain of this function? It can’t be G as a set because
we don’t have a notion of “closeness” for pairs of vertices, and we really want to think of
an edge as a line-like thing.
The trick is to start imagining abstract spaces that are not sitting in any ambient geomet-
ric space. This is where the formalisms of topology shine, but unfortunately a satisfying
overview of the basic definitions of topology is beyond the scope of this book. It suffices
for our purposes to understand two concepts:
One can take the disjoint union of two abstract spaces and get another abstract space
in which the points comprising the two pieces are different. In other words, we can take
lots of different copies of the same space (in our case [0, 1]), their disjoint union is like a
bunch of lines, but we aren’t presuming any way to compare the different pieces. Each
piece retains its internal geometry in the composite.
The second idea is that one can identify two points in an abstract space. Intuitively, one
can “glue together” two points and maintain the rest of the space unhindered. For 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. This foreshadows a topic in a later chapter
called the equivalence relation, which formalizes how to identify points in a consistent
way.
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 , require it
to be injective, 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!
If this interests you and you’d like to see it made more formal, pick up a book on
topology. Appendix C contains some references. Unfortunately I haven’t yet found a
topology book that I genuinely like. Most books tend to be terse and contain few pictures
(which is the opposite of how topology is done!). Topology aims to generalize much of
calculus, so waiting until after Chapter 14 might be prudent.
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 amphetamines, 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 gave combinatorics
credibility as a field of study and established its reputation as a field that prioritizes prob-
lem 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 stylistic antithesis to Paul Erdős is the French
mathematician Alexander Grothendieck. He also lived a curiously eccentric lifestyle in-
volving radical anti-military politics and an eventual self-exile to a small village in South-
ern France. Grothendieck declined various prizes for his life’s work, and decried the math-
ematical 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. Even within a single subfield such as geometry, mathematicians can have entirely
different styles. Henri Poincaré remarks in his essay, “Intuition and Logic in Mathemat-
ics,”
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 a full
breakfast. 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: 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 Chapter 14 we can survey many of the important features of a 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 corresponds to a vertical change, while the difference in x’s
corresponds to a horizontal change. The slope is an invariant of the line because it does
not depend on the choice of points. This can be proved rigorously by appealing to similar
triangles. Lines and other simple functions often represent the 1-dimensional position of
an object over time, while the steepness—the ratio of the change in position to the change
in time—is the velocity of that object.
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 y = f (x), then
1
For example, the line {(x, y) : x = 1} cannot be written as a function of x. It can be written as a function
of the y variable, but then the concept of slope is rotated by 90 degrees.
97
f (x2 ) − f (x1 )
slope(f ) = .
x2 − x1
In this way, the concept of slope requires an orientation of the line and the coordinate
system it is represented in. The input coordinate is defined as “horizontal” while the
output coordinate is “vertical.” This is the mathematician’s choice, though calling x the
“horizontal” coordinate is standard.
Now let’s try to translate the concepts to the curved function f (x) in Figure 8.2. It has
a complicated formula we won’t write down. The curve 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 numerically and consistently how the two
steepnesses compare, we’d be at a loss. The picture gives only qualitative information.
We must 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.3.
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
difference. So x and x′ play similar roles, but x′ is slightly different from x in some way.2
2
It’s a shame that the tick symbol is also used in calculus to denote the derivative of a function, but this will
be a good opportunity to practice disambiguating notation using context.
98
A
f
Figure 8.2: For a general curve, steepness depends on where you measure.
A'
A
f
x x'
Figure 8.3: We can use the slope of a line as a proxy for the corresponding “steepness”
measurement on a curve.
99
A'
A B'
x x'
Figure 8.4: Two different lines show how the approximation can be better or worse, de-
pending on where it is.
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. While imperfect,
the approximation is good enough to distinguish it from a similarly bad approximation
of the steepness of f at B, as shown by Figure 8.4. Concrete numbers for the slopes of
these two lines suggest that f is twice as steep at A as at B. Our brains itch 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 improve our estimate.
Once blessed with the idea of approximating the steepness of f at A by drawing a
line from (x, f (x)) to some other (x′ , f (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.5.
f (x1 ) − f (x)
steepness at A ≈
x1 − x
f (x2 )−f (x)
Our yearnings are destined for iteration. Do it again, and again, getting x2 −x and
f (x3 )−f (x)
x3 −x , and so on.
With each step the line approximation gets better and better, closer
and closer to our brain’s intuitive picture of the steepness at A.
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 approach some
concrete number? If so, we could reasonably call that number the “true” steepness of f
at A.
100
x x1 x'
That is exactly what limits do. A limit is 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 un-
derstanding for what a limit should look like. It’s the “end result” of iteratively improving
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 example, 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:
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
particularly vexing pathological examples. They’re the penetration testers of mathemat-
ics. You have have heard of a particularly famous one called the Cantor Set.
Indeed, much like a program, once a mathematical definition is written down it must be
judged on its own merits. It must behave properly under any “input.” Best practices also
suggest definitions reduce cognitive load and avoid too many special cases. Achieving
the right balance is a serious challenge.
An unfortunate consequence of all this is that math books start with the final
definition—the end result of this arduous design process—followed by many pages of
theorems and proofs explaining why it doesn’t succumb to edge cases. Calculus is no
different, and in fact most of how Isaac Newton and Gottfried Leibniz originally did cal-
culus was in an informal, intuitive setting, without much rigor at all. It was a less famous
mathematician, Karl Weierstrass, who is considered to have finally “set calculus straight”
(though it was really a team effort over decades). Modern calculus textbooks are a strange
mix. They want to capture the informality of Leibniz, feel obliged to Weierstrass’s rigor,
but can’t commit to either approach fully. Going full Leibniz would be error-prone. On
the other hand, The cult of Weierstrass requires detailed proof-reading skills. Alas, math-
ematicians are usually the only ones who enjoy the elaborate tour of blunders and false
starts that historically sculpted a modern definition. One could hardly cajole the average
student to care, or even the brightest student, until after those blunders come to bear on
their own work.
To my delight, you’re still reading. My goal for the rest of the chapter is to whet your
appetite for definition crafting. Let’s continue with the “steepness of a function” as our
prototypical example of a limit. Here’s one of those pathological examples that makes
limits hard. I’m going to define a non-curve and not-even-connected function f : R → R
as follows: if x is 1/k for some integer k, then f (x) = 2x, otherwise f (x) = x. Figure 8.6
sketches f .
Now we can ask: what’s the steepness of f at x = 0? We pick some starting x1 , com-
pute the slope, pick an x2 , compute the slope, and keep going until we see convergence.
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
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.6: This pathological function admits two different possibilities for the derivative
depending on the sequence of approach.
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
4
If you want more, check out the book “Counterexamples in Calculus.”
5
We say “the” limit because the definition makes it unique. You will prove this in Exercise 8.4.
103
FOO there is a BAR,” means there’s a computational relationship. If you give me a FOO
as input, I can produce a BAR with the desired property as output.6 In fact there may be
many such BARs. 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. They’re
at least as close as specified by ε.
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.
First let’s see a concrete example of the threshold-to-sequence-index functional rela-
tionship. If you require ε = 1/4, I need to find an index after which all xn are within 1/4
of 1. I.e., all these xn ’s should satisfy 1 − 1/4 < xn < 1 + 1/4. Another way to write this
is with the absolute value: |xn − 1| < 1/4. Since we already see that 3/4, also known
as 1 − 1/4, is one of the sequence elements, it should be easy to guess that everything
starting at k = 5 will be close enough to 1. Indeed, we can do the algebra.
( )
1 1 1
|xn − 1| = 1 − − 1 = − = ,
n n n
and 1/n < 1/4 when n > 4.
Now let ε > 0 be unknown, but fixed. We can do the same algebra as above. How
large of an index k do we need to ensure |xn − 1| < ε for all n > k? In other words,
can I write ε in terms of n so that all of the above equations and inequalities are still true
when I replace 1/4 with ε?
Above we showed that |xn − 1| = 1/n, so to ensure that 1/n < ε we can rearrange
to get n > 1/ε. Picking any index k bigger than that will work.7 Since ε is fixed, pick
k = ⌈1/ε⌉ (the “ceiling” of 1/ε). Let me restate all of this as a theorem with a proof as
you might see in a book.
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
6
It isn’t strictly true in math that it can be computed. 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.
7
The fraction 1/ε doesn’t risk division by zero because Definition 8.1 requires ε > 0.
104
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 converge to L. But if you can always produce a good
k no matter what, the sequence converges to L.
As a notational side note, the phrase “for every x there is a y” can be long and annoying
to write all the time. It also makes it difficult to study the syntactic structure of statements
like this, since dependence among variables may be unclear. Mathematicians designed an
unambiguous notation for this situation called quantifiers. We briefly introduced quanti-
fiers in Chapter 4, and promised we wouldn’t use them in this book. However, standard
textbook definitions 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 meaning 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!).
105
Figure 8.7: 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.
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.
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 and xk ̸= 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 the threshold required by the definition of f (xn ) → 3. We’ll 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.7 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,
106
the bottom row is the black box subroutine for xn → 2. Given the initial ε > 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.
To that effort, at the end of the day we need to show that
|f (xn ) − 3| = |x2n − 4| = |(xn + 2)(xn − 2)| < 5|xn − 2| < 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 (and continuous) 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.
This proof embraces a style of mathematics called analysis. The term “analysis” can
refer to specific subfields of study, such as real analysis or complex analysis which are
the formalizations of calculus for real and complex numbers. More broadly, an area of
math called “analysis” stresses proof techniques that deal with bounds and approxima-
tions. The error in these approximations can be controlled to achieve the necessary goals:
loosely when attempting to simplify complexity that is irrelevant to the goal, or tightly
107
when that complexity needs to be understood to achieve the goal. As Weierstrass prac-
ticed in formalizing calculus, analysis aims to reduce problems to parts that can be inde-
pendently understood with number sense. This is why, as we recalled in Chapter 7, Henri
Poincaré calls the analytical approach a “prolongation of arithmetic.” This also motivates
the Taylor series that we’ll see later in this chapter. They further prolong our arithmetic
abilities to express things that finite sums cannot, giving us further tools to control the
quality of an approximation.
As we saw with our pathological “two lines” example from Figure 8.6, not every func-
tion has a limit at every point. For the “two lines” f (x), we computed the slope as
f (xn )−f (0)
xn −0 where xn was part of a sequence tending to zero. I.e., we informally computed
f (x)−f (0)
the limit limx→0 x−0 . But then we found two sequences an , bn that both converge
to zero, but their slope-sequences f (aann)−f
−0
(0) f (bn )−f (0)
, bn −0 gave different values. As a con-
sequence, the limit cannot be equal to either value. So we’ve seen that this definition of
the limit passes a litmus test: good functions have limits, and bad functions do not.
Before continuing, here are a few basic propositions for working with limits that will
come in handy in the rest of the chapter and in the exercises. Most calculus or real
analysis textbooks will contain a detailed proof. Basically, they say that most arithmetic
operations are compatible with limits, provided the limits involved exist. These formalize
the general rule that, absent of any strange function behavior, you can “plug in” the
sequence limit to get a function limit, i.e., that f (a) = limx→a f (x).
f (x) − f (c)
lim
x→c x−c
This value is denoted f ′ (c).8 In the limit, sequences xn → c are taken so that xn ̸= c
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
x2 − 6x + 9
= 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 for which xn ̸= 3.
Hence, we never divide zero by zero and may simplify.9
(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
differentiable 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 ,
9
Another way to view it is that the two functions p(x) = (x−3)(x−3)
x−3
and q(x) = x − 3 have the same
values at all inputs except x = 3, in which case p(x) is undefined. Hence, we may replace p with q for any
calculation that ensures the input is never 3.
109
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.
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:
110
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.
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 the facts that are, in
my view, the most important for applications.
Let F be the set of all functions R → R that have derivatives. Let D be the function
that takes as input a function f and produces as output its derivative f ′ . Note the domain
of D is F , but its codomain is not F because some differentiable functions are not twice
differentiable.
As a function, “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.
10
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 arccos(x)”
should they forget.
111
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.
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.11
A'
A
f
x x'
Figure 8.8: The line between A and A′ does not approximate f well close to A.
in the statement. Sadly, this doesn’t work. Take our example from earlier, replotted in
Figure 8.8. There, the line between A and A′ is not the tangent line at A, and it is also
far closer to f at A′ than the tangent line would be. However, for points close to A, the
tangent line is a much better approximator. If we’re trying to approximate f “at” A, we
care more about points closer to A than points far from A. Here’s how we make this clear
in the math.
Take any line K(x) that is supposedly challenging the tangent line for the title of
“best approximating line of f at x = c.” Then I claim I can choose a small enough interval
around c (the width of this interval depends on the features of the challenger K) so that L
beats K on all points in this interval. Here’s the formal theorem I’ll prove momentarily.
Theorem 8.11. Let f : R → R be a function and A = (c, f (c)) be a point on f at which
f is differentiable. Let L(x) be the tangent line at c, i.e. L(x) = f ′ (c)(x − c) + f (c). Then
for every line K(x) 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
“open interval” 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 an ε > 0 such that when |x − c| < ε the following inequality is true.
113
We don’t yet know this inequality is true, but we can “work backwards” by doing valid
algebraic manipulations until we get to something we know is true. In particular, one
might recognize the definition of the derivative hiding in there and divide by |x − c| to
get
′
f (x) − f (x) − f (c) ≤ m − f (x) − f (c) .
x−c x−c
and let x1 , x2 , x3 , . . . be the corresponding x’s violating the inequality for each εi . Since
each xi is in (c − εi , c + εi ), it follows that xi → c, but because (by assuming the contra-
dictory hypothesis) the inequalities are false, the sequence f (xxi )−fi −c
(c)
does not converge
to f ′ (c). The contradictory hypothesis says it’s closer to m instead. This contradicts the
definition of the derivative.
We have proved that derivatives provide the best linear approximation to a function
at a point for a concrete sense of “best.” This raises a natural question. Can we improve
this approximation by using more complicated functions than lines? The answer is yes.
The tool is called the Taylor polynomial.
Taylor Polynomials
One nice thing about polynomials is that they have a grading. By that I mean, if you
increase the degree of your polynomial, you can express a wider variety of functions. In
principle, higher degree allows a polynomial to express more complexity, and produces
better approximations of f .
You can derive exactly how this works by following the steps of Theorem 8.11, and
asking for a degree at most 2 polynomial whose derivative best approximates f ′ close to a.
Suppose our candidate is the following (where below q ∗ ∈ R is the unknown parameter
we must set to get a degree 2 polynomial).
114
p′ (x) = 2q ∗ (x − a) + f ′ (a).
Plugging in x = a leaves only f ′ (a). 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)).
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 difference 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 attempt to use the second derivative only makes sense if
f has a first derivative at that point, 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 cumbersome. 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. If f has infinitely many derivatives
(i.e., it is n-times differentiable for every n ∈ N), f is called smooth. The typical example
of a smooth function is sin(x) or 2x . A default modeling 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)
p(a) + (x − a)n
n!
n=1
As if possessed by the spirit of Leonhard Euler, we write down examples. Here are the
first three terms of the general summation.
x2 x3 x4
1+x+ + + .
2 6 24
Figure 8.9 contains a picture of ex and its approximation by the degree 4 Taylor poly-
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 a classifier is wrong. 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 it. 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, for example, that the classification error decreases
with more data.
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
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. Convergence of functions requires a definition of
what it means for two functions to be “close” together, which has subtleties beyond the
scope of this chapter. But suppose we did that and we can make sense of this expression,
116
we’d hope that this limit was also equal to f , at least when x is sufficiently close to 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 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 sequence 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.13
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.10
2
contains a plot of this function. You will prove in Exercise 8.11 that f (n) (0) = 0 for every
n ∈ N. As a consequence, all of its Taylor polynomials at x = 0 are the zero function,
13
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. Defining
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.
117
Figure 8.10: A function f (x) = 2−1/x , all of whose derivatives are zero at x = 0.
2
and the “limit function” should be the constant zero function.14 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
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.
14
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.
118
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)-st 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.
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.
119
Figure 8.11: A function whose root does not have a nice formula.
c d
Figure 8.12: An example of Newton’s method outperforming a binary search. The tangent
line at d is better than the slow approach from c.
xn x n+1
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.13. To find the intersection
point, set y = 0 in the equation for the tangent line, and solve for x:
Let f : R → R be a function which is “nice enough” (it has some properties we’ll
explain after the proof). Let r ∈ R be a root of f inside a known interval c < r < d,
and pick a starting value x1 in that interval. Define x2 , x3 , . . . using the formula xn+1 =
xn − f (xn )/f ′ (xn ). Call ek = |xk − r| the error of xk .
Theorem 8.15 (Convergence of Newton’s Method). For every k ∈ N, the error ek+1 ≤
Ce2k , where C is a constant defined as18
|f ′′ (z)|
C = max
c≤z,y≤d 2|f ′ (y)|
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.
18
A tighter choice of C is possible, where we set z = y, but it requires a more detailed proof and uses the
Taylor polynomial in the same way as this proof.
123
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, in
which case you can check to see if f (x) = 0 at each step to avoid the edge case of
hitting r exactly. Otherwise we risk dividing by zero, or worse, getting stuck in a
loop (as we’ll see in the example below).
• 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.
• 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-12
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.
124
Figure 8.14: An example where the starting point of Newton’s method fails to converge
due to an unexpected loop.
(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.14, drives Newton’s method in the wrong
direction! By the end of a hundred iterations, Newton’s method cycles between three
points:
...
(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.
125
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.
Nevertheless, there is a technique called Householder’s method that generalizes New-
ton’s method to higher degree Taylor polynomials. Higher degrees unlock order-of-
magnitude better convergence. The tradeoff, as expected, is that it takes progressively
more work to compute each step in the update (and existence and good behavior of higher
derivatives). Moreover, there are additional requirements at each step on the suitability
of a starting point to guarantee convergence. You will explore this in an exercise.
• 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
used as a number, but rather notation for the concept, “xn grows in magnitude
without bound.” This unifies it with the usual limit definition.
2. A function f is said to diverge at a, written limx→a f (x) = ±∞, if whenever
xn → a then f (xn ) diverges.
3. A function f : R → R is called concave up at a if the second derivative f ′′ (x) is
positive at x = a. Likewise, if f ′′ (a) < 0, f is called concave down at a. How does
the numerical property of being concave up/down relate to the geometric shape
of a curve?
4. A function f : R → R is called continuous at a if for every ε > 0, there is a
δ > 0 such that whenever |x − a| < δ, then |f (x) − f (a)| < ε. A function
is called continuous if it it continuous at all inputs. Most functions in this book
are continuous. Find an example function defined in this chapter which is not
continuous according to this definition.
8.2. Prove the following basic facts using the definitions from Exercise 8.1.
1. Prove Theorem 8.9 that the map f 7→ f ′ is linear.
2. Using the definition of the limit of a function, prove that
( )( )
lim [f (x)g(x)] = lim f (x) lim g(x) ,
x→a x→a x→a
8.3. Prove that the numeric value for the slope of a line doesn’t depend on the choice of
points.
8.4. Prove that the limit of a sequence, if it exists, is unique. In other words, the limit L
does not change depending on the choices of ε and k used to satisfy the definition. This
justifies us calling it “the” limit of a sequence. Hint: Suppose you had an L and an L′ that
both worked, and prove that L = L′ .
8.6. Compute the Taylor series for f (x) = e−2x , and compare this to the procedure of
plugging in z = −2x into the Taylor series for ez . Find an explanation of why this works.
127
√
8.7. Compute the Taylor series for f (x) = 1 + x2 at x = 0. We will use this in
Chapter 12 to simplify a model for a physical system.
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. A
plot of f will help..
8.10. Find a differentiable function f : R → R with the property that limx→∞ f (x) = 0,
but limx→∞ f ′ (x) does not exist.
8.12. There are two definitions of the number e. One is the number used as an exponent
base in the exponential
( ) function e , for which the derivative of e is e . The other is
x x x
1 n
e = limn→∞ 1 + n . 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.13. 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.
128
8.14. 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.15. Write a program that implements the binary search root-finding algorithm and
compare 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.16. 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.17. Look up an exposition of the degree-2 Householder method for finding roots of
differentiable functions, and implement it in code.
8.18. In the chapter I mentioned that parts of calculus and real analysis are formalized
in such a way that maintains backwards compatibility with “legacy math.” The experi-
enced programmer might protest: why not redesign analysis from scratch to avoid that?
This has been done, and the field of nonstandard analysis is one such redesign. Look up
an introductory exposition about nonstandard analysis and identify where it becomes
backwards incompatible with standard calculus.
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
There are two topics I want to discuss in this chapter that don’t fit elsewhere in the
progression of the book. First, on how the organizational structure of a proof can guide
the reader’s attention. Second, on equivalence relations and quotients, the standard ab-
straction for building and representing complicated mathematical spaces. Both are new
ways to reduce reader’s cognitive burden by hiding technicalities. The latter will also
prepare us for the use of equivalence relations through the rest of the book.
In Chapter 8 we worked entirely with functions whose type signature was R → R.
Although we only intuitively 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 of some process. For example, a car manufacturer tunes how many of
each car model to manufacture based on their costs and sales figures. Another example
is an algorithm that fails with some measurable probability—or whose output quality
degrades—depending on a tunable parameter.
The recipe for doing this is taught in most undergraduate calculus courses. It reduces
the optimal parameter choice from a continuum of options to a discrete set to check by
hand. Define f : R → R whose input is the parameter of interest, and whose output
you’d like to minimize (maximizing is analogous).
129
130
• 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 ubiquitous Taylor polynomial. 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. It requires all your working memory. Opti-
mizing a parameter using standard tools is easy once you’ve done it enough times. Leav-
ing it to the end compartmentalizes the two jobs. Big picture comprehension first, and
rote computation last. Indeed, the ability to maximize an elementary function rarely de-
pends on memory of how you created that function, so why not shed a few mental stack
frames 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,y≤d 2|f ′ (y)|
as a tuple of length 3. Likewise, given a set A, we denote by An the tuples of length n for
some fixed n ∈ N. Quotients provide a way to make our “ignoring” rigorous.
Quotients are usually defined in terms of an equivalence relation,3 which generalizes
the concept of equality. Given a set A, an equivalence relation on A 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 reminds one of the equal sign without asserting that
it’s an equivalence relation before it’s proved to be.
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 essentially 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. For this
reason, an equivalence relation is also called a partition.
An equivalence relation allows us to do math in a world (on a set) in which an equiva-
lence relation is enforced as equality. This world is the quotient.
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 identify each equivalence class in R/∼ with our favorite “representative”
from that class.
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
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. One abbreviates 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. Noting that [0] = [1], we can summarize:
Curious plants spring from fertile soil. In[ this world] [1 + 1] = [0], and a sequence
which diverges in R converges here: xn = 2n+1 2 + 1
n .
R/∼ inherits operations from R, as if R/∼ were a wrapper class encapsulating R.
Define [x] + [y] to be [x + y] for any representatives x, y. We must prove this definition is
well-defined, i.e., that any chosen representatives result in the same operation. We need
to show that if x ∼ x′ and y ∼ y ′ , then x + y ∼ x′ + y ′ . Indeed, (x + y) − (x′ + y ′ )
is an integer because (x − x′ ) and (y − y ′ ) both are. Note you cannot say the same of
multiplication (find a counterexample!).
We can also think of R/∼ geometrically. Imagine standing at 0 on R and walking
in the positive direction, say, following a sequence xn = 0.001n. On R you increase
unboundedly. When we pass to the quotient, you cycle every thousand steps. This is an
animated way to see that R/∼ is geometrically a circle. In fact, we can design a nice
bijection that makes this formal. Call C = {(cos(θ), sin(θ)) : 0 ≤ θ < 2π}. Define
f : R/∼ → C by f ([t]) = (cos(2πt), sin(2πt)). Prove that f is well-defined (doesn’t
depend on the choice of which member of [t] you choose), and a bijection.
This example generalizes nicely. Given a surjective function f : X → Y , define ∼f so
that a ∼f b if and only if f (a) = f (b). Show that this is always an equivalence relation,
and notice that you get a new function f : X/∼f → Y defined by f ([x]) = f (x) that is
guaranteed to be a bijection when f is a surjection (and an injection otherwise). Describ-
ing an equivalence relation in terms of a function has an advantage: the structure of the
function f can be used to “move” properties between one space and the other. In the case
of R/∼ and the circle C, since f is differentiable,4 functions defined on the circle can be
converted to functions on R/∼ with most properties intact. This is how we can ultimately
say that R/∼ has the “same geometry” as C, though to do this in general—connect two
generic spaces in which one can make geometric statements—requires extensive ground-
work 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
4
We’ll see more about what it means for a function with multiple inputs and outputs to have a derivative in
Chapter 14, but in this case it just means each component of the output is differentiable as a single-variable
function of the input.
133
different things to be the same in a principled manner. You override equality, show it
meets standards of decency, and then introduce it to your friends.
We can now make the “ignoring” of nested pairs in the set product rigorous. Define
the sets
Now define an equivalence relation on L∪R so that (a, (b, c)) ∼ ((a′ , b′ ), c′ ) if and only
if a = a′ , b = b′ , c = b′ . 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 8 3000 is one plus a multiple of 9. Similar tricks with conve-
niently chosen moduli can extract useful information about 83000 without computing it
exactly, such as the last few digits of the number in base 10. Another useful tool when
studying equations of integer variables is to recognize that if an equation has a solution,
then the same solution must exist if the equation is considered mod n for any n.
Equivalence relations and quotients reduce the burden of ignoring irrelevant differ-
ences on a domain of study. You establish once that there’s an equivalence relation, and
you prove the important operations are well-defined on equivalence classes. Then you
can safely suppress the type difference between [x] and x forever. In fact, after defining a
quotient and proving its well-definition, mathematicians immediately drop the brackets.
You can also freely choose the most advantageous equivalence class representative for
your task, possibly easing computation. It’s similar to the programmer’s adage: work
hard now to allow yourself to be lazy later. Mathematicians are well practiced in that
philosophy.
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 are interesting kinds
of sets. 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 assembly. A compiler preserves the semantic behavior of a valid
input program in the target language when you run it. In that sense, it preserves the
structure of the input by representing that structure appropriately in the codomain.
Moreover, a computer program written in a compiled language like C is truly only
defined by the behavior of the compiler. This is never more visible than when dealing
with language forms that have “undefined behavior.” Different compilers run on the same
source produce programs that behave differently. Languages like C, in which behavior
can vary depending on the arbitrary contents of uninitialized memory, widen such pitfalls.
This isn’t how we want to work with programs. We want to consider programs in their
most natural environment, the semantics defined by a language’s documentation.
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 (Cf.
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.
135
136
Chapter 9). This allows us to identify and isolate structure in new settings, and mentally
disregard impertinent information.
The vector space, which encompasses mathematical objects with a linear structure, is a
foundational example. It’s the basic object of study in linear algebra. 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). Matrices 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.
Linear algebra is obscenely practical. The application we’ll see in this chapter, singular
value decomposition (SVD), is a staple of data science and machine learning. But linear
algebra seems to seep into all applied mathematics, most simply because linear approxi-
mations are easy to compute. When faced with a challenging phenomenon, the first step
is to try a linear model and see how well it does. Many more complex techniques also
end up “linearizing” a non-linear thing behind the scenes so as to apply linear algebraic
tools.
We devote a fair chunk of this chapter’s application to studying a specific linear model
for movie ratings and text documents, using SVD to cluster the latter. An additional goal
of this chapter is to prepare us for multivariable calculus and optimization. These subjects
use vectors, vector spaces, and linear maps as primitive types.
f (x + y) = f (x) + f (y)
Simple, yet something is missing. 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 isolate the important properties of addition, and the result
will be called a vector space. Any set can be a vector space, and we call the elements of a
vector space vectors. One defines a + operation and establishes that the isolated addition
properties hold.
Now we can define a vector space. The gist is that vectors can be any type, scalars
must be nicely-behaved numbers, and almost every arithmetic identity you expect to be
true is true, so long as you formally prove the axioms according to this definition. The
only missing thing is that vector spaces don’t have multiplication or division of vectors
2
We also need preservation of scalar multiples, but we are in inspiration mode. The formal definition is in
Section 10.2
137
by other vectors. Moreover, the concepts defined here, particularly the zero vector and
additive inverses, can be proven to be unique from the definition. You will do this in the
exercises, and it justifies the use of the notation post hoc.
Definition 10.1. A set V is called a vector space over R if it has two operations + and ·
with the following properties:
2. Every v ∈ V has an additive inverse, i.e., a vector w for which v + w = 0. This spe-
cial vector is denoted −v, and is used in conjunction with + to perform subtraction:
u − v = u + (−v).
a) v + w = w + v
b) (u + v) + w = u + (v + w)
c) 0 + u = u + 0 = u
d) u + (−u) = (−u) + u = 0
This is a monumental definition, and it’s not even the most general definition (see
the Chapter Notes for more). But it’s entirely contained in the implementation of the
operations + and ·. The miniature proofs that +, · have the needed properties constitute
a proof that the chosen implementation is a vector space. This proof is rarely a challenge.
In the examples that follow, I’ll skip detailed proofs, but if you want more practice, fill in
the details.
The simplest natural 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 both the scalar 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 is the zero vector. As an aside, the secret sharing application 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 option. As a specific example,
the space of all differentiable functions f : R → R is a vector space, and the derivative
operation f 7→ f ′ is a linear map from that space to the space of all functions.
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 tuple of numbers. 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 vector space axioms hold because they apply independently to each entry, and
each entry is just arithmetic in R.
With a few examples handy, let’s turn to the geometric side of Definition 10.1. A vector
space is designed to be the simplest way to define what addition means in a context
that is useful for geometry (defining an “algebra” for geometric objects). Let’s expand
this. The first thing a geometry needs is a space of points. In a vector space, the points
are the vectors themselves. 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).
139
(1, 2)
(-2, 1)
(1, -1)
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. 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.”
Second, a geometry needs lines. In a vector space, 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 through (1, 2).
You can further get a line not passing through the origin by taking some other vector
w and adding it to every point on the line, i.e. {w + c · v : c ∈ R}. This is the line through
the point w parallel to Lv , shown in Figure 10.4.
All this said, a plain vector space isn’t quite enough to get all of geometry. For example,
we can’t compute distances or angles without more structure in the vector space. We
will enhance the geometric picture by the end of the chapter, but for now we see there
are connections between vectors and geometry. We’ll keep this geometric foundation in
mind while dealing with linear maps more abstractly (which, to be frank, is the hard part
140
Figure 10.2: An example of vector addition. The dark dashed vector is the sum of the two
solid vectors, and the light dashed vector shows the geometric addition process.
v = (1, 2)
Lv = {c·v : c ∈ R }
{w + c·v : c ∈ R }
Figure 10.4: An example of a line described as all possible scalings of a given vector v,
then shifted away from the origin by a second vector w.
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)
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
In just a little bit more detail at the expense of a big ugly formula, if p = a0 + a1 x +
· · · + ak xk and q = b0 + b1 x + · · · + bm xm , then p + q is the polynomial formed by adding
the coefficients together. If we suppose that m ≥ k, 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 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, 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 unique zero vector in
every vector space, and every linear map preserves the zero, then using the same symbol
for both zero vectors is not so strange, even if the types of the two zero vectors may be
very different.
The proof of this fact “falls out” from the definition. To distinguish 0 the vector from
0 the scalar, I’ll make the vector bold, like 0.
Proof. 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 together,
f (0) = f (0 · 0) = 0 · f (0) = 0,
143
Subtracting f (0) from both sides gives 0 = f (0). Now it’s your turn: prove the facts
in Exercises 10.1-10.4 which establish basic properties of linear maps.
5 4
(1, 0) = (3, 4) + (−1, −5)
11 11
From the above, one can write (0, 1) as 14 ((3, 4) − 3 · (1, 0)). Once (1, 0) and (0, 1) are
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
6
An infinite size basis is possible. We will remark on them mostly as commentary for your enticement and
further investigation.
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.
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
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 ).
This process of expressing a vector’s coordinates with respect to a different basis is
analogous to the process of writing integers in a different number base, such as binary
or hexadecimal. You choose a base that’s useful to you. And just like with numbers, if
you find a basis with useful properties, you study it in depth and learn its computational
secrets.
The brief and formal way to say a vector v “can be written using sums and scales of
other vectors” is the following definition.
∑
n
x = a1 v1 + · · · + an vn = ai vi
i=1
145
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.
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 .
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 could have said the set spans V . That would have been incomplete, and
now we’re ready for the formal definition.
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.
It doesn’t make sense as a coordinate system either, because points don’t have unique
representations.
We will have a lot more to say about bases. Many insights and applications of linear
algebra revolve around computing a clever basis. 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.
146
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.
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 v1 + · · · + an vn ⇒ 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 linearly dependent (i.e., not lin-
early independent) 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 coefficient of x, the newly added vector. More-
over, a0 ̸= 0 since, if it were, that would provide a nontrivial linear combination equal
to 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:
147
1
x=− (a1 v1 + · · · + an vn )
a0
This proves that x ∈ span(B). Because 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
At first the concept of a basis seems tame. But 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 (The Steinitz exchange lemma). 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 . Using the 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 |.
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!
148
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
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 = U − {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 provides well-definition for the notion of the dimension of a vector space.
Dimension does not depend on which basis you choose. This reinforces our intuitive un-
derstanding of what dimension should be for Rn , i.e., how many coordinates are needed
to uniquely specify a point. R is one-dimensional, the plane R2 is two-dimensional, phys-
ical 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.
10.5 Matrices
Now we can finally get to the heart of linear algebra.
Linear maps seem relatively complicated at first glance, but 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 to be arbitrary. 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
If we know what f does to the basis vectors, the above formula tells us how f behaves
on x. In other words, a linear map is completely determined by how it acts on a basis.
This is such an important revelation that I want to shout it from the mountaintops! Chisel
it on the forearm of the Statue of Liberty! Put a fuchsia HTML marquee on the front page
of Google!
This implies the data representation of any linear map f : V → W can be reduced to
a fixed number dim(V ) of vectors in W : the output of f for each input basis vector.
Now let’s say we know that f (v1 ) = y1 , f (v2 ) = y2 , etc., the vectors yi now being in
W . We can do the same decomposition of each yi in terms of the chosen basis for W .
to consider the relationship between matrices, linear maps, and the basis you’ve chosen
more abstractly. It does this by defining a new algebra for manipulating linear maps.
Both the visual representation and the algebra merge seamlessly with the functional
description of linear maps. As we’ll see, composition of functions corresponds to matrix
multiplication. Natural operations on linear maps correspond to operations on the cor-
responding matrices, and conversely operations on matrices correspond to new, useful
operations on functions. We will explore this in even more detail in Chapter 12.
So here’s the abstraction that works for any linear map f : V → W . Again, we fix
a basis {vi } for V and {wj } for W . Write the numbers from β describing the linear
map f : V → W in a table according to the following rule. The columns of the table
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
a 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 (v )
1 ··· f (v ) n
wm | |
152
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
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
Visually it has always helped me to imagine picking up the first row and rotating it
90 degrees clockwise; that motion lines up the β entry with the α entry that it should
11
As we’ll see later in this chapter, this “entrywise product with sum” is called the inner product.
153
be multiplied by. Then the sum gives you the first entry γ1 , and you continue down the
rows of M . Here’s an example with a 2 × 3 matrix.
( ) 3 ( )
9 2 1 a
−1 =
7 −2 0 b
4
The first step:
( ) 3 9 3
9 2 1
−1 −−−→ 2 −1
4 1 4
−−−→ a = 9 · 3 + 2 · (−1) + 1 · 4 = 29
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
bases). 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
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.
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.
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 }
14
The map M provides an isomorphism of algebras, but rather than introduce this term now, we will discuss it
at length in Section 10.7, and again in later chapters.
156
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 e1 + · · · + αn en in. Write down a system of n
equations with n unknowns, as follows. First express each of the vectors in B in terms
of E. I’m going to use the notation (e.g.) v2,4 to denote the 4th coefficient of v2 as it’s
written in the basis E. Finally, write down an equation for each ei , which asserts that the
coefficient αi of x in E is the same as the sum of the ei coefficients of the (hypothetical)
representation of x in B. 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
This was a mouthful, but refer back to the two-dimensional example above and identify
how that generalizes to this system of equations. Next, we can rewrite the system of
equations as a single matrix equation.
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 can be phrased as com-
puting 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 express x in
the basis (v1 , . . . , vn ), we 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, changing a basis must be a bijection. More usefully,
and now that we have a matrix multiplication operation, the inverse of a matrix A is
157
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
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 right to left: 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.
Generally, forming the matrix 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. Taking a cue from Chapter 9, matrix similarity is
an equivalence relation, and the equivalence classes correspond to linear maps.
To 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 uses the
tabular format of the matrix equation to help organize. Gaussian elimination is important,
but it’s both inefficient15 and it computes a lot of extra information.
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-
15
It’s polynomial-time in n = dim(V ), but in the worst case its runtime is more than n3 . Here’s a more
complete story: http://cstheory.stackexchange.com/questions/3921
158
ers to industry-standard techniques for changing bases, and Chapter 12 for an extended
parable on the value of a good basis.
Proposition 10.16. Let Pm be the vector space 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 )
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∑ (injection); if
(b0 , b1 , . . . , bm ) is a vector in Rm+1 , then it is the image of p(t) = m
k=0 k t under f .
b k
16
In particular, without using the so-called Axiom of Choice, a somewhat unintuitive postulate, one cannot
even conclude that all infinite dimensional vector spaces have bases! This fact led to an amusing—if some-
what 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
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 from Chapter 2 relies only on the linear structure
of polynomials. Noticing this 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.
Definition 10.18. Let∑ v, w be vectors in ∑Rn , and let {e1 , . . . , en } be the standard basis
for Rn , so that v = i=1 αi ei and w = ni=1 βi ei . The standard inner product (or dot
n
∑
n
⟨v, w⟩ = α1 β1 + · · · + αn βn = αi βi .
i=1
160
∥v – w∥
∥v∥
θ
∥w∥
Figure 10.7: The lengths of the sides of the triangle satisfy the law of cosines.
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.
We’ll also need two facts in the proof, whose proofs follow from the formula for the
inner product and simple arithmetic. We will see in Chapter 12 how these properties
become a definition.
Proposition 10.19. The inner product is symmetric, i.e., ⟨v, w⟩ = ⟨w, v⟩, and 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.
Theorem 10.20. The inner product ⟨v, w⟩ is equal to ∥v∥∥w∥ cos(θ), where 0 <= θ <= π
is the angle between the two vectors.17
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 symmetry and linearity.
⟨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
Theorem 10.21. Two nonzero vectors v, w ∈ Rn are perpendicular if and only if ⟨v, w⟩ =
0.
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.
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(θ),
162
meaning one of ∥v∥, ∥w∥, or cos(θ) must be zero. Perpendicularity is not defined if one
of the two vectors is zero,18 so both vectors must be nonzero and have a nonzero norm.
This leaves cos(θ) = 0. The vectors are perpendicular.
Proof. 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
A similar proof shows that if x is a vector perpendicular to the plane spanned by two
vectors y, z, then the set {x, y, z} is a linearly independent set. In general, given a set of
linearly independent vectors, adding a vector that’s perpendicular to their span increases
the dimension of the spanned subspace by one.
Next we define the projection of one vector onto another.
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.20 In this
case the formula defined above for the projection is just ⟨v, w⟩v. Now (trivially) write
w – projv(w)
projv(w)
I need to prove to you that the two terms projv (w) and w − projv (w) are geometrically
perpendicular. Indeed, I need to show you that
span{v1 , . . . , vk }. If the basis vectors vi are pairwise perpendicular, then you can also
define the projection of a vector w onto a subspace as the sum of projections onto each
vector in the subspace basis:
∑
k
projV (w) = projvi (w).
i=1
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
166
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 and
unit vectors. 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.22 I.e., A(p1 ) = c1 q1 for some constant c1 , likewise for p2 , p3 , etc.
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
depends on completely superficial aspects of the movie, such as how much it was adver-
tised or whether it’s a sequel. Nevertheless, much of the usefulness of the SVD abstraction
relies on not being domain-specific. The more a model encodes about movie-specific fea-
tures, 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.
22
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.”
168
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.23
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.
You can also build these best-fitting subspaces recursively. The 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 fine detail, with sidebars to explain
some notational choices.
23
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.
169
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 equivalent to the pseudo-Python snippet:
The analogous expression which evaluates to the input vector v (instead of the value of
the expression 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
minimizers 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 (wi )∥2 +
∥wi − projv (wi )∥2 = ∥wi ∥2 , rearranging to replace each term in the sum:
∑
m
( )
arg min ∥wi ∥2 − ∥projv (wi )∥2
v
i=1
170
Next, notice that the ∥wi ∥2 don’t depend on the input v, meaning we can’t optimize
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 use the inner product formula for the projection instead
of the squared-norm. We’ve reduced the best fitting line optimization to finding a unit
vector v which maximizes
∑
m
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.
Theorem 10.25 (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
171
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)2 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, considering only those vectors
perpendicular to 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,
Proof. Recall we’re trying to prove that the first k singular vectors span the k-dimensional
subspace of best fit for the vectors that are the rows of A. That is, they span a linear
subspace Y which maximizes the sum-of-squares of the projections of the data onto Y .
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 Y 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 Y ).
172
The right hand side of this inequality is maximal by assumption, so they must actually
be equal and both be maximizers.
For the general case of k, the inductive hypothesis tells us that the first k terms of the
objective for k + 1 singular vectors is maximized, and we just have to pick any vector
yk+1 that is perpendicular to all v1 , v2 , . . . , vk , and the rest of the proof is just like the
2-dimensional case. We encourage the skeptical reader to fill in the details.
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 sigma 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
173
And the domain basis: a matrix V whose columns are the vi , or equivalently V T whose
rows are the vi .24 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
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.
The One-dimensional Problem
Now that we’ve seen that the SVD can be computed by greedily solving a one-
dimensional optimization problem, we can turn our attention to solving it. We’ll use
what’s called the power method for computing the top eigenvector. The next chapter will
be all about eigenvectors, but we don’t need to know anything about eigenvectors to see
this algorithm. In lieu of knowledge about eigenvectors, the algorithm will just appear
to use a clever trick.
The idea is to take A, the original input data matrix, and instead work with AT A.
Why is this helpful? Using our decomposition from the previous section, we can write
A = U ΣV T , where U, V are change of basis matrices (whose columns are perpendicular
unit vectors!) and V actually contains as its columns the vectors we want to compute.
So we can do a little bit of matrix algebra to get
T
AT A = (U ΣV T ) (U ΣV T ) = V ΣT 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-
24
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 .
174
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.26 (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
xk = B k x = ci σi2k vi
i=1
Notice that, since σ1 is larger than σ2 (and hence all other singular values), the coeffi-
cient for σ1 grows faster than the others. Normalizing xk causes the coefficient of σ1 to
tend to 1 while the other coefficients 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 coefficient growth is the ratio between the two
biggest singular 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.25 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.
25
They, along with most applied linear algebraists, view vectors as matrices with one column.
175
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.
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.26
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
26
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.
176
∑kvi . To exclude all the vectors in the span of {v1 , . . . , vk }, you replace A with
vectors
A − i=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.27 The parameter k stores the number of singular
values to compute before stopping.
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.
27
See pimbook.org
177
Then we run SVD on A to get a low-dimensional subspace of the vector space of words,
in our case, a 10-dimensional subspace. 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
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.28 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
28
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
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.
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,29 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.
29
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
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.
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 if a linear map f is a bijection, then the inverse f −1 is also a linear map.
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 ?
180
10.5. Prove that the image of a linear map f : V → W is a subspace of the codomain,
W . Prove that the subset {v ∈ V : f (v) = 0} is a subspace of V .
10.6. In R2 we have colorful names for special classes of linear maps that correspond to
geometric transformations. Look up definitions and pictures to understand matrices that
perform rotation, shearing, and reflection through a line.
10.7. 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.
5. The direct sum of two subspaces of a vector space.
10.8. 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.12. Again in Chapter 2, return to exercise 2.9 on Newton interpolation. Find a source
that explains how Lagrange and Newton interpolation correspond to solving matrix in-
version problems using different bases for a vector space of polynomials.
10.13. 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 Bézier curves. Like
Taylor polynomials, Bernstein polynomials can be used to approximate functions R → R
181
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.14. 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.17. Continuing the previous exercise, the classical algorithm for solving linear pro-
grams is called the simplex method. It was invented in the 1940’s by George Dantzig30 . At
its core, the algorithm builds up a vector space basis corresponding to the variables in the
solution that have nonzero values. Then it iteratively uses the objective (and Gaussian-
elimination-style elementary row operations) to guide how to improve the solution. Re-
search this algorithm and implement it in its basic form.
10.18. 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.
Find, or discover yourself, the aforementioned proof that all n-dimensional inner product
spaces are isometric.
10.19. 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.12 for an introduction to weighted graphs.
30
Similar independent previous work by Leonid Kantorovich and Tjalling Koopmans, both of whom shared
the 1975 Nobel prize in economics for their work. Dantzig was not included.
182
10.20. 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.21. 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.7) 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.
10.22. Research the details of the winning submission for the Netflix Prize competition.
Identify what other ways a linear model is incorporated into the solution.
• Addition and multiplication have identity elements which are distinct. Call them
zero and one, respectively.
• Addition and multiplication both have inverses, and every element is invertible,
with the exception that zero has no multiplicative inverse.
The field is the triple (K, +, ·), or just K if the operations are clear from context.
a field, but there are many others. For example, the set of fractions of integers (rational
numbers) forms a field denoted Q with the normal addition and multiplication. Another
example is the binary field {0, 1} with the logical AND and OR operations.
Now a vector space can be defined so that its scalars come from some field K in the
same way we used scalars from R. We say that V is a vector space over K to mean that the
scalars come from K. As long as the operations in K have the properties outlined above,
you can do all the same linear algebra we’ve done in this chapter. To be particularly clear,
a linear combination of vectors in V requires coefficients coming from K, and so they’re
called K-linear combinations. Also note that K-linear combinations must be finite sums.
Linear algebra can have more nuance for some special fields, but to understand when
and how they are different you need to study a bit of field theory. If you’re interested,
look up the notion of field characteristic and in particular what happens when fields have
characteristic 2.
To leave you with one example of an interesting vector space over a field that’s not
R, consider V = R as a vector space over K = Q. This might not seem interesting at
first until you ask what a basis might be. Take the set C = {1, 2, 3, 4, 5}, for example. Is
it possible to write π (an element of V ) as a Q-linear combination of the vectors in C?
You could only do so if π itself was rational, which it’s not. So how, then, might one find
a basis so that π (and every other irrational number) can be written as a finite Q-linear
combination of the elements in the basis? A curious thought indeed.
Bias in Word Embeddings
The process of turning English language words into vectors in such a way that arithmetic
on vectors corresponds to semantic transformations of words (“king” — “man” + “woman”
= “queen”) is called semantic word embedding. This approach has roots in linguistics and
information retrieval, and was popularized in computer science in the early 2000’s by
Yoshua Bengio and others. In 2013, Google released an open source tool called “word2vec”
that constructs embeddings using neural networks, and there are many other tools (such
as GloVe) that have become popular since then.
Semantic word embeddings are an interesting case study into the shortcomings of lin-
ear models. In a 2016 paper, “Man is to Computer Programmer as Woman is to Home-
maker?” a team of researchers at Microsoft Research studied how human bias expressed
itself through word embeddings. Here a corpus of documents is used to train a linear
model, in which pairs of words like “woman” and “receptionist” show up more often
than, say, “woman” and “architect.” These associations (intended or not) will manifest
themselves in the resulting embedding. As a consequence, any system based on these
word embeddings is likely to associate women with receptionists more than architects.
This outcome is not surprising, considering the adage, “a word is characterized by the
company it keeps.”
Whether one is willing to accept this outcome depends on the goal of the application,
but awareness is crucial. Mathematical assumptions baked into algorithms and models—
even simple ones like linearity—can dupe the unwitting. Take care when applying them
to situations that involve people’s lives or livelihoods.
Chapter 11
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 taught 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, called the Fourier basis.1 The Fourier transform has a whole host
of properties that make it useful for science, but in brief, the input functions are often
thought of as “signals,” such as composite sound waves, and the output is thought of
as constituent tonal frequencies. For example, automated phone systems (“For English,
press one…”) recognize the buttons you press using Fourier analysis. Each button corre-
sponds to two overlapping pure frequencies, and the receiving end applies the Fourier
transform to identify the frequencies, and hence which number was pressed. The Fast
Fourier Transform, or FFT for short, is a particularly efficient algorithm for writing (fi-
nite approximations of) signals in the Fourier 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.
FFT is a cornerstone of electrical engineering, but the technique is much deeper than
simply interpreting electrical signals. For example, FFT can be used to multiply large
integers much faster than the usual algorithm. Lev was well familiar with the FFT and its
advanced applications. 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
1
This is nontrivial because the vector spaces involved are infinite dimensional.
185
186
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.
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 and new paradigms initially
hide the core idea of a program. This can be complex type declarations, or the orthodoxy
of a particular pattern (promises, streams, coroutines, etc.), which are foundationally im-
portant, but unrelated 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
long revisiting programming basics when learning a fourth language, 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 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.
Terry Tao summarizes it well in his essay,3 “There’s more to mathematics than rigour
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.
3
https://terrytao.wordpress.com/career-advice/
187
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 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. Chapter 10, Exercise 10.19). In number theory, vector spaces drive the idea of tran-
scendental numbers, those numbers like e and π which can’t be represented as the root
of a polynomial with rational coefficients. Since R is a vector space over Q, one studies
a transcendence basis of this vector space (cf. Chapter 2, Exercise 2.5). In fields like alge-
braic 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
188
techniques from linear algebra can be applied. The form and function of the applications
shapes one’s understanding of the basic 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 a higher rung on the ladder.
One might expect a basis-free theory could completely eliminate messy 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 colleagues 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 master algebra.
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,
189
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
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-
190
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 she intimately understood
what she was doing and why it worked. She shouted, “The proof is trivial!”
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 language: 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 web of concepts around 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. And just like humans under-
stand 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, or the derivative, with a new appreciation for its purpose
and use. Mathematical intuition in particular is the graduation from purely analytical
and mechanical analysis to a 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 solutions. 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
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
prespecified family of transformations. In this case, eigenvalues are invariant under the
operation of changing a basis. Invariance is a natural property to require for something
which purports to reveal the divine secrets of a linear map.
This is also related to our earlier discussion in Chapter 8 of the well-definition of
the limit. We’re saying that the eigenvalues of a linear map don’t depend on the ar-
bitrary choices you make to represent them in the nice computational setting of ma-
trix algebra. However, this time it’s a bit different because we didn’t intentionally bake
basis-invariance into the definition. If you stumbled across a matrix-vector equation like
Av = 2v in the wild, perhaps while modeling some physical system, it might not occur to
you that the number 2 is a special property of the system. In other words, this invariance
feels discovered. On the other hand, the definition of a limit had an explicit invariance
baked in.
Invariance is a “smell.” Invariant properties point toward the soul of mathematics.
We’ll have more to say on this when we study hyperbolic geometry in Chapter 16.
An eigenvector v of A has another sort of “invariance” under the operation of left-
multiplication by A. That is, if you ignore scaling—or rescale v to a unit vector before
and after left multiplying by A—then A sends v to itself. This is why we say that the
eigenvectors span the “best axes” in which to view A, because A sends any vector on
the axis to another vector within the same line. They exhibit maximal invariance when
the linear map is applied to them. And for the limited scope of this chapter, the set of all
eigenvalues and eigenvectors of a linear map allows one to represent the entire map in
terms of these invariant, independent pieces.
This is the best high-level intuition I can give without getting too deep in the math.
Before we do, let’s see a compelling example of why eigenvalues are so interesting and
complex for specific matrices called adjacency matrices. In the next section we won’t
prove any of the theorems we state.
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
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}.
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
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 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 adjacency matrices have n eigenvalues, though 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 simple statistics in 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,
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
3
More specifically, it’s called an Erdős-Rényi random graph, and the output is a draw from the uniform
195
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 t, 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 Kt for some t. 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 for this method to work, the planted
clique must have size at least t ∼ n log n. The following algorithm succeeds for a much
smaller t:4
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 at least 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.5 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
linear maps V → V .”
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
not have any eigenvalues! For example, the 2-dimensional linear map that rotates a vector
by π/4 radians clockwise. In fact, the existence of eigenvalues and eigenvectors is similar
in nature to the existence of roots of single-variable polynomials. We will reveal the first
step toward making this connection concrete in Exercise 12.11. As a consequence, some
linear maps only have eigenvalues that are complex numbers, and the corresponding
eigenvectors have complex entries. Each complex eigenvalue a linear map has reduces
the number of real eigenvalues it can have.
Introducing complex numbers makes other things simpler, while making some things
more complicated. But 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.
A second reason is that multiple linearly independent eigenvectors can exist for the
same eigenvalue, and there may or may not be “enough” eigenvectors to provide a com-
plete picture. This topic is nuanced—and not needed for our application—so we omit it
except to mention some pointers in Section 12.5.
Luckily, there is a nice way to avoid dealing with these problems 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 (not
necessarily distinct) 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
197
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, using
Exercise 12.1 that (AB)T = B T AT ,
Theorem 12.7. Let A be a real-valued n × n matrix, and let ⟨−, −⟩ denote6 the standard
inner 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
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 maximizes7 the norm ∥Ax∥, and let c = ∥Ax∥. Then
Ax = cy for some unit vector y. If y is in the span of x (which happens most of the
time), then we are done, because Ax = cy = (cd)x for some d ∈ R, which makes
6
The notation ⟨−, −⟩ is used to signify that the function will be expressed in this nonstandard “pairing”
notation. If the inputs are v, w ∈ V , the interpretation is to substitute the dashes with the inputs in order,
i.e. ⟨v, w⟩.
7
Why must such a vector exist? This is not trivial, but is true due to a generalization of the Extreme Value
Theorem to Rn . It is a standard result which usually involves a little bit of topology (compact sets and
continuous functions), and is hence beyond the scope of this book.
198
cd an eigenvalue. Otherwise, we may assume going forward that x and y are linearly
independent. By the maximality of x we know that ∥Ay∥ ≤ c.
We will 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
to (or elucidate structure in) the underlying space. The standard inner product on Rn is
defined by the formula
∑
n
⟨x, y⟩ = xi yi .
i=1
This formula is intimately connected with geometry. It can be used to compute the
angle between two nonzero 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 length of
the other).
The Power of a Generalized Inner Product
Over the years mathematicians have extracted the generic properties of this formula that
conjure up its geometric magic. The result is a distilled definition of an inner product.
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 ⟨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.
variable f : R → R whose square has a finite integral.8 Call this space L2 (R), or just L2
for short (the exponent reminds us we’re squaring):
{ ∫ }
∞
2
L (R) = f : R → R
2
f (x) dx is finite
−∞
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) =
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 is9
∫ ∞
⟨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 actually
these functions are not even in L2 . 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:10
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.
Properties of an Inner Product
Definition 12.9 implies some easy consequences. Here are two examples.
Proposition 12.10. Let 0 be the zero vector of V , and 0 the real number zero. Then ⟨v, w⟩ =
0 for every w ∈ V , if and only if v = 0.
Proof. For the forward direction, if ⟨v, w⟩ = 0 for every w, then fix w = v. The defining
properties of an inner product require v = 0. For the reverse direction, fix any w and note
that f (v) = ⟨v, w⟩ is a linear map. Linear maps preserve the zero vector, so f (0) = 0.
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.
Since this is an inner product, we can pull out the scalar multiples on the far left and
right-hand sides to get λ⟨v, w⟩ = µ⟨v, w⟩. The only way for this equation to be true in
spite of λ ̸= µ is if ⟨v, w⟩ = 0.
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.
202
Definition 12.12. Two vectors u, v ∈ V in an inner product space are called orthogonal
if ⟨u, v⟩ = 0.
Another way to say Proposition 12.11 is that if two eigenvectors are not orthogonal,
then they must have the same corresponding eigenvalue (this is the contrapositive state-
ment11 ).
√
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.
Proposition 12.14. Any set of nonzero vectors {v1 , . . . , vk } which is pairwise orthogonal
(for each i ̸= j, ⟨vi , vj ⟩ = 0) is linearly independent.
Proof. Let {v1 , . . . , vk } be as in the statement of the proposition, and suppose c1 v1 +
· · · + ck vk = 0. To show linear independence, recall, we need to show that all the ci = 0.
Fix any i. To show ci is zero, inspect ⟨c1 v1 + · · · + ck vk , vi ⟩, which∑
is zero because the
first argument is zero by assumption. By linearity, this splits up as kj=1 cj ⟨vj , vi ⟩. By
pairwise orthogonality, all the terms in the sum are zero except ci ⟨vi , vi ⟩. Thus, this sum
reduces to ci ⟨vi , vi ⟩ = 0. Then either vi = 0 (ruled out by assumption) or ci = 0. The
same argument applies to every ci .
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 .
And so the inner product gives us exactly the coefficient we wanted.
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. The singular values played this role in Chapter 10. For our physics applica-
tion 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.12
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 , with the vi written in
terms of some other basis {e1 , . . . , en }. Let B be the corresponding 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.
12
Integration is not always computationally easy, but you choose the orthonormal basis so that it is.
204
One may wonder if it’s also necessary to show BB T = 1n in order to conclude that
B T 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.13 from Chapter 4, a one-sided inverse of a bijection is
automatically a two-sided inverse. 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.
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.
If we wanted to prove this without set theory hijinks, we could have done so by proving
−1
(AT ) = (A−1 ) . You will do this in the exercises.
T
Our next task is to compute orthonormal bases. For finite dimensional inner product
spaces there’s an algorithmic method called the Gram-Schmidt process. It falls short of an
algorithm by not defining how to do one important step. First, a definition:
1. Let S0 = {} be the empty set. Si will contain the partial 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 .
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. Instead, it serves as a proof of existence.
As a quick exercise, prove that the kernel of a linear map is a subspace of V . Rephrasing
the above, the eigenvectors of f corresponding to the eigenvalue λ = 0, along with the
zero vectors, are exactly the kernel of f .
206
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 ker-
nels. For the next proposition, I denotes the identity map I(x) = x, with corresponding
matrix In for n-dimensions.
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 is a second kind of multiplicity, related to the geometric multiplicity, which al-
lows one to build a complete characterization of a linear map. We will leave this for the
Chapter Notes while we plow on to the main theorem and application.
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 eigen-
vectors. This is the centerpiece theorem of this chapter and the secret ingredient in the
physics application to follow.
Theorem 12.22 (The Spectral Theorem). A real-valued matrix A is symmetric if and only
if it has eigenvectors that form an orthonormal basis.
This theorem requires some nontrivial amount of work, pieces of which we have al-
ready proved in this chapter. The easy part is the reverse direction. It uses the fact that
(AB)T = B T AT , and Proposition 12.16 that for an orthonormal change of basis matrix
U , U −1 = U T .
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 a way to “peel off” one dimension from the matrix A
in a way that’s independent of the rest of the argument. Given A, we’ll find an eigenvector
v with corresponding eigenvalue λ, normalize it, and use it as the first vector in the basis.
Then we’ll decompose Rn into two subspaces, a one-dimensional space spanned by 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
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.
Construct the rest of this basis as follows. Let W be the subspace of Rn consisting of
all vectors orthogonal to v.13 Use Gram-Schmidt to choose an orthonormal basis B ′ =
{w2 , . . . , wn } of W . Joining together, B = B ′ ∪ {v} is an orthonormal basis of all of Rn .
Note that only v need be an eigenvector; the other vectors in the basis are not necessarily
eigenvectors of A, but the whole basis is orthonormal.
Because B is orthonormal, the same argument as Proposition 12.23 implies that A,
when written with respect to the basis B, is symmetric. So when we write A with respect
to B, the matrix decomposes into blocks (we prove this below). In what follows, I am
abusing notation by using B for both the basis (a set) and the relevant change of basis
matrix (which implies the basis vectors are in a certain order).
( )
λ 0
change of basis by B
A −−−−−−−−−−−→ = B T AB
0 A′
Figure 12.2: A system in which five beads are equidistantly spaced on a taut string.
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 creates a wave that propagates through
the string from end to end.
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
position of the object, in e1 , e2 units, respectively, relative to (0, 0). The obvious choices
15
pimbook.org
211
of coordinates are the standard basis vectors (1, 0) and (0, 1) representing horizontal and
vertical, as aligned with the picture.
Model 12.24. 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 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.
As part of the mathematical model, forces “act” on objects. By that I mean they are ap-
plied to objects and influence their motion. If you pluck a string, it moves. The following
revolutionary observation allows us to describe exactly how forces that act on an object
influence their motion.
Model 12.26 (Newton’s n-th law for some n). If F1 , . . . , Fn are all of the 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
212
Figure 12.3: A simpler system that has only one bead, displaced from its equilibrium and
released.
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
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, 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.27 (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 ≥ 0 is a con-
213
F1+F2
F2 F1
Figure 12.4: The forces pull in opposite directions toward the wall, and together sum to
a vertical force.
stant 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
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. Initially at time zero x1 (t) = 0 and x2 (t) < 0. 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
214
F2 F1
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.
215
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
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
216
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.16
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
b3
b2 b5
b1
b4
b3
b2
b1 ϑ2
ϑ1 y3
y2
y1
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 vertical 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
219
−T sin(θ1 ) + T sin(θ2 ), with the sign flip because the left side pulls the bead down.18
Now we’ll use two Taylor series approximations:
θ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
And rearranging gives
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
y1′′ = −2y1 + y2
y2′′ = y1 − 2y2 + y3
y3′′ = y2 − 2y3 + y4
y4′′ = y3 − 2y4 + y5
y5′′ = y4 − 2y5
Rewrite this as a linear map y ′′ = Ay with
−2 1 0 0 0
1 −2 1 0 0
A= 0 1 −2 1 0
0 0 1 −2 1
0 0 0 1 −2
At last, we turn to eigenvalues. This matrix is symmetric and real valued, and so by
Theorem 12.22 it has an orthonormal basis of eigenvectors which A is diagonal with
18
When b1 is above b2 , the angle is negative and that reverses the sign: sin(−θ) = − sin(θ). So the orientations
work out nicely.
220
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.
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
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
221
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).
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.20
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
terms of the vi . It uses the simple formula from Proposition 12.15.
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.
20
This is suspiciously similar to the singular value decomposition in Chapter 10, though there we focused on
the geometric perspective.
223
>>> 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):
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 eigenvector21
are called the resonant frequencies of the 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
21
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.
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.2. Let V be an n-dimensional inner product space, whose norm ∥x∥2 = ⟨x, x⟩ is
given by the inner product. Prove the following.
1. The only vector with norm zero is the zero vector.
2. The distance function d(x, y) = ∥x − y∥ is nonnegative and symmetric.
3. The distance function satisfies the triangle inequality. That is, d(x, y) + d(y, z) ≤
d(x, z) for all x, y, z ∈ V .
12.3. 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.4. 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.5. Prove the following basic facts about eigenvalues, eigenvectors, and inner prod-
ucts.
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/∥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
orthonormal 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 orthonormal 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.6. Look up a proof of Theorem 12.28, on the uniqueness of the sine function, that uses
Taylor series. The analytical tool required to understand the standard proof is the concept
of absolute convergence. The central difficulty is that if you’re defining a function by an
infinite series, you have to make sure that series converges with the properties needed
to make it a valid Taylor series. Repeat the proof for sin(ax).
227
12.7. 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 this vector space in terms of V ? What is a natural description of the basis
of Rn 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.8. 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.9. 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.10. 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.12. 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.15 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
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. Because the roots of a polynomial might be
228
complex numbers, this implies the eigenvalues of a matrix (when viewed as a linear map
on a vector space of complex numbers) 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.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. Use this to understand why we com-
puted AT A in the SVD algorithm from Chapter 10.
12.16. Generate a “random” symmetric 2000 × 2000 matrix via the following scheme:
pick a distribution (say, 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 choice of distribution?
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
beads, with fixed walls on each boundary side. Pluck the beads perpendicularly to the
lattice. Can you design a symmetric 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?
229
12.21. PageRank is a ranking algorithm that was a major factor in the Google search
engine’s domination of the early 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?
Definition 12.29. 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 ).
From this definition, we can see that the algebraic multiplicities of λ = 1 are different
for A and B in Section 12.5. 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.
Algebraic and geometric multiplicity work together to give a characterization of any
linear map, considered over the complex numbers, 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 (with com-
plex scalars) there is a basis for V , for which the matrix of that linear map consists en-
tirely 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
231
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.
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.30. Let f : V → V 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.
Chapter 13
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 architecture
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, my
compilers professor observed that software architecture was much more impressive than
building 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.
233
234
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.
A single improperly set permission bit can cause troves of private data to become pub-
lic. Financial insecurity is almost synonymous with digital currencies, one particularly
relevant example 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 withdraw the equivalent of 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 little 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 journalism).
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.
235
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
some perspective.
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.
236
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. If you believe Michael Atiyah that the proof is the very last step of mathematical
inquiry, 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 slack 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
237
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
239
240
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.
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 − c∥ of real numbers converges to zero.
Despite sequence convergence generalizing, the obvious first attempt to adapt the
derivative violates well-definition. We might try the same formula as Definition 14.1,
interpreting x and c as vectors, and using the norm in the denominator. Unfortunately,
the “value” of this derivative depends on the sequence chosen.
An easy example demonstrates. 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 coordi-
nate 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 like limn→∞ −1+1(1/n)
= 0, while
−1−(2/n)−(1/n2 )+1
the latter would be limn→∞ (1/n)
= −2. This is illustrated in Figure 14.1.
We are right to be suspicious. With multiple variables, the underlying idea of “steep-
ness” now inherently depends on direction. This is something one intuitively understands
241
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: parameterize by the direction of approach.
f (c + tv) − f (c)
Dir(f, c, v) = lim
t→0 t
If this limit exists, we say f is differentiable at c in the direction of v.
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, tn ̸= 0 which converges to zero.
In Chapter 8 we started with the derivative and developed an optimal linear approx-
imation that was easy to compute. That was extremely useful. Now we ask, how can
we compute a similar linear approximation of a multivariable function? The directional
derivative alone falls short. The corkscrew surface shown in Figure 14.2 illustrates the
problem.
On this surface at (0, 0), the directional derivative exists in every direction, but jumps
sharply as the direction rotates past the negative x1 axis. In the technical parlance we left
to Exercises 8.1 and 14.1 to define, the directional derivative isn’t continuous with respect
to direction. Informally, 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 destroys the possibility that a
derivative based on the directional derivative can serve 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. It will provide
a linear map representing the whole function, and applying linear algebra produces the
directional derivative in any direction. Being linear algebra, we may choose a beneficial
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.
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.
the graph of f near c than any other line. We proved this in detail in Theorem 8.11.
This approximator is more than just a line. It’s a linear map, and now that we
have the language of linear algebra we can discuss it. Define by Lf,c the linear map
Lf,c (z) = f ′ (c)z. As input, this linear map takes a (one-dimensional) vector z repre-
senting a deviation 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)]. More-
over, 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 .
f f
Figure 14.3: Left: a linear approximation without shifting f . Right: shifted so that
(c, f (c)) is at the origin.
The linear approximator has the following 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 Lc (x),
f (x) − Lc (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.
f (x) − Lc (x)
lim =0
x→c ∥x − c∥
If such an A exists, we call Lc 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.
bad such an approximation might be). This rules out the confounding corkscrew example.
The jump in the directional derivative violates Definition 14.5.
If the definition is satisfied, then near c the function f can be approximated by a linear
map A. The term A(x−c) makes the linear map apply to deviations from c. Equivalently,
the shift by −c translates f to the origin to apply A, and the addition of f (c) translates
back to (c, f (c)). L and A are related by the conjugation of these two translations.
Geometrically in two dimensions, the linear approximation defines a plane touching
the graph of the surface z = f (x, y) at the point (c, f (c)). If the limit above holds, then
no matter the direction of approach, the steepness of f matches the slope of the plane in
that direction. If f has discontinuous jumps, then the linear approximator can only line
up with f on one side of the jump. Figure 14.4 shows an example of the tangent plane to
f (x, y) = −x2 − y 2 at (x, y) = (1, 1).
The computational centerpiece of Definition 14.5 is the linear map A. It helps concep-
tually to isolate A and ignore the shifting by c and f (c) in a principled manner. Let’s do
this now. We want to make the linear map A the focus of our analysis, and here’s how
we’ll do that. For every point c ∈ Rn , we “attach” a copy of the vector space denoted
Tf (c) = Rn to (c, f (c)), and we call it the tangent space of f at c. The tangent space is the
set of inputs to A. Because we view Tf (c) as “attached” to f at (c, f (c)), as in Figure 14.4,
we declare the tangent space’s origin to be (c, f (c)). From that perspective, the linear
approximation of f at c is just a linear map Tf (c) → R, without the shifting by c and
245
f (c).3
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) √ = 2 + 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 (2) is a copy of R, and the total derivative at c = 2 is A(x) =
4 x. The affine linear map is L(x) = 4 (x − 2) + 4.
1 1
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
Many elementary calculus books have students compute this (“the equation of the
plane tangent to the surface of f ”) as something of an afterthought, ignoring that it is the
conceptual centerpiece of the derivative. Next we turn to some questions of consistency
of the definition of the total derivative.
Proof. Suppose there are two functions LA with matrix A and LB with matrix B that are
both total derivatives of f at c. We will show that A = B, and hence that LA = LB .
First, notice the difference of the two defining limits of the total derivative is related
to the difference between B and A. Below, LB,c (x) = B(x − c) + f (c) and likewise for
LA .
Since both LA and LB are total derivatives, both of their defining limits exist and are
zero. This reduces the above to
(A − B)(x − c)
0 = lim
x→c ∥x − c∥
Assume to the contrary that B ̸= A. Then there must be some unit vector v ∈ Rn for
which (A − B)v ̸= 0. Define the sequence xk → c by xk = c + (1/k)v. Then, noting
the change in limit index from x to k,
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 .
Now we’d like to compute total derivatives. To make this process cleaner, we first
deviate to generalize the derivative to functions Rn → Rm .
∥f (x) − Lc (x)∥
lim =0
x→c ∥x − c∥
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 composition of two functions is the composition (product) of their total derivative
matrices.
Note that this should not be surprising! The best linear approximation of a composition
should naturally be the compositions of the best linear approximations of the composed
functions. This formalizes it, and allows us to compute it using 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 . Define by g : R →
Rn the map t 7→ c + tv1 . Then define h(t) = f (g(t)).6
t7→c+tv f
h : R −−−−−→
1
Rn −−−−→ R
By the definition of the directional derivative, h′ (0) = Dh(0) = Dir(f, c, v1 ).7 Apply
the chain rule to h, and get Dh(0) = Df (c)Dg(0). Note 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):
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.
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.8 That
theorem implies that our initial corkscrew counterexample (with a jump as the direc-
tion 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.
of c), the example above can be written as ∂f /∂x1 = 2x1 x2 . One refers to the operation
∂
of taking a partial derivative with respect to x by the function named ∂x , with the jux-
taposition of the f in the numerator taking place of the standard parenthetical function
application. Mathematicians have built up a hodgepodge of notations throughout his-
tory for this. In part, it’s because parentheses are slow to write on a chalkboard—though
they are easy for computers to parse, every new Lisp (or Scheme, or Racket) 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 paren-
theses would not fit nicely in an inner product or summation. Syntactic sugar is a strong
incentive.
When your chosen basis is the standard basis for each variable, the resulting total
derivative matrix Df is called the gradient of f , 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 a useful geometric property.
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 .
( ) v1
∇f (1, 2, π/2) = 2x1 x2 x1 − sin(x3 ) · v2
2
v3
( ) v1
= 4 1 −1 · v2
v3
= 4v1 + v2 + −v3
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.9 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.
v. We studied this in Chapters 10 and 12, and there we noted some interesting facts. Let’s
recall them here. Let v be a unit vector and w an arbitrary vector of the same dimension.
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 proofs. I’ll just share one that uses the co-
sine formula above to emphasize the geometry. You’ll do a different proof in the exercises.
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, −1 < cos(θ) < 1. This proves the “if and only if” part of the theorem,
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 light of our linear
algebraic preparation it is a trivial consequence of how linear projection works. We
can exploit this further. A level curve of f at c is the set of constant-height inputs
{(x, f (x)) : f (x) = f (c)}, like the topographic altitude lines on a map. For a differ-
entiable function, the gradient at c is perpendicular to the vector pointing along the level
curve at c. If v is a direction on the level curve, then the value of f doesn’t change in that
direction, so 0 = Dir(f, c, v) = ⟨∇f, v⟩. Such inner products occur when two vectors
are perpendicular. This allows us to easily compute level curves.
253
Since many things in life and science can be modeled using functions Rn → R, a
common desire is to find an input x ∈ Rn which maximizes or minimizes such a func-
tion. For the sake of discussion, let’s suppose we’re looking for a minimum. Even when
a mathematical 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 diffi-
cult 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 opti-
mality. 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 enu-
merate 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 (or
continuous but trivial) 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.
254
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.10
Gradient descent 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.
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
will never provide the whole story.11 In particular, Theorems 14.14 and 14.18 are only
sufficiency tests for a max/min. They cannot guarantee the detection of optima.
We start with the presence of a local maximum or minimum in a single variable func-
tion. To be rigorous I need to clarify what is meant by a local max or min. When I say any
property for f holds locally at a point c, I mean that there is an interval (a, b) containing
c, such that the property is true when f is restricted to (a, b). (a, b) may be very small if
need be. In other words, it you “zoom in” to f at c, then the property is true as far as you
can see.
To specifically say a point c, f (c) is a local minimum of f means there is an interval
(a, b) around c for which f (c) ≤ f (x) for all x ∈ (a, b). In the example function in
Figure 14.6, f (x) = 21 (x − 1)2 (x − 4)(x + 2), a sufficiently small interval around x = 1
proves that f has a local max at (1, 0), and likewise a local minimum close to (3, −10).
Now we can prove the theorem that concavity is sufficient to detect a local min/max.
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 ′′ (c).
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 ′′ (c) would dic-
tate 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 ′′ (c) > 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,
11
As we saw in Chapter 8, there are nonzero functions so flat at a point that all of their derivatives are zero!
257
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 estimating,
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),12 and let M > 0 be the maximum value of
|f (3) (z)/(3f ′′ (c))| on that interval. I.e., M is the largest magnitude of the coefficient
of (x − c)3 in the above inequality that can occur close to c. Then we need to find an
interval, perhaps smaller than (c − 0.01, c + 0.01), for which the following (simplified)
inequality is true for all x in that interval.
(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
12
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.
258
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. An important way is called a saddle point. The shape
of these is quite literal: the surface looks like the saddle of a horse, or the shape of a
potato chip, in 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-order partial derivative with respect
to xi and xj is the partial derivative of the partial derivative, if it exists. A compact
notation for this is
( )
∂2f ∂ ∂f
=
∂xi ∂xj ∂xi ∂xj
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
259
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
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∂j ∂xi
, is not all that much better.
One might expect the mixed partials with respect to xi , xj and xj , xi to be different
due to the order of the computation. Under sufficiently strong conditions, 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
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
the surface were an ellipsoidal bowl, the eigenvectors would be 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:
261
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.
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 might run multiple copies of this
loop, and output the most optimal run. If the inputs are chosen randomly, there’s a good
chance one avoids the avoidable plateaus.
The bottleneck of gradient descent is computing the gradient. When f is complicated,
such as in a neural network, efficient use of the chain rule is the primary tool for making
gradient computations manageable. The rest of this chapter is dedicated to doing exactly
that.
One might wonder, if the Hessian gives more information about the curvature of f ,
why not use the Hessian in determining the next step to take? You can! But unfortu-
nately, since the Hessian is often an order of magnitude more difficult to compute than
the gradient—and the gradient already requires mountains of engineering to get right—
it’s simply not feasible to do so. And, as you’ll get to explore in the exercises, there
are alternative techniques that allow one to “accelerate” gradient descent in a principled
fashion without the Hessian.
D(g ◦ f ) = (∇g)Df
∂f ∂f1 ∂f1
∂x1
1
∂x2 ··· ∂xn
( )
∂f2 ∂f2
··· ∂f2
∂g ∂g ∂g ∂x. 1 ∂x2 ∂xn
= ∂f1 ∂f2 ··· ∂fm . .. .. ..
. . . .
∂fm ∂fm ∂fm
∂x1 ∂x2 ··· ∂xn
notation suggests that the fi in the last sum “cancel,” which is not true but some find it
helpful.
∂f
1
∂x1
∂g ( )
∂f2
∑ m
∂g ∂fi
∂g
= ∂f ∂g
··· ∂g ∂x. 1 =
∂x1 ∂f2 ∂fm .
. i=1 ∂fi ∂x1
1
∂fm
∂x1
This situation often happens. A function depends on some input parameter transitively
through many layers of function composition. To compute the derivative with respect to
that parameter requires a long “chain” of partial derivatives, summed across the different
paths to get from the input to the output. Chains that may look like:
∂f ∂f ∂g ∂h ∂i ∂j
= .
∂x ∂g ∂h ∂i ∂j ∂x
Notice that the terms in this chain can be grouped and re-grouped arbitrarily. For ex-
ample, 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 recur-
sively through such a network.
4. There is exactly one vertex v ∈ V with no outgoing edges designated as the output
vertex.
13
Recall, a directed edge e = (v, w) is said to have source v and target w, and represents a dependency of w
on v. A graph is acyclic if it contains no cycles, i.e., no circular dependencies.
264
x1 —
+
x2
*
x3 log
Figure 14.9: A computation graph. Each node N is an input or some mathematical oper-
ation on the outputs of dependent nodes feeding into N .
If there’s an edge (v, w), we say that v is an argument to w and that w depends on v.
∑
k
∂G ∂hi
∂G/∂f = · .
∂hi ∂f
i=1
265
a1 h1
a2 f h2
... ...
an hk
Figure 14.10: 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.
Once we have that, each ∂G/∂ai = (∂G/∂f ) · (∂f /∂ai ), as desired. Note that if G
depends on ai via another path through the computation graph, then ∂G/∂ai sums over
all such paths.
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.
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.11: An example decision tree classifying an image by looking at specific pixels.
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
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
267
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.12: A training point for a digit 7 (aligned to make it easier to see).
the cleaned copy on LeCun’s website.14 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.12 shows an example of a training point with
label 7, pretty-printed from its raw format as a flat list of 784 ints.
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.
14
http://yann.lecun.com/exdb/mnist/
268
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
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. See the Chapter Notes for more on
this.
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 ̸= 0 defines an (n − 1)-dimensional vector space w⊥ = {v : ⟨w, v⟩ = 0},
which splits Rn into two halves.15 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 the set {x : ⟨w, v⟩ + b = 0}
is w⊥ 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,16 is the following. Given labeled data points chosen randomly
from a distribution over Rn that can be classified perfectly 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.17
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 Lw,b from Definition 14.21. We’ll call a vertex of the computation
graph corresponding to a linear halfspace a linear node, and each linear node will have
its own independently tunable set of parameters, w and b.
In principle, 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 complicated enough to warrant more help. We
should include operations in our computation graph that transform the input 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.13. The
sigmoid is nonlinear, differentiable, and its output is confined to [0, 1]. You may hear this
operation compared to the “impulse” of a neuron in a brain, which is why the sigmoid is
often called an activation function. Though neural networks are called “neural,” the name
is merely 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. 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.
271
x1
linear → sigmoid
. . .
. . .
. . .
x784
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 only fails to
have a derivative at x = 0, and in practice one can simply ignore the problem. The ReLU
implements the thresholding of the linear halfspace, but with the twist that “activated”
neurons can express how activated they are. Another advantage, which is particularly
nice for hardware optimization, is that evaluating a ReLU and its derivative requires only
branching comparisons and constants. No exponential math is required.
The network we’ll build is architected (quite arbitrarily) as depicted in Figure 14.14. 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.
272
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 mil-
lions 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 round-
ing 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 brittle optimiza-
tion techniques (gradient descent) applied to massively complex and opaque networks
create a house of cards, and that theory and rigor can alleviate these problems. I tend to
agree. But brittle or not, gradient descent on neural networks has proved to be remark-
ably 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 fw on
an example from that example’s label. Note we’re not doing any rounding here, so that
fw (x) ∈ [0, 1].
If we wanted to convert this to a loss function for an entire training dataset, we could,
1 ∑
as Etotal (w) = |S| (x,l)∈S (fw (x) − l)2 . Then the natural method is to use gradient
273
descent to minimize Etotal . However, this loss function requires us to loop over the entire
training dataset for each step of gradient descent. That is prohibitively slow. Instead, one
typically 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.19
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.8, 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.
3. local_gradient: a list of floats, the values (∂f /∂z1 , . . . , ∂f /∂zm ); i.e., the com-
ponents of ∇f that correspond to the non-tunable arguments of f .
4. local_parameter_gradient: the same thing as local_gradient, but for the
components of ∇f corresponding to the tunable parameters of f .
5. global_parameter_gradient: the same thing as local_parameter_gradient,
but for the components of ∇E corresponding to the tunable parameters of f .
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, the child class must set self.parameters
# (this is not a good software engineering practice)
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:
275
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.
276
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
277
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:
278
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.20 We can use it to re-
set the caches at every node. We can also trivially define the evaluate function and
compute_error functions as wrappers.
20
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
279
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
280
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.15 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. This hurts
generalization accuracy.
So there we have it! A functioning neural network, built as a computational graph of
arbitrary operations, with automatic gradient computations.
281
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.11 Exercises
14.3. Prove the analogue of Theorem 14.10 for functions Rn → Rm . In that case, if
f = (f1 , . . . , fm ), the total derivative matrix should be:
282
Dir(f1 , c, v1 ) Dir(f1 , c, v2 ) ··· Dir(f1 , c, vn )
Dir(f2 , c, v1 ) Dir(f2 , c, v2 ) ··· Dir(f2 , c, vn )
.. .. .. ..
. . . .
Dir(fm , c, v1 ) Dir(fm , c, v2 ) · · · Dir(fm , c, vn )
Hint: the same proof works, but the construction of the single-variable function to apply
the chain rule to is slightly different.
14.7. Prove that the rule for computing partial derivatives by assuming other variables
are constant is valid.
14.8. Make sense of the Hessian as a linear map.
14.11. Perhaps the most famous theoretical machine learning model is called the Proba-
bly Approximately Correct model (abbreviated PAC). This model formalizes much of mod-
ern 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
evaluating 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
283
high accuracy 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 learn-
ing algorithm to be considered successful in the PAC model, it must provably succeed
for any distribution 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.
14.12. 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.13. Write a program that uses gradient descent to learn linear threshold functions. In
particular: write a function that samples data uniformly from the set [0, 1]5 ⊂ R5 , and
labels them (unbeknownst to the learning algorithm) according to their value under a
fixed linear threshold function Lw,b . Design a learning algorithm to learn w and b from
the data. That is, determine what the appropriate loss function should be, determine a
formula for the gradient, and enshrine it in code. How much data is needed to successfully
and consistently learn? How does this change as the exponent 5 grows?
14.14. 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 technique is 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.15. Another popular technique for training neural networks is the so-called mini-
batch, 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
considered a good idea, and augment the program in this chapter to incorporate it. Does
it improve the error rate of the learned hypothesis?
14.16. 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.
284
14.17. 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.
14.19. 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?
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 has a dependency graph like in Figure 14.16, 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
285
f1 f2 ... fm
x1 x2 ... xn
Figure 14.16: The dependence of g ◦ f on each xi contains paths through each of the fj .
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.
∂(g ◦ h) ∑ ∂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
286
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 C∥errH (t)∥, where C = ∥∇g(c)∥ is
constant. So the limit above is
∥errH (t)∥
C lim = 0.
t→0 ∥t∥
287
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 familiar: it’s the inside of the limit for the direc-
tional derivative of h (rather, a vector of directional derivatives). As ∥t∥ → 0 it gets close
to the directional derivatives, so for a 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.
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.
288
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.
Scaling Neural Networks
Our neural network and computation graph are almost laughably small. And, having
written our network in pure Python, training proceeds at a snail’s pace. It should be
obvious that our toy implementation falls far short of industry-strength deep learning
libraries, even though the underlying concepts of computation graphs are the same. I’d
like to lay out a few specific reasons.
Our network for learning (a subset of) MNIST has roughly 7, 500 tunable parameters.
Large-scale neural networks can have millions or even billions of tunable parameters.
Many additional mathematical and engineering tricks are required to achieve such scale.
One aspect of this is hardware. Top-tier neural networks take advantage of the struc-
ture of certain nodes (for example, many nodes are linear) and the typical architecture
of a network (nodes grouped in layers) to convert evaluation and gradient computations
to matrix multiplications. Once this is done, graphics cards (GPUs) can drastically ac-
celerate the training process. Even more, companies like Google develop custom ASICs
(application-specific integrated circuits) that are particularly fast at doing the operations
neural networks need for training. One such chip is called a Tensor Processing Unit
(TPU). The proliferation of graphics cards and custom hardware has resulted in the abil-
ity to train more ambitious models for applications like language translation and playing
board games like Go.
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
290
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 book. The designer
of Keras also wrote a book, “Deep Learning with Python,” which—beyond including a
multitude of examples—covers the nitty-gritty engineering details with plenty of refer-
ences.
Chapter 15
– Donald Knuth
291
292
popular notation in number theory. It was not until mid-century 1900’s that big-O found
its way to computer science, in part because computer science had to be invented. 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 x7 x9
sin(x) = x − + − + − ···
3! 5! 7! 9!
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.
Definition 15.1. Let a ∈ R and let f, g : R → R be two functions with g(x) ̸= 0 on
some interval around a. We say f (x) = O(g(x)) as x → a if the limit of their ratios does
not diverge.
f (x)
lim <∞
x→a g(x)
The limit notation needs a disambiguation. We’re not saying that the limit has to exist.
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)| ≤
D|g(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. In each of these, assume that the expressions inside the big-O are
nonzero on some interval around a.
Take care, because when we say f = O(g), the symbol = doesn’t mean equals in the
usual sense. For example, it’s not symmetric or transitive; x3 = O(x) and x2 = O(x) as
x → 0, but x3 ̸= 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 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
sees in physics books all the time, but instead of using the mathematically valid big-O,
295
they say “we’ll ignore this term” or “assume this term is zero.” Being sloppy in this un-
controlled 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 too much more. When two terms get combined, you’re
forced to ask if the combined error is too big. The interface helps prevent careless mis-
takes. 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 claim
that 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, x2 = ̸ Ox→∞ (x)
and x ̸= Ox→∞ (x). Instead, x = Ox→∞ (x ), x = Ox→∞ (x ), and so x2 + x3 =
3 2 3 3 3
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.
296
Definition 15.4. Let f, g be functions as before, with both nonzero on some interval
around a.
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)
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 having principle to guide our decisions. 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.”
298
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 it’s 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 programs
that automate the running of the tests of the correctness of the 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. By analogy, if you study the way a refactoring
tool changes the behavior of a program, that can help you understand how the program
works. Even more, it can help you understand how to write clearer and more refactorable
programs. 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
1
By many accounts attributed to a Frenchman named Évariste Galois in the early 1800’s
301
302
roots of a single-variable polynomial have symmetric structure. Such structure can be for-
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.
303
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 geometry can be studied via the groups that transform geometric space. Finally,
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 : Q → Q has the
property that for every pair of points (x1 , y1 ), (x2 , y2 ), the distance between (x1 , y1 ) and
304
These properties make an arbitrary function sensible enough that one could reasonably
call it a “distance” function. Of particular interest is 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.
We often emphasize that we’re talking about isometries that preserve the square—map
points in the square to other points in the square—by calling these isometries symmetries
of the square. Such a provocative name encourages the natural question: what are all
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. Of course, you want the definition of an operation to be independent
of what operations are applied before or after it, so this configuration-independent definition is best.
305
B C A D D C
A D B C A B
ρ σ ρρσρ
of the different symmetries of the square? There are infinitely many ways to compose
symmetries on paper, but two symmetries 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. 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.
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 corners 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 completely 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.
306
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 ρ.
307
2. For every x ∈ G there is some element y ∈ G called an “inverse” for which x·y = e
and y · x = e. (A priori there may be more than one such inverse.)
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. For some groups, this is the hardest axiom to establish.
308
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 except N are groups under addition:
rational numbers, real numbers, complex numbers, etc. If we want to work with multi-
plication, 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: 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 copies of x.
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 copies. 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.
309
( )( ) ( )
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
Addition of the inputs corresponds exactly to multiplication of the corresponding ma-
trices! 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 representations for
the same abstract algebraic structure. Not only are the elements in bijective correspon-
dence, but the operations are as well! Even better, this family of matrices has a geometric
interpretation as a shear transformation along the horizontal axis. This bijection shows
that the compositional structure of shearing (in a fixed direction) is identical to the ad-
ditive structure of the magnitude of the distortion. With that comes all the benefits of
understanding real numbers.
Any mathematical setting that expresses the abstract group R can be identified by find-
ing this sort of group-correspondence with (R, +). A mathematician sees this wonderful
example and dreams: 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.
Proof. Let G be a group with identity 1G , and H a group with identity 1H . Let f : G → H
be a homomorphism. Since f preserves the group operation,
Since H is a group, all its elements have inverses, including f (1G ). So multiply both
ends by f (1G )−1 to get
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 (Exercise: prove this is a homomorphism). 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 .
311
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.
but a group G may have generating sets of different sizes. Hence, any concept of “group
dimension” must be more nuanced.5
One of the simplest ways to build a larger group from smaller pieces is the direct product.
This construction simply forms the product of two groups as sets, and defines the group
operation component-wise. E.g., Z × Z/2Z is the set of pairs {(n, b) | n ∈ Z, b ∈ {0, 1}},
where (n, b) + (n′ , b′ ) = (n + n′ , b + b′ ). If a group decomposes as a direct product of
subgroups, the symmetry structure can be seen to have independent components.
The set Z/nZ forms a group under multiplication if we remove the numbers k such
that gcd(n, k) ̸= 1. This guarantees that inverses exist. In the special case that n is prime,
we need only remove zero. This group is denoted (Z/nZ)× , and it’s substantially more
interesting than integers under addition. Up to isomorphism it is always possible to write
(Z/nZ)× as a direct product of cyclic groups. However, there is no known generic method
for finding generators of the cyclic pieces. This computational difficulty is exploited by
RSA public-key cryptography, which we will explore in an exercise.
Next we have the symmetry groups of regular convex polygons6 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.
Confusingly, the dihedral group for a polygon with n sides is sometimes denoted Dn
instead of D2n , which makes D8 terribly ambiguous. We’ll use D2n .
5
A cyclic group has a generating set consisting of a single element, called “the” generator, but “generator” can
loosely refer to any element of a generating set, even if that element alone does not generate the group.
6
Regular means all the angles have the same measure and all sides have the same length, and convex means
every line between points in the polygon is completely contained in the polygon.
314
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 reasonably well-behaved number system that
has addition and multiplication, say R for example, we can form a group of square matri-
ces under matrix multiplication, 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 specific 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.
This group is closely related to the symmetry group of Euclidean space we’ll study
in Section 16.5. Another interesting facet of groups of matrices is that they have enough
structure that one can do calculus on them. 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. Entire books have been written about this.
special cases of projective geometry and hyperbolic geometry had been discovered, but
it was largely unclear how different geometries were related.
In general, to define a geometry you need to define a few things:
• 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
preserves that quantity. Every geometry has a group. Every group corresponds to some
geometry.
Klein called his view the Erlangen Program.7 One striking result8 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 symmetry
group of the projective geometry. We get containments of the spaces as sets, and of the
groups as subgroups.
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.9
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,
Let f be an isometry that fixes the origin; 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, 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
318
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.3)
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).11 Recall it has the following characterization.
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,
11
Not to be confused with big-O notation.
319
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 claim, the more it seems the claim might be false. Indeed, the parallel postulate
can be 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 and Exercises contain more historical details.
320
The universe of points for the Poincaré disk is the interior of the unit disk,12 D2 =
{(x, y) ∈ R2 | x2 + y 2 < 1}. One is supposed to colorfully imagine the boundary of
the unit disk 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
Now we can immediately see why the parallel postulate fails: parallel lines are just
circles that don’t intersect! Given one such circle C and a point not on that circle, we
can find many circles passing through the point that don’t intersect C. This is pictured
in Figure 16.6, where C is the dotted line.
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. A priori, it’s not clear what a circle would look like in this model, since some
lines are defined as parts of Euclidean circles. We will have to define such a “Poincaré
circle.” 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 Euclidean circle.
Definition 16.15. Let C be a Euclidean 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.
12
We will refer to a “circle” strictly as the boundary set {(x, y) ∈ R2 | x2 + y 2 = 1}, and the disk as the circle
jointly with its interior. This is standard mathematical parlance.
321
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.
322
C
q
p'
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
p′ = x + r2 (p − x)/∥p − x∥2 .
Working out the details,
p q''
q'
L1
L2
Figure 16.8: The construction of a Poincaré circle with center p and radius pq.
Next, we define 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.7. Given two
points p, q ∈ D2 , pick one that’s not the center of D2 and invert it in the unit circle to
get a third point s outside the unit disk. 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. To extend this segment to a
line, include the entire arc within the interior of D2 .
Second, we define a Poincaré circle. We take a cue from Euclidean geometry, where a
circle has the property that it is perpendicular to every line through its center, and use
this property to guide the construction. 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 hy-
perbolic 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. See Figure 16.8 for a diagram. 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 centers necessarily lie 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.
324
Definition 16.17. Let w, x, y, z be four distinct 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.
Lemma 16.18. Two hyperbolic reflections agreeing on two distinct pairs of inversion 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 reflecting across 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.13 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
13
Equivalently, one could parameterize the line by picking a unit vector v = (x − z)/∥x − z∥ and letting
x = z + av, y = z + bv.
325
C
x'
x
y
z
y'
Figure 16.9: The image of two points uniquely determines the circle of inversion (the easy
case).
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′ . Note that if b − a = a′ − b′ ,
then the two points must be interchanged by the inversion, and hence they are not two
distinct pairs of inversions.
Lemma 16.18 fails in the case that the two points are exchanged by the inversion. It
simplifies the pair of equations used in the proof 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.
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):
326
w' w y y'
c
(wy) (cw)
Figure 16.10: The central claim is that (w′ y ′ ) = (cy ′ ) .
/
(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.
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(p, q) = log[xy; qp] = log
2 2 (x − p)(y − q)
327
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,14 this completes the proof.
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.15 Combining
these two equations and rearranging we get
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.
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
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 reflec-
tions. This proof relies on a fact whose proof I have omitted for brevity: isometries of the
Hyperbolic plane map lines to lines, just like in the Euclidean setting.
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)).
Figure 16.16: Cloth, Hawaii. From The Grammar of the Ornament. A pattern which has
two linearly independent directions of translational symmetry.
ally infinitely large) set of all such transformations forms a group. This group uniquely
describes the geometry of the tessellation.
The Euclidean plane provides a notable example before we return to hyperbolic ge-
ometry. Let’s consider the set of all patterns that have discrete repetition in two lin-
early independent directions (as opposed to a pattern that only repeats when shifted, say,
right), such as in Figure 16.16. The groups that describe such patterns—which include the
tessellations used in many 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
332
Figure 16.17: A figure which, when used Figure 16.18: Irregular pentagonal
to tile the plane, has p4 as its symmetry tilings of the Euclidean plane. Figure
group. Figure by Martin von Gagern. by David Eppstein.
Theorem 16.23. There is no tessellation of the Euclidean plane by a single n-sided convex
polygon for any n > 6.
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 subdivided16 ) 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 6). 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 .
16
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.
333
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.
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.
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
334
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
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.17
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.18
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.19 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.
17
Here we used |Vext | = O(A1/2 ), which is true because every exterior edge touches at most two exterior
vertices, and the number of exterior edges is O(A1/2 ).
18
The intrepid reader will revisit the proof of Theorem 16.23 and determine where it fails for hyperbolic geom-
etry.
19
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.
335
Figure 16.20: Left: a tiling of the hyperbolic plane by 7-gons with 3 meeting per vertex.
Right: with 4 meeting per vertex.
Theorem 16.24. 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.
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.25. 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.
336
Figure 16.21: Left: Circle Limit IV, M.C. Escher, 1960. Right: annotated showing the center
6-gon that is tessellated.
With these basic objects and operations, we can compute the hyperbolic line pass-
ing through two points. The inputs are 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 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 at least one point is not on the circle, then the output is computed as follows. Invert
the non-circle point in the circle (Proposition 16.16 guarantees orthogonality), and the
result is a set of three points, which uniquely determine the equation of a circle.
338
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]
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
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
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
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
342
B
π/q 2 2
r = (bx– gx ) + by
π/p
A D G
Figure 16.26: The unknown points computed in Lemma 16.26 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.
343
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
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.num_polygon_sides
q = tessellation_configuration.num_polygons_per_vertex
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]
The remainder of the code21 involves rendering the edges of the polygons as SVG arcs.
We also created a simple data structure that allows one to compare polygons for equal-
ity in a principled way (since the process of reflecting them changes the order of their
vertices).
class TessellationConfiguration(
namedtuple('TessellationConfiguration',
['num_polygon_sides', 'num_polygons_per_vertex'])):
def __init__(self, num_polygon_sides, num_polygons_per_vertex):
if not self.is_hyperbolic():
raise Exception("Configuration {}, {} is not hyperbolic.".format(
(self.num_polygon_sides, self.num_polygons_per_vertex)))
def is_hyperbolic(self):
return (self.num_polygon_sides - 2) * (self.num_polygons_per_vertex - 2) > 4
class HyperbolicTessellation(object):
def __init__(self, configuration):
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.num_polygon_sides
"""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
21
See pimbook.org
345
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.9 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
the composition of the corresponding maps. A cycle of length 2 is called a transposition.
346
Prove that every permutation can be written as a product of disjoint cycles. Prove that
the n-cycle (1 2 3 · · · n) and a single transposition (1 2) are a generating set for Sn .
16.4. 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.6. Prove that x ∈ Z/nZ has a multiplicative inverse if and only if gcd(x, n) = 1.
16.7. Let G be a finite 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.
16.9. Prove Theorem 16.12, assembling the pieces laid out in the chapter.
348
16.10. 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.11. Research and implement the ElGamal digital signature scheme using (Z/nZ)× .
16.12. 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.13. 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?”22
16.14.( Recall
) an undirected graph G = (V, E) is a set of vertices V and a set of edges
E ⊂ V2 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.15. 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.
16.16. Prove that any Euclidean isometry in E(n) can be written as the product of at
most n + 1 reflections.
16.17. Read about determinants and understand why the formula we presented in Sec-
tion 16.7 for the circle passing through three given points is correct.
16.18. Research the cross ratio in the context of projective geometry. How is it defined
there? What are the projective transformations, and why is do they preserve the cross
ratio?
22
https://www.akalin.com/quintic-unsolvability
349
16.21. 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.22. 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.23. 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. For these exercises it may help to read
the section in the chapter notes about the complex matrix representation of hyperbolic
isometries. Prove the following.
1. 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.
2. Find a formula for inversion in a circle (reflection in an upper-half-plane-model
line) as a Möbius transformation.
3. 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?
4. All Möbius transformations preserve the cross ratio.
5. Find a bijection between the upper half plane and the Poincaré disk that preserves
hyperbolic lines.
350
16.24. Yet another model of hyperbolic geometry is the Minkowski hyperboloid model.
This model has as points the vectors {(x, y, z) : x > 0 and x2 − y 2 − z 2 = 1}. These
points lie on a hyperboloid. Find the resource that explains the following for this model:
1. Defines hyperbolic lines.
2. Defines hyperbolic distance.
3. How hyperbolic lines in the Minkowski model correspond to lines in the Poincaré
disk model.
4. Using the above, write a program that draws a hyperbolic tessellation in the
Minkowsi model, and then projects it to the Poincaré disk. What are the advan-
tages and disadvantages to doing it this way, instead of directly in the Poincaré
model?
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?
[ ]
∑ σ
∏
n
det A = (−1) ai,σ(i)
σ∈Sn i=1
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.
A more useful definition of the determinant explains why it shows up in so many
geometric formulas: 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
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 volume of the triangular prism of height 1 whose apex is the origin and whose
base is T . We set the prism’s height to 1 so that 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. Note
that swapping two rows of a matrix multiplies the determinant by −1.
The Hyperbolic Isometry Group as a Group of Matrices
Multiple times throughout this book, we’ve avoided using complex numbers, resulting in
some slightly nonstandard work. This was essentially a cop out.23 Be that as it may, the
group structure of hyperbolic isometries is best studied with complex numbers.
The briefest review: the set C = {a+ib : a, b ∈ R} is called the set of complex numbers,
where i is the “complex unit,” i.e., it’s a unit vector defined to be linearly independent
from 1. There is a bijection C → R2 via a + ib 7→ (a, b), so that complex numbers
can be viewed as a plane. Using this view, denote by arg(a + bi) the angle between
(a, b) and (1, 0) (chosen to be in the interval [0, 2π)), denote by |a + bi| the length of
(a, b), and define multiplication of a + ib = (a, b) by i as the rotation of (a, b) by 90
degrees counterclockwise. Extrapolate from this that i2 = −1, and assert that the usual
arithmetic rule that (a + ib)(c + id) = ac − bd + i(ad + bc).
As an elegantly stated consequence, if z, w ∈ C then their multiplication is uniquely
determined by the two properties arg(zw) = arg(z) + arg(w) and |zw| = |z||w|. Multi-
plying two complex numbers adds their angles and multiplies their lengths. Inverses are
also defined: 1/z is the unique complex number whose angle is 2π − arg(z) and whose
length is 1/|z|, provided z ̸= 0. If we define the complex conjugate a + bi = a − bi, then
1/z = z/|z|2 . This formula looks familiar, it’s because z 7→ 1/z is a geometric inversion
in the unit circle.
23
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.
352
+ 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.27. 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.
+
The functions fa,b are “orientation preserving” isometries of D2 , meaning they are a
product of an even number of reflections.24 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.23). ( )
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.
24
Orientation has a technical definition that encodes the intuitive idea that “reversing orientation” turns “hello”
into “olleh” and vice versa—though for hyperbolic isometries it will have the expected additional warping.
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
that I have in the combination of math and programming.
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! I included an annotated
list of resources in Appendix C to whet your appetite.
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!
1
https://vimeo.com/36579366
353
354
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-
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 es-
say “Kill Math,” Victor argues that a pencil and paper is the most antiquated and unhelpful
medium for using mathematics. Victor opines on what a shame it is that so much knowl-
edge is only accessible to those who have the unnatural ability to manipulate symbols on
paper. How many good ideas were never thought because of that high bar?
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
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.
355
mathematics.3 I’m surprised Victor does not reference algebra as a perfect example of a
tool for thinking new thoughts, even if before arguing its time has passed.
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,
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. As
Sir Arthur Eddington said, group theory studies operations that are as unknown as the
quantities that they operate on. 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.
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
4
Layouts of buttons and toggles in general, of which QWERTY is one
357
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 the assignment and management of meaning 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. And it should be easy to recall what semantics are applied to which objects
later. 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
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 interactive 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
5
https://github.com/kenperlin/chalktalk
358
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 paint-
brush to paint a flower on a canvas, 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
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
359
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.
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.
6
http://youtu.be/lFQGSGsXbXE
360
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-
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 realize
that if any cell is cloneless, as must be true after a finite number of moves, we 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 test bed 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
361
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.
Notation
A lookup table for the notation used in this book, roughly ordered by chapter. Refer to
the ‘notation’ entry of this book’s index to find the page where the notation is introduced.
Wikipedia also has an excellent table.1
1
https://en.wikipedia.org/wiki/List_of_mathematical_symbols
363
364
A Summary of Proofs
365
366
P Q P →Q Q→P (P → Q) and (Q → P ) P ↔Q
T T T T T T
T F F T F F
F T T F F F
F F T T T T
as a human you may not know how to tell if a statement is true or false (such as “there
are infinitely many prime integers”), but its truth value doesn’t depend on information
not specified in the proposition itself. Of course, if you might use the name P to refer
to a generic proposition, but that is a “variable” of our analysis, not part of the logic
itself. Confusingly, some call generic propositions “propositional variables.” This will be
contrasted with first-order logic momentarily, where variables are first-class citizens.
The core operations performed on propositions are logical connectives, like “and,” “or,”
and “if-then.” A compound proposition might be “7 is even and 12 is divisible by 4,” or “if
7 is odd then 7 + 1 is even.” Another logical connective is equivalence, often written as
“if and only if,” which connects two propositions P and Q by asserting that the truth of
P is identical to the truth of Q. That is, if P is true then Q must be true, and if P is false
Q must be false.
“If-then” statements in propositional logic are often written using an arrow, which
denotes “logical implication.” You might see P → Q, which is the same as “if P then Q.”
Likewise, P ← Q would represent “if Q then P .” Finally, if and only if is often written
as a double-ended arrow, P ↔ Q. One initially strange convention is that if P is false,
then any implication of the form P → Q is defined as true. In this way, P → Q can be
defined as shorthand for “not P , or Q.” In other words, the only time P → Q can be false
is if P is true, but Q is false.
For any generic compound proposition, one can write down a truth table that describes
the full range of possible truth values the syntactic statement can assume. For example,
Figure B.1 shows the truth table that proves P ↔ Q is an equivalent statement to “P → Q
and Q → P .” This holds regardless of the semantic content of P and Q.
First order logic adds variables to propositional logic, meaning statements can have
unknown truth values. A claim in first-order logic is called a formula. For example if x is
stated to be a variable ranging over the integers, then “x is even” is a formula, but its truth
value is undetermined absent more knowledge about x. However, if you interpret x as 8,
then “x is even” is a true formula; “for every x, x is even,” is a false formula; and “there is
an x such that x is even” is a true formula. These are the three ways that a variable can
become “bound” in first-order logic. A variable can be assigned a concrete value. A vari-
able can be universally quantified, meaning we claim the formula is true for all possible
assignments. Or, finally, a variable can be existentially quantified, meaning we claim the
formula is true for at least one possible assignment. If all variables in a formula are bound,
then the formula has a truth value. Often the symbol ∀ is used for the universal quantifier,
367
There is another important technique, called proof by induction, that does not fit neatly
in every first-order logical framework (though it does in some, see below). In second-
order logic, induction is actually an axiomatic inference rule of the form: for all boolean-
valued functions F : N → {True, False}, if (P (1) and for all k ∈ N, P (k) → P (k + 1)),
then for all n ∈ N, P (n). As we have seen many times in the book, to prove by induction
you prove the base case (P (1)) and the recursive/inductive step (for all k ∈ N, P (k) →
P (k + 1)) separately, and you can infer the theorem is true for any natural number.
For a first-order logic where the universe is the universe of sets, the concept of natural
numbers is usually baked into other axioms, and so the induction inference rule can be
proved as a theorem. In a logic whose universe of elements are integers, it is baked into
axioms about well-ordering. In the end, it is usually singled out as a particularly handy
proof technique for the times when you have no other ideas on how to prove a theorem.
Most proofs combine these four basic techniques—direct proof, contrapositive, con-
tradiction, and induction—at different layers. For example, one might start a proof by
induction, but then prove the sub-claim “for all k ∈ N, P (k) → P (k + 1)” by contradic-
tion.
Different subfields of mathematics have further groups of techniques which often don’t
get catchy names. For example, we’ve used one technique in this book that is common to
analytical proofs, which goes as follows. To prove A(x) < B for all x, first find a simpler
quantity C for which you know that C < B, and then prove A(x) ≤ C. In combina-
torics one often finds useful formulas for counting things by regrouping and applying
natural formulas. For example, in Chapter 4 we had a proof involving grouping games of
a tournament by the losers instead of the rounds of winners. There is also the technique
of establishing an invariant, used in multiple chapters in this book (Chapters 6, 12, 16 at
least). Mathematicians enjoy identifying the patterns of proof techniques, and general-
izing them as much as possible. Programmers often similarly yearn to generalize their
programs.
Pólya’s advice. Before we get to that, there are a number of ways that you can stumble
upon something you want to prove.
One common way is when working on another problem and you notice a pattern. For
example, you may be working on a number theory problem about square numbers and
notice that the difference between successive square numbers is always odd. For example,
25 − 16 = 9 and 81 − 64 = 17. You have already noticed a pattern, you know what it is
you want to prove, and you can set out trying to prove it.
Another more tenuous situation is when you have some known inputs, and a known
state you’d like to get to, but otherwise no clue on how to get there. For example you
may have some quantity you believe is bounded from above by 2, but you don’t know
how to prove it. An example I was working on at the time of this writing consisted of a
sum like
With a clear problem in hand, the simplest next step is to write down many examples,
and draw pictures, and try to gain an understanding of why the problem resists a proof.
Often, simple examples show that my belief about the problem was completely wrong,
and it’s actually false for trivial reasons. For the cosine problem above, I plotted a number
of different values of the various parameters, and tried to understand a rough idea about
what shifts would make the peaks line up, and what shifts would make the troughs line
up (both bad situations). I determined that in this case this problem did actually have
some meat to it, so I proceed.
Now there are a few techniques I can try. The simplest and most reliable technique,
in my opinion, is to make the problem progressively simpler and simpler until you can
solve it, and then slowly add back in complexity until you can’t solve it anymore. For
the cosine problem above, we can start by fixing all the Ai = 1, and the mi to sequential
370
integers mi = i. After thinking about that version of the problem for a while, it’s still too
hard, so I simplify it further by fixing n to small values. Since n = 1 defeats the problem
instantly (the maximum is unchanged no matter what you do), the simplest nontrivial
choice is n = 2.
We can spot one further simplification by noticing that the shift of p in one cosine
combined with a shift of q in the other is the same as shifting just the second by q − p, so
we may as well have only one parameter r = q − p. The sum is now simply cos(2πt) +
cos(4πt + r).
Now what further resists a proof? We could try to simplify further by letting the two
periods 2π and 4π be the same value (say, both 2π). We can ignore for the moment that
this violates one of the constraints of the problem, in order to determine if that constraint
is important. Indeed, such a simplification makes the problem too trivial, because an
easily chosen shift of π cancels both curves out completely to the zero function. The
differing periods (and, it appears, the fact that their ratio is rational) are core ingredients
in the fact that a nontrivial minimum can be achieved. At this point, one can try to
manually optimize the function to find the right value of r, using techniques from calculus.
In so accomplishing this task, one reflects on the results. Will the techniques applied
generalize to the more complex case of 3 or more curves? If not, at what step does it break
down? What, precisely, is the core reason that technique fails? Does that say anything
about whether related techniques would also fail? Does that provide any insight into
what properties are required of a technique if it is to succeed?
There are a number of other questions naturally raised when doing this simplify-solve-
generalize loop. What known problems seem related to this one? For example, the prob-
lem above looks like a decomposition called the Fourier series, so one could look for
information pertaining to how to tell where the maximum of a finite Fourier series lies.
Another question: can we restate the problem differently to suggest different ap-
proaches? For one, I notice that I will fail at my minimization goal if I unluckily cause
many peaks of different curves to line up, or many troughs. So somehow I want to mis-
align all the peaks relative to all the other peaks, and all the troughs relative to the other
troughs. But I can easily compute the peaks and troughs of each curve, they form a dis-
crete set, so maybe it is enough to find an alignment that keeps the peaks and troughs as
far away from each other as possible. This idea of mis-aligning peaks and troughs is also
a sort of heuristic reasoning that may guide me to a more precise proof.
Another question: can I make the problem more general in a way that helps? Knowing
a bit about complex analysis and the famous formula eit = cos(t) + i sin(t) suggests to
write cos(k2πt + p) = Re(ei(k2πt+p) ) and work there. Indeed, from that perspective
the cosine is the projection of a vector onto the x-axis, and the function is a sum of
continuously rotating vectors. I want to keep the projection of those vectors from sticking
out too far in the horizontal direction left or right (but they may stretch as high or as low
as they want). It is worth noting that complex numbers have a rich history of making
371
some complicated calculus problems much simpler, so it’s reasonable to expect they might
help in this situation.
Another question: can I find a good approximation to the thing I want to prove? Per-
haps instead of finding the exact minimum of the exact function, I can find the exact
minimum of an approximation to the function, or an approximate minimum to the exact
function, or an approximate minimum to an approximate function. In each of these I
could apply various types of approximations, such as Taylor series (Chapter 8), and try
to measure the quality of the approximations.
I would be remiss to neglect the programmer’s favorite approach: I could try to do a
brute-force search for solutions to a large class of simple examples, and look for patterns
in the results. I have applied this to surprising success on multiple occasions, deriving
conjectures that turned into pleasantly simple proofs.
There are many other questions I could ask, but in puzzling over each I form a rough
plan of attack for the problem. I get leads for topics to read about that may help, I find a
new way to picture the problem, and I can apply each to my list of examples to evaluate
whether it is worth pursuing. As I learn more mathematics in general, I find more and
newer ways to approach problems.
What’s most important about having all of these leads is that one feels like one is mak-
ing progress. It is completely useless to write down a problem with nowhere to go from
there. The process of simplifying and generalizing in different ways, in addition to the
loop of conjecture and proof or refutation, preserves momentum, upholds determination,
and preserves sanity through trying times.
Beyond searching for leads, there is the practical matter of prioritization. How do you
choose which lead to follow, and for how long should you keep at it before switching
tacks? How do you keep track of your progress so that you can easily resume where you
left off, or revisit an approach later? In my view the answers to this are deeply personal.
Everyone has different styles of managing their “To Do” list and project management.
But I will share some thoughts.
Many mathematicians I know, including myself, keep notebooks of various sorts for
the off-hand thoughts and tinkering that is too embarrassing to show to the world. Some
mathematicians take this a bit further. They rely on the idea expounded in this book
that, once you have a nugget of insight, you can hide the details to reduce overhead and
re-derive them as needed. As such, the way to “keep track” of a lead may be as simple
as a line “Try Fourier series.” One might spend a working session working that angle
on a blackboard or scratch paper, or do an extensive literature search, and at the end
derive one clear limitation or bit of progress, such as “odd/even makes a big difference.”
The intermediate scratch work is often discarded, and can be recreated (often clearer and
more concisely) later. As my advisor’s advisor would say (told to me by my advisor), “If
you can’t recreate it later, then it was probably wrong anyway.” Luckily, the nugget of
insight is often much easier to write down or remember.
Many others use the process of typesetting their notes (and cleaning/pruning them)
to preserve the important bits. This has often helped me find mistakes in my scratch
work early—or at least, earlier than when my colleagues and I have declared victory and
372
are ready to typeset it in a paper submission. Typesetting forces you to slow down and
reexamine your work, similar to how writing something manually with a paper and pen
makes it easier to remember and makes you more deliberate about what you write.
Appendix C contains references to more formal treatments of logic, and practical proof
reading and writing skills.
Appendix C
Annotated Resources
One of the primary outcomes I hope readers get from this book is the ability and confi-
dence to engage with mathematics outside these pages. Mathematics is full of excellent
books, lecture notes, blogs, videos, and talks. Most importantly, mathematics is a broad
community of people. With some of the ideas in this book, a jump start on notation and
proofs, and hopefully an idea of what you want to learn next, you can take full advantage
of the broad literature and wonderful people.
In putting so much content into this book, I necessarily had to compress and omit. I
encourage readers to read other books alongside this one. In particular, readers have
written to me that they found it helpful to read a supplementary introduction to proofs,
to reinforce and practice mechanics in tandem.
Extending that spirit, I have assembled a catalog of additional resources, broken down
by general topic. I have included many of my favorites. I will also include some resources
that I have not fully consumed myself—such as books I have skimmed or partially read.
One reliably gentle class of math books is Springer-Verlag’s series Undergraduate Texts
in Mathematics. These are so good precisely because they’re aimed at undergraduate
students, where familiarity and maturity cannot be assumed. I will label these by the
acronym “UTM.” A similarly accessible series is the Dolciani Mathematical Expositions.
2. Naive Set Theory (UTM), Paul Halmos. A short derivation of set theory, axiomati-
cally from the ground up. The first 50 pages contain everything you would need
to know in terms of notation, definitions, and basic properties of sets, functions,
relations, etc.
373
374
basic methods of proofs, with many exercises. Covers topics like sequences, com-
pleteness, inequalities, and basic number theory. Notable for how cheap the phys-
ical book is (at the time of this writing, 10 USD).
4. Logic as Algebra, Paul Halmos, Steven Givant. A book that covers propositional
logic, but primarily works to show how logic exhibits algebraic structure (an aside
from Chapter 2). This is also the book where I first saw the tournament problem
proof described in Chapter 4.
5. Reading, Writing, and Proving: A Closer Look at Mathematics (UTM), Ulrich Daepp,
Pamela Gorkin. A more expository-focused book introducing propositional logic,
proofs, and sets.
C.2 Polynomials
1. Polynomials, E. J. Barbeau. A compendium of results about polynomials, phrased
as problems and puzzles.
2. Ideals, Varieties, and Algorithms, David Cox, John Little, Danal O’Shea. Covers a
large range of computationally relevant aspects of polynomials, and graduates the
reader toward a mature view of polynomials in terms of rings. An overview of
algorithms for manipulating and solving systems of polynomials, and interesting
applications like robot motion planning.
2. A Walk Through Combinatorics, Miklós Bóna. An extensive text covering the basics
of combinatorics and its particular methods of proof. After going through basic
counting problems and tools, it moves on to advanced tools like generating func-
tions, and then proceeds to cover a large subset of important combinatorics and
graph theory topics from matchings and colorings to error-correcting codes and
block designs (only in more recent editions). I studied the first half of this book in
detail as an undergraduate.
3. Graph Theory and Its Applications, Jonathan L. Gross, Jay Yellen, Mark Anderson. A
comprehensive undergraduate level introduction to graph theory, with particular
attention paid to algorithms that are relevant in computer science such as graph
traversals and tree-based encoding techniques.
4. Networks, Crowds, and Markets, David Easley, Jon Kleinberg. Covers a wide breadth
of applications of graph theory, specifically oriented around processes and dynamic
systems that occur on a graph. A natural next step for the reader who enjoyed the
discussion of stable matchings in this book. Leans heavily toward modeling and is
relatively light on mathematical technicalities.
2. Introduction to Real Analysis, Robert Bartle, Donald Sherbert. The text I originally
used to learn real analysis as an undergraduate. I found the approach of using
“tagged partitions” to define Riemann integrals to be so natural and helpful that
the idea has lodged itself in my head despite having no need to use it after that
class.
376
3. Calculus, Michael Spivak. Revered as a classic, but dense and focused primarily
on proving single-variable calculus from the ground up with rigor. I have found it
useful as a reference and a third pass over calculus.
4. The Cauchy-Schwarz Master Class, J. Michael Steele. One of my favorite math books,
which covers the many forms of the Cauchy-Schwarz inequality, and, more impor-
tantly, shows the delightful qualities of analytical proofs. Focuses on techniques
for exploring and proving inequalities of all kinds. I read about two thirds of the
book, and it deepened my appreciation for the beauty of the analytical style of
doing math.
6. The Fourier Transform and its Applications, Brad Osgood. This is the text I learned
Fourier analysis from, along with Osgood’s excellent online lecture videos when
he taught this course at Stanford University. Very entertaining, and I particularly
enjoyed how Osgood (a mathematician) does not sidestep the mathematical cor-
rectness often eschewed in physics books.
2. Linear Algebra and Its Applications, Gilbert Strang. Strang is the author of a number
of classic and authoritative texts on linear algebra. Opposite to Axler’s text, this
one focuses heavily on practical aspects of matrix computations, including matrix
decompositions and determinants, at the expense of theoretical foundations.
4. Quantum Algorithms via Linear Algebra, Richard Lipton and Kenneth Regan. A
self-contained approach to quantum computing algorithms, relying only on linear
algebra background knowledge. Includes proofs of all the amazing results on fac-
toring and searching.
1
I have yet to find a textbook introducing differential equations that I like. That being said, Gian-Carlo Rota’s
essay “Ten Things I Wish I Had Learned Before I Started Teaching Differential Equations” may help you
wade through the sea of mediocre books.
377
C.6 Optimization
1. Combinatorial Optimization, William Cook, William Cunningham, William Pulley-
blank, Alexander Schrijver. A dense, but authoritative reference on the classical
combinatorial optimization algorithms, including matchings, matroids, flow prob-
lems, and traveling salesman. Includes a self-contained appendix introducing lin-
ear programming, and frames linear programming as a central workhorse of many
important problems.
2. Abstract Algebra, David Dummitt, Richard Foote. A revered classic, but very terse
and also very oriented toward the applications of abstract algebra in pure mathe-
matics. Legend has it one of the two authors (I forget which) sprinkled the text
with puns and jokes, and the other author insisted on removing them. The only
jokes that remain were the ones that were too subtle to be detected.
3. Algebra, Michael Artin. A denser book, but one which approaches abstract algebra
using linear algebra as the unifying representation. As an undergraduate I spent a
lot of time working through parts of this book as self-study, and found it got me
into the mindset of filling in the gaps left by authors.
advanced topics. This was my first-year graduate Algebra book, and it demystified
category theory for me while also acting as a synthesizing text for my disparate
algebra knowledge. It leans toward commutative algebra and algebraic geometry.
It is written in an engaging and welcoming style, but is otherwise a difficult text.
6. Permutation Groups, John Dixon, Brian Mortimer. A graduate level text discussing
permutation groups as a proxy for all groups. Focuses on concrete representations
and touches on algorithmic aspects.
C.8 Topology
Topology is a very heavy subject, but it is fun to think about. I’d recommend not diving
into a standard reference of point-set topology (Munkres) until you feel comfortable with
set theory, calculus (Chapter 8), and standard proof techniques.
1. The Shape of Space, Jeffrey Weeks. A light introduction to topology, with pictures,
motivation, and lots of exercises. I have not read it, but it has been recommended
to me.
2. Introduction to Topology, Theodore Gamelin, Robert Greene. The (very cheap) text
I originally learned point-set topology from. Concise and with lots of exercises.
3. Topology, James Munkres. The gold standard topology book aimed at math under-
graduates. I have used it as a reference.
4. Algebraic Topology, Allen Hatcher. The gold standard for an advanced subfield of
topology whose task is to use group and ring structures to compute interesting
properties of topological spaces. I studied this book in detail as a graduate student.
Not for the faint of heart.
6. Computational Geometry, Mark de Berg, Otfried Cheong, Marc van Kreveld, Mark
Overmars. A text that covers the algorithmic questions around geometry, such as
computing convex hulls, triangulations, and Voronoi diagrams. A great introduc-
tion to the challenges of graphics programming. Includes a nice introduction to
linear programming, and focuses heavily on efficient data structures and numeri-
cal stability. I studied this text in depth as a first-year graduate student.
7. Course Lecture Notes. Most CS Theory groups at universities post excellent lec-
ture notes. In particular, many universities teach a course along the lines of “A
Theorist’s Toolkit,” that introduces the practical and provable results they want all
graduate students to know, from error correcting codes to basic machine learn-
ing techniques and linear programming. For example, Ryan O’Donnell’s course
at CMU, Sanjeev Arora’s at Princeton, and Tim Roughgarden and Greg Valiant’s
course at Stanford. All are wonderful springboards to learning about new exciting
topics.
2. My Best Mathematical and Logic Puzzles, Martin Gardner. Another book of puzzles
from the most prominent luminary of recreational mathematics.
3. Charming Proofs, A Journey into Elegant Mathematics, Claudi Alsina, Roger Nelsen.
A volume full of aesthetic proofs from all over mathematics, with each proof being
roughly 2 pages long.
5. The Harmony of the World, Gerald Alexanderson (Ed). A selection of the editor’s
favorite articles from the 75 year history of Mathematics Magazine.
6. The Best Writing on Mathematics Ed. Mircea Pitici. An annual anthology of the
year’s best mathematics writing.
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.
381
Index
383
384