@@ -90,8 +90,40 @@ def plot_julia_set(julia):
90
90
91
91
# + {"internals": {"slide_helper": "subslide_end"}, "slide_helper": "slide_end", "slideshow": {"slide_type": "-"}}
92
92
plot_julia_set (juliaset_python (x , y , c , lim , maxit ))
93
- # -
94
93
94
+ # + [markdown] {"internals": {"slide_type": "subslide"}, "slideshow": {"slide_type": "slide"}}
95
+ # ## numba
96
+ #
97
+ # [Numba](https://numba.pydata.org) will accelerate the pure python function just with
98
+ # the decorator `@jit`. Numba does everything for you.
99
+
100
+ # + {"internals": {"slide_helper": "subslide_end"}, "slide_helper": "subslide_end", "slideshow": {"slide_type": "slide"}}
101
+ from numba import jit
102
+
103
+ @jit (nopython = True , parallel = True )
104
+ def juliaset_numba (x , y , c , lim , maxit ):
105
+ julia = np .zeros ((x .size , y .size ))
106
+ lim2 = lim * lim
107
+
108
+ c = complex (c ) # needed for numba
109
+ for j in range (y .size ):
110
+ for i in range (x .size ):
111
+
112
+ z = complex (x [i ], y [j ])
113
+ ite = 0
114
+ while (z .real * z .real + z .imag * z .imag ) < lim2 and ite < maxit :
115
+ z = z * z + c
116
+ ite += 1
117
+ julia [j , i ] = ite
118
+
119
+ return julia
120
+
121
+
122
+ # + {"slideshow": {"slide_type": "slide"}}
123
+ plot_julia_set (juliaset_numba (x , y , c , lim , maxit ))
124
+
125
+
126
+ # + [markdown] {"slideshow": {"slide_type": "slide"}}
95
127
# ## PyJulia
96
128
#
97
129
# [PyJulia](https://pyjulia.readthedocs.io/en/latest/#) is a python module to import
@@ -110,6 +142,8 @@ def plot_julia_set(julia):
110
142
#
111
143
# print the value of `sys.executable` to know the python path. But the cell above could do the job.
112
144
145
+
146
+ # + {"slideshow": {"slide_type": "slide"}}
113
147
import julia
114
148
julia .install ()
115
149
from julia .api import Julia
@@ -381,7 +415,7 @@ def juliaset_cython(double [:] x, double [:] y, double complex c, double lim, in
381
415
# As f2py we can use openmp with the Cython `prange` function
382
416
383
417
# + {"internals": {"slide_helper": "subslide_end"}, "slide_helper": "subslide_end", "slideshow": {"slide_type": "-"}}
384
- % % cython - - v - f - c - fopenmp - - link - args = - fopenmp
418
+ % % cython - f - c - fopenmp - - link - args = - fopenmp
385
419
import numpy as np
386
420
import cython
387
421
from cython .parallel import prange
@@ -416,38 +450,6 @@ def juliaset_cython_omp(double [:] x, double [:] y, double complex c, double lim
416
450
417
451
plot_julia_set (juliaset_cython_omp (x , y , c , lim , maxit ))
418
452
419
- # + [markdown] {"internals": {"slide_type": "subslide"}, "slideshow": {"slide_type": "slide"}}
420
- # ## numba
421
- #
422
- # [Numba](https://numba.pydata.org) will accelerate the pure python function just with
423
- # the decorator `@jit`. Numba does everything for you.
424
-
425
- # + {"internals": {"slide_helper": "subslide_end"}, "slide_helper": "subslide_end", "slideshow": {"slide_type": "slide"}}
426
- from numba import jit
427
-
428
- @jit (nopython = True , parallel = True )
429
- def juliaset_numba (x , y , c , lim , maxit ):
430
- julia = np .zeros ((x .size , y .size ))
431
- lim2 = lim * lim
432
-
433
- c = complex (c ) # needed for numba
434
- for j in range (y .size ):
435
- for i in range (x .size ):
436
-
437
- z = complex (x [i ], y [j ])
438
- ite = 0
439
- while (z .real * z .real + z .imag * z .imag ) < lim2 and ite < maxit :
440
- z = z * z + c
441
- ite += 1
442
- julia [j , i ] = ite
443
-
444
- return julia
445
-
446
-
447
- # + {"slideshow": {"slide_type": "slide"}}
448
- plot_julia_set (juliaset_numba (x , y , c , lim , maxit ))
449
- # -
450
-
451
453
# ### Set number of threads used for parallel functions
452
454
453
455
% env OMP_NUM_THREADS = 4
0 commit comments