Newbie to Cython (perhaps this is a basic question). Consider two examples both taken from this blog here:
# code 1
import numpy as np
def num_update(u):
u[1:-1,1:-1] = ((u[2:,1:-1]+u[:-2,1:-1])*dy2 +
(u[1:-1,2:] + u[1:-1,:-2])*dx2) / (2*(dx2+dy2))
and
# code 2
cimport numpy as np
def cy_update(np.ndarray[double, ndim=2] u, double dx2, double dy2):
cdef unsigned int i, j
for i in xrange(1,u.shape[0]-1):
for j in xrange(1, u.shape[1]-1):
u[i,j] = ((u[i+1, j] + u[i-1, j]) * dy2 +
(u[i, j+1] + u[i, j-1]) * dx2) / (2*(dx2+dy2))
Suppose I compile the first file with the following setup.py
script:
# setup file for code 1
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext = Extension("laplace", ["laplace.pyx"],)
setup(ext_modules=[ext], cmdclass = {'build_ext': build_ext})
and the second file with the following setup.py
script:
# setup file for code 2
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy
ext = Extension("laplace", ["laplace.pyx"],
include_dirs = [numpy.get_include()])
setup(ext_modules=[ext], cmdclass = {'build_ext': build_ext})
In the 1st case, I used regular numpy
and didn't import numpy
in the setup file, while in the 2nd case I imported numpy
using cimport
, declared variables using cdef
but then also included numpy
in the setup file.
Cython
compiles the first code anyway (and the first code seems to work).
What would be advantages of using cimport
and cdef
before compiling with Cython (via the setup file) versus not using cimport
and cdef
before compiling with Cython (via the setup file)?