|
| 1 | +"""Author Alexandre De Zotti |
| 2 | + |
| 3 | +Draws Julia sets of quadratic polynomials and exponential maps. More |
| 4 | + specifically, this iterates the function a fixed number of times then plot |
| 5 | + the absolute value of the last iterate and whether the absolute value of the |
| 6 | + last iterate is greater than a fixed threshold (named "escape radius"). For |
| 7 | + the exponential map this is not really an escape radius but rather a |
| 8 | + convenient way to approximate the Julia set with bounded orbits. |
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | +import numpy |
| 13 | +from matplotlib import pyplot |
| 14 | + |
| 15 | +c_polynomial = 0.25 + 0.0j |
| 16 | +c_exponential = -2.0 |
| 17 | +nb_iterations = 56 |
| 18 | +window_size = 2.0 |
| 19 | +nb_pixels = 666 |
| 20 | + |
| 21 | + |
| 22 | +def eval_exponential(c, z): |
| 23 | + """ |
| 24 | + >>> eval_exponential(0, 0) |
| 25 | + 1.0 |
| 26 | + """ |
| 27 | + return numpy.exp(z) + c |
| 28 | + |
| 29 | + |
| 30 | +def eval_quadratic_polynomial(c, z): |
| 31 | + """ |
| 32 | + >>> eval_quadratic_polynomial(0, 2) |
| 33 | + 4 |
| 34 | + >>> eval_quadratic_polynomial(-1, 1) |
| 35 | + 0 |
| 36 | + >>> eval_quadratic_polynomial(1.j, 0) |
| 37 | + 1j |
| 38 | + """ |
| 39 | + return z * z + c |
| 40 | + |
| 41 | + |
| 42 | +def prepare_grid(window_size, nb_pixels) -> numpy.array: |
| 43 | + """ |
| 44 | + Create a grid of complex values of size nb_pixels*nb_pixels with real and |
| 45 | + imaginary parts ranging from -window_size to window_size (inclusive). |
| 46 | + Returns a numpy array. |
| 47 | + |
| 48 | + >>> prepare_grid(1,3) |
| 49 | + array([[-1.-1.j, -1.+0.j, -1.+1.j], |
| 50 | + [ 0.-1.j, 0.+0.j, 0.+1.j], |
| 51 | + [ 1.-1.j, 1.+0.j, 1.+1.j]]) |
| 52 | + """ |
| 53 | + x = numpy.linspace(-window_size, window_size, nb_pixels) |
| 54 | + x = x.reshape((nb_pixels, 1)) |
| 55 | + y = numpy.linspace(-window_size, window_size, nb_pixels) |
| 56 | + y = y.reshape((1, nb_pixels)) |
| 57 | + return x + 1.0j * y |
| 58 | + |
| 59 | + |
| 60 | +def iterate_function(eval_function, function_params, nb_iterations, z_0): |
| 61 | + """ |
| 62 | + Iterate the function "eval_function" exactly nb_iterations times. |
| 63 | + The first argument of the function is a parameter which is contained in |
| 64 | + function_params. The variable z_0 is an array that contains the initial |
| 65 | + values to iterate from. |
| 66 | + This function returns the final iterates. |
| 67 | + |
| 68 | + >>> iterate_function(eval_quadratic_polynomial, 0, 3, numpy.array([0,1,2])) |
| 69 | + array([ 0, 1, 256]) |
| 70 | + """ |
| 71 | + |
| 72 | + z_n = z_0 |
| 73 | + for i in range(nb_iterations): |
| 74 | + z_n = eval_function(function_params, z_n) |
| 75 | + return z_n |
| 76 | + |
| 77 | + |
| 78 | +def show_results(function_label, function_params, escape_radius, z_final): |
| 79 | + """ |
| 80 | + Plots the absolute value of z_final as well as whether it is greater than |
| 81 | + the value of escape_radius. Adds the function_label and function_params to |
| 82 | + the title. |
| 83 | + """ |
| 84 | + |
| 85 | + abs_z_final = (abs(z_final)).transpose() |
| 86 | + pyplot.matshow(abs_z_final) |
| 87 | + pyplot.title( |
| 88 | + f"Absolute value of last iterate\n{function_label}, c={function_params}" |
| 89 | + ) |
| 90 | + pyplot.colorbar() |
| 91 | + pyplot.show() |
| 92 | + pyplot.matshow(abs_z_final < escape_radius) |
| 93 | + pyplot.title(f"Escaped or not\n{function_label}, c={function_params}") |
| 94 | + pyplot.show() |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + |
| 99 | + z_0 = prepare_grid(window_size, nb_pixels) |
| 100 | + z_final = iterate_function( |
| 101 | + eval_quadratic_polynomial, c_polynomial, nb_iterations, z_0 |
| 102 | + ) |
| 103 | + escape_radius = 2 * abs(c_polynomial) + 1 |
| 104 | + show_results("z²+c", c_polynomial, escape_radius, z_final) |
| 105 | + |
| 106 | + z_final = iterate_function(eval_exponential, c_exponential, nb_iterations, z_0 + 2) |
| 107 | + escape_radius = 10000.0 |
| 108 | + show_results("exp(z)+c", c_exponential, escape_radius, z_final) |
0 commit comments