Created
September 12, 2012 11:21
-
-
Save YukiSakamoto/3706035 to your computer and use it in GitHub Desktop.
pass numpy-object into C modules via cython
This file contains hidden or 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
| #include <stdio.h> | |
| void listup(int *array, int size) | |
| { | |
| int i; | |
| for(i = 0; i < size; i++) { | |
| printf("%d\n", *(array + i)); | |
| } | |
| } |
This file contains hidden or 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
| void listup(int *array, int size); |
This file contains hidden or 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
| python setup.py build_ext --inplace |
This file contains hidden or 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
| import numpy as np | |
| cimport numpy as np | |
| cdef extern from "clistup.h": | |
| void listup(int*,int) | |
| cdef call_org_listup(np.ndarray py_array): | |
| cdef int *int_array = <int*>py_array.data | |
| cdef int length = len(py_array) | |
| listup(int_array,length) | |
| def listup_array(np_array): | |
| call_org_listup(np_array) |
This file contains hidden or 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
| import numpy as np | |
| import Listup | |
| zero_array = np.zeros(5) | |
| Listup.listup_array(zero_array) | |
| print "==================================" | |
| ar2 = np.array([1000, 1000, 1200, 1500], np.int32) | |
| Listup.listup_array(ar2) |
This file contains hidden or 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 | |
| import numpy | |
| source = ['./clistup.c', './listup.pyx'] | |
| # mac XXX ここは環境に合わせて変える | |
| numpy_include = ['/System/Library/Frameworks/Python.framework/Versions/Current/Extras/lib/python/numpy/core/include'] | |
| setup( | |
| cmdclass = dict(build_ext = build_ext), | |
| ext_modules = [ | |
| Extension( | |
| 'Listup', | |
| source, | |
| include_dirs = numpy_include, | |
| ) | |
| ] | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment