Reputation: 592
I'm having trouble wrapping a small project that uses Eigen (the linear algebra package) using SWIG. I'm getting a python error that I don't understand and can't find much about online - but I suspect there is some C++ memory corruption somewhere. I've boiled this down to a toy example.. but unfortunately it's still reasonable long:
--- testfunc.cxx ----
#include "Eigen/Dense"
Eigen::VectorXd test(Eigen::MatrixXd data){
Eigen::VectorXd temp;
return 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* data = ((double *)PyArray_DATA( array ));
for (int i = 0; i != 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));
for (long int i = 0; i != 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 am building this in a folder with all 3 files and a folder 'Eigen' containing the Eigen headers. The commands are :
swig -c++ -python -I./ testswig.i
python setup.py install
------- error ----------
Then I run a python file containing
import testswig
import numpy as np
print testswig.test(np.array([[2,3],[4,5]]))
Which gives the error "SystemError: new style getargs format but argument is not a tuple".
Note a few things: 1) The same commands run fine directly from a python interpreter. 2) If the function does not return an Eigen::VectorXd, or does not take an Eigen:MatrixXd, it works fine.
Thanks for your time.
Upvotes: 2
Views: 1677
Reputation: 88751
In your in typemap you have:
PyArrayObject *temp;
PyArg_ParseTuple($input, "O", &temp);
This is incorrect - $input
is a PyObject that has already been extracted from the arguments at this stage, but not a tuple, so you want:
PyArrayObject *temp=NULL;
if (PyArray_Check($input))
temp = (PyArrayObject*)$input;
To verify that it's the correct type and then cast if it is.
Upvotes: 3