Using ctypes with GSL to pass array

138 views Asked by At

According to the GSL documentation, the signature of is:

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:

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. 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.

1

There are 1 answers

0
DBrowne On BEST ANSWER

You need to set the restype of the function like this:

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.