I have knowledge of C and Python, but I am new to Cython.
I have a view on a (contiguous) array of double. I want to iterate over the items of the view through a pointer and an index, as we would do in C. However, I cannot express that with Cython:
from cpython cimport array
import array
arr = array.array("d", (1,2,3,4))
cdef double[::1] view = arr[::1]
cdef unsigned l = len(view)
cdef double *ptr = view.as_doubles
# Iterate over the view items
cdef double acc = 0.0
cdef unsigned i
for i in range(l):
acc += ptr[i]
The code above is rejected with an error:
a.pyx:8:5: Storing unsafe C derivative of temporary Python reference
How can I fix that?