[Numpy-discussion] (newbie) How can I use NumPy to wrap my C++ class with 2-dimensional arrays?

Matthieu Brucher matthieu.brucher@gmail....
Thu Jul 30 01:52:07 CDT 2009


Hi,

In fact, it's not that complicated. You may know the way how copying a
vector, and this is all you need
(http://matt.eifelle.com/2008/01/04/transforming-a-c-vector-into-a-numpy-array/).
You will have to copy your data, it is the safest way to ensure that
the data is always valid.
For the std::vector<>[], I suggest you convert it to a single vector,
as the data inside this "array" is not contiguous and it can thus be
cumbersome to create a Numpy array from that.

Once the structure is prepared, you have to allocate the dimensions,
but this may be available online.

In case you know that the C++ data won't go away before the Python
array, you can always wrap the container
(http://matt.eifelle.com/2008/11/04/exposing-an-array-interface-with-swig-for-a-cc-structure/)
with SWIG.

Matthieu

2009/7/30 Raymond de Vries <reedev@zonnet.nl>:
> Hi everyone,
>
> (I sent this message yesterday as well but somehow it didn't come
> through...)
>
> I would like to ask your advice how I can use NumPy to wrap my existing
> C++ library with 2-dimensional arrays. I am wrapping the library with
> swig and I have the typemap function declaration. But now I am
> struggling a lot to find an implementation for getting access to the
> 2-dimensional arrays in my C++ classes. I have wrapped most of the
> library but the multi-dimensional arrays are problematic. From what I
> have read, I can use NumPy for this..
>
> There is so much information about NumPy, Numeric etc that it is not
> clear anymore which example I can use best. It's clear that NumPy is
> probably the way to go.
>
> This is a small example of the class that I would like to wrap:
> struct MyClass {
>  float array1[2][2];
>  std::vector<float> array2[2];
> };
>
> I've also read that I need to call import_array() but where should I put
> this?
>
> Is there a small example with NumPy c-api to wrap/convert my
> 2-dimensional arrays to python?
>
> Thanks a lot already
> Raymond
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Information System Engineer, Ph.D.
Website: http://matthieu-brucher.developpez.com/
Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92
LinkedIn: http://www.linkedin.com/in/matthieubrucher


More information about the NumPy-Discussion mailing list