python - 将 boost::python::numpy::ndarray 作为 boost::python 函数的(默认与否)参数传递?
问题描述
是否可以将 boost::python::numpy::ndarray 作为 boost::python 函数的(默认或不)参数传递?
dummy 没有 ndarray。愚蠢的一个 ndarray 参数,但没有默认值。silly 有一个 ndarray 作为默认值。
>> more dummy.cpp stupid.cpp silly.cpp
::::::::::::::
dummy.cpp
::::::::::::::
#include <boost/python.hpp>
namespace bp = boost::python;
int f(double x, double y=1.0) {return (int)(x+y);};
BOOST_PYTHON_MODULE(dummy)
{
bp::def("f", f, ( bp::arg("x"), bp::arg("y")=1.0 ) );
}
::::::::::::::
stupid.cpp
::::::::::::::
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
namespace bp = boost::python;
namespace np = boost::python::numpy;
int f(np::ndarray x, double y=1.0) {return (int)(x.shape(0)+y);};
BOOST_PYTHON_MODULE(stupid)
{
bp::def("f", f, ( bp::arg("x"), bp::arg("y")=1.0 ) );
}
::::::::::::::
silly.cpp
::::::::::::::
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
namespace bp = boost::python;
namespace np = boost::python::numpy;
int f(np::ndarray x, np::ndarray * y=nullptr) {return (int)(y ? x.shape(0)+y->shape(0) : x.shape(0));};
BOOST_PYTHON_MODULE(silly)
{
bp::def("f", f, ( bp::arg("x"), bp::arg("y")=nullptr ) );
}
>> make
g++ -I /usr/include/python2.7 -o dummy.so -fPIC -shared dummy.cpp -lboost_python -lboost_numpy -lpython2.7
g++ -I /usr/include/python2.7 -o stupid.so -fPIC -shared stupid.cpp -lboost_python -lboost_numpy -lpython2.7
g++ -I /usr/include/python2.7 -o silly.so -fPIC -shared silly.cpp -lboost_python -lboost_numpy -lpython2.7
>> python
Python 2.7.17 (default, Oct 19 2019, 23:36:22)
[GCC 9.2.1 20191008] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dummy; dummy.f(1)
2
>>> import numpy; import stupid; stupid.f(numpy.array([1, 2, 3]))
Segmentation fault
更新
尝试添加没有Py_Initialize(); np::initialize();
成功f
>> more stupid.cpp silly.cpp
::::::::::::::
stupid.cpp
::::::::::::::
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
namespace bp = boost::python;
namespace np = boost::python::numpy;
int f(np::ndarray x, double y=1.0) {
Py_Initialize();
np::initialize();
return (int)(x.shape(0)+y);
};
BOOST_PYTHON_MODULE(stupid)
{
bp::def("f", f, ( bp::arg("x"), bp::arg("y")=1.0 ) );
}
::::::::::::::
silly.cpp
::::::::::::::
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
namespace bp = boost::python;
namespace np = boost::python::numpy;
int f(np::ndarray x, np::ndarray * y=nullptr) {
Py_Initialize();
np::initialize();
return (int)(y ? x.shape(0)+y->shape(0) : x.shape(0));
};
BOOST_PYTHON_MODULE(silly)
{
bp::def("f", f, ( bp::arg("x"), bp::arg("y")=nullptr ) );
}
>> make
g++ -I /usr/include/python2.7 -o stupid.so -fPIC -shared stupid.cpp -lboost_python -lboost_numpy -lpython2.7
g++ -I /usr/include/python2.7 -o silly.so -fPIC -shared silly.cpp -lboost_python -lboost_numpy -lpython2.7
>> python
Python 2.7.17 (default, Oct 19 2019, 23:36:22)
[GCC 9.2.1 20191008] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy; import stupid; stupid.f(numpy.array([1, 2, 3]))
Segmentation fault
更新
好的,让它与调用一起工作 BOOST_PYTHON_MODULE
。仍然是带有默认参数的 KO(silly
示例)。
>> more stupid.cpp silly.cpp
::::::::::::::
stupid.cpp
::::::::::::::
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
namespace bp = boost::python;
namespace np = boost::python::numpy;
int f(np::ndarray x, double y=1.0) {
return (int)(x.shape(0)+y);
};
BOOST_PYTHON_MODULE(stupid)
{
Py_Initialize();
np::initialize();
bp::def("f", f, ( bp::arg("x"), bp::arg("y")=1.0 ) );
}
::::::::::::::
silly.cpp
::::::::::::::
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
namespace bp = boost::python;
namespace np = boost::python::numpy;
int f(np::ndarray x, np::ndarray * y=nullptr) {
return (int)(y ? x.shape(0)+y->shape(0) : x.shape(0));
};
BOOST_PYTHON_MODULE(silly)
{
Py_Initialize();
np::initialize();
bp::def("f", f, ( bp::arg("x"), bp::arg("y")=nullptr ) );
}
>> make
g++ -I /usr/include/python2.7 -o stupid.so -fPIC -shared stupid.cpp -lboost_python -lboost_numpy -lpython2.7
g++ -I /usr/include/python2.7 -o silly.so -fPIC -shared silly.cpp -lboost_python -lboost_numpy -lpython2.7
>> python
Python 2.7.17 (default, Oct 19 2019, 23:36:22)
[GCC 9.2.1 20191008] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy; import stupid; stupid.f(numpy.array([1, 2, 3]))
4
>>> import numpy; import silly; silly.f(numpy.array([1, 2, 3]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: No to_python (by-value) converter found for C++ type: decltype(nullptr)
解决方法
>> more silly.cpp
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
namespace bp = boost::python;
namespace np = boost::python::numpy;
int f(np::ndarray x, bp::object y) {
np::ndarray yy = np::array(bp::list());
if (!y.is_none()) yy = bp::extract<np::ndarray>(y);
return (int)(x.shape(0)+yy.shape(0));
};
BOOST_PYTHON_MODULE(silly)
{
Py_Initialize();
np::initialize();
bp::def("f", f, ( bp::arg("x"), bp::arg("y") ) );
}
>> make
g++ -I /usr/include/python2.7 -o silly.so -fPIC -shared silly.cpp -lboost_python -lboost_numpy -lpython2.7
>> python
Python 2.7.17 (default, Oct 19 2019, 23:36:22)
[GCC 9.2.1 20191008] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy; import silly; silly.f(numpy.array([1, 2, 3]), numpy.array([1, 2]))
5
>>> import numpy; import silly; silly.f(numpy.array([1, 2, 3]), None)
3
解决方案
为了能够使用numpy
首先初始化Python
运行时和numpy
模块:
Py_Initialize();
np::initialize();
未能调用这些会导致分段错误。
==================================================== ======================
对于silly.cpp
解决方法是不需要的。这是实现:
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
namespace bp = boost::python;
namespace np = boost::python::numpy;
int f(np::ndarray x, np::ndarray y = np::array(bp::list()) ) {
return (int)(x.shape(0)+y.shape(0));
};
BOOST_PYTHON_MODULE(silly)
{
Py_Initialize();
np::initialize();
bp::def("f", f, ( bp::arg("x"), bp::arg("y") = np::array(bp::list()) ) );
}
以及测试结果:
>>> import numpy, silly; silly.f(numpy.array([1, 2, 3]), numpy.array([1, 2]))
5
>>> import numpy, silly; silly.f(numpy.array([1, 2, 3]))
3
推荐阅读
- javascript - 大范围输入的素数总和
- angular - 带有选择的属性的角度 i18n
- javascript - 单击链接时关闭移动设备上的菜单
- docker - 我是否在容器上运行?
- python - 如何解决“模型用作中间模型但它没有模型的外键”?
- big-o - How can i solve the recursion T(n) = 5T(n/7) + logn?
- android - 自定义 CardViews 以匹配模型
- openshift - 启动 etcd 失败
- python - 重复项上的熊猫删除功能正在删除无效行
- swift - 如何在 Swift 的表视图中使用 didSelectRowAt 识别选择了哪个 Firebase 子项?