如何将 2d numpy 数组传递给 C++ pybind11?

发布于 01-13 05:43 字数 508 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初与友歌2025-01-20 05:43:56

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文