[Numpy-discussion] SWIG, typemaps, 2D argout arrays

Sebastian Haase haase@msg.ucsf....
Tue Oct 14 03:02:36 CDT 2008

On Tue, Oct 14, 2008 at 6:40 AM, T J <tjhnson@gmail.com> wrote:
> Hi,
> I'm new to using SWIG and my reading of numpy_swig.pdf tells me that
> the following typemap does not exist:
>  (int* ARGOUT_ARRAY2, int DIM1, int DIM2)
> What is the recommended way to output a 2D array?  It seems like I should use:
>  (int* ARGOUT_ARRAY1, int DIM1)
> and then provide a python function which reshapes the 1D array?  Is it
> correct that there will be insignificant performance disadvantages to
> this?  Also, is there any way to do this in an automated fashion?  My
> first thought is that I'd need to create this function outside of the
> python module that SWIG creates.
> Thanks!

Hi tjhnson,

I have been using SWIG for many years with numpy and before with  numarray;
but I still feel somewhat "insecure"...

a) There should be no performance difference between 1d and 2d -- its
sets just a different shape. (right?)
b) I don't want to use Python / numpy API code in the C functions I'm
wrapping - so I limit myself to "input" arrays! Since array memory
does not distinguish between input or output (assuming there is no
copying needed because of alignment or contiguity issues) the only
implication of this is that I have to allocate the array outside the C
functions.  I don't expect any noticable performance hit from this;
only caveat is that I have to know the "output" array SIZE before I go
into the C function.
The advantage of this is that I don't have to worry about using the
correct malloc (or rather equivalent numpy API) function and/or
garbage collection (object destructor)
(( there was a post by Travis Oliphant (Sep.10:: NumPy arrays that use
memory allocated from other libraries or tools) were he mentioned a
blog http://blog.enthought.com/?p=62   where is further discussed ))
An extra 2-line Python wrapper-function handles this
input<->conversion transparently for my --- do you need to handle
non-contiguous arrays ?

HTH, I'm eager to learn here ;-)

- Sebastian Haase

More information about the Numpy-discussion mailing list