Simple NumPy-compatible vector w/C++ & SWIG?
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? And: Any ideas on what to do on the C++ side? Is there any code/ library out there for a vector-thing that works well in C++ *and* that has wrapping code for NumPy? (I know the STL vector is wrapped to a Python list by default -- I'm just thinking that including those things in the equation would lead to lots of copied data...) -- Magnus Lie Hetland http://hetland.org
Magnus, I send you attached a SWIG file I use to interface PETSc libraries and NumPy. It is a series of macros (perhaps a bit nested), but I think this can help you to quickly define IN/OUT/INOUT typemaps for arguments like this: (int size, double* data). This approach was always enough for me. If your use case is more elaborated, please feel free to ask for other alternatives. On 2/24/06, Magnus Lie Hetland <magnus@hetland.org> 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?
And: Any ideas on what to do on the C++ side? Is there any code/ library out there for a vector-thing that works well in C++ *and* that has wrapping code for NumPy? (I know the STL vector is wrapped to a Python list by default -- I'm just thinking that including those things in the equation would lead to lots of copied data...)
-- Magnus Lie Hetland http://hetland.org
------------------------------------------------------- This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory! http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642 _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion
-- Lisandro Dalcín --------------- Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC) Instituto de Desarrollo Tecnológico para la Industria Química (INTEC) Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET) PTLC - Güemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594
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@physics.mcmaster.ca
participants (3)
-
David M. Cooke -
Lisandro Dalcin -
Magnus Lie Hetland