Hello,
I'm facing a strange issue, likely due to my lack of understanding of the memoryview syntax.
My code layout is like this:
1) I have a fortran function that accepts 1-d arrays. To pass references, I use memoryviews.
2) My input is a 3-d array, and I have to pass arr[x,y,:] in each call to the fortran function.
The way I tried to do this was
cdef np.double_t[::1] arr_view
#shape of array = (X,Y,Z)
def func(np.ndarray[np.double_t, dim=3] arr):
global arr_view
arr_view = np.zeros((Z), dtype=np.double, order='F')
for x in range(X):
for y in range(Y):
arr_view[:] = arr[x,y,:]
fortran_func(<double *>&arr_view[0])
However, this code crashes at
arr_view[:] = arr[x,y,:]
saying "TypeError: only length-1 arrays can be converted to Python scalars"
however, if I modify this code to read
def func(np.ndarray[np.double_t, dim=3] arr):
global arr_view
cdef np.double_t[::1] ptr
arr_view = np.zeros((Z), dtype=np.double, order='F')
for x in range(X):
for y in range(Y):
ptr = arr[x,y,:]
arr_view[:] = ptr[:]
fortran_func(<double *>&arr_view[0])
This compiles and runs successfully.
Is there something obviously wrong with the first version that I don't understand?
TIA,
Joy