将numpy指针(dtype=np.bool)传递给C++

Passing a numpy pointer (dtype=np.bool) to C++

本文关键字:C++ bool np 指针 numpy dtype      更新时间:2023-10-16

我想在C++中通过Cython传递指针来使用bool类型的numpy数组。我已经知道如何使用uint8等其他数据类型。以与布尔值相同的方式执行它是不起作用的。我可以编译,但在运行时出现以下异常:

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    c = r.count(b, 4)
  File "rect.pyx", line 41, in rect.PyRectangle.count (rect.cpp:1865)
    def count(self, np.ndarray[bool, ndim=1, mode="c"] array not None, int size):
ValueError: Does not understand character buffer dtype format string ('?')

这是我的c++方法:

void Rectangle::count(bool * array, int size)
{
    for (int i = 0; i < size; i++){
        std::cout << array[i] << std::endl;
    }
}

Cython文件:

# distutils: language = c++
# distutils: sources = Rectangle.cpp
import numpy as np
cimport numpy as np
from libcpp cimport bool
cdef extern from "Rectangle.h" namespace "shapes":
    cdef cppclass Rectangle:
        Rectangle(int, int, int, int) except +
        int x0, y0, x1, y1
        void count(bool*, int)
cdef class PyRectangle:
    cdef Rectangle *thisptr      # hold a C++ instance which we're wrapping
    def __cinit__(self, int x0, int y0, int x1, int y1):
        self.thisptr = new Rectangle(x0, y0, x1, y1)
    def __dealloc__(self):
        del self.thisptr
    def count(self, np.ndarray[bool, ndim=1, mode="c"] array not None, int size):
        self.thisptr.count(&array[0], size)

这里是调用方法并产生错误的python脚本:

import numpy as np
import rect
b = np.array([True, False, False, True])
c = r.count(b, 4)

如果你需要更多信息,请告诉我。非常感谢。

看起来问题出在数组类型声明上。根据上的文件https://cython.readthedocs.org/en/latest/src/tutorial/numpy.html布尔型arays还不受支持,但您可以通过将它们强制转换为无符号八位整数的数组来使用它们。这里有一个简单的例子,它取布尔值的1D数组的和(与用于布尔NumPy数组的sum()方法相同)

from numpy cimport ndarray as ar
cimport numpy as np
cimport cython
@cython.boundscheck(False)
@cython.wraparound(False)
def cysum(ar[np.uint8_t,cast=True] A):
    cdef int i, n=A.size, tot=0
    for i in xrange(n):
        tot += A[i]
    return tot

在你的C++代码中,根据你正在做的事情,你可能需要将指针投射回bool,我不确定。

编辑:这里有一个如何在Cython中投射指针的例子,它应该可以做你想做的事情。我仍然需要将数组键入为无符号的8位整数,但随后我将指针强制转换回布尔。

from numpy cimport ndarray as ar
cimport numpy as np
from libcpp cimport bool
cimport cython
def cysum(ar[np.uint8_t,cast=True] A):
    cdef int i, n=A.size, tot=0
    cdef bool *bptr
    bptr = <bool*> &A[0]
    for i in xrange(n):
        tot += bptr[i]
    return tot

如果您想将数组作为指针传入,您可以在Cython文件中使用以下函数:

cdef bool* arptr(np.uint8_t* uintptr):
    cdef bool *bptr
    bptr = <bool*> uintptr
    return bptr

可以称为

arptr(&A[0])