2024-06-01 08:41:59 发布
网友
假设我有几个数组,可能大小不同:
A0 = rand(3,3) A1 = rand(4,4)
在Cython中,我可以声明它们的类型以获得快速项目访问:
但是,假设我想通过索引访问它们:
A = (A0,A1) A[0][2,1] += A[1][1,0]
但是,现在Cython不知道A[0]和{}的类型,这使得访问速度很慢。我不认为Cython有“类型元组”的概念。那么,如何声明A(或类似的对象),以便在上面的表达式中仍能快速访问项?在
A[0]
A
这是否有效cython是推测性的,但其灵感来自结尾处的cython示例:
cython
https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html#putting-the-inner-loop-in-cython
我建议:
cdef foo(aTuple): cdef np.ndarray[double, ndim=2] A0 cdef np.ndarray[double, ndim=2] A1 A0, A1 = aTuple # use unpacking A0[2,1] += A1[1,0]
https://github.com/cython/cython/blob/master/tests/run/unpack.pyx-是解包测试pyx。在
pyx
或者可以使用A0 = aTuple[0]和A1 = aTuple[1];其思想是在重要的地方使用数组类型,用于它自己的索引。元组只保存指针,所以类型并不重要。在
A0 = aTuple[0]
A1 = aTuple[1]
使用cython中的数组时,另一个有用的页面是http://cython.readthedocs.io/en/latest/src/userguide/memoryviews.html
这是否有效
cython
是推测性的,但其灵感来自结尾处的cython
示例:https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html#putting-the-inner-loop-in-cython
我建议:
https://github.com/cython/cython/blob/master/tests/run/unpack.pyx-是解包测试
pyx
。在或者可以使用
A0 = aTuple[0]
和A1 = aTuple[1]
;其思想是在重要的地方使用数组类型,用于它自己的索引。元组只保存指针,所以类型并不重要。在使用
cython
中的数组时,另一个有用的页面是http://cython.readthedocs.io/en/latest/src/userguide/memoryviews.html相关问题 更多 >
编程相关推荐