Skip to content

Commit 569af2a

Browse files
committed
Try to import numba before pyjulia
1 parent 7d8eb85 commit 569af2a

File tree

3 files changed

+385
-160
lines changed

3 files changed

+385
-160
lines changed

03.julia-set.py

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,40 @@ def plot_julia_set(julia):
9090

9191
# + {"internals": {"slide_helper": "subslide_end"}, "slide_helper": "slide_end", "slideshow": {"slide_type": "-"}}
9292
plot_julia_set(juliaset_python(x, y, c, lim, maxit))
93-
# -
9493

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"}}
95127
# ## PyJulia
96128
#
97129
# [PyJulia](https://pyjulia.readthedocs.io/en/latest/#) is a python module to import
@@ -110,6 +142,8 @@ def plot_julia_set(julia):
110142
#
111143
# print the value of `sys.executable` to know the python path. But the cell above could do the job.
112144

145+
146+
# + {"slideshow": {"slide_type": "slide"}}
113147
import julia
114148
julia.install()
115149
from julia.api import Julia
@@ -381,7 +415,7 @@ def juliaset_cython(double [:] x, double [:] y, double complex c, double lim, in
381415
# As f2py we can use openmp with the Cython `prange` function
382416

383417
# + {"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
385419
import numpy as np
386420
import cython
387421
from cython.parallel import prange
@@ -416,38 +450,6 @@ def juliaset_cython_omp(double [:] x, double [:] y, double complex c, double lim
416450

417451
plot_julia_set(juliaset_cython_omp(x, y, c, lim, maxit))
418452

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-
451453
# ### Set number of threads used for parallel functions
452454

453455
%env OMP_NUM_THREADS=4

notebooks/03.julia-set.ipynb

Lines changed: 332 additions & 108 deletions
Large diffs are not rendered by default.

requirements.txt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
cython
2-
fortran-magic
3-
imageio
4-
ipykernel
5-
ipywidgets
6-
julia
7-
jupyter
8-
jupyter-book
9-
matplotlib
10-
numba
11-
numpy
12-
pandas
13-
pillow
14-
pythran
15-
scipy
16-
seaborn
17-
setuptools
18-
tqdm
1+
Cython==0.29.21
2+
fortran-magic==0.7
3+
gast==0.3.3
4+
imageio==2.9.0
5+
ipykernel==5.3.4
6+
ipywidgets==7.5.1
7+
julia==0.5.4
8+
jupyter==1.0.0
9+
jupyter-book==0.7.4
10+
llvmlite==0.33.0
11+
matplotlib==3.3.1
12+
numba==0.50.1
13+
numpy==1.19.1
14+
Pillow==7.2.0
15+
pythran==0.9.6
16+
seaborn==0.10.1
17+
tqdm==4.48.2

0 commit comments

Comments
 (0)