您可以使用ndpointer http://docs.scipy.org/doc/numpy/reference/routines.ctypeslib.html#numpy.ctypeslib.ndpointer from numpy.ctypeslib http://docs.scipy.org/doc/numpy/reference/routines.ctypeslib.html将第一个参数声明为类型的一维连续数组numpy.complex64
:
import numpy as np
from numpy import ctypeslib
# ...code to load the shared library as `lib` not shown...
# Declare the argument types of lib.foo:
lib.foo.argtypes = [ctypeslib.ndpointer(np.complex64, ndim=1, flags='C'), c_int]
然后你可以这样做,例如,
z = np.array([1+2j, -3+4j, 5.0j], dtype=np.complex64)
lib.foo(z, z.size)
您可能想将其包装在不需要第二个参数的函数中:
def foo(z):
# Ensure that we use a contiguous array of complex64. If the
# call to foo(z, z.size) modifies z in place, and that is the
# intended effect of the function, then the following line should
# be removed. (The input z is then *required* to be a contiguous
# array of np.complex64.)
z = np.ascontiguousarray(z, dtype=np.complex64)
# Call the C function.
lib.foo(z, z.size)