C++、Python、不兼容的数字类型

发布于 2024-12-09 06:49:12 字数 1468 浏览 0 评论 0原文

我在 Boost 中使用 vector_indexing_suite 时遇到困难。 在 C++ 中,我定义了:

  class_<std::vector<double> >("PyVecDouble")
                         .def(vector_indexing_suite<std::vector<double> >());

  class_<std::vector<long> >("PyVecLong")
                         .def(vector_indexing_suite<std::vector<long> >());

在 python 中,我尝试在以下简单程序中使用它们:

def NumpyArrayToPyVecDouble(vec):
    n = len(vec)
    p_vec = jp.PyVecDouble()

    for i in xrange(0,n):
        p_vec.append(vec[i])

    return p_vec

def NumpyArrayToPyVecLong(vec):
    n = len(vec)
    p_vec = jp.PyVecLong()

    for i in xrange(0,n):
        p_vec.append(vec[i])

    return p_vec

example_array = np.array([1.1, 2.2, 3.3, 4.4])
example = NumpyArrayToPyVecDouble(double_array)

dates_array = np.array([01122011, 01062012, 01122012, 01062013])
dates = NumpyArrayToPyVecLong(dates_array)

结果,该程序计算向量示例,但在尝试计算向量日期时返回以下错误:

TypeError: Attempting to append an invalid type

和想法为什么? C++ 中的 Long 与 Python 不兼容吗?当我用 int 替换所有地方的 long 时,这也不起作用。非常感谢帮助!

!更新! 当将输入作为 python 列表而不是 numpy 数组给出时,NumpyArrayToPyVecLong 可以正常工作。我尝试过制作各种类型的 numpy 数组(int16、int32、int64、uint16 等),但它们都不起作用。它仅在给定一个简单的 python 列表时才有效。知道为什么这些类型都与 C++ long 不兼容吗?

!更新!第二个: 解决方案是使用 p_vec.append(vec[i]) ,但这实际上并不能解决 numpy 数组和 C++ 类型如何对齐的问题。所以理论上问题仍然悬而未决......

I am having difficulty using the vector_indexing_suite in Boost.
In C++ I have defined:

  class_<std::vector<double> >("PyVecDouble")
                         .def(vector_indexing_suite<std::vector<double> >());

and

  class_<std::vector<long> >("PyVecLong")
                         .def(vector_indexing_suite<std::vector<long> >());

And in python, I have tried to use these in the following simple program:

def NumpyArrayToPyVecDouble(vec):
    n = len(vec)
    p_vec = jp.PyVecDouble()

    for i in xrange(0,n):
        p_vec.append(vec[i])

    return p_vec

def NumpyArrayToPyVecLong(vec):
    n = len(vec)
    p_vec = jp.PyVecLong()

    for i in xrange(0,n):
        p_vec.append(vec[i])

    return p_vec

example_array = np.array([1.1, 2.2, 3.3, 4.4])
example = NumpyArrayToPyVecDouble(double_array)

dates_array = np.array([01122011, 01062012, 01122012, 01062013])
dates = NumpyArrayToPyVecLong(dates_array)

As a result, the program computes the vector example, but returns the following error when it tries to compute the vector dates:

TypeError: Attempting to append an invalid type

And ideas why? Are Longs in C++ incompatible with Python? This also does not work when I replace long everywhere with int. Help much appreciated!

!UPDATE!
NumpyArrayToPyVecLong works fine when given the input as a python list as opposed to a numpy array. I've tried making various types of numpy arrays (int16, int32, int64, uint16, etc) but none of them work. It only works when given a plain python list. Any ideas why these types are all incompatible with the C++ long?

!UPDATE! the second:
A solution for this is just to use p_vec.append(vec[i]) but this doesn't actually answer the problem of how numpy arrays and C++ types are aligned. So the questions is still open in theory...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

标点 2024-12-16 06:49:12

这里列出了 Numpy 和 C 类型之间的关系(检查“兼容:C ...”部分):
http://docs.scipy.org /doc/numpy/reference/arrays.scalars.html#built-in-scalar-types

指定大小的类型(int16 等)映射到 C int, longlong long 等以特定于平台的方式。然而,numpy/ndarrayobject.h 定义了 typedef npy_int8 等。

The relationships between Numpy and C types is listed here (check the "compatible: C ..." sections):
http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html#built-in-scalar-types

The size-specified types (int16 etc.) map to C int, long, long long etc. in a platform-specific way. numpy/ndarrayobject.h however defines typedefs npy_int8 and so on.

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