将numpy指针(dtype=np.bool)传递给C++
Passing a numpy pointer (dtype=np.bool) to C++
我想在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])
相关文章:
- 写入向量<向量<bool>>
- 让bool方法返回其他整数
- 如何在 std::vector 中找到<bool>哪些索引是真的?
- Arduino-C++ bool 不会从 false 变为 true
- 为什么在这种情况下,bool 类型的输出等于 0?
- 显式 std::exception_ptr 转换为 bool 不存在.VS2010 错误?
- 错误:不能使用"显式"说明符声明 bool'
- 为什么 C++ 11 在 ios 类中添加了运算符 bool
- 从标准::字符串到标准::矢量<bool>的快速转换
- 为什么 bool 和 _Bool 如果它们在内存中占用 1 个字节,它们只能存储 0 或 1
- 通过 mpi 发送 c++ 标准::矢量<bool>
- 为什么使用Pool和Bool而不是int8_t或char
- 如何为地图< map<int,int> 、bool > 分配值?
- 为什么更改包含 psapi.h 的顺序会产生编译错误?(标识符 BOOL 未定义)
- 使用 bool 和 const char 重载的 C++ 函数会在没有警告的情况下产生歧义 (MSVC2012)
- 返回 bool 作为 CPP 中 bool 运算符 [] 中的值
- 如何告诉自动推断向量<bool>元素的非引用类型
- 当 Type = bool 时,运算符 bool() 与模板 Type() 运算符冲突
- 为什么"std::set::erase(const key_type&)"返回"size_type"而不是"bool"?
- 使用LASlib时BOOL typedef重新定义