-
Notifications
You must be signed in to change notification settings - Fork 7
/
multibrot.mojo
73 lines (55 loc) · 1.89 KB
/
multibrot.mojo
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from complex import ComplexFloat64
from python import Python
from tensor import Tensor
from utils.index import Index
alias FloatType = DType.float64
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 multibrot_kernel(c: ComplexFloat64) -> Int:
z = c
for i in range(MAX_ITERS):
z = z * z + c # Change this for different Multibrot sets (e.g., 2 for Mandelbrot)
if z.squared_norm() > 4:
return i
return MAX_ITERS
def compute_multibrot() -> Tensor[FloatType]:
# create a matrix. Each element of the matrix corresponds to a pixel
t = Tensor[FloatType](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)] = multibrot_kernel(ComplexFloat64(x, y))
x += dx
y += dy
return t
# def show_plot(tensor: Tensor[FloatType]):
# 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))
# 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.savefig("multibrot.mojo.png")
# plt.show()
def main():
_ = compute_multibrot()
# multibrot = compute_multibrot()
# show_plot(multibrot)