
Arnold's Cat Map
by marimo team
Watch a rainbow grid scramble and reassemble under Arnold's cat map, with editable matrices.
About this notebook
This notebook shows Arnold's cat map on an N-by-N grid. Each tile sits at a coordinate (x, y). The map multiplies that coordinate by a 2-by-2 integer matrix and takes the result mod N, so every tile moves to a new cell inside the same grid. Repeat the map and the tiles scramble, then land back in their start positions. The number of steps before that return is the recurrence period, and it depends only on N, not on the picture. A numpy array holds the matrix and computes the period. A custom anywidget, built with anywidget and traitlets, draws the grid on a canvas and animates each step.
You drive the animation with four controls. A play switch starts and stops the motion. A speed slider sets the steps per second. A grid-size slider sets N, which is also the modulus. An editable mo.ui.matrix holds the four integer entries of matrix A, and it starts at the classic [[2, 1], [1, 1]]. When you change the matrix, the notebook recomputes the period for that exact matrix and modulus. If you enter a matrix whose determinant shares a factor with N, the map is not invertible and cannot reassemble the grid. In that case the widget reports the map will not return home instead of showing a period.
The widget splits each step into two visible phases. First it applies the pure matrix multiply as a stretch, with no wrap. Then it applies the mod as a fold, which slides the overhang back into the box by a multiple of N. The canvas auto-frames both phases. It zooms out during the stretch and back in during the fold, so wrapped tiles slide in from the neighboring copies of the grid and you see where each folded piece comes from.











