[SciPy-User] SWIG/numpy help

Leon Sit wing1127aishi@gmail....
Fri Apr 30 09:28:41 CDT 2010

Hi all:

I am trying to pass data around the numpy and boost::ublas layers. I
have written an ultra thin wrapper because swig cannot parse ublas'
header correctly. The code is shown below

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/lexical_cast.hpp>

#include <algorithm>
#include <sstream>
#include <string>

using std::copy;
using namespace boost;

typedef boost::numeric::ublas::matrix<double> dm;
typedef boost::numeric::ublas::vector<double> dv;

class dvector : public dv{
 dvector(const int rhs):dv(rhs){;};
 dvector(const int size, double* ptr):dv(size){
    copy(ptr, ptr+sizeof(double)*size, &(dv::data()[0]));
with the SWIG interface that looks something like

%apply(int DIM1, double* INPLACE_ARRAY1) {(const int size, double* ptr)}
 class dvector{
 dvector(const int rhs);
  dvector(const int size, double* ptr);
	%newobject toString;
  char* toString();
I have compiled them successfully via gcc 4.3 and vc++9.0. However
when I simply run

a = dvector(array([1.,2.,3.]))

it gives me a segfault. This is the first time I use swigh with numpy
and not have fully understanding between the data conversion and
memory buffer passing. Does anyone see something obvious I have
missed? Anything is appreciated.

Best Regards,

Leon Sit
Graduate Student in Statistics at Rutgers University

More information about the SciPy-User mailing list