node99.org | cv

arnold's cat map.

The cat map is a classic example of a 2D chaotic map, taught in introductory dynamical systems courses. Mathematically it is a toral automorphism, described by the following iterated function:

\left( \begin{array}{c} x_{n+1} \\ y_{n+1} \end{array} \right) = \left( \begin{array}{cc} 2 & 1 \\ 1 & 1 \end{array} \right) \left( \begin{array}{c} x_n \\ y_n \end{array} \right) \;\mathrm{mod}\;1

One way to visualize the map is by applying it to an image, treating the image as a grid of pixels and mapping each pixel to a new location on the grid:

Visualizing the cat map
(courtesy Jim Crutchfield)

The mapping is periodic, meaning each pixel will eventually map back to its original location.

Code

Processing is a Java meta-language that provides a simple API for working with multimedia. The following code loads an image of a cat and displays each iteration of the mapping in realtime, until the pixels map back to their original location. By changing the recordMovie flag, the output can be written to a Quicktime movie. The input image should have equal width and height.
import processing.video.*;

PImage img, imgOrig;
MovieMaker mm;	
boolean recordMovie = true;
int iteration = 0;

void setup()
{
  imgOrig = loadImage("puffball.png");
  img = imgOrig;
  size(img.width, img.height, P2D);
		
  if(recordMovie)
  {
    mm = new MovieMaker(this, img.width, img.height, "catmap.mov");
  }
}

boolean equal(PImage p1, PImage p2)
{
  for(int i = 0; i < p1.pixels.length; i++)
  {
    if(p1.pixels[i] != p2.pixels[i]) return false;
  }
  
  return true;
}

void draw()
{
  image(img, 0, 0);	
  
  if(recordMovie) mm.addFrame();

  if(equal(img, imgOrig) && iteration > 0)
  {
    noLoop();
    if(recordMovie) mm.finish();
  }
  
  PImage imgNext = createImage(img.width, img.height, RGB);
  		
  for(int y = 0; y < img.height; y++)
  {
    for(int x = 0; x < img.width; x++)
    {
      int pixel = img.pixels[y*img.width + x];
      int xNew = (2*x + y) % img.width;
      int yNew = (x + y) % img.height;
      imgNext.pixels[yNew*img.width + xNew] = pixel;
    }
  }
  
  img = imgNext;
  iteration++;
}

Video

Links

Download the Processing sketch.
Download the cat image referenced in the sketch.
Jim Crutchfield's nonlinear physics course at UCDavis.
Arnold's cat map at Wolfram and Wikipedia.