2014-02-27 24 views
2

SciPy的splprep(花鍵水面浮油)產生一個元組tckpPython的元組到用Cython STRUCT

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

+0

你打算打電話給脾嗎?你打算使用SciPy中的一個,還是使用相應的[fortran庫](http://www.netlib.org/dierckx/)中'splev'函數的封裝?調用過程將取決於你想如何調用它。 – IanH

+0

SciPy Wrapper的第一步 - 但我擔心需要更多的優化。 – martburg

+0

好吧,SciPy封裝器實際上只是Fortran例程的Python封裝器。你將需要在Cython中調用它,就像你用Python調用它一樣。這將是一個Python函數調用,並且仍然會有相應的開銷。如果您需要避免通過Python進行函數調用的開銷,則必須爲Fortran例程獲得某種包裝。我敢打賭,在http://www.fortran90.org/src/best-practices.html#interfacing-with-c中展示的方法將是一個很好的開始。您可以嘗試直接從Scipy公開函數指針。 – IanH

回答

0

正如在評論中討論使用

splev(樣條評價),這取決於你將如何傳遞tckp轉爲其他功能。存儲此信息並傳遞給其他功能的一種方法是使用struct

在下面你的例子中使用的struct列表tckp傳遞給cdef函數,它接受一個void *作爲輸入,模擬C函數...這個例子中功能加1所有陣列假設int0是大小陣列。

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