I would like to use something like structarray in cython, and I would like this structarray to be easily accessible in python, as in cython. Based on a whim, I used repetition using a dtype that looks like the structure I would like to use. Curiously, it just works and allows me to use c structarray, which is above the hood;), is a multiple repetition for the python user.
Here is my example
import numpy as np
cimport numpy as np
cdef packed struct node:
double x
double y
nodetype = [('x' , np.float64),('y', np.float64)]
def fun():
mynode1 = np.recarray(10,dtype=nodetype)
mynode1 = np.recarray(10,dtype=nodetype)
mynode1[2].x=1.0
mynode1[2].y=2.0
ny = cfuny(mynode1[2])
assert ny==2.0 # works!
cdef node [:] nview = mynode1
ny = cfunyv(nview,2)
assert ny==2.0 # works!
cfunyv_set(nview,5,9.0)
assert mynode1[5].y==9.0 # alsow works!
return 0
cdef double cfuny(node n):
return n.y
cdef double cfunyv(node [:] n, int i):
return cfuny(n[i])
cdef int cfunyv_set(node [:] n,int i,double val):
n[i].y = val
return 0
Of course, I am not the first to try this.
, , , , , . , - . , (, ), , , cstruct , - .
, , . - , ?
,
- numpy cython?
- , , , . , , , . , , numpy , cython ?