[Numpy-discussion] Global Numpy vector with Swig

Bill Spotz wfspotz at sandia.gov
Fri Feb 16 15:18:07 EST 2007


OK, I looked at the varin, varout descriptions in the online manual,  
and they specifically mention global variables, but WOW is that  
documentation minimal.  I would suggest asking Swig- 
user at lists.sourceforge.net for some assistance.

On Feb 16, 2007, at 1:10 PM, Bill Spotz wrote:

> Andrea,
>
> It is my understanding that swig typemaps only apply to function  
> arguments.  Since what you are talking about is a global variable,  
> I don't believe typemaps will help you.  I would try
>
> %{
> #include "header-that-contains-vec.h"
> npy_intp vec_dims[ ] = { (npy_intp) length_of_vec };
> %}
> %rename(vec) numpy_vec;
> %ignore vec;
> %include "header-that-contains-vec.h"
> %inline %{
> PyObject * numpy_vec = PyArray_SimpleNewFromData(1, vec_dims,  
> NPY_DOUBLE, (void*)vec);
> %}
>
> I guess it would be nice if you could force a "Py_DECREF 
> (numpy_vec);" before the module is destroyed, but I'll leave that  
> as an exercise...
>
> On Feb 16, 2007, at 10:59 AM, Andrea Tomadin wrote:
>
>> Il giorno 15/feb/07, alle ore 22:26, Bill Spotz ha scritto:
>>
>>> It seems to me you would need to %ignore vec, so that it is not
>>> wrapped as a raw pointer to a double, and then in your interface
>>> file create a PyArrayObject whose data buffer points to vec (the
>>> most efficient way to do this is with %inline).  Then use %rename
>>> to rename whatever you called your PyArrayObject to "vec" (or not,
>>> if you do not care about the name).  Finally,
>>>
>>>   foo.cvar.vec = numpy.zeros(10)
>>>
>>> is going to decrement (and probably delete) your original vec and
>>> replace it with the new array of zeros (I think,
>>
>> Thank you Bill for your help, even if writing this code is not
>> straightforward for me. Anyway, while I was reading the docs, I
>> became aware of the varin, varout typemap options. I wonder if I can
>> define a
>> typemap(varin) double *
>> declaration, and insert in it the same code that in "numpy.i"
>> translates the PyArrayObject into a C array and vice versa.
>> But I cannot find an example of typemp(varin) for Python, so I don't
>> know what to use for the output, instead of $1. Where are the  
>> typemaps
>> (varin) for Python?
>> It seems strange that nobody wrote a public code for such a simple
>> task, as I read that Swig supports Python since the 90s! I suspect I
>> have just not looked in the right places?!
>>
>> Thank you again for help!
>> Andrea
>
> ** 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 **
>
>

** 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