[Numpy-discussion] Access to C/C++, typemaps, pyrex...

Francesc Altet faltet at carabos.com
Thu Mar 23 10:17:12 CST 2006

A Dijous 23 Març 2006 18:58, Christopher Barker va escriure:
> > http://scipy.org/Cookbook/Pyrex_and_NumPy
> > http://scipy.org/Cookbook/SWIG_and_NumPy
> Has anyone done this (for either SWIG or Pyrex) for the generic array
> interface, rather than explicitly for the numpy types? I'd love to see
> examples of how to write extensions that can take ANY nd-array. (I now
> that's only NumPy, Numeric and numarray now, but I'm hopeful it will
> become a standard)

I don't think this is feasible yet, although I don't think this is a
big limitation. You can always compile your program against NumPy or
numarray or Numeric and follow the previous recipes (adapted to the
package of choice) to access to its data. The only thing that you must
be aware of is to convert all the objects that foes to the extension
to an object of the package of choice.

For example, if you have chosen NumPy as your package to compile with,
then you only have to convert Numeric/numarray objects into a NumPy
one with:

native_numpy_object = numpy.asarray(your_foreign_object)

The array protocol will ensure that the conversion will be as
efficient as possible. What's more, if the "foreign" objects are
well-behaved (i.e. contiguous and native-endian ordered), then a data
copy will not be made, so letting you to access to the original data

PyTables follows this approach and I must say that's very handy.


>0,0<   Francesc Altet     http://www.carabos.com/
V   V   Cárabos Coop. V.   Enjoy Data

More information about the Numpy-discussion mailing list