[Numpy-discussion] [EXTERNAL] swig + numpy + variable length arrays

Bill Spotz wfspotz at sandia.gov
Mon Jul 16 16:49:19 EDT 2012


Andrew,

Since you are writing your own %inline function, the ARGOUT_ARRAY1 typemap is not appropriate.  You should put the argout logic in your function (ie, allocate the numpy array and give its data pointer to abelescalcall when you call it, and then return the array at the end).  Your signature for my_abeles should only take 4 arguments, and the typemaps should reduce it to two.

-Bill

On Jul 16, 2012, at 4:30 AM, Andrew Nelson wrote:

> Dear list,
> I am trying to SWIG a C function with the signature:
> 
> void abelescalcall(long numcoefs, double *coefP, long npoints , double
> *yP, double *xP);
> 
> numcoefs corresponds to the number of points in the coefP array.
> npoints corresponds to the number of points in yP and xP arrays.
> 
> coefP and xP are inputs (IN_ARRAY1), yP is an output (ARGOUT_ARRAY1).
> 
> I have been trying to use the following input file:
> 
> %module reflect
> 
> %{
>    #define SWIG_FILE_WITH_INIT
>    #include "myfitfunctions.h"
> %}
> 
> %include "numpy.i"
> 
> %init %{
>    import_array();
> %}
> 
> %apply (long DIM1, double* IN_ARRAY1){(long numcoefs, double* coefP)}
> %apply (long DIM1, double* ARGOUT_ARRAY1){(long len2, double* yP)}
> %apply (long DIM1, double* IN_ARRAY1){(long len3, double *xP)}
> 	
> %include "myfitfunctions.h"
> %rename (abelescalcall) my_abeles;
> 
> %inline %{
> my_abeles(long numcoefs, double* coefP, long len2, double* yP, long
> len3, double *xP) {
> 	if (len2 != len3) {
>        PyErr_Format(PyExc_ValueError, "Arrays of lengths (%d,%d)
> given", len2, len3);
>        return 0.0;
>    }
> 	abelescalcall(numcoefs, coefP, len2, yP, xP);
> }
> %}
> 
> However, if I look at the wrapper code, and if I try to call
> my_abeles, then I am asked for 6 parameters. I only want to supply two
> numpy arrays - coefP and xP.
> 
> thanks for any help you are able to give. (I spent the whole afternoon
> trying to get this bloomin thing to work).
> 
> cheers,
> Andrew.
> 
> -- 
> _____________________________________
> Dr. Andrew Nelson
> 
> 
> _____________________________________
> _______________________________________________
> 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