Featured post
Passing Numpy arrays to C code wrapped with Cython -
i have small bit of existing c code want wrap using cython. want able set number of numpy arrays, , pass arrays arguments c code functions take standard c arrays (1d , 2d). i'm little stuck in terms of figuring out how write proper .pyx code handle things.
there handful of functions, typical function in file funcs.h looks like:
double innerproduct(double *a, double **coords1, double **coords2, const int len)
i have .pyx file has corresponding line:
cdef extern "funcs.h": double innerproduct(double *a, double **coords1, double **coords2, int len)
where got rid of const because cython doesn't support it. i'm stuck wrapper code should pass mxn numpy array **coords1 , **coords2 arguments.
i've struggled find correct documentation or tutorials type of problem. suggestions appreciated.
you want cython's "typed memoryviews" feature, can read in full gory detail here. newer, more unified way work numpy or other arrays. these can exposed in python-land numpy arrays, or can export them python (for example, here). have pay attention how striding works , make sure you're consistent e.g. c-contiguous vs. fortran-like arrays, docs pretty clear on how that.
without knowing bit more function it's hard more concrete on best way - i.e., c function read-only arrays? (i think yes based on signature gave, not 100% sure.) if don't need worry making copies if needed c-contiguous states, because c function doesn't need talk python-level numpy array. typed memoryviews let of minimum of fuss.
- Get link
- X
- Other Apps
Comments
Post a Comment