[Numpy-discussion] [EXTERNAL] Numpy + SWIG

Bill Spotz wfspotz at sandia.gov
Mon Jun 4 18:17:31 EDT 2012


Gideon,

For these use cases, you will need to write short wrapper functions yourself.  In the online docs,

    http://docs.scipy.org/doc/numpy/reference/swig.interface-file.html

in the section entitled "Beyond the Provided Typemaps", subsection "A Common Example", there is an example of how to do this for a similar, but subtly different use case.  This example looks more like your second problem than your first, but you tackle the first problem using the same technique.

If you have trouble getting something to work, feel free to contact me off-list.

-Bill

On Jun 4, 2012, at 3:00 PM, Gideon Simpson wrote:

> There are two types of swig problems that I was hoping to get some help with. First, suppose I have some C function
> 
> void f(double *x, int nx, double *y, int ny);
> 
> where we input one array, and we output another array, both of which should be the same size.
> 
> I have used in my .i file:
> %apply(double *IN_ARRAY1, int DIM1){(double *x, int nx)}
> %apply(double *ARGOUT_ARRAY1, int DIM1){(double *y, int ny)}
> 
> and this produces a workable function.  However, it expects, as the functions second argument, the length of the array x. Now, it's easy enough to call:
> module.f(x, x.shape[0])
> 
> but is there a way to automatically get it to use the length of the array?
> 
> The second problem I have is for a function of the fomr
> 
> void g(double *x, int nx, double *y, int ny, double *z, int nz);
> 
> which evaluates some function g at all (x,y) pairs.  The the thing is that nx and ny need not be the same size, but nz should be nx * ny.  I'd like to wrap this too, and ideally it would also automatically handle the array lengths, but I'd be happy to have anything right now.  I'm also quite comfortable with the idea of packing z as a column array and reshaping it as necessary. 
> 
> 
> -gideon
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

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