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

Antoine Delmotte antoinedelmotte@hotmail....
Thu Apr 15 18:33:33 CDT 2010


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
https://signup.live.com/signup.aspx?id=60969
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100415/932b2893/attachment.html 


More information about the NumPy-Discussion mailing list