Skip to content

Instantly share code, notes, and snippets.

@adiamaan92
Created September 26, 2021 20:29
Show Gist options
  • Save adiamaan92/3463588db07449a5f77deec2b9bf5c25 to your computer and use it in GitHub Desktop.
Save adiamaan92/3463588db07449a5f77deec2b9bf5c25 to your computer and use it in GitHub Desktop.
Sample complex numbers that are not in the Mandelbrot set
import numpy as np
def make_non_mandelbrot_set(nsamples: int, max_iterations: int) -> np.ndarray:
"""Generate a set of complex numbers that are not in the Mandelbrot set.
This employs some of the optimizations from this page,
http://en.wikipedia.org/wiki/Mandelbrot_set#Optimizations
In order to minimize run time, we are trying to reduce the number of points
that we already know that are in the mandelbrot set. Points inside the within the
cardioid and in the period-2 bulb can be eliminiated.
Args:
nsamples (int): Number of samples to generate.
max_iterations (int): Maximum number of iterations to perform.
Returns:
np.ndarray: Array of complex numbers that are not in the Mandelbrot set.
"""
non_mandels = np.zeros(nsamples, dtype=np.complex128)
n_non_mandels = 0
cardioid = (
np.random.random(nsamples) * 4 - 2 + (np.random.random(nsamples) * 4 - 2) * 1j
)
p = (((cardioid.real - 0.25) ** 2) + (cardioid.imag ** 2)) ** 0.5
cardioid = cardioid[cardioid.real > p - (2 * p ** 2) + 0.25]
cardioid = cardioid[((cardioid.real + 1) ** 2) + (cardioid.imag ** 2) > 0.0625]
z = np.copy(cardioid)
for _ in range(max_iterations):
z = z ** 2 + cardioid
mask = np.abs(z) < 2
new_non_msets = cardioid[~mask]
non_mandels[n_non_mandels : n_non_mandels + len(new_non_msets)] = new_non_msets
n_non_mandels += len(new_non_msets)
cardioid = cardioid[mask]
z = z[mask]
return non_mandels[:n_non_mandels]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment