Extend python in C ++ (Boost.NumPy)

Why bind C ++ classes / functions to Python?

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.

How to bind?

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.

document build

cd libs/numpy/doc
make

You can see the documentation by opening libs / numpy / doc / _build / html / index.html as

Install with cmake

Detailed in the above document (omitted) Some changes are needed to compile in the python3 environment.

How to use

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

Extend python in C ++ (Boost.NumPy)
Next Python in C
C API in Python 3
Boost.NumPy Tutorial for Extending Python in C ++ (Practice)
Binary search in Python / C ++
ABC166 in Python A ~ C problem
Solve ABC036 A ~ C in Python
How to wrap C in Python
Solve ABC037 A ~ C in Python
Write C unit tests in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Solve ABC175 A, B, C in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Algorithm in Python (ABC 146 C Binary Search
Implement FIR filters in Python and C
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Write O_SYNC file in C and Python
N-Gram in Python
Programming in python
python C ++ notes
Plink in Python
Constant in python
python, openFrameworks (c ++)
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Generate C language from S-expressions in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Run Python in C ++ on Visual Studio 2017
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python