Re: [SciPy-user] How to: Pass NumPy arrays to C & use SWIG to generate modules
![](https://secure.gravatar.com/avatar/96e8dcae45ffdf4ce9e7cc8d1bc10606.jpg?s=120&d=mm&r=g)
If all you want to do is pass an array of doubles (with its length) to C, you can use the attached typemap and swig file. I think they represent the maximum possible distillation of the process. Read the short comments in example.i. (Uh, I'm afraid the posting might break the newlines... I will also send you the files in attachment). Let me know if you need help with compiling. Cheers, Michele ==== the following is my example.i (the SWIG file) ==== %module example %{ #include "example.h" %} // suppose your example.h contains the function declaration // void examplefunction(double *array,long arraylength); // to which you want to pass a numpy 1-D array; // what we do here is to apply the numpy-to-array typemap // defined in numpy_typemaps.i to your function, which is then // included by the %include line below %include numpy_typemaps.i %typemap(in) (double *array,long arraylength) = (double *numpyarray,long numpyarraysize); %include "example.h" ==== the following is file numpy_typemaps.i ==== #include "numpy/arrayobject.h" #define ISCONTIGUOUS(m) (PyArray_ISCONTIGUOUS(m)) #define PyArray_CONTIGUOUS(m) (ISCONTIGUOUS(m) ? Py_INCREF(m), m : \ (PyArrayObject *)(PyArray_ContiguousFromObject((PyObject *)(m), (m)->descr->type_num, 0,0))) %} %init %{ import_array(); %} /* %typemap(in) (double* numarray, long length) { */ %typemap(in) (double *numpyarray,long numpyarraysize) { PyArrayObject *arr; if (!PyArray_Check($input)) { PyErr_SetString(PyExc_TypeError,"First argument is not an array"); return NULL; } if (PyArray_ObjectType($input,0) != PyArray_DOUBLE) { PyErr_SetString(PyExc_TypeError, "Incorrect array type: we need an array of DOUBLE"); return NULL; } arr = PyArray_CONTIGUOUS((PyArrayObject *)$input); if (arr->nd == 1) { $1 = (double *)arr->data; $2 = (long)arr->dimensions[0]; } else if (arr->nd == 2) { $1 = (double *)arr->data; $2 = (long)arr->dimensions[0] * (long)arr->dimensions[1]; } else { PyErr_SetString(PyExc_TypeError, "Incorrect number of dims: we want a 1D or 2D array"); return NULL; } Py_DECREF(arr); /* Release our local copy of the PyArray */ } ==== --- SciPy Users List <scipy-user@scipy.org wrote: Thanks. I saw that site in a previous google. But it
seems to be aimed at Windows. I am on a Mac. Rather than fight that battle now, I will try today to just generate my own simple C extension test routine. If I get that far, then that will probably be the way for me to go. I haven't checked out pyrex, though. I might look at that when I take a break.
If I get the C extension working, I will post an announcement about it. It might help non-gurus like me get started.
-- Lou Pecora
Naval Research Lab, Washington, DC
--- Gael Varoquaux <gael.varoquaux@normalesup.org>
wrote:
Lou,
I am not a very advanced python user. I do not master writing C extentions. I have tried simple things, and had success with them because they were simple enough for me. In order of difficulty this is what I did:
- scipy.weave (great, but won't probably suit your purposes, have a look at
the wiki page)
- pyrex (same reamrk than above, though the concept
is fabulous)
- passing array to C code with ctypes. I think this can suit you. It might not be as versatile or powerful as SWIG, but I found it was simple enough for my limited knowledge. I started out with simple hello word C functions, and was able to pass numpy arrays back and forth between numpy and C thanks to Albert Strasheim's wiki page http://scipy.org/Cookbook/Ctypes2
I cannot help you out with SWIG, as I have decided it was
to complicated
for me so far.
Ga�l
____________________________________________________________________________________
Want to start your own business? Learn how on Yahoo! Small Business.
http://smallbusiness.yahoo.com/r-index _______________________________________________
SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
participants (1)
-
vallis.35530172@bloglines.com