2

This is the code I've got so far:

import numpy as np
def mandelbrot(resol):

    R, I = np.meshgrid(np.linspace(-2,.5,resol), np.linspace(1.25j,-1.25j,resol))
    Z = R + I
    C = np.array(Z)

    iterations = np.zeros(Z.shape, dtype=np.uint8)
    escape_vals = np.zeros(Z.shape, dtype=np.complex128)

    for i in np.arange(MAXITERS):
        #only the complex values that haven't diverged get iterated
        _ = np.abs(Z) <= 2
        Z[_] *= Z[_]; Z[_] += C[_]

        #np.invert(_, _) just toggles all of the boolean values in _
        #So, wherever np.abs(Z) > 2...
        np.invert(_, _)
        iterations[_] = i
        escape_vals[_] = Z[_]

    #Take the last set of points np.abs(Z) > 2,
    #invert it; those are the points in the  Mandelbrot set.
    np.invert(_, _)
    iterations[_] = MAXITERS
    escape_vals[_] = Z[_]

    iterations += 2
    escape_vals *= escape_vals; escape_vals += C
    escape_vals *= escape_vals; escape_vals += C

    moduli = np.array(np.abs(escape_vals), np.float64)
    mus = np.array(iterations - np.log(np.log(moduli))/np.log(2))
    return mus

My problem is that I am getting modulus values between 0 and 1, and when I take the log of those twice I get nan. I am also not sure of the best way to map these values to my chosen color palette! Does anyone see a mistake I am making?

Broseph
  • 1,655
  • 1
  • 18
  • 38
  • Your code works fine for me ... those `nan`s seem to just be points that [aren't members of the set](http://mathworld.wolfram.com/MandelbrotSet.html). `plt.imshow`, for example, plots `nan` as white by default, but you can change this [by using a masked array](http://stackoverflow.com/a/2578873/1730674). – askewchan Nov 11 '13 at 00:18
  • @askewchan but if the modulus is small, doesn't that mean it is close to the axes? How are you so sure that the nans are not in the set? – Broseph Nov 11 '13 at 05:36
  • To be honest, I'm not that sure, but when I ran your code, the `nan`s were in the same place as [the black areas in this plot](https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Mandel_zoom_00_mandelbrot_set.jpg/322px-Mandel_zoom_00_mandelbrot_set.jpg) – askewchan Nov 11 '13 at 16:00
  • Oh. So they Are in the set :p . Not All of the black parts of that image should be nans. The black parts should be everything less than or equal to 2... But since I'm taking a double log they really need to be between 1 and 2 (1, 2]. Actually, since I don't want any of the final values to be obscenely negative, the escape radius needs to be the base of whatever log I am taking, and the value of the points in the set need to be that base. So I switch to log2 and set all of the points in the set at the end of the iterations equal to sqrt(2) + sqrt(2)j. Or 2 – Broseph Nov 11 '13 at 16:29

0 Answers0