Python Tuple到Cython Stru

2024-09-30 10:35:30 发布

您现在位置:Python中文网/ 问答频道 /正文

Scipy splprep(样条线准备)生成一个元组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(样条求值)


Tags: 节点scipy结构array向量cythonint样条
1条回答
网友
1楼 · 发布于 2024-09-30 10:35:30

正如注释中所讨论的,这取决于如何将tckp传递给其他函数。存储此信息并传递给其他函数的一种方法是使用struct。在

在下面的示例中,使用structtckp列表传递给一个cdef函数,该函数以void *作为输入,模拟一个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

相关问题 更多 >

    热门问题