[Numpy-discussion] Global Numpy vector with Swig

Bill Spotz wfspotz@sandia....
Thu Feb 15 15:26:50 CST 2007


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, unless there are  
some safeties put in place for cvars, in which case you would get an  
exception).  Use

   foo.cvar.vec[:] = ...

to manipulate the data in-place.

On Feb 15, 2007, at 1:18 AM, Andrea Tomadin wrote:

> Hi,
> I need to pass a Numpy array to a C code wrapped by Swig.
> The array in the C code is a global variable declared as
> double *vec
> and I would like to set it in the calling Python module foo using e.g.
> foo.cvar.vec = numpy.zeros(10)
> so that the array is manipulated in place.
>
> I found out the examples in the numpy/doc/swig dir and adapted them
> to my code, so now I can pass a Numpy array to a function. But in the
> examples
> I was not able to recognize any suggestion (any typedef?) to
> translate an array into a
> variable. I'm just writing an intermediate function that does the job,
> but I feel like writing a wrapping function that will be wrapped
> again by Swig...
>
> Any practical example is highly appreciated!
>
> Regards,
> Andrea
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>

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





More information about the Numpy-discussion mailing list