Code Mandelbrot set

Asked

Viewed 69 times

0

I am studying a specific code of the set of Mandelbrot (equation with complex numbers) with a focus on object-oriented programming. However, there is a specific part I did not understand of the code, as below:


columns = 2000

rows = 2000

result = numpy.zeros([rows, columns])

for row_index, Re in enumerate(numpy.linspace(-2, 1, num=rows)):

   for column_index, Im in enumerate (numpy.linspace(-1, 1, num=columns)):

        result[row_index, column_index] = mset(Re, Im, 200)

Below, follow the full code:

import numpy

from numba import autojit

import matplotlib.pyplot as plt

@autojit

def mset (Re, Im, max_inter):

        c = complex (Re, Im)
        z = 0

        for i in range (max_inter):
            z = z*z + c
            if (z.real*z.real + z.imag*z.imag) >= 4:
                return i
        return max_inter
columns = 2000

rows = 2000

result = numpy.zeros([rows, columns])

for row_index, Re in enumerate(numpy.linspace(-2, 1, num=rows)):

    for column_index, Im in enumerate (numpy.linspace(-1, 1, num=columns)):

        result[row_index, column_index] = mset(Re, Im, 200)

plt.figure(dpi=100)

plt.imshow(result.T, cmap='hot', interpolation='bilinear', extent=[-2, 1, -1, 1])

plt.xlabel('Real')

plt.ylabel('imagin')

plt.show()
  • Greetings, Vanderson; we’re on [en.so], so could you translate your question?

1 answer

0

The code highlighted in the question traverses a space in the complex plane to verify whether or not each point belongs to the Mandelbrot set.

Initially, the code defines a array to store the test result of each point in the plane (in question, set to size 2000x2000):

result = numpy.zeros([rows, columns])

In the sequence, there are 2 loopings that travel throughout space (real x imaginary).

The range is defined by the function linspace(), that generates num numbers within that range:

# Looping da parte real, composto de 2000 samples no intervalo de -2.0 a 1.0
for row_index, Re in enumerate(numpy.linspace(-2, 1, num=rows)):

    # Looping da parte imaginária, composto de 2000 samples no intervalo de -1.0 a 1.0
    for column_index, Im in enumerate (numpy.linspace(-1, 1, num=columns)):

The loopings will generate the test points.

Example: (-2.0,-1.0), (-2.0,-0.99)...(-1.99,-1.0),(-1.99,-0.99)...... até (1.0,1.0)

Within the looping and for each point, the function mset calculates whether or not the point belongs to the Mandelbrot set and returns the number of iterations needed to determine that membership.

This result is stored inside the array result:

result[row_index, column_index] = mset(Re, Im, 200)

The other commands plot the obtained result and display the array result: Fractal de Mandelbrot

Although mentioned in the question, this program does not include concepts of object orientation.

More information on: Wikipedia - Mandelbrot set

  • Thank you very much, Gomiero, you helped a lot.

  • You’re welcome!! If the answer was helpful, please tick as accepted

Browser other questions tagged

You are not signed in. Login or sign up in order to post.