[Numpy-discussion] SWIG and numpy.i

Kevin Françoisse kfrancoi@gmail....
Tue Mar 24 09:33:47 CDT 2009

Hi everyone,

I have been using NumPy for a couple of month now, as part of my research
project at the university.  But now, I have to use a big C library I wrote
myself in a python project.  So I choose to use SWIG for the interface
between both my python script and my C library. To make things more
comprehensible, I wrote a small C methods that illustrate my problem:

/* matrix.c */

#include <stdlib.h>
#include <stdio.h>
/* Compute the sum of a vector of reals */
double vecSum(int* vec,int m){
   int  i;
   double sum =0.0;

       sum += vec[i];
   return sum;


/* matrix.h */

double vecSum(int* vec,int m);


/* matrix.i */

%module matrix
#include "matrix.h"

extern double vecSum(int* vec, int m);

%include "numpy.i"

%init %{

%apply (int* IN_ARRAY1, int DIM1) {(int* vec, int m)};
%include "matrix.h"


I'm using a python script to compile my swig interface and my C files
(running Mac OS X 10.5)

/* matrixSetup.py */

from distutils.core import setup, Extension
import numpy

setup(name='matrix', version='1.0', ext_modules =[Extension('_matrix',
    include_dirs = [numpy.get_include(),'.'])])


Everything seems to work fine ! But when I test my wrapped module in python
with an small NumPy array, here what I get :

>>> import matrix
>>> from numpy import *
>>> a = arange(10)
>>> matrix.vecSum(a,a.shape[0])
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: in method 'vecSum', argument 1 of type 'int *'

How can I tell SWIG that my Integer NumPy array should represent a int*
array in C ?

Thank you very much,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090324/8b3feca3/attachment.html 

More information about the Numpy-discussion mailing list