Created
October 5, 2023 16:34
-
-
Save nbecker/820a6f740e4bad43bb09a52103d5b19b to your computer and use it in GitHub Desktop.
mandel test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alias width = 960 | |
alias height = 960 | |
alias MAX_ITERS = 200 | |
alias min_x = -2.0 | |
alias max_x = 0.6 | |
alias min_y = -1.5 | |
alias max_y = 1.5 | |
# Compute the number of steps to escape. | |
def mandelbrot_kernel(c: ComplexFloat64) -> Int: | |
z = c | |
for i in range(MAX_ITERS): | |
z = z * z + c | |
if z.squared_norm() > 4: | |
return i | |
return MAX_ITERS | |
def compute_mandelbrot() -> Tensor[float_type]: | |
# create a matrix. Each element of the matrix corresponds to a pixel | |
t = Tensor[float_type](height, width) | |
dx = (max_x - min_x) / width | |
dy = (max_y - min_y) / height | |
y = min_y | |
for row in range(height): | |
x = min_x | |
for col in range(width): | |
t[Index(row, col)] = mandelbrot_kernel(ComplexFloat64(x, y)) | |
x += dx | |
y += dy | |
return t | |
def show_plot(tensor: Tensor[float_type]): | |
alias scale = 10 | |
alias dpi = 64 | |
np = Python.import_module("numpy") | |
plt = Python.import_module("matplotlib.pyplot") | |
colors = Python.import_module("matplotlib.colors") | |
numpy_array = np.zeros((height, width), np.float64) | |
for row in range(height): | |
for col in range(width): | |
numpy_array.itemset((col, row), tensor[col, row]) | |
fig = plt.figure(1, [scale, scale * height // width], dpi) | |
ax = fig.add_axes([0.0, 0.0, 1.0, 1.0], False, 1) | |
light = colors.LightSource(315, 10, 0, 1, 1, 0) | |
image = light.shade(numpy_array, plt.cm.hot, colors.PowerNorm(0.3), "hsv", 0, 0, 1.5) | |
plt.imshow(image) | |
plt.axis("off") | |
plt.show() | |
show_plot(compute_mandelbrot()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment