[Numpy-discussion] Simple NumPy-compatible vector w/C++ & SWIG?

David M. Cooke cookedm at physics.mcmaster.ca
Fri Feb 24 11:51:04 CST 2006

On Fri, Feb 24, 2006 at 01:26:16PM +0100, Magnus Lie Hetland wrote:
> Hi!
> I'm working on a data structure library where one of the element  
> types most likely will be a vector type (i.e., points in a  
> multidimensional space, with the dimensionality set by the user). In  
> the data structure (which is disk-based) I have work with raw bytes  
> that I'd like to copy around as little as possible.
> The library itself is (being) written in C++, but I'm wrapping it  
> with SWIG so I can drive and test it with Python. It seems to me that  
> something NumPy-compatible might be the best choice for the vector  
> type, but I'm not sure how I should do that.
> I've been thinking about simply implementing a minimal compatibility  
> layer for the NumPy Array Interface; is it then possible to construct  
> a NumPy array using this custom array, and get full support for the  
> various array operations without actually copying the data?

I assume you've looked at the array interface at
http://numeric.scipy.org/array_interface.html ?

If you implement that (if you're working with C or C++, adding just
__array_struct__ is probably the easiest), then numpy can use your
vectors without copying data. Call numpy.asarray(v), and you have a
numpy array with all the numpy methods.

|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca

More information about the Numpy-discussion mailing list