I am trying to expose eigen3 in python using Boost.Python.
I cannot find a way to expose the function unaryExpr (const CustomUnaryOp &func=CustomUnaryOp())
What I would like to have is something that allow me to something like that:
python
import libMatrix as mat
a = mat.Matrix(10, 10)
mat.unary_expr( lambda x : 1)
Do you have any idea ?? It may look like that:
void unary_expr(Matrix const& self, PyObject* callable_object)
{
cpp_callable = ??(callable_object)
self.unaryEpxr(cpp_callable);
}
=== What I tried: ==========================================
1) I tried to use a simple callback definition
typedef double(*UnaryExprType)(double);
void unary_expr(Matrix const& self, UnaryExprType a);
{
self.unaryEpxr( a );
}
but boost does not convert the python function into a UnaryExprType.
2) I tried to implement a struct PythonCallBack, Nevertheless, it does not work, I have got an error that the python signature did not match the c++ signature.
struct PythonCallBackBase
{
public:
virtual ~PythonCallBackBase() {}
virtual double operator() (double const & x) { return 0; }
};
struct PythonCallBack : PythonCallBackBase, boost::python::wrapper<PythonCallBackBase>
{
public:
typedef boost::python::wrapper<PythonCallBackBase> wrap;
double default_op(double const & x)
{
return 0;
}
double operator() (double const & x)
{
if (boost::python::override f = wrap::get_override("__call__"))
return f(x);
return PythonCallBackBase::operator ()(x);
}
};
void unary_expr(Matrix const& self, PythonCallBack a)
{
self.unaryEpxr( a );
}
Error message
ArgumentError: Python argument types in
Matrix.unary_expr(Matrix, Boost.Python.class)
did not match C++ signature:
unary_expr(Eigen::Matrix<double, -1, -1, 0, -1, -1>, PythonCallBack)
unary_expr(Eigen::Matrix<double, -1, -1, 0, -1, -1>, double (*)(double))