[Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL valarray <=> numpy.array typemap to share?
Xavier Gnata
gnata at obs.univ-lyon1.fr
Mon Sep 10 16:20:06 EDT 2007
yeah! Looks good!
Thanks a lot.
Xavier
> Thanks!
>
> DG
>
> Lisandro Dalcin wrote:
>
>> David, I'll try to show you what I do for a custom C++ class, of
>> course this does not solve the issue resizing (my class does not
>> actually support resizing, so this is fine for me):
>>
>> My custom class is a templatized one called DTable (is like a 2d
>> contiguous array), but currently I only instantiate it for 'int' and
>> 'double'. This class have two relevant methods: getShape(), returning
>> a std::pair, and getArray(), returning a reference to an underliing
>> std::vector. So I am able to automatically support array interface by
>> using this:
>>
>> First, I define a templatized utility function 'array_interface'
>>
>> %header %{
>> namespace numpy {
>>
>> template<typename T> static char typechar() { return '\0'; }
>> template<> static char typechar<int>() { return 'i'; }
>> template<> static char typechar<double>() { return 'f'; }
>>
>> template<typename T>
>> static PyObject*
>> array_interface(const DTable<T>* self)
>> {
>> const std::pair<int,int>& shape = self->getShape();
>> const std::vector<T>& data = self->getArray();
>> void* array = const_cast<T*>(&data[0]);
>> char endian = PyArray_NATIVE;
>> char kind = typechar<T>();
>> int elsize = sizeof(T);
>> return Py_BuildValue("{sNsNsNsN}",
>> "shape", Py_BuildValue("ii", shape.first, shape.second),
>> "typestr", PyString_FromFormat("%c%c%d", endian, kind, elsize),
>> "data", Py_BuildValue("NO", PyLong_FromVoidPtr(array), Py_False),
>> "version", PyInt_FromLong(3));
>> }
>> }
>> %}
>>
>> Now define a SWIG macro to apply it to instantiations of my class
>>
>> %define %array_interface(Class)
>> %extend Class { PyObject* __array_interface__; }
>> %{
>> #define %mangle(Class) ##_## __array_interface__ ## _get(_t) \
>> numpy::array_interface(_t)
>> #define %mangle(Class) ##_## __array_interface__ ## _set(_t, _val) \
>> SWIG_exception_fail(SWIG_AttributeError, "read-only attribute")
>> %}
>> %enddef
>>
>> and finally instantiate my class with different names for 'int' and
>> 'double' in SWIG and finally apply previous macro
>>
>>
>> %template(DTableI) DTable<int>;
>> %template(DTableS) DTable<double>;
>>
>> %array_interface( DTable<int> );
>> %array_interface( DTable<double> );
>>
>> I think you can implement someting similar for std::vector,
>> std::valarray, or whatever... For use other data types, the only you
>> need is to specialize the 'typecode' function.
>>
>> Hope this help you.
>>
>>
>> On 9/4/07, David Goldsmith <David.L.Goldsmith at noaa.gov> wrote:
>>
>>
>>> Anyone have a well-tested SWIG-based C++ STL valarray <=> numpy.array
>>> typemap to share? Thanks!
>>>
>>> DG
>>> --
>>> ERD/ORR/NOS/NOAA <http://response.restoration.noaa.gov/emergencyresponse/>
>>> _______________________________________________
>>> Numpy-discussion mailing list
>>> Numpy-discussion at scipy.org
>>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>>
>>>
>>>
>>
>>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
--
############################################
Xavier Gnata
CRAL - Observatoire de Lyon
9, avenue Charles André
69561 Saint Genis Laval cedex
Phone: +33 4 78 86 85 28
Fax: +33 4 78 86 83 86
E-mail: gnata at obs.univ-lyon1.fr
############################################
More information about the NumPy-Discussion
mailing list