[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 04:54:09 CDT 2009


2009/7/30 Raymond de Vries <reedev@zonnet.nl>:
> Hi,
>
> I'm sorry, I guess I did not search properly before.... For the record,
> I solved my import_array() question: just need to add
> %init %{
> import_array();
> %}

Indeed, the solution is as simple as this ;) The trouble is to find
the information!

> and the typemap for the std::vector<double> works ok. Thanks for that!

You're welcome.

> Now the rest...
>
> Thanks
> Raymond
>
>
> Raymond de Vries wrote:
>> Hi Matthieu,
>>
>> Thanks for your quick reply! I did not find this page earlier. I have
>> studied the first page and now I realize again that all my efforts so
>> far (last few days) have crashed on the lack of calling import_array().
>> Do you have a suggestion for calling import_array() as well? Everywhere
>> I see *that* import_array() needs to be called, but not how...
>>
>> I will take a look at the conversion to a single vector. I hope this can
>> be avoided because I cannot simply change the library.
>>
>> regards
>> Raymond
>>
>>
>> Matthieu Brucher wrote:
>>
>>> 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
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
>
> _______________________________________________
> 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