Numpy et fractale de Mandelbrot
Bonjour à toutes et à tous et bonne année
J'ai trouvé dans le livre "Numpy cookbook" le code suivant pour dessiner l'ensemble de Mandelbrot.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| # -*- coding: utf-8 -*-
import numpy
import matplotlib.pyplot
import sys
if(len(sys.argv) != 2):
print "Please input the number of iterations for the fractal"
sys.exit()
ITERATIONS = int(sys.argv[1])
SIZE=1024
MAX_COLOR = 255.
x_min, x_max = -2.5, 1
y_min, y_max = -1.75, 1.75
# Initialize arrays
x, y = numpy.meshgrid(numpy.linspace
(x_min, x_max, SIZE),
numpy.linspace(y_min, y_max, SIZE))
c = x + 1j * y
z = c.copy()
fractal = numpy.zeros(z.shape,
dtype=numpy.uint8) + MAX_COLOR
# Generate fractal
for n in range(ITERATIONS):
# print n
mask = numpy.abs(z) <= 4
z[mask] = z[mask] ** 2 + c[mask]
fractal[(fractal == MAX_COLOR) &
(-mask)] = (MAX_COLOR - 1) * n / ITERATIONS
# Display the fractal
matplotlib.pyplot.imshow(fractal)
matplotlib.pyplot.title('Mandelbrot')
matplotlib.pyplot.axis('on')
matplotlib.pyplot.colorbar()
matplotlib.pyplot.axis('off')
matplotlib.pyplot.title('Mandelbrot')
matplotlib.pyplot.show() |
Ce code fonctionne parfaitement et me semble très efficace mais j'ai un gros problème de compréhension de la boucle for et en particulier des lignes 28 et 29.
Si une âme charitable et experte pouvait m'expliquer je l'en remercie par avance.