Boost.Python을 사용하여 파이썬에서 eigen3을 노출하려고합니다.C++의 인수로 파이썬 함수
내가하고 싶은 무엇 functionunaryExpr (const CustomUnaryOp &func=CustomUnaryOp())
를 노출 할 수있는 방법을 찾을 수없는 것은 그런 일에 저를 허용 뭔가 :
파이썬
import libMatrix as mat
a = mat.Matrix(10, 10)
mat.unary_expr(lambda x : 1)
당신이 어떤이 있습니까 아이디어 ?? 다음과 같이 보일 수 있습니다.
void unary_expr(Matrix const& self, PyObject* callable_object)
{
cpp_callable = ??(callable_object)
self.unaryEpxr(cpp_callable);
}
=== 내가 시도한 것 : ============== ============
1) 나는 UnaryExprType에 파이썬 기능을 변환하지 않습니다 간단한 콜백 정의
typedef double(*UnaryExprType)(double);
void unary_expr(Matrix const& self, UnaryExprType a);
{
self.unaryEpxr(a);
}
하지만 부스트를 사용했습니다.
2) 나는 implement a structPythonCallBack
을 시도했지만, 작동하지 않습니다. 파이썬 서명이 C++ 서명과 일치하지 않는다는 오류가 있습니다.
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);
}
오류 메시지
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))
불가능한 것 같다 여기
는 완전한 최소한의 예입니다. http://www.boost.org/doc/libs/1_52_0/libs/python/doc/v2/faq.html – Setepenre