mirror of
https://github.com/Findus23/linalgorithm.git
synced 2024-08-27 19:52:15 +02:00
add script
(NOTE: this commit is backdated from 2017-12-26)
This commit is contained in:
commit
b863c3bccd
1 changed files with 61 additions and 0 deletions
61
mandelbrot.py
Normal file
61
mandelbrot.py
Normal file
|
@ -0,0 +1,61 @@
|
|||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from multiprocessing.dummy import Pool as ThreadPool
|
||||
import os.path
|
||||
|
||||
|
||||
def f(x):
|
||||
return 2 ** (1 - x)
|
||||
|
||||
|
||||
def mandelbrot(zoomlevel):
|
||||
print(zoomlevel)
|
||||
zoom = f(zoomlevel)
|
||||
maxit = 30 + 10 * zoomlevel
|
||||
xmin = center[0] - zoom
|
||||
xmax = center[0] + zoom
|
||||
ymin = center[1] - zoom
|
||||
ymax = center[1] + zoom
|
||||
|
||||
loadfile = str(zoomlevel) + ".npy"
|
||||
if os.path.isfile(loadfile):
|
||||
divtime = np.load(loadfile)
|
||||
return [zoomlevel, divtime, [xmin, xmax, ymin, ymax]]
|
||||
|
||||
"""Returns an image of the Mandelbrot fractal of size (h,w)."""
|
||||
y, x = np.ogrid[ymin:ymax:h * 1j, xmin:xmax:w * 1j]
|
||||
c = x + y * 1j
|
||||
z = c
|
||||
divtime = maxit + np.zeros(z.shape, dtype=int)
|
||||
|
||||
for i in range(maxit):
|
||||
z = z ** 2 + c
|
||||
diverge = z * np.conj(z) > 2 ** 2 # who is diverging
|
||||
div_now = diverge & (divtime == maxit) # who is diverging now
|
||||
divtime[div_now] = i # note when
|
||||
z[diverge] = 2 # avoid diverging too much
|
||||
plt.close()
|
||||
np.save(str(zoomlevel), divtime)
|
||||
return [zoomlevel, divtime, [xmin, xmax, ymin, ymax]]
|
||||
|
||||
|
||||
h = w = 1024
|
||||
# center = [-0.73, -0.205]
|
||||
center = [-0.7768816266387022, -0.13675506794031342]
|
||||
|
||||
pool = ThreadPool(3)
|
||||
|
||||
results = pool.map(mandelbrot, range(17))
|
||||
pool.close()
|
||||
pool.join()
|
||||
|
||||
for zl, dt, coordinates in results:
|
||||
print("Bild:{nr}".format(nr=zl))
|
||||
plt.imshow(dt, extent=coordinates)
|
||||
plt.ticklabel_format(style='sci', scilimits=(0, 0))
|
||||
|
||||
# plt.axis("off")
|
||||
plt.savefig("{img}.png".format(img=zl), dpi=350)
|
||||
|
||||
# for zoomlevel in range(20):
|
||||
# mandelbrot(zoomlevel)
|
Loading…
Reference in a new issue