Compiling cython - import vs cimport

New to Keaton (perhaps this is the main question). Consider two examples 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 compiled the first file with the following setup.pyscript:

# 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 a second file with the following setup.pyscript:

# 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 first case, I used the usual one numpyand did not import numpyinto the installation file, and in the second case, I imported numpyusing the cimportdeclared variables using cdef, but then also included numpyin the installation file.

Cython compiles the first code anyway (and the first code seems to work).

cimport cdef Cython ( ) cimport cdef Cython ( )?

+4
2

import numpy Cython Python, cimport numpy Cython :

https://github.com/cython/cython/blob/master/Cython/Includes/numpy/ init.pxd

, C-API, , numpy/arrayobject.h.

np.ndarray[...], Cython , c, , Python [].

c, numpy setup.py, numpy.get_include(), .

+6

Cython python, .

, cython, . , .

cython -a your_test.pyx, , cython . , C ( ), , C.

, -, .

0

All Articles