如何将 2d numpy 数组传递给 C++ pybind11?
在 C++ pybind11 包装器中:
.def("calcSomething",
[](py::array_t<double> const & arr1,
py::array_t<double> const & arr2)
{
// do calculation
}
)
在 python 中:
example.calcSomething(
arr1=np.full((10, 2), 20, dtype='float64'),
arr2=np.full((10, 2), 100, dtype='float64')
)
我收到此错误消息:
ValueError: array has incorrect number of dimensions: 2; expected 1
那么我应该如何将 2d 或 nd 数组传递给 pybind11?
In C++ pybind11 wrapper:
.def("calcSomething",
[](py::array_t<double> const & arr1,
py::array_t<double> const & arr2)
{
// do calculation
}
)
In python:
example.calcSomething(
arr1=np.full((10, 2), 20, dtype='float64'),
arr2=np.full((10, 2), 100, dtype='float64')
)
And I got this error message:
ValueError: array has incorrect number of dimensions: 2; expected 1
So how should I pass 2d or nd array to pybind11?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

在 pybind11 中,
array_t
是一个 n 维数组,就像 numpy 数组一样。所以没有维数限制。
该错误消息可能来自其他地方。不是来自 pybind11。
至少您显示的代码不应导致此行为。
In pybind11
array_t
is an n-dimensional array, just like a numpy array.So there are no restrictions on dimensionality.
The error message is probably coming from somewhere else. Not from pybind11.
At least the code you show should not cause this behavior.