I am currently learning how to call a C library from Python. I do not quite understand the proper way to pass an array from Python to a C function for computation and get the returned value back to Python.
Suppose that I have a C function, namely cfun
, which takes an integer, dim, and an array, double *a
, for computation and return the results to another array, double *b
. In my Python routine, I have declared two arrays, arr1 and arr2 and I would like to tell the cfun to use the actual locations of the allocated memory of arr1 and arr2 for computation.
Is my *.pyx
code which is shown below efficient enough for my purpose, whereas cfun is computationally expensive and is frequently called?
import numpy as np
cimport numpy as np
cdef extern from "header.h":
void cfun ( int dim, double *a, double *b )
def pyfun ( dim, np.ndarray[double,ndim=1] arr1, np.ndarray[double,ndim=1] arr2 )
cfun ( dim, <double*> arr1.data, <double*> arr2.data )
return None