Skip to content

Instantly share code, notes, and snippets.

Created September 22, 2013 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6659007 to your computer and use it in GitHub Desktop.
Save anonymous/6659007 to your computer and use it in GitHub Desktop.
import numpy as np
cimport numpy as np
from cpython cimport PyCObject_AsVoidPtr
from scipy.linalg.blas import cblas
# The following doesn' work (compiles, but returns incorrect values).
# ctypedef float (*sdot_ptr) (const int *N, const float *X, const int *incX, const float *Y, const int *incY) nogil
# cdef sdot_ptr sdot=<sdot_ptr>PyCObject_AsVoidPtr(cblas.sdot._cpointer)
# Works. Why does the return value have to be "double"?
ctypedef double (*sdot_ptr) (const int *N, const float *X, const int *incX, const float *Y, const int *incY) nogil
cdef sdot_ptr sdot=<sdot_ptr>PyCObject_AsVoidPtr(cblas.sdot._cpointer)
def test_sdot():
cdef int one = 1
cdef int size = 100
x = np.arange(1, size + 1).astype(np.float32)
y = 1.0 / x
return sdot(&size, <float *>(np.PyArray_DATA(x)), &one, <float *>(np.PyArray_DATA(y)), &one)
print test_sdot() # 100.0 when "double", but 0.0 when "float"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment