[Numpy-discussion] Numpy+SWIG with arrays in input and output of a function

Bill Spotz wfspotz@sandia....
Thu Apr 15 20:07:27 CDT 2010


Antoine,

You want a python function with two python array arguments, therefore  
you are dealing with two different typemaps, each of which requires  
its own %apply directive:

%apply (double* IN_ARRAY1    , int DIM1) {(double* vec , int m)};
%apply (double* ARGOUT_ARRAY1, int DIM1) {(double* vec2, int n)};

-Bill

On Apr 15, 2010, at 7:33 PM, Antoine Delmotte wrote:

> Dear Numpy and SWIG users,
>
> I am currently trying to use SWIG and Numpy to launch C++ codes from  
> python.
>
> My C++ code takes an array as an input (as well as integers, but  
> this will be my next problem...) and returns a different array (with  
> different dimensions).
>
> I have managed to make very simple codes taking an array in input OR  
> returning an array in output. However, I have not managed to develop  
> a code with an array in input and returning a different array in  
> output. Here is one of my attempts, which simply copies a vector (I  
> know I could use inplace for this example, but this is not what I  
> will need later since I want to return a different kind of array  
> than what I have as an input):
>
> /* matrix.c */
>
> #include <stdlib.h>
> #include <stdio.h>
> void vecOut(double* vec,int m,double* vec2,int n){
>    int  i,j;
>    double sum =0.0;
>
>    for(i=0;i<m;i++){
>        vec2[i] = 2*vec[i];
>    }
>
> }
>
>
> /* matrix.h */
>
> void vecOut(double* vec,int m,double* vec2,int n);
>
>
> /* matrix.i */
>
> %module matrix
> %{
> #define SWIG_FILE_WITH_INIT
> #include "matrix.h"
> %}
>
>
> %include "numpy.i"
>
> %init %{
> import_array();
> %}
>
> %apply (double* IN_ARRAY1, int DIM1, int* ARGOUT_ARRAY1, int DIM1)  
> {(double* vec, int m, double* vec2, int n)};
> %include "matrix.h"
>
>
> /* matrixSetup.py */
>
> from distutils.core import setup, Extension
> import numpy
>
> setup(name='matrix', version='1.0', ext_modules =[Extension('_matrix',
> ['matrix.c','matrix.i'],
>     include_dirs = [numpy.get_include(),'.'])])
>
>
>
> So, this is not working. The compilation is fine but when I try to  
> launch it, Python asks for 4 arguments. However, I would just like  
> to put one...
>
> matrix.vecOut([1,2,3])
> Traceback (most recent call last):
> TypeError: vecOut() takes exactly 4 arguments (1 given)
>
> I expected to get [1,2,3]...
>
>
> Is it possible to do this with SWIG? Or should I use something else?  
> Any advice?
>
> I thank you in advance for your precious suggestions.
>
> Best regards,
>
> Antoine
> Hotmail : un service de messagerie gratuit, fiable et complet  
> Profitez-en <ATT00002.txt>

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








More information about the NumPy-Discussion mailing list