Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This is a "structarray in cython with numpy recarrays" testfile
- import numpy as np
- cimport numpy as np
- # My structarray has nodes with fields x and y
- # This also works without packed, but I have seen packed used in other places where people asked similar questions
- # I assume that for two doubles that is equivalent but is necessary for in8s in between
- cdef packed struct node:
- double x
- double y
- # I suppose that would be the equivalent numpy dtype?
- # Note: During compilation it warns me about double to float downcasts, but I do not see where
- nodetype = [('x' , np.float64),('y', np.float64)]
- def fun():
- # Make 10 element recarray
- # (Just looked it up. A point where 1-based indexing would save a look in the docs)
- mynode1 = np.recarray(10,dtype=nodetype)
- # Recarray with cdef struct
- mynode1 = np.recarray(10,dtype=nodetype)
- # Fill it with non-garbage somewhere
- mynode1[2].x=1.0
- mynode1[2].y=2.0
- # Brave: give recarray element to a c function assuming its equivalent to the struct
- ny = cfuny(mynode1[2])
- assert ny==2.0 # works!
- # Test memoryview, assuming type node
- cdef node [:] nview = mynode1
- ny = cfunyv(nview,2)
- assert ny==2.0 # works!
- # This sets the numpy recarray value with a c function the gts a memoryview
- cfunyv_set(nview,5,9.0)
- assert mynode1[5].y==9.0 # alsow works!
- return 0
- # return node element y from c struct node
- cdef double cfuny(node n):
- return n.y
- # give element i from memoryview of recarray to c function expecting a c struct
- cdef double cfunyv(node [:] n, int i):
- return cfuny(n[i])
- # write into recarray with a function expecting a memoryview with type node
- cdef int cfunyv_set(node [:] n,int i,double val):
- n[i].y = val
- return 0
Add Comment
Please, Sign In to add comment