[Numpy-discussion] swig numpy2carray converters

Bill Spotz wfspotz at sandia.gov
Tue Nov 20 14:22:57 EST 2007

On Nov 20, 2007, at 7:24 AM, Georg Holzmann wrote:

> Yes but this means that you again allocate an array of the same size.
> E.g. in my algorithm I can have a very big internal matrix in C++

OK, so the key here is the *internal* matrix.  I think you need to  
provide a way to extract that matrix from the C++ application as a  
numpy array.  Then you can provide it to your function/method as an  
INPLACE array.  No new memory will be allocated.

>> that is not what you want, then you should probably be using the
>> INPLACE typemap.
> Yeah, but this results in the same as above ...

The INPLACE typemaps force you to provide the allocated memory.  If  
you do it by accessing whatever your C++ app has already allocated,  
you should be fine.

> Yes I would very much appreciate that.
> My use case is (as described above) an ARGOUT 1D and/or 2D array in C
> and/or Fortran style storage without copying the data (and also not
> allocating it again).

So assuming the INPLACE solution is sufficient for you, then the only  
new feature is fortran ordering.

> Hm ... maybe this is a misunderstanding ? - I mean with 2D output a  
> two
> dimensional numpy array (simply a matrix).
> In numpy2carray.i the macros are called ARRAY2_OUT and FARRAY2_OUT.

I guess my assumption was that in the general case you would want to  
retain the dimensions as input arguments, since it is logical for  
ARGOUT typemaps to allocate new memory.  Since swig typemaps only  
allow numinputs=0 or numinputs=1, this was problematic.  I guess the  
user could provide a sequence (tuple or list) as the single  
argument . . . don't know why I didn't think of that before.

But I still think you want an INPLACE typemap.

> Again, see above for my use case.
> But the fortran ordering should not be that hard (only setting the  
> flags
> and strides right, as in FARRAY2_OUT in numpy2carray.i) - but of  
> course
> someone has to do it ... ;)

Yes, it shouldn't be too hard.  And I like your FARRAY notation for  
the interface.  So your experience is that the flags and strides are  
all that have to be set differently?

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

More information about the NumPy-Discussion mailing list