1- #! /usr/bin/env python
2- # -*- coding: utf-8 -*-
3- from __future__ import print_function , division , absolute_import
41
5- '''
6- Taken from https://github.com/numba/numba/blob/master/examples/mandel/mandel_jit.py
7- On June 3, 2017, then modified by mattip
8- Original copyright:
9-
10- Copyright (c) 2012, Continuum Analytics, Inc.
11- All rights reserved.
12-
13- Redistribution and use in source and binary forms, with or without
14- modification, are permitted provided that the following conditions are
15- met:
16-
17- Redistributions of source code must retain the above copyright notice,
18- this list of conditions and the following disclaimer.
19-
20- Redistributions in binary form must reproduce the above copyright
21- notice, this list of conditions and the following disclaimer in the
22- documentation and/or other materials provided with the distribution.
23- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34-
35- Contact GitHub API Training Shop Blog About
36-
37- '''
38-
39- from timeit import default_timer as timer
40-
41- import sys
42- fname = 'test'
43- if len (sys .argv ) > 1 and 'nojit' in sys .argv [1 ]:
44- def jit (f ):
45- print ('pure python' )
46- return f
47- fname = 'test_nojit'
48- else :
49- from numba import jit
50- fname = 'test_numba'
51-
52-
53- @jit
54- def mandel (x , y , max_iters ):
2+ def mandel (x , y , max_iters , value ):
553 """
564 Given the real and imaginary parts of a complex number,
575 determine if it is a candidate for membership in the Mandelbrot
@@ -63,51 +11,8 @@ def mandel(x, y, max_iters):
6311 for i in range (max_iters ):
6412 z = z * z + c
6513 if (z .real * z .real + z .imag * z .imag ) >= 4 :
66- return i
67-
68- return 255
69-
70- @jit
71- def create_fractal (image , width , height , iters ,
72- min_x = - 2.0 , max_x = 1.0 , min_y = - 1.0 , max_y = 1.0 ):
73- pixel_size_x = (max_x - min_x ) / width
74- pixel_size_y = (max_y - min_y ) / height
75- for y in range (height ):
76- imag = min_y + y * pixel_size_y
77- yy = y * width
78- for x in range (width ):
79- real = min_x + x * pixel_size_x
80- color = mandel (real , imag , iters )
81- image [yy + x ] = color
82-
83- return image
14+ value [0 ] = i
15+ return 0
16+ value [i ] = 255
17+ return 1
8418
85- width = 1500
86- height = 1000
87- if 'cffi' in sys .argv :
88- import cffi
89- ffi = cffi .FFI ()
90- image = ffi .new ('unsigned char[{}]' .format (width * height ))
91- img_as_bytes = bytes (ffi .buffer (image , width * height ))
92- fname += '_cffi'
93- else :
94- import numpy as np
95- image = np .empty (width * height , dtype = np .uint8 )
96- fname += '_numpy'
97- img_as_bytes = image
98- s = timer ()
99- create_fractal (image , width , height , 20 )
100- e = timer ()
101- print (e - s )
102- if 0 :
103- from matplotlib .pylab import imshow , jet , show , ion
104- imshow (image )
105- show ()
106- else :
107- from PIL import Image
108- try :
109- im = Image .frombuffer ('L' , (width , height ), img_as_bytes , 'raw' , 'L' , 0 , 1 )
110- im .save (fname + '.png' )
111- except :
112- import traceback ;traceback .print_exc ()
113- import pdb ;pdb .set_trace ()
0 commit comments