質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++11

C++11は2011年に容認されたC++のISO標準です。以前のC++03に代わるもので、中枢の言語の変更・修正、標準ライブラリの拡張・改善を加えたものです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

494閲覧

pybind11で、クラス内関数にfloatのポインタを与える方法

taka3391

総合スコア5

C++11

C++11は2011年に容認されたC++のISO標準です。以前のC++03に代わるもので、中枢の言語の変更・修正、標準ライブラリの拡張・改善を加えたものです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2023/03/30 10:07

編集2023/03/31 08:05

以下のようなfloatのポインタを引数として受け取るクラス内関数があるとき、どのようにpython側から呼べば良いのでしょうか?

#include <pybind11/numpy.h> #include <pybind11/pybind11.h> namespace py = pybind11; class MyClass { public: void myFunc(float *arr, int size) { for (int i = 0; i < size; i++) { arr[i] += 1.0f; } } }; PYBIND11_MODULE(example, m) { py::class_<MyClass>(m, "MyClass") .def(py::init<>()) .def("my_func", &MyClass::myFunc); }

以下のようにpython側で呼んでいるのですが、エラーが出ていて困っている状況です。

import numpy as np import sys sys.path.append( "./build" ) import example # Create an instance of MyClass my_obj = example.MyClass() # Create a NumPy array arr = np.array([1.0, 2.0, 3.0], dtype=np.float32) # Call myFunc with the NumPy array my_obj.my_func(arr, arr.size) # Print the updated array print(arr)

以下がエラーです。

Traceback (most recent call last):
File "test2.py", line 13, in <module>
my_obj.my_func(arr, arr.size)
TypeError: my_func(): incompatible function arguments. The following argument types are supported:
1. (self: example.MyClass, arg0: float, arg1: int) -> None
Invoked with: <example.MyClass object at 0x7f987db50ef0>, array([1., 2., 3.], dtype=float32), 3

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

クラスをwrapするクラスを作成することで解決しました。

cpp

1#include <pybind11/pybind11.h> 2#include <pybind11/numpy.h> 3#include <pybind11/stl.h> 4 5namespace py = pybind11; 6 7class MyClass { 8public: 9 void myFunc(float *arr, int size) { 10 for (int i = 0; i < size; i++) { 11 arr[i] += 1.0f; 12 } 13 } 14}; 15 16class MyClassWrapper { 17private: 18 MyClass my_py_class; 19public: 20 void myPyFunc(py::array_t<float> input_array, int size) { 21 // Get a pointer to the underlying data 22 float *data = static_cast<float *>(input_array.request().ptr); 23 24 my_py_class.myFunc(data, size); 25 } 26}; 27 28PYBIND11_MODULE(example, m) { 29 py::class_<MyClassWrapper>(m, "MyClassWrapper") 30 .def(py::init<>()) 31 .def("myPyFunc", [](MyClassWrapper& wrapper, py::array_t<float> input_array, int size){ 32 wrapper.myPyFunc(input_array, size); 33 }); 34}

python側のコードは以下の通りです。

python

1import numpy as np 2import sys 3sys.path.append( "./build" ) 4import example 5 6# Create an instance of MyClass 7my_obj = example.MyClassWrapper() 8 9# Create a NumPy array 10arr = np.array([1.0, 2.0, 3.0], dtype=np.float32) 11 12# Call myFunc with the NumPy array 13my_obj.myPyFunc(arr, arr.size) 14 15# Print the updated array 16print(arr) #[2. 3. 4.] 17

投稿2023/04/01 07:12

taka3391

総合スコア5

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問