[SciPy-user] port C/C++ matlab mexing code to numpy

Albert Strasheim fullung at gmail.com
Mon Apr 24 12:28:39 EDT 2006


Hello Gen

I was able to get going with SWIG using the following:

http://projects.scipy.org/scipy/numpy/browser/trunk/numpy/doc/swig/
http://www.scipy.org/Cookbook/SWIG_and_NumPy

If you're building on Windows, check out:

http://projects.scipy.org/scipy/numpy/ticket/77

My SWIG file so far:

/* -*- C -*- */
%module _svmlight
%{
#define SWIG_FILE_WITH_INIT
#include "svm_light/svm_common.h"
#include "svm_light/svm_learn.h"
#include "svmlight_wrap.h"
%}
%include "numpy.i"
%init %{
   import_array();
%}
%apply (double* IN_ARRAY2, int DIM1, int DIM2) {(double* const features, int
const rows, int const cols)};
%apply (double* IN_ARRAY1, int DIM1) {(double* const labels, int const
size)};
#include "svmlight_wrap.h"
/* eof */

This wraps the following function defined in svmlight_wrap.h:

void* svmlearn(double* const features,
               int const rows,
               int const cols,
               double* const labels,
               int const size);

>From Python I can call it like so:

x = array([[1.0,2.0,3.0],[4.0,5.0,6.0]])
y = array([1,-1])
model = svmlight.svmlearn(x, y)

Check the example in the SWIG NumPy docs for more info.

Regards,

Albert

> -----Original Message-----
> From: scipy-user-bounces at scipy.net [mailto:scipy-user-bounces at scipy.net]
> On Behalf Of Gennan Chen
> Sent: 24 April 2006 18:17
> To: SciPy Users List
> Subject: Re: [SciPy-user] port C/C++ matlab mexing code to numpy
> 
> Thanks!! Since we have more C code than C++, is there a example for
> using SWIG with numpy in the numpy or scipy's repository?
> I need to access and return a numpy's 3d array for most of my
> calculation.
> 
> Gen
> 
> 
> On Apr 24, 2006, at 9:09 AM, Tom Denniston wrote:
> 
> > Look at swig and boost python.  Boost is more pythonic.  Swig a little
> > more automatic.  I would try both on small examples and determine what
> > you prefer.  I personally like boost a little better.
> >
> > On 4/24/06, Gennan Chen <gnchen at cortechs.net> wrote:
> >> Hi! All,
> >>
> >> We have a a lot of C/C++ code written for interacting with Matlab
> >> (i.,e mexing code). I was wondering what's the best approach to port
> >> them into python/numpy/scipy? How about using SWIG? Any
> >> recommendation will be welcomed....
> >>
> >> Gen-Nan Chen, PhD
> >> Chief Scientist
> >> Research and Development Group
> >> CorTechs Labs Inc (www.cortechs.net)
> >> 1020 Prospect St., #304, La Jolla, CA, 92037
> >> Tel: 1-858-459-9700 ext 16
> >> Fax: 1-858-459-9705
> >> Email: gnchen at cortechs.net




More information about the SciPy-User mailing list