[Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL valarray <=> numpy.array typemap to share?

Bill Spotz wfspotz at sandia.gov
Wed Sep 5 14:04:37 EDT 2007


On Sep 5, 2007, at 11:19 AM, Christopher Barker wrote:

> Bill Spotz wrote:
>> I have been considering adding some C++ STL support to numpy/doc/ 
>> swig/
>> numpy.i.  Probably std::vector<TYPE> <=> PyArrayObject (and some
>> std::complex<TYPE> support as well).  Is this what you had in mind?
>
> well, std::valarray is not the same as std::vector, though there are
> similarities, so the example would be helpful.

Ah, silly me.  Back when I first learned C++, valarray wasn't around  
yet (or at least it wasn't taught to me), and it is not in the  
(clearly outdated) references I use.  But it is a more logical choice  
than vector.

> Of greatest concern to me is the memory management issue -- it  
> would be
> nice to be able to share the data black between the valarray and the
> numpy array (rather than copying back and forth), but as the both
> valarrays and vectors are re-sizeable, that might get tricky. I'm
> assuming that you can get the pointer to the data block from both, but
> it might change on you.  If you solve that for std::vector, the  
> solution
> is likely to be similar for std:valarray. (I hope).

Yes, this resizing memory management issue is the main reason I  
haven't tried to implement it in numpy.i yet.

<thinking out loud>
A possibly better solution would be to develop a class that inherits  
from std::valarray<TYPE> but also implements the array interface  
attributes (these attributes would have to be dynamic, in that they  
check the std::valarray attributes when accessed rather than storing  
copies).  We could then write typemaps that utilize the array  
interface.  So pure input arguments could be numpy.ndarrays (or any  
reasonable sequence, really), but output arrays would be a wrapped  
version of this new class.  (Which would behave both like  
std::valarrays and like numpy.ndarrays.  I think...)
</thinking out loud>

** Bill Spotz                                              **
** Sandia National Laboratories  Voice: (505)845-0170      **
** P.O. Box 5800                 Fax:   (505)284-5451      **
** Albuquerque, NM 87185-0370    Email: wfspotz at sandia.gov **






More information about the NumPy-Discussion mailing list