[Numpy-discussion] NumPtr vs NumPy.i to access C
Sat May 17 19:24:19 CDT 2008
On Sat, May 17, 2008 at 5:55 PM, Jose Martin <firstname.lastname@example.org> wrote:
> Hi, I'd like to access a C function from python, and the function takes input/output arrays. I'd probably use SWIG to do the interface to the C code. I found 2 options:
> -NumPtr module, to access Numeric arrays as pointers
> - numpy.i, a SWIG interface file for NumPy that defines typemaps
> I'm not sure if there is significant differences between the 2 options (besides using either NumPy or Numeric). Does numpy.i interface file use pointers to access NumPy arrays? or does it make a copy of the array to pass it to/from the C function?
> I'm new to programming and I'd like to make sure of this. I need to use in C very large arrays frequently, so I want to avoid making copies of it, because speed will be an important factor.
I'm fairly confident that numpy.i *will not* copy an array unless it
is necessary to do so. Nice contiguous arrays should be passed
I use SWIG extensively in scipy.sparse:
One nice aspect of SWIG is that you can have it automatically dispatch
the right function. For instance, if you have
foo(float * arr) and foo(double * arr)
then you can use SWIG typemaps pick the correct function to use.
You can also make SWIG upcast to the appropriate types. For example,
if in Python you passed an int array and a double array to:
foo(double * arr1, double * arr2)
then you can have SWIG automatically upcast the int array to double
and delete it before returning to Python.
Nathan Bell email@example.com
More information about the Numpy-discussion