繁体   English   中英

Python元组到Cython结构

[英]Python Tuple to Cython Struct

Scipy splprep(样条预处理)产生一个元组tckp

tckp:元组(t,c,k)一个元组,包含结向量,B样条系数和样条程度。

tckp = [array[double,double ,..,double], 
       [array[double,double ,..,double],          
        array[double,double ,..... ,double], 
        array[double,double ,..... ,double]], int]                                        

如何构造和填充等效的Cython结构以能够使用

Cython中的splev(样条评估)

如评论中所述,这取决于如何将tckp传递给其他功能。 存储此信息并传递给其他功能的一种方法是使用struct

在下面的示例中,您将使用structtckp列表传递给一个以void *作为输入的cdef函数,模拟了一个C函数...此示例函数假定所有数组的大小为int0则向所有数组加1。

import numpy as np
cimport numpy as np

cdef struct type_tckp_struct:
    double *array0
    double *array1
    double *array2
    double *array3
    int *int0

def main():
    cdef type_tckp_struct tckp_struct
    cdef np.ndarray[np.float64_t, ndim=1] barray0, barray1, barray2, barray3
    cdef int bint

    tckp = [np.arange(1,11).astype(np.float64),
            2*np.arange(1,11).astype(np.float64),
            3*np.arange(1,11).astype(np.float64),
            4*np.arange(1,11).astype(np.float64), 10]
    barray0 = tckp[0]
    barray1 = tckp[1]
    barray2 = tckp[2]
    barray3 = tckp[3]
    bint = tckp[4]
    tckp_struct.array0 = &barray0[0]
    tckp_struct.array1 = &barray1[0]
    tckp_struct.array2 = &barray2[0]
    tckp_struct.array3 = &barray3[0]
    tckp_struct.int0 = &bint

    intern_func(&tckp_struct)

cdef void intern_func(void *args):
    cdef type_tckp_struct *args_in=<type_tckp_struct *>args
    cdef double *array0
    cdef double *array1
    cdef double *array2
    cdef double *array3
    cdef int int0, i
    array0 = args_in.array0
    array1 = args_in.array1
    array2 = args_in.array2
    array3 = args_in.array3
    int0 = args_in.int0[0]

    for i in range(int0):
        array0[i] += 1
        array1[i] += 1
        array2[i] += 1
        array3[i] += 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM