Boost.Python和NumPy之间的另一个接口可以在这里找到:
它是NumPy C-API在Boost.Python接口中的一个中等完整的包装器,目的是最终将它提交给Boost。我不确定文档总体上比boost :: python :: numeric更好,但是有很多代码示例,至少它正在积极开发中。它非常低级,主要集中在如何解决如何在不复制的情况下将C数据传入NumPy和从NumPy传递C数据的更困难的问题,但是这里是你如何使用它复制std :: vector返回:
#include "boost/numpy.hpp"
namespace bp = boost::python;
namespace bn = boost::numpy;
std::vector myfunc(...);
bn::ndarray mywrapper(...) {
std::vector v = myfunc(...);
Py_intptr_t shape[1] = { v.size() };
bn::ndarray result = bn::zeros(1, shape, bn::dtype::get_builtin());
std::copy(v.begin(), v.end(), reinterpret_cast(result.get_data()));
return result;
}
BOOST_PYTHON_MODULE(example) {
bn::initialize();
bp::def("myfunc", mywrapper);
}