2018年2月19日

C++ から任意の Python 関数を実行する

Calling any Python function from C++
Abstract
  There is a few methods to run python functions on C++ like boost::python or using of Python.h. For example, using boost.python needs to convert C++ types like std::vector<T> frequently used as a standard implementation to Python types like numpy enable to give them as a function arguments. While this method will provide high execution efficiency, users need to prepare or convert types like std::vector<T> to Python types. For these reasons, sstd::c2py has been developed in order to call any Python functions with built-in, std::vector<T>, sstd::mat_c<T> or sstd::mat_r<T> (T is limitted by built-in types.) types from 2 lines of C++ codes.

  C++ から Python 関数を呼び出すには,boost::python や Python.h ヘッダからの API 呼び出しが知られている.例えば,boost::python を利用する場合は,C++ 側で Python 側へそのまま受け渡しができる numpy 型等を用意した上で python 関数の呼び出しを行う ([8] より推察).この手法は,高い実行効率を得られる反面,標準実装として C++ で頻繁に用いられる std::vector<T> 型をそのまま利用しようとした際に,ユーザ側で変換しておく必要がある ([9] より推察) など,実装に必要な手間も大きい.そこで,C++ の built-in 型 および std::vector<T>, sstd::mat_c<T>, sstd::mat_r<T> (ただし,T は built-in 型に限る.) と同等の numpy 型を引数に取る任意の Python 関数を C++ から 2 行で実行するライブラリを開発した.