传递一个numpy的指针(DTYPE = np.bool),以C ++ [英] Passing a numpy pointer (dtype=np.bool) to C++

查看:3306
本文介绍了传递一个numpy的指针(DTYPE = np.bool),以C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用通过用Cython传递它的指针要在C型布尔++的numpy的数组。我已经知道如何与其它数据类型一样UINT8做到这一点。这样做与布尔以同样的方式这是行不通的。我能够编译但在运行时出现以下异常:

I'd like to use a numpy array of type bool in C++ by passing its pointer via Cython. I already know how to do it with other datatypes like uint8. Doing it the same way with boolean it does not work. I am able to compile but there is the following Exception during runtime:

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 ++方法:

Here is my c++ method:

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

在用Cython文件:

The Cython file:

# 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脚本调用该方法,并产生错误:

And here the python script that calls the method and produces the error:

import numpy as np
import rect

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

请让我知道如果你需要更多的信息。谢谢!

Please let me know if you need more information. Thank you!

推荐答案

它看起来像问题是与数组类型声明。
据当时 https://cython.readthedocs.org/en/文档最新/ src目录/教程/ numpy.html 布尔一阳还不支持,但是你可以通过铸造他们为无符号的八位整数数组使用它们。
下面是一个简单的例子,需要布尔值的一维数组的总和(同为和()方法会为一个布尔numpy的数组)

It looks like the problem is with the array type declaration. According to the documentation at https://cython.readthedocs.org/en/latest/src/tutorial/numpy.html boolean arays aren't yet supported, but you can use them by casting them as arrays of unsigned eight bit integers. Here's a simple example that takes the sum of a 1D array of boolean values (the same as the sum() method would for a boolean NumPy array)

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 ++ code,这取决于你在做什么,你可能需要转换指针回一个布尔值,我不知道这一点。

In your C++ code, depending on what you are doing, you may need to cast the pointer back to a bool, I'm not sure on that.

编辑:这里有一个如何转换指针在用Cython,这应该做你想做的一个例子。
我仍然需要数组类型为无符号的8位整数,但后来我投的指针回一个布尔值。

here's an example of how to cast the pointer in Cython, which should do what you want. I still had to type the array as an unsigned 8 bit integer, but I then cast the pointer back into a bool.

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文件:

If you want to pass the array in as a pointer, you could just use the following function in your Cython file:

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

这可以被称为

arptr(&A[0])

这篇关于传递一个numpy的指针(DTYPE = np.bool),以C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆