[英]Using ctypes with GSL to pass array
According to the GSL documentation, the signature of is: 根据GSL文档,签名为:
double gsl_stats_correlation (const double data1[],
const size_t stride1,
const double data2[],
const size_t stride2,
const size_t n)
When I try to call it from PyPy with: 当我尝试通过PyPy调用它时:
from ctypes import CDLL, RTLD_GLOBAL
gslcblas = CDLL('libgslcblas.0.dylib',mode=RTLD_GLOBAL)
libgsl = CDLL('/usr/local/lib/libgsl.0.dylib')
from ctypes import c_double, c_size_t, pointer
a1 = (c_double * 5)(1, 2, 3, 4, 5)
a2 = (c_double * 5)(1, 2, 3, 6, 5)
print(libgsl.gsl_stats_correlation(a1, c_size_t(1),
a2, c_size_t(1), c_size_t(5)))
The result on my machine is 1086463496 at the moment, although it changes from run to run. 目前,我的机器上的结果是1086463496,尽管每次运行时结果都不同。 That obviously is far from correct. 这显然是不正确的。 What am I doing wrong? 我究竟做错了什么? Note that changing the function call to: 请注意,将函数调用更改为:
libgsl.gsl_stats_correlation(pointer(a1), c_size_t(1),
pointer(a2), c_size_t(1), c_size_t(5)))
gives exactly the same result. 给出完全相同的结果。
You need to set the restype
of the function like this: 您需要设置restype
这样的功能:
libgsl.gsl_stats_correlation.restype = c_double
Have a look at this ctypes tutorial (particularly this section ) to read about specifying argument and response types properly. 查看此 ctypes教程(特别是本节 ),以了解有关正确指定参数和响应类型的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.