File: mandelbrot-seq.py

package info (click to toggle)
mpi4py 4.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,540 kB
  • sloc: python: 34,465; ansic: 16,475; makefile: 614; sh: 325; cpp: 193; f90: 178
file content (54 lines) | stat: -rw-r--r-- 885 bytes parent folder | download | duplicates (2)
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
import time

import numpy as np

tic = time.time()

x1 = -2.0
x2 = +1.0
y1 = -1.0
y2 = +1.0

w = 150
h = 100
maxit = 127


def mandelbrot(x, y, maxit):
    c = x + y * 1j
    z = 0 + 0j
    it = 0
    while abs(z) < 2 and it < maxit:
        z = z**2 + c
        it += 1
    return it


dx = (x2 - x1) / w
dy = (y2 - y1) / h

C = np.empty([h, w], dtype="i")
for k in np.arange(h):
    y = y1 + k * dy
    for j in np.arange(w):
        x = x1 + j * dx
        C[k, j] = mandelbrot(x, y, maxit)

M = C

toc = time.time()
print(f"wall clock time: {toc - tic:8.2f} seconds")

# eye candy (requires matplotlib)
if 1:
    import contextlib

    with contextlib.suppress(Exception):
        from matplotlib import pyplot as plt

        plt.imshow(M, aspect="equal")
        try:
            plt.nipy_spectral()
        except AttributeError:
            plt.spectral()
        plt.pause(2)