|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +####################################################### |
| 4 | +# Copyright (c) 2015, ArrayFire |
| 5 | +# All rights reserved. |
| 6 | +# |
| 7 | +# This file is distributed under 3-clause BSD license. |
| 8 | +# The complete license agreement can be obtained at: |
| 9 | +# http://arrayfire.com/licenses/BSD-3-Clause |
| 10 | +######################################################## |
| 11 | + |
| 12 | +import arrayfire as af |
| 13 | +import sys |
| 14 | +from math import sqrt |
| 15 | + |
| 16 | +width = 400 |
| 17 | +height = 400 |
| 18 | + |
| 19 | +def complex_grid(w, h, zoom, center): |
| 20 | + x = (af.iota(d0 = 1, d1 = h, tile_dims = (w, 1)) - h/2) / zoom + center[0] |
| 21 | + y = (af.iota(d0 = w, d1 = 1, tile_dims = (1, h)) - w/2) / zoom + center[1] |
| 22 | + return af.cplx(x, y) |
| 23 | + |
| 24 | +def mandelbrot(data, it, maxval): |
| 25 | + C = data |
| 26 | + Z = data |
| 27 | + mag = af.constant(0, *C.dims()) |
| 28 | + |
| 29 | + for ii in range(1, 1 + it): |
| 30 | + # Doing the calculation |
| 31 | + Z = Z * Z + C |
| 32 | + |
| 33 | + # Get indices where abs(Z) crosses maxval |
| 34 | + cond = af.cast((af.abs(Z) > maxval), af.Dtype.f32) |
| 35 | + mag = af.maxof(mag, cond * ii) |
| 36 | + |
| 37 | + C = C * (1 - cond) |
| 38 | + Z = Z * (1 - cond) |
| 39 | + |
| 40 | + af.eval(C) |
| 41 | + af.eval(Z) |
| 42 | + |
| 43 | + return mag / maxval |
| 44 | + |
| 45 | +def normalize(a): |
| 46 | + mx = af.max(a) |
| 47 | + mn = af.min(a) |
| 48 | + return (a - mn)/(mx - mn) |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + if (len(sys.argv) > 1): |
| 52 | + af.set_device(int(sys.argv[1])) |
| 53 | + |
| 54 | + af.info() |
| 55 | + |
| 56 | + print("ArrayFire Fractal Demo\n") |
| 57 | + |
| 58 | + win = af.Window(width, height, "Fractal Demo") |
| 59 | + win.set_colormap(af.COLORMAP.SPECTRUM) |
| 60 | + |
| 61 | + center = (-0.75, 0.1) |
| 62 | + |
| 63 | + for i in range(10, 400): |
| 64 | + zoom = i * i |
| 65 | + if not (i % 10): |
| 66 | + print("Iteration: %d zoom: %d" % (i, zoom)) |
| 67 | + |
| 68 | + c = complex_grid(width, height, zoom, center) |
| 69 | + it = sqrt(2*sqrt(abs(1-sqrt(5*zoom))))*100 |
| 70 | + |
| 71 | + if (win.close()): break |
| 72 | + mag = mandelbrot(c, int(it), 1000) |
| 73 | + |
| 74 | + win.image(normalize(mag)) |
0 commit comments