C ++ 11 has become very useful. It's even more convenient with Boost. But python is easier to use. Projects based on NumPy (SciPy, matplotlib, pandas, ...) Is very easy to use and is maintained on a common platform, and operates reasonably fast. Should I use C ++ or bind to Python? Also, I haven't come to a conclusion, but I'll summarize the information for binding for the time being.
There are two problems.
--How to simply bind to Python --How to bind to NumPy
One of the reasons for using Python is to use NumPy-based libraries, so
It needs to be able to convert to NumPy's ndarray
etc.
The basis is Python C API and [NumPy C API](http://docs.scipy.org/doc/ numpy / reference / c-api.html). Bind C ++ functions and classes to Python through these. C ++ can of course call the Python C API directly, You have to control the reference count yourself. This is too difficult to play, so Boost.Python wraps that side like C ++. The reference count is managed by the smart pointer.
On the other hand, the NumPy C API is different from the Python C API, so Boost.Python itself does not include a wrapper for the NumPy API. So, based on Boost.Python, an interface for NumPy The implementation is Boost.NumPy. I wrote that it is currently in the Boost sandbox, It seems that Sandbox has not been in operation since the development of Boost itself moved to Github. Development is done on github. I'm not sure where I stand as Boost.
Install
git clone https://github.com/ndarray/Boost.NumPy
cd Boost.NumPy
I will proceed on the premise that I have done it.
cd libs/numpy/doc
make
You can see the documentation by opening libs / numpy / doc / _build / html / index.html
as
Detailed in the above document (omitted) Some changes are needed to compile in the python3 environment.
To use various SciPy algorithms
A function that takes numpy.ndarray
as an argument and returns the same ndarray
is required.
Create ndarray, which is the heart of NumPy, in C ++
project/CMakeLists.txt
numpy.cpp
numpy.cpp
#include "boost/numpy.hpp"
namespace p = boost::python;
namespace np = boost::numpy;
np::ndarray new_zero1(unsigned int N) {
p::tuple shape = p::make_tuple(N);
np::dtype dtype = np::dtype::get_builtin<double>();
return np::zeros(shape, dtype);
}
BOOST_PYTHON_MODULE(mymodule) {
Py_Initialize();
np::initialize();
p::def("new_zero", new_zero1);
}
Write like this. It's pretty intuitive to write.
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
# set(CMAKE_VERBOSE_MAKEFILE 1)
find_package(Boost COMPONENTS python3 REQUIRED) # for python3
find_package(PythonLibs REQUIRED)
include_directories(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
add_library(mymodule SHARED numpy.cpp)
target_link_libraries(mymodule ${Boost_LIBRARIES} ${PYTHON_LIBRARY} boost_numpy)
set_target_properties(mymodule PROPERTIES PREFIX "") #prefix'lib'To omit
cmake .
make
This will create mymodule.so
.
If you do not write set_target_properties, it will be libmymodule.so
.
Note that it may be LIBRARY or LIBRARIES.
More detailed usage next time.
Recommended Posts