[Numpy-discussion] swig numpy2carray converters
grh at mur.at
Tue Nov 20 15:38:55 EST 2007
> 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.
> 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.
Hm ... I still don't understand that ...
With INPLACE I have to allocate the numpy array before, then pass it to
my getMyMatrix(my_inplace_numarray) method, then copy the data in that
method to the my_inplace_numarray - right ?
But I am still not convinced by the INPLACE method - the more natural
way for me is to simply return the matrix ...
Maybe the most usefull way would be to add also OUT_WITHOUTCOPY macros
to numpy.i, where the data is allocated with PyArray_FromDimsAndData() ?
(as a "long term" goal ...)
>> 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.
Again I do not see the problem - see e.g. ARRAY2_OUT_COPY in
numpy2carray.i, shouldn't this be the same ?
> 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?
Yes, so far I had no crash ... ;)
The only thing to do is the following:
PyArrayObject *tmp = (PyArrayObject*)obj;
tmp->flags = NPY_FARRAY;
int s = tmp->strides;
tmp->strides = s;
tmp->strides = s * dim0;
More information about the NumPy-Discussion