Created
May 24, 2012 22:53
-
-
Save zonca/2784725 to your computer and use it in GitHub Desktop.
Cython Numpy accumulate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cimport numpy as np | |
ctypedef np.long_t DTYPE_int_t | |
ctypedef np.double_t DTYPE_double_t | |
cimport cython | |
@cython.boundscheck(False) # turn of bounds-checking for entire function | |
@cython.wraparound(False) | |
def accumulate( | |
np.ndarray[DTYPE_double_t, ndim=1] a not None, | |
np.ndarray[DTYPE_int_t, ndim=1] section_lengths not None, | |
np.ndarray[DTYPE_double_t, ndim=1] out not None, | |
): | |
cdef unsigned int i_el, i_bas, sec_length, lenout | |
cdef double tmp | |
lenout = out.shape[0] | |
i_el = 0 | |
for i_bas in range(lenout): | |
tmp = 0 | |
for sec_length in range(section_lengths[i_bas]): | |
tmp += a[i_el] | |
i_el+=1 | |
out[i_bas] = tmp | |
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from distutils.core import setup | |
from distutils.extension import Extension | |
from Cython.Distutils import build_ext | |
ext_modules = [Extension("accumulate", ["accumulate.pyx"], extra_compile_args=["-O3",])] | |
setup( | |
name = 'Accumulate', | |
cmdclass = {'build_ext': build_ext}, | |
ext_modules = ext_modules | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cython -a output for the last line (return 0):