Skip to content

Commit

Permalink
Feature/mandelbrot (#106)
Browse files Browse the repository at this point in the history
* add complex

* fix pylint

* add julia

* add lowres

* add highres
  • Loading branch information
imjoseangel committed Mar 20, 2022
1 parent ca8ecf5 commit bff9056
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions python/mandelbrot/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import matplotlib.pyplot as plt
import numpy as np

np.warnings.filterwarnings("ignore")


def is_stable(candidate, num_iterations):
z = 0
for _ in range(num_iterations):
z = z ** 2 + candidate
return abs(z) <= 2


def complex_matrix(xmin, xmax, ymin, ymax, pixel_density):
re = np.linspace(xmin, xmax, int((xmax - xmin) * pixel_density))
im = np.linspace(ymin, ymax, int((ymax - ymin) * pixel_density))
return re[np.newaxis, :] + im[:, np.newaxis] * 1j


def get_members(candidate, num_iterations):
mask = is_stable(candidate, num_iterations)
return candidate[mask]


c = complex_matrix(-2, 0.5, -1.5, 1.5, pixel_density=21)
members = get_members(c, num_iterations=20)

plt.scatter(members.real, members.imag, color="black", marker=",", s=1)
plt.gca().set_aspect("equal")
plt.axis("off")
plt.tight_layout()
plt.show()

c = complex_matrix(-2, 0.5, -1.5, 1.5, pixel_density=512)
plt.imshow(is_stable(c, num_iterations=20), cmap="binary")
plt.gca().set_aspect("equal")
plt.axis("off")
plt.tight_layout()
plt.show()

0 comments on commit bff9056

Please sign in to comment.