-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmandelbrot.pyx
More file actions
39 lines (33 loc) · 1.12 KB
/
mandelbrot.pyx
File metadata and controls
39 lines (33 loc) · 1.12 KB
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
# cython: language_level=3
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the BSD 3-Clause
# (see plotpy/__init__.py for details)
"""Mandelbrot algorithm"""
cimport cython
cimport numpy as np
@cython.profile(False)
cdef inline int mandel(double real, double imag, int nmax):
cdef double z_real=0., z_imag=0.
cdef int i
for i in range(nmax):
z_real, z_imag = (z_real*z_real - z_imag*z_imag + real,
2*z_real*z_imag + imag)
if z_real*z_real + z_imag*z_imag > 4:
return i
return -1
@cython.boundscheck(False)
def mandelbrot(double x1, double y1, double x2, double y2,
data, unsigned int nmax):
"""Compute Mandelbrot fractal"""
cdef double dx, dy, real, imag
cdef unsigned int row, col
cdef unsigned int rows = data.shape[0]
cdef unsigned int cols = data.shape[1]
dx = (x2-x1)/(cols-1)
dy = (y2-y1)/(rows-1)
for col in range(cols):
real = x1 + col*dx
for row in range(rows):
imag = y1 + row*dy
data[row, col] = mandel(real, imag, nmax)