[SciPy-User] SWIG/numpy help

Leon Sit wing1127aishi at gmail.com
Fri Apr 30 10:28:41 EDT 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{
 public:
 dvector(const int rhs):dv(rhs){;};
  dvector();
 dvector(const int size, double* ptr):dv(size){
    copy(ptr, ptr+sizeof(double)*size, &(dv::data()[0]));
  }
 ~dvector(){}
};
-------------------------------------------------------------------------------------------------
with the SWIG interface that looks something like
-----------------------------------------------------------------------------------------------

%apply(int DIM1, double* INPLACE_ARRAY1) {(const int size, double* ptr)}
 class dvector{
 public:
 dvector(const int rhs);
  dvector();
  dvector(const int size, double* ptr);
	%newobject toString;
  char* toString();
	~dvector();
};
---------------------------------------------------------------------------------------------------
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
wing1127aishi at gmail.com
http://eden.rutgers.edu/~wingsit/



More information about the SciPy-User mailing list