%pylab inline

"""
Explanation: Mandelbrot Set
by Paulo Marques, 2013/09/22

This notebook shows how to implement and visualize the Mandelbrot Set in Python. The Mandelbrot set is a very well known and beautiful fractal discovered in the 70s. It was named after Benoit Mandelbrot, a well-known mathematician. The first images of this object were computed in 1978.

Let's do it in Python.

We start by importing some basic libraries:
End of explanation
"""


def mandel_pt(c, MAX_ITER=100):
    """Calculates the number of iterations for point <c> for the Mandelbrot set"""

    iter = 0
    z    = 0
    while (iter < MAX_ITER):
        z = z**2 + c
        iter+= 1
        if abs(z) > 2.0:
            break

    return iter if iter!=MAX_ITER else 0

"""
Explanation: The basic idea behind the Mandelbrot set is that you have a 2D plane that represents an image. Each point of that plane is a complex number $z = x + y i$. Now, suppose what happens if, for each point $c$ in the plane, you iterate over and over calculating a new number as follows:
$$ z_{n+1} \gets z_{n}^2 + c$$
In this formula $z_0$ is 0 and $c$ is the point in the plane you are looking at. 
In this situation, two different things can happen. As the number of iterations advances either $\|z\|$ tends to infinity or it tends to 0. Mathematicaly, points where it tends to zero, are part of the set and are colored black. Points that tend to infinity are not part of the set and are colored white. 
Since in a computer you cannot iterate an infinite number of times, typically you put a bound on how many times you do this calculation. Also, you can use the "how fast" $\|z\|$ is going to infinity to color the set much more nicely. You can also check if $z$ is going to infinity just by comparing $\|z\|$ with 2. If it has grown that big, it certainly is not part of the set.
So, for a point $c$, lets define a function mandel_pt() that tells us the number of iterations that point is taking to diverge to infinity. (Note: points that are truly inside the set will take MAX_ITER iterations. We will map those to zero as it makes for nicer colors.)
End of explanation
"""


def mandel(width, height, zmin, zmax):
    """Calculates the mandelbrot set for a certain image size (width, height) over a certain rectangle (zmin->zmax)"""

    # This function maps an (i,j) position of the screen to a complex number inside of the rectangle
    def map_pos(i, j):
        return zmin + j*(zmax-zmin).real/width + i*(zmax-zmin).imag/height*1j    
    
    # Now return the Mandelbrot set by mapping each point to the corresponding number of iterations
    return [[mandel_pt(map_pos(i, j)) for j in range(width)] for i in range(height)]

"""
Explanation: Now we want to define a function that for a rectangle of $width \times height$ pixels, representing the complex plane that goes from $z_{min}$ to $z_{max}$, calculates the corresponding image. I.e., we are going to map all the points in that rectangle to a color, using mandel_it(), returning the corresponding image:
End of explanation
"""


# Width and height of the image
(width, height) = (1024, 768)

# The complex plane to map
(zmin, zmax) = (-2.2-1.5j, +1.2+1.5j)

# The Mandelbrot set
mandel = mandel(width, height, zmin, zmax)

"""
Explanation: We are all set! Let's calculate it:
End of explanation
"""


figure(figsize=(16,10))
axis('off')
imshow(mandel)

"""
Explanation: The corresponding image:
End of explanation
"""


