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

Raymond de Vries reedev@zonnet...
Thu Jul 30 05:19:16 CDT 2009


Hi

> Indeed, the solution is as simple as this ;) The trouble is to find
> the information!
>   
Yes, there is so much information everywhere. And it's hard to make the 
first steps.
>>>> 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.
>>>>         
I am now ready to do this. To be certain, 'contiguous' means that the 
std::vector<>'s are not the same length, right? Would that mean that I'd 
better use a tuple of lists or so? (or list of lists or so).

thanks for your time
Raymond

>>>> 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
>>
>>     
>
>
>
>   



More information about the NumPy-Discussion mailing list