Tuesday, 26 August 2014

The Complex Plane And Plottable Arrays

Some readers have asked me to explain the slightly complicated translation between the complex plane (where the Mandelbrot set really lives) and the Python arrays used to plot the images.

The reason for the complexity is that:
  • The complex plane is continuous, just like the real number line.
  • Python arrays are discrete, filled with finite boxes. 
  • What's more, the elements of python arrays are labelled using integers starting from zero. You can have array[2, 3] but not array[-2.34, +4.3398]. 
  • We have to plot arrays, even though we really want to see the complex plane. This is the core reason we need to translate between the complex number plane world, and the python array world.

The translation itself is simple. We divide up the complex plane into equally spaced and sized sections. There are an integer number of these - and so they can be represented by the elements of an array.

So when we choose an element n out of N along a section which started at x1 and ended ay x2, the element n corresponds to x1 + (x2-x1)*(n/(N-1)). You can see here that n/N is the proportion between x1 and x2 that n lies.

If this expression looks complicated to you - it's just working out how far from x1 towards x2 we need to go in the same proportion as n out of N pieces.

Ah - and don't forget n starts at 0 and ends at N-1, which makes sense so that when n=0 we have the position x1. Similarly when n is the last N-1,  it corresponds to x2.

The following diagram shows our explanation visually: (click to enlarge)