Debian Codegraph @

src/mandelbrot.pyx

raw
1 # cython: language_level=3
2 # -*- coding: utf-8 -*-
3 #
4 # Licensed under the terms of the BSD 3-Clause
5 # (see plotpy/__init__.py for details)
6
7 """Mandelbrot algorithm"""
8
9 cimport cython
10 cimport numpy as np
11
12 @cython.profile(False)
13 cdef inline int mandel(double real, double imag, int nmax):
14 cdef double z_real=0., z_imag=0.
15 cdef int i
16 for i in range(nmax):
17 z_real, z_imag = (z_real*z_real - z_imag*z_imag + real,
18 2*z_real*z_imag + imag)
19 if z_real*z_real + z_imag*z_imag > 4:
20 return i
21 return -1
22
23 @cython.boundscheck(False)
24 def mandelbrot(double x1, double y1, double x2, double y2,
25 data, unsigned int nmax):
26 """Compute Mandelbrot fractal"""
27 cdef double dx, dy, real, imag
28 cdef unsigned int row, col
29 cdef unsigned int rows = data.shape[0]
30 cdef unsigned int cols = data.shape[1]
31
32 dx = (x2-x1)/(cols-1)
33 dy = (y2-y1)/(rows-1)
34
35 for col in range(cols):
36 real = x1 + col*dx
37 for row in range(rows):
38 imag = y1 + row*dy
39 data[row, col] = mandel(real, imag, nmax)