Error wrapping Eigen/C++ with Python using Swig -
Error wrapping Eigen/C++ with Python using Swig -
i'm having problem wrapping little project uses eigen (the linear algebra package) using swig. i'm getting python error don't understand , can't find much online - suspect there c++ memory corruption somewhere. i've boiled downwards toy example.. unfortunately it's still reasonable long:
--- testfunc.cxx ----
#include "eigen/dense" eigen::vectorxd test(eigen::matrixxd data){ eigen::vectorxd temp; homecoming temp; }
--- testswig.i -----
%module testswig %{ #define swig_file_with_init #include "eigen/core" #include <python.h> #include <numpy/arrayobject.h> #include "testfunc.cxx" %} %init %{ import_array(); %} %include "numpy.i" %typemap(out) eigen::vectorxd { npy_intp dims[1] = {$1.size()}; pyobject* array = pyarray_simplenew(1, dims, npy_double); double* info = ((double *)pyarray_data( array )); (int = 0; != dims[0]; ++i){ *data++ = $1.data()[i]; } $result = array; } %typemap(in) eigen::matrixxd (eigen::matrixxd temp) { int rows = 0; int cols = 0; rows = pyarray_dim($input,0); cols = pyarray_dim($input,1); pyarrayobject* temp; pyarg_parsetuple($input, "o", &temp); temp.resize(rows,cols); temp.fill(0); double * values = ((double *) pyarray_data($input)); (long int = 0; != rows; ++i){ for(long int j = 0; j != cols; ++j){ // std::cout << "data " << data[i] << std::endl; temp(i,j) = values[i*rows+j]; } } } %include "testfunc.cxx"
--- setup.py ----
from distutils.core import setup, extension import numpy numpyinclude = numpy.__file__[:-12] + 'core/include/' testswig = extension('_testswig', sources=['testswig_wrap.cxx'], include_dirs=['../', numpyinclude]) setup (name = 'testswig', version = '0.1', author = "noname", description = """ """, ext_modules = [testswig], py_modules = ["testswig"])
----- building ------
i building in folder 3 files , folder 'eigen' containing eigen headers. commands :
swig -c++ -python -i./ testswig.i python setup.py install
------- error ----------
then run python file containing
import testswig import numpy np print testswig.test(np.array([[2,3],[4,5]]))
which gives error "systemerror: new style getargs format argument not tuple".
note few things: 1) same commands run fine straight python interpreter. 2) if function not homecoming eigen::vectorxd, or not take eigen:matrixxd, works fine.
thanks time.
in in typemap have:
pyarrayobject *temp; pyarg_parsetuple($input, "o", &temp);
this wrong - $input
pyobject has been extracted arguments @ stage, not tuple, want:
pyarrayobject *temp=null; if (pyarray_check($input)) temp = (pyarrayobject*)$input;
to verify it's right type , cast if is.
python c++ swig
Comments
Post a Comment