
Hello! I’m Peter!
Blog
-
How to Make Animated Math GIFs: LaTeX + TikZ
The first animated GIF that I ever made was made with the LaTeX package TikZ and the command line utility ImageMagick. In this post, I’ll give a quick example of how to make a simple GIF that works by layering images with transparent backgrounds on top of each other repeatedly.
TikZ code
In our first step toward making the above GIF, we’ll make a PDF where each frame contains one of the above circles on a transparent background. In this case, each circle is placed uniformly at random with its center in a \(10 \times 10\) box, with a radius in \([0,1]\), and whose color gets progressively more red and less green with a random amount of blue at each step.
\documentclass[tikz]{standalone} \usepackage{tikz} \begin{document} \foreach \red[evaluate=\red as \green using {255 - \red}] in {0,1,...,255} { \begin{tikzpicture} \useasboundingbox (0,0) rectangle (10,10); \pgfmathrandominteger{\blue}{0}{255} \definecolor{myColor}{RGB}{\red,\green,\blue} \fill[myColor] ({10*random()},{10*random()}) circle ({1+random()}); \end{tikzpicture} } \end{document}
Starting with
\documentclass[tikz]{standalone}
says to make eachtikzpicture
its own page in the resulting PDF.Next we loop through values of
\red
from0
to255
, each time setting\green
to be equal to255 - \red
so that with each step the amount of red goes up and the amount of green goes down.The command
\useasboundingbox (0,0) rectangle (10,10);
gives each frame a \(10 \times 10\) bounding box so that all of the frames are the same size and positioned the same way.The command
\pgfmathrandominteger{\blue}{0}{255}
chooses a random blue values between0
and255
.The command
\fill[myColor] ({10*random()},{10*random()}) circle ({1+random()});
places a circle with its center randomly chosen in the \(10 \times 10\) box and with a radius between \(1\) and \(2\).ImageMagick
When we compile this code, we get a PDF with one circle on each page. In order do turn this PDF into an animated GIF, we convert the PDF using ImageMagick, a powerful command-line utility for handling images. If we named our PDF
bubbles.pdf
then running the following command in the terminal (in the same directory asbubbles.pdf
) will give us an animated GIF calledbubbles.gif
.convert -density 300 -delay 8 -loop 0 -background white bubbles.pdf bubbles.gif
I hope that you use this blog post as a jumping off point for making animated GIFs of your own! If you do, please reach out to share them with me!
-
XOR Triangles
In this post, I’ll explore the math behind one of my Twitter bots, @xorTriangles. This bot was inspired by the MathOverflow question “Number triangle,” asked by user DSM posted in May 2020.
(I gave an overview of my Twitter bots @oeisTriangles in my post “Parity Bitmaps from the OEIS“. And if you want to build your own bot, I showed the steps for building @BotfonsNeedles in parts I, II, and III of my three-part series “A π-estimating Twitter bot”.)
In May 2020, MathOverflow user DSM posted a question “Number triangle” where they asked about the following construction:
Start with a list of \(n\) bits (\(0\)s and \(1\)s), and to get the next row, combine all adjacent pairs via the XOR operation, \(\oplus\). (A mathematician might call this “addition modulo \(2\).”) That is $$\begin{alignat*}{2} 0 \oplus 0 &= 1 \oplus 1 &&= 0 \quad\text{and} \\ 0 \oplus 1 &= 1 \oplus 0 &&= 1.\end{alignat*}$$
For example, starting with the row \(110101\), the resulting triangle is $$1~~~1~~~0~~~1~~~0~~~1\\ 0~~~1~~~1~~~1~~~1\\ 1~~~0~~~0~~~0\\ 1~~~0~~~0\\ 1~~~0\\ 1$$
We’re especially interested in understanding the triangles with rotationally symmetric boundaries (and consequently insides)! OEIS sequence A334556 enumerates all of the rotationally symmetric triangles with a \(1\) in the upper left corner.
@xorTriangles
My Twitter bot @xorTriangles posts these triangles four times a day, alternating between the triangles described above and a “mod \(3\)” analog. If you want to see how it works, you can check it out on Github.
Here is an illustration by Michael De Vlieger that shows many small XOR triangles and which is available on the OEIS.
Michael De Vlieger’s image on the OEIS.
My Twitter bot @xorTriangles currently follows the same convention.Mathematica
One way to find a rotationally symmetric XOR triangle is by setting up a system of equations. Using the convention that the top row of the triangle is labeled \(x_0, x_1, \dots, x_n\), we can use the Pascal’s Triangle-like construction to set up a system of linear equations over \(\mathbb{Z}/2\mathbb{Z}\). The solutions of this system of equations are precisely those that correspond to the XOR triangle we care about! This allows us to write down a matrix whose null space is the set of such triangles:
$$\begin{align}x_n &= x_0\\x_{n-1} &= x_0 + x_1\\x_{n-2} &= x_0 + 2x_1 + x_2\\ &\vdots \\ x_k &= \sum_{i=0}^{n-k} \binom{n-k}{i}x_i \\ &\vdots \\ x_0 &= \binom{n}{1}x_0 + \binom{n}{1}x_1 + \dots + \binom{n}{n}x_n\end{align}$$
Using Mathematica to find the null space works like this:
value[i_, j_, n_] := Binomial[i, j] - If[j == n - i, 1, 0]; xorSeedsBasis[n_] := NullSpace[ Table[value[i, j, n], {i, 0, n}, {j, 0, n}], Modulus -> 2 ];
Because this is the null space of a \(n+1 \times n+1\) matrix over \(\mathbb{Z}/2\mathbb{Z}\), we know that there must be \(2^M\) rotationally symmetric XOR triangles, where \(M\) is an integer—which isn’t obvious from the original construction!
Related OEIS sequences
If you’re interested in learning more about this construction, reach out to me or take a look at some of these related OEIS sequences:
- A334596: Number rotationally symmetric triangles of size \(n\) with \(1\)s in the corners.
- A334591: Largest triangle of \(0\)s in the XOR triangle generated by the binary expansion of \(n\).
- A334592: Total number of \(0\)s in the XOR triangle generated by the binary expansion of (n).
- A334593: Total number of \(1\)s in the XOR triangle generated by the binary expansion of \(n\).
- A334594: Binary interpretation of the \(k\)-th row of the XOR triangle generated by the binary expansion of \(n\).
- A334930: Numbers that generate rotationally symmetrical XOR-triangles featuring singleton zero bits in a hexagonal arrangement. (from Michael De Vlieger, see his illustration)
-
Robot Walks
I’ve gotten a lot of mathematical inspiration from Project Euler questions, but perhaps the question that has gotten me thinking the most is Project Euler Problem 208: Robot Walks. In this problem, a robot takes steps either to the right or the left, and at each step, it turns \(\frac 15\) of the way of a circle.
Project Euler Problem 208 Demo
I started thinking about this problem more seriously after I met Chase Meadors at the 2018 Graduate Student Combinatorics Conference and learned about his Javascript applet which allows a user to control a (virtual) robot 🤖 by using the left and right arrow keys. By repeating the same sequence of moves (e.g. \(4\) steps to the right followed by \(2\) steps to the left) I found that the robot traced out surprising symmetric patterns.
I cloned Chase’s Github Repo so that I could customize the robot further. If you go to the URL
https://peterkagey.github.io/project-euler-208/?n=8&w=3,2,5,1
you’ll see an example of a robot walk, wheren=8
means that each step will be \(\displaystyle \frac 18\) of a circle, andw=3,2,5,1
means that the robot will follow the pattern of \(3\) steps to the right followed by \(2\) steps to the left, followed by \(5\) steps to the right, followed by \(1\) step to the left, and repeating this pattern until it returns to where it began.Stack Exchange Questions
I’ve asked a number of questions on Math Stack Exchange (MSE) and Code Golf Stack Exchange (CGSE) about these problems.
- Non-self-intersecting “Robot Walks” (MSE, May 2018)
- Area enclosed by robot walk (MSE, May 2018)
- Points in \(\mathbb{R}^2\) that can be reached via steps which are \(1/5\) of a unit circle. (MSE, August 2019)
- Circular Robot Instructions (CGSE, November 2019)
Openish Problem Collection
I’ve also asked about this setup in my Openish Problem Collection in Problem 41 and in Problem 69.
@RobotWalks
Twice each day, my Twitter Bot @RobotWalks tweets a randomly generated Robot Walk cycle. Check out the Github code if you want to see how it works, and read my series on making a Twitter Bot if you want to make something like it for yourself.
Jessica’s Robot Walk Prints
I ordered some canvas prints of some numerologically significant walks for my friend Jessica, which she hung behind her TV. There’s no doubt that this caused her to form a deep mental association between me and The Good Place.
Some prints that I made for my friend Jessica What’s next?
Some day, I want to laser cut or 3D print some coasters based on these designs. Reach out to me if that’s something that you’re interested in, and we can do it together!