将boost :: python :: numpy :: ndarray传递为boost :: python函数的参数(默认值或否)?

问题描述 投票:0回答:1

是否有可能将boost :: python :: numpy :: ndarray作为boost :: python函数的参数传递(默认或不传递)?

虚拟没有ndarray。愚蠢的一个ndarray参数,但没有默认值。傻有一个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

UPDATE

试图在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

UPDATE

确定,使其可以与调用in BOOST_PYTHON_MODULE一起使用。仍然使用默认参数(例如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)

WORKAROUND

>> 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
python c++ numpy boost-python numpy-ndarray
1个回答
0
投票

为了能够使用numpy,首先初始化Python运行时和numpy模块:

Py_Initialize();
np::initialize();

无法调用这些结果会导致细分错误。

© www.soinside.com 2019 - 2024. All rights reserved.