forked from arrayfire/arrayfire-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfractal.py
More file actions
80 lines (55 loc) · 1.77 KB
/
fractal.py
File metadata and controls
80 lines (55 loc) · 1.77 KB
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
74
75
76
77
78
79
80
#!/usr/bin/env python
#######################################################
# Copyright (c) 2019, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################
import sys
from math import sqrt
import arrayfire as af
width = 400
height = 400
def complex_grid(w, h, zoom, center):
x = (af.iota(d0=1, d1=h, tile_dims=(w, 1)) - h/2) / zoom + center[0]
y = (af.iota(d0=w, d1=1, tile_dims=(1, h)) - w/2) / zoom + center[1]
return af.cplx(x, y)
def mandelbrot(data, it, maxval):
C = data
Z = data
mag = af.constant(0, *C.dims())
for ii in range(1, 1 + it):
# Doing the calculation
Z = Z * Z + C
# Get indices where abs(Z) crosses maxval
cond = ((af.abs(Z) > maxval)).as_type(af.Dtype.f32)
mag = af.maxof(mag, cond * ii)
C = C * (1 - cond)
Z = Z * (1 - cond)
af.eval(C)
af.eval(Z)
return mag / maxval
def normalize(a):
mx = af.max(a)
mn = af.min(a)
return (a - mn)/(mx - mn)
if __name__ == "__main__":
if len(sys.argv) > 1:
af.set_device(int(sys.argv[1]))
af.info()
print("ArrayFire Fractal Demo\n")
win = af.Window(width, height, "Fractal Demo")
win.set_colormap(af.COLORMAP.SPECTRUM)
center = (-0.75, 0.1)
for i in range(10, 400):
zoom = i * i
if not i % 10:
print("Iteration: %d zoom: %d" % (i, zoom))
c = complex_grid(width, height, zoom, center)
it = sqrt(2*sqrt(abs(1-sqrt(5*zoom))))*100
if win.close():
break
mag = mandelbrot(c, int(it), 1000)
win.image(normalize(mag))