Numpy NDPointer:不知道如何转换参数1

时间:2016-08-11 00:57:30

标签: python c++ numpy

我正在尝试将Numpy数组推送到C ++代码中。

C ++函数是,

extern "C"
void propagate(float * __restrict__ H, const float * __restrict__ W,
               const float * __restrict__ U, const float * __restrict__ x,
               float a, int h_len, int samples);

我的python代码是,

from numpy import *
from numpy.ctypeslib import ndpointer
import ctypes

lib = ctypes.cdll.LoadLibrary("libesn.so")

propagate = lib.propagate
propagate.restype = None
propagate.argtype = [ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"),
                     ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"),
                     ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"),
                     ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"),
                     ctypes.c_float, ctypes.c_int, ctypes.c_int]

H = W = U = X = zeros((10, 10))
a = 5.0
propagate(H, W, U, X, a, U.shape[0], X.shape[0])

我收到错误,

Traceback (most recent call last):
  File "./minimal.py", line 23, in <module>
    propagate(H, W, U, X, a, U.shape[0], X.shape[0])
ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

愚蠢的错别字......应该是propagate.argtypes。这解决了导致其他错误的神秘错误,这些错误已经在StackOverflow上有了答案。

propagate = lib.propagate
propagate.restype = None
propagate.argtypes = [ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"),
                      ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"),
                      ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"),
                      ndpointer(ctypes.c_float, flags="C_CONTIGUOUS"),
                      ctypes.c_float, ctypes.c_int, ctypes.c_int]