pyodide/benchmark/benchmarks/julia.py

29 lines
872 B
Python
Raw Normal View History

2018-10-03 18:59:01 +00:00
# setup: N=10
# run: julia(1., 1., N, 1.5, 10., 1e4)
2018-04-05 22:07:33 +00:00
2018-10-03 18:59:01 +00:00
# pythran export julia(float, float, int, float, float, float)
2018-04-05 22:07:33 +00:00
import numpy as np
2018-10-03 18:59:01 +00:00
2018-04-05 22:07:33 +00:00
def kernel(zr, zi, cr, ci, lim, cutoff):
2020-09-24 10:28:10 +00:00
"""Computes the number of iterations `n` such that
|z_n| > `lim`, where `z_n = z_{n-1}**2 + c`.
"""
2018-04-05 22:07:33 +00:00
count = 0
2018-10-03 18:59:01 +00:00
while ((zr * zr + zi * zi) < (lim * lim)) and count < cutoff:
2018-04-05 22:07:33 +00:00
zr, zi = zr * zr - zi * zi + cr, 2 * zr * zi + ci
count += 1
return count
2018-10-03 18:59:01 +00:00
def julia(cr, ci, N, bound=1.5, lim=1000.0, cutoff=1e6):
2020-09-24 10:28:10 +00:00
"""Pure Python calculation of the Julia set for a given `c`. No NumPy
array operations are used.
"""
2018-04-05 22:07:33 +00:00
julia = np.empty((N, N), np.uint32)
grid_x = np.linspace(-bound, bound, N)
for i, x in enumerate(grid_x):
for j, y in enumerate(grid_x):
2018-10-03 18:59:01 +00:00
julia[i, j] = kernel(x, y, cr, ci, lim, cutoff)
2018-04-05 22:07:33 +00:00
return julia