C++ 如何创建boost::python::对象类型的numpy数组
我正在尝试创建一个2x2 numpython对象数组:C++ 如何创建boost::python::对象类型的numpy数组,c++,boost,boost-python,C++,Boost,Boost Python,我正在尝试创建一个2x2 numpython对象数组: #include <boost/python.hpp> #include <boost/python/numpy.hpp> int main() { Py_Initialize(); boost::python::numpy::initialize(); boost::python::tuple shape = boost::python::make_tuple(2, 2); boos
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
int main()
{
Py_Initialize();
boost::python::numpy::initialize();
boost::python::tuple shape = boost::python::make_tuple(2, 2);
boost::python::object obj;
boost::python::numpy::dtype dt = boost::python::numpy::dtype(obj);
boost::python::numpy::ndarray array = boost::python::numpy::empty(shape, dt);
std::cout << "Datatype is: " << boost::python::extract<char const *> boost::python::str(array.get_dtype())) << std::endl;
}
#包括
#包括
int main()
{
Py_初始化();
boost::python::numpy::initialize();
boost::python::tuple shape=boost::python::make_tuple(2,2);
boost::python::objectobj;
boost::python::numpy::dtype dt=boost::python::numpy::dtype(obj);
boost::python::numpy::ndarray数组=boost::python::numpy::empty(shape,dt);
std::cout您正确地使用了dtype
构造函数;是obj
造成了问题
默认结构boost::python::object obj;
将obj
设置为'None'
python对象。与'None'
关联的dtype
是NPY\u默认的数组描述符类型。在创建numpy
数组时,它映射到一个双精度
,这解释了您的output。(从Python的角度来看这很有意义-默认的numpy
数组类型是双精度浮点类型。)
您可以使用
在你的例子中,这就是修复方法。我还冒昧地使用了一个匿名临时文件,这是Boost文档中的做法。“O”
表示对象类型
boost::python::numpy::dtype dt = boost::python::numpy::dtype(boost::python::object("O"));