[Numpy-discussion] SWIG and numpy.i
Kevin Françoisse
kfrancoi@gmail....
Mon Mar 30 03:40:33 CDT 2009
Hello Bill,
Finaly, I just change my function header to take a double* rather than a
double**. It's working fine now. Thank you for all your answer and your
help!
Swig and numpy.i are really cool when you now how to use it!
I also use INPLACE array as a way output 2D arrays from my C function.
Kevin
On Wed, Mar 25, 2009 at 3:03 PM, Bill Spotz <wfspotz@sandia.gov> wrote:
> Kevin,
>
> In this instance, the best thing is to write a wrapper function that calls
> your matSum() function, and takes a double* rather than a double**. You
> can %ignore the original function and %rename the wrapper so that the python
> interface gets the name you want.
>
>
> On Mar 25, 2009, at 7:39 AM, Kevin Françoisse wrote:
>
> Thanks Bill, it helps me a lot ! My function works fine now.
>>
>> But I encounter an other problem. This time with a NumPy array of 2
>> dimensions.
>>
>> Here is the function I want to use :
>>
>> /****************/
>> double matSum(double** mat, int n, int m){
>> int i,j;
>> double sum = 0.0;
>> for (i=0;i<n;i++){
>> for (j=0;j<m;j++){
>> sum += mat[i][j];
>> }
>> }
>> return sum;
>> }
>> /****************/
>>
>> I supposed that the typemaps to use is the following :
>>
>> %apply (double* IN_ARRAY2, int DIM1, int DIM2) {(double** mat, int n, int
>> m)};
>>
>> But it is not working. Of course, my typemaps assignement is not
>> compatible with my function parameters. I tried several ways of using a two
>> dimensional array but I'm not sure what is the best way to do it ?
>>
>> Thanks
>>
>> ---
>> Kevin Françoisse
>> Ph.D. at Machine Learning Group at UCL
>> Belgium
>> kevin.francoisse@uclouvain.be
>>
>> On Tue, Mar 24, 2009 at 6:13 PM, Bill Spotz <wfspotz@sandia.gov> wrote:
>> Kevin,
>>
>> You need to declare vecSum() *after* you %include "numpy.i" and use the
>> %apply directive. Based on what you have, I think you can just get rid of
>> the "extern double vecSum(...)". I don't see what purpose it serves. As
>> is, it is telling swig to wrap vecSum() before you have set up your numpy
>> typemaps.
>>
>>
>> On Mar 24, 2009, at 10:33 AM, Kevin Françoisse wrote:
>>
>> 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;
>>
>> for(i=0;i<m;i++){
>> sum += vec[i];
>> }
>> return sum;
>> }
>>
>> /***/
>>
>> /* matrix.h */
>>
>> double vecSum(int* vec,int m);
>>
>> /***/
>>
>> /* matrix.i */
>>
>> %module matrix
>> %{
>> #define SWIG_FILE_WITH_INIT
>> #include "matrix.h"
>> %}
>>
>> extern double vecSum(int* vec, int m);
>>
>> %include "numpy.i"
>>
>> %init %{
>> import_array();
>> %}
>>
>> %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',
>> ['matrix.c','matrix.i'],
>> 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,
>>
>> Kevin
>> <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 **
>>
>>
>>
>>
>>
>>
>>
>>
> ** 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 **
>
>
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090330/a0cdbfbe/attachment.html
More information about the Numpy-discussion
mailing list