Today I received a very nice package from the AMS — a complete color printout of a draft of the book!  This means it’s time to proofread, and think of all the things I should have done along the way.

So here is a retrospective look at what I should have done.

1.  Created a stylesheet as I was writing.  For example, when abbreviating circa (as in Euclid lived c.300 BCE), is there a space as in “c. 300” or not as in “c.300”.  I’ll look it up… but my life would have been easier if I had kept a running list of style details for consistency throughout the manuscript.  Credit goes to Ellen Muehlberger for this advice — when I told my wife about this problem, she said that her advisor (E.M.) told her to maintain such a stylesheet throughout her dissertation.  Great advice!
2. Used a version control system throughout.  I just took the Udacity course on Git and GitHub yesterday (better late than never).  It’s an excellent quick class, and I’ll be using GitHub for producing a webpage for the book, sharing teaching materials, etc..  Unfortunately my current book files are organized like my camping gear… randomly stashed in large not-quite-clean tubs.
3. Considered CMYK color issues from the outset.  I had read and heard about color issues before, but didn’t do anything about it.  So now I have a large book, and of course the colors look great on a monitor (in RGB) and varying stages of terrible in print (in CMYK).  Fortunately, I have been using xcolor throughout, which gives me precise control over colors in the CMYK (or any other) colorspace.  I was a bit surprised at how different things ended up in print.  For example, a standard red  corresponds to 0% Cyan, 100% Magenta, 100% Yellow, 0% Black in CMYK space.  I wanted to desaturate the red when filling large areas; the xcolor setting red!50 yields the result 0% Cyan, 50% Magenta, 50% Yellow, 0% Black, which makes sense.  But on paper, this looks distinctly orange, in a sort of carrot-left-in-the-freezer-too-long way.  Similarly blue turns pale purple as blue!50.  The solution is fun, if a bit time-consuming.  I printed a CMYK color chart which I found via stackexchange, asking the local print shop to use their nicest inkjet and book-quality paper.  I’m using this printed color chart to choose my colors now.  So, instead of using a command like blue!50, I’ll define my own color (blueB perhaps) as 60% Cyan, 10% Magenta, 0% Yellow, 7% Black, and use this wherever I used blue!50 before.  I’ll probably give the local print shop some business with color experiments over the next month.  There’s really no way for me to match what the eventual offset printer will do, but I’m hoping to get as close as possible.

Prime distribution, before and after

In the Illustrated Theory of Numbers, the pictures serve different purposes.  Some lend geometric insight to proofs.  Others display logical flow.  Others render an abstract concept.  This post is about those which are data visualizations.

Using the term “data visualization” automatically increases web traffic, but I’m not just doing it for the hits.  Instead, I think it’s time that the best data visualization practices are directed towards the most interesting data in number theory:  the prime numbers being the prime example.  While the data visualization community typically studies people and places and money and the natural world, the Illustrated Theory of Numbers gets its data from numbers themselves.  It is one of the fascinating things about number theory that the data is entirely deterministic while at the same time obeying heuristics for random variables.

In this spirit, I’ve provided two drafts of a data visualization below, displaying the distribution of prime numbers up to 5 million.  I’ll explain the editing process that led me from left to right.

My goal in this image is to provide the reader with a sense of the microscopic irregularity and the macroscopic regularity of the prime numbers.  In the left column, the prime numbers are thick bars (10 points, I think).  Each column displays a range of prime numbers:  the first displays primes up to 50, the second the primes up to 500, etc..  The rightmost column displays the primes up to 5 million.  In some ways, this is the simplest kind of data set — a one-dimensional distribution.

From the beginning, I decided on this basic layout of columns, so that by the rightmost column the image would appear smooth, and gradually getting lighter towards the top as the primes spread out.  The numbers which represent primes on the far left are replaced by densities on the far right.  A number near 5 million has about a 6.5% chance of being prime.

I made a lot of changes to this image, starting with the draft on the left (from a few years ago) and ending at the draft on the right (a few weeks ago).  First, I pushed the prime number labels onto the bars.  There might be some printing/clarity risks with white text on black bars, but it gets across the idea that the bars are the prime numbers and it reduces the chance of confusion that the same “ticks” apply to all columns.

In that spirit, I narrowed and separated the columns.  This, I think, lightens the whole page, saves ink, and increases clarity.  The red lines now indicate how each column is effectively contained in a tenth of the column to its right.  I’ll admit there’s a bit of influence from the cover of Tufte’s Visual Display of Quantitative Information, though the subject matter is completely different.  I hope the red lines also break the tendency to scan directly left-to-right, and indicate how data is squeezed into shorter intervals.

Also lightening the page, I changed the shading in columns 3-6.  In the first two columns, solid black bars are used to represent prime numbers.  But in columns 3-6, a shade of gray is used according to the density of primes in each bin.  Among the numbers between 4000 and 4499, there are 60 prime numbers.  Since 60/500 = 12%, I used a line segment at 12% black in the later draft.  (With TikZ, this is accomplished by setting the color to black!12).

At first I was concerned that this would be too light, and I’ll see how it all looks when it’s printed professionally.  But on the Ricoh printers here, the result looks good — even at 6.5% black (the density around 5 million), the gray is easily distinguishable from the white paper.  And this fits with the principle of “smallest effective difference” described in Tufte’s Visual Explanations.  It’s a bit hard (though not impossible) to see the primes spread out, as their density goes from 8.5% to 6.5% in the rightmost column.  But that’s also part of the honest representation — it would be dishonest to the data to exaggerate the image to make the primes appear to spread out more quickly.  The table of densities at the far right exhibits the gradual spreading unambiguously with numbers.

A note to the reader — the images tend to render with horizontal stripes on a computer monitor!  Another reminder to print on a regular basis.

There’s probably a bit more tuning to do before publication.  The primes deserve the effort.

Determinants and Pick’s theorem, in color.

Like most blogs, the Illustrated Theory of Numbers blog went on a long hiatus.  Unlike most blogs, this one has returned!  Really, there has not been too much to show over the past few months, but I’ve gotten back to work on Part II of the book, covering binary quadratic forms (including all discriminants, Pell-like equations, a bit on SQUFOF factorization perhaps, the class number and the “Siegel bound”).  This might strike the experts as a bit out of order — where is modular arithmetic already? — but I’m sticking with “global” number theory until Part III of the book.  Of course, some readers might skip Part II and go directly to Part III, but I hope they will return to Part II to learn Conway’s beautiful “topographic” approach to binary quadratic forms.

I was heavily influenced by Conway’s visual approach to binary quadratic forms, found in Chapter 1 of “The (sensual) Quadratic Form.”  It’s amazing how far you can go with his topographs.  I think I can go through reduction theory, symmetries (a.k.a. orthogonal groups), and finiteness of the class number, without ever needing to multiply two matrices.  As recent work of Savin and Bestvina illustrates, along with recent work of my PhD student Chris Shelley, Conway’s approach generalizes in interesting ways to binary Hermitian forms and beyond.

One thing I use often in Part II is the determinant.  Fortunately, for two-by-two determinants, the geometry is relatively simple.  Below is a two-page spread introducing the geometric interpretation of the determinant.  A helpful bonus, presented in parallel, is the discrete version: Pick’s theorem for lattice parallelograms.

From mathematical and design perspectives, I like the idea of presenting parallel proofs in visual parallel on opposite pages.  The left displays a theorem in continuous Cartesian geometry; the right displays a theorem in discrete geometry.  The idea of dissection is the same, but the discrete version requires a bit more care.

I’m not exactly sure what to call the theorem on the right side of the page.  It certainly falls under the purview of Pick’s Theorem but really, someone must have proved it before Pick, at least for parallelograms!  I wouldn’t be surprised to see it in the work of Gauss or Eisenstein, if not earlier. Unfortunately, my German is not so good (nicht so gut?), though I can recognize the frequency of “Gitterpunkt” (grid-point) and “Gitterpolygon” (polygon with vertices on grid-points) in 19th century sources.  Any reader who can find an earlier reference for Pick’s theorem, even for parallelograms (rectangles don’t count on their own!) gets an acknowledgment in the book!

The utility of Pick’s Theorem is the following — it gives a cute geometric proof of the following fact well-known to algebraists:  A pair $(a,b)$ and $(c,d)$ of integer vectors forms an integer basis of $Z^2$ if and only if $ad - bc = \pm 1$.  Indeed, a grid-parallelogram of area one cannot cover any grid-points except its corners, by Pick’s Theorem.  This avoids any mention of matrix inversion, for example.

From a design perspective, this two-page spread was a lot of fun (and a bit of work).  A combination of \foreach and scoped \clip commands in TikZ allowed for the easy creation of dot-textures on the right page.  Perhaps the toughest decision (and one that isn’t final) was the choice of four colors; they are called “pinkish,” “blueish”, “greenish”, and “orangeish” in my source file.  Following some technical color-theory advice, I worked with a color tetrad — literally a rectangular arrangement in the HSV color wheel, converted for LaTeX via the xcolor package.  Analogous regions, such as the triangles, are in the nearby colors blue and green.  Less saturated colors are on the left page, where there are large regions of solid color.  Fully saturated colors are on the right page, where the colors are in small dots.  The real test of color will come when I print this out, along with another half-dozen copies with other rectangular tetrads of color, and see what looks the best.

Generalization with purpose

Modern number theory begins with the integers, all those whole numbers, positive, negative, and zero. A few times, when teaching number theory, I began simultaneously with the integers, the Gaussian integers, and the Eisenstein integers. One pedagogical reason is this: if you spend the first few weeks of number theory class proving things that students “learned” by fifth grade, then students will not likely be excited. The Euclidean algorithm can excite students, but proving the existence and uniqueness of prime decomposition is — from the students’ perspective — proving something they already (think they) know. Number theory teachers can get flustered by this. “No!” they say, “You only think you know this! It needs to be proven. It’s really not trivial! Really! Pay attention!”

I think there are two important roles for the instructor at this stage. One is to guide the student to wiser ignorance, as in Plato’s Meno, where Socrates says “Is he not better off in knowing his ignorance?”.  To this end, the instructor gives counterexamples, where irreducible elements $p$ (those that cannot be factored further) are not prime (do not satisfy the implication $p \vert ab \Rightarrow p \vert a$ or $p \vert b$). The first counterexample that I learned was in the ring $Z[\sqrt{-5}]$ in which $(1 + \sqrt{-5}) \cdot (1 - \sqrt{-5}) = 2 \cdot 3$. The element $2$ is irreducible but not prime. I think this counterexample is natural to the number theorist and instructor — having a prior background in quadratic rings — but most unusual to the elementary number theory student.  It also leaves the instructor having to convince the student that $1 \pm \sqrt{-5}$ and $2$ and $3$ are irreducible.  That is probably too difficult for the beginning student.  A more elementary example occurs in Hilbert’s monoid $\{ 1,5,9,13,17,\ldots \}$ of natural numbers congruent to one modulo $4$. In this monoid (under multiplication), 9 is irreducible. On the other hand, $9 \vert 21 \cdot 21$ but 9 does not divide 21. Another way to say this is that 441 factors into irreducible in two ways:  $9 \cdot 49$ and $21 \cdot 21$.  So this Hilbert monoid might break the student’s false intuition that irreducible elements are prime, and that factorization must be unique.

Besides leading the student to wiser ignorance, the instructor should motivate the proof of existence and uniqueness of prime decomposition by demonstrating its application to number systems beyond the integers.  To this end, one strategy is to simultaneously teach students the basic arithmetic of integers, Gaussian integers, and Eisenstein integers. I think students appreciate the details of proofs when they are proving something they don’t already “know”. Only those already devoted to pure mathematics will appreciate a proof of prior “knowledge”.

I have devoted Chapter 5 to a study of Gaussian and Eisenstein integers. I hope that some instructors will follow this chapter as they go through Chapters 2 and 3 to pique the interest of students. It is not technically necessary for what comes later, but Gaussian and Eisenstein integers are beautiful and important in their own right. I have always liked their kaleidoscopic symmetry, so I devoted an entire two-page spread to visualizations of Gaussian and Eisenstein primes.

The pictures display the ramified, inert, and split primes in different colors, and the inert primes are displayed below on a number line to provide a scale. A fundamental domain is highlighted to exhibit the kaleidoscopic symmetry. Symmetry will reappear in the study of orthogonal groups of binary quadratic forms later, so it is good to see a few examples here.  Hecke proved equidistribution of prime angles in the Gaussian context (and much more), in efforts to prove the infinitude of primes of the form $x^2 + 1$. Thus I have drawn attention to the prime angles (with tick marks on the circumference) and the primes of the form $x^2 + 1$ (with parallel horizontal lines across the Gaussian plane).  Analogously, I have drawn attention to the primes of the form $x^2 - x + 1$ in the diagram of Eisenstein integers.

Why else study Gaussian and Eisenstein integers?  Some students think that generalization is a worthy goal for its own sake, but I prefer generalization with purpose.  In Chapter 5, we use properties of Gaussian and Eisenstein primes to study properties of ordinary primes (infinitude of primes congruent to one modulo three and modulo four, and relations to primes of the form $x^2 + 1$).  One “big picture” lesson of this book is that the study of larger and stranger number systems sheds light on the natural numbers.  Exhibit G:  The Gaussian Integers.  Exhibit E:  The Eisenstein Integers.  Later on, quadratic rings, and the rings of modular arithmetic.

Why is the treatment of Gaussian and Eisenstein integers delayed until Chapter 5, instead of following my pedagogical advice to include them in the teaching of prime decomposition?  There are two reasons.  First, for shorter courses, I can imagine an instructor skipping Chapter 5.  Second, Gaussian and Eisenstein integers connect the first part of the book (foundational properties of integers and rational numbers) to the second part of the book (on binary quadratic forms).  By Chapter 7, we will study definite binary quadratic forms, and the Gaussian and Eisenstein integers will reappear in relation to the unique forms of discriminants $-3$ and $-4$, respectively.

Colorful programming

A few months ago, I signed up for CS 101 at Udacity.com.  This provided a wonderful introduction to the Python programming language.  I was a pretty good Turbo Pascal programmer in high-school (early 1990s), but my programming since then had only involved short snippets in R (for statistics), SAGE (for research mathematics), and a bit of experimentation in javascript.  With Python, I finally feel like I can playfully program again.   I don’t have to worry about declaring various classes and main() things to control window objects.  Programming is fun and simple again, even if I’m only using basic functional programming constructs.

Beyond the fun, Python is useful to me in two ways. First, SAGE is wisely built upon Python, so by learning Python I can do much more in SAGE. Second, by using a Python script to output TikZ code (nothing fancy – just some string manipulations), I can create data-dense PDF graphics with mathematical precision and quality typography and color.  In other words, I have control over the graphics in my book.  Any ugliness or error is my own. An example of a graphic created with a Python script, TikZ, and LaTex, is the opening graphic for Chapter 3 found below. The Sieve of Eratosthenese is visualized by different color-filters for each prime; multiples of 2 (starting with 4) are sieved with a red line, multiples of 3 with a blue line, etc.. The white-space remaining is prime. But to see the resulting primes at the end, I switch to black-and-white, and reverse the white-space of the primes to thin black lines for the primes. This allows the reader to see the distribution of primes between 2 and 577, irregular jumps and all. To label the vertical axis, I have minimized ink by placing labels only at primes; there is no use labeling 100, 200, 300, or placing additional tick marks, when the primes themselves can be used for the same purpose.  Some fine-tuning might still be in order.

Now that I’ve become a Python advocate, I decided to go one step further and give Python code snippets throughout the Illustrated Theory of Numbers. You can see one in the margin on the right page. Inserting these snippets of code raises a lot of natural questions.

Why insert Python code instead of some universal pseudo-code? I am actively advocating Python, since I think the reader can benefit from learning Python, especially for future work in SAGE. Python is freely available and cross-platform. A C++ or Java programmer should be able to read Python code and translate easily enough into his or her native language.  Also, Python is surprisingly fast for an interpreted language!

Why insert code at all? Why not just make a download freely available? I plan to eventually post all code in the book, fully commented and a bit expanded (for exception-handling). This way, readers will be able to download the python script, import it, and compute to their heart’s content. But personally, I find something satisfying about personally typing short snippets of code and then clicking RUN. Maybe it reminds me of programming 20 years ago. It might be an issue of pacing. I like writing on the chalkboard as I lecture, because it sets a deliberate thoughtful pace. When I type in a code snippet myself, I think about each line and why it works. I hope readers can enjoy typing and modifying the short (5-15 lines) code snippets on their own.

Don’t existing software packages work better? The answer is certainly yes. I have not given code snippets which are perfectly optimized. I have aimed for a balance between readability and speed. Hopefully a reader can learn Python by experimenting with these snippets, and a reader with programming background can understand exactly how the snippets work. Optimizing code requires a sacrifice in clarity, or much more space for explanation. On the other hand, I have tried to give code that works well in practice, for a student of number theory. You’re not going to factor 50-digit numbers with the included code, but 10-digit numbers should factor very quickly. The included sieve code makes a list of prime numbers up to 1 million in 0.07 seconds on my MacBook Pro. This should be a good start for students, and if they want more power they can use PARI and SAGE, or maybe NZMath if they want pure Python.

A final word about the code-snippets: I still have not figured out how to properly attribute them. The Sieve of Eratosthenes snippet on the 2-page spread above is based on code posted by Robert William Hanks to StackOverflow on July 27, 2010. I will be asking the publisher for suggestions on how to make code citations in a wildly open-source world, and I appreciate any suggestions along the way.

Most authors of math texts do not design their books with two-page spreads in mind. Typically, an author types in LaTex, and trusts LaTex to handle all the formatting, page layout, etc.. But for an illustrated book, I care about page layout. I want the reader to open the book to any page, and find the content on the left complementary to the content on the right.

A few software innovations have made the illustration and layout of this book possible and free. One is the Tufte-LaTex package, which imitates the layout of Tufte’s books, especially the spacious margins for sidenotes. Another is the combination of PGF and TikZ for illustrations within LaTex. This package outputs to beautiful PDF, with color control in the CMYK colorspace. Moreover, using basic programming constructs within PGF/TikZ (and sometimes using personal Python scripts to output TikZ code), one can precisely place dots and weighted lines as necessary for mathematical figures, and integrate these figures seamlessly with text.

Below is a two-page spread (click to enlarge!) from the first chapter of the book, in which I count pairs in the set $\{1,2,3,4,5,6,7,8,9,10\}$.

The figure on the right-page exhibits what Tufte calls “small multiples”. There are 45 possible 2-element subsets of $\{1,2,3,4,5,6,7,8,9,10\}$. To illustrate the connection between 2-element subsets and points in a triangular arrangement, I produced a picture of all 45 subsets! Each small illustration (small triangle) fits into a large triangle, and the location of a small triangle in the large triangle also matches the location of a blue dot within the small triangle. The reader is meant to connect Figure 1.4 on the left page to the large figure on the right page, as the blue dots, red dots, and blue lines correspond (after a bit of rotation and folding).