python – 定义更复杂的静态数组

在数值方法中,常常有很多系数是静态的,因为它们对于特定方法是固定的.我想知道在Cython / C中设置这样的数组或变量的最佳方法是什么.

在我的情况下,除了系数和阶段数之外,Runge-Kutta积分方法大致相同.现在我做的事情(简化)

# Define some struct such that it can be used for all different Runge-Kutta methods
ctypedef struct RKHelper:
    int numStages
    double* coeffs

cdef:
    RKHelper firstRKMethod
    # Later secondRKMethod, thirdRKMethod, etc.

firstRKMethod.numStages = 3
firstRKMethod.coeffs = <double*> malloc(firstRKMethod.numStages*sizeof(double))

# Arrays can be large and most entries are zero
for ii in range(firstRKMethod.numStages):
    firstRKMethod.coeffs[ii] = 0.

# Set non-zero elements
firstRKMethod.coeffs[2] = 1.3

一些要点:

>我知道malloc不适用于静态数组,但我不知道如何在Cython中将“numStages”或“RKHelper”声明为静态,所以我不能使用静态数组…或者我做的事情就像RKHelper中的“double [4]”,它不允许对所有RK方法使用相同的结构定义.
>我想知道是否有比循环更好的方法.我不想手动设置整个数组(例如,数组= [0.,0.,0.,0,1.3,……大部分数字大多为零]).
>据我所知,Cython中没有“真正的”静态变量,有吗?

有没有更好的方式做我想做的事情?

干杯

最佳答案 实现您想要实现的目标的一种方法是将Runge Kutta方案的系数设置为全局变量,这样您就可以使用静态数组.这会很快,但绝对是丑陋的

丑陋的解决方案:

cdef int numStages = 3
# Using the pointer notation you can set a static array
# as well as its elements in one go
cdef double* coeffs = [0.,0.,1.3]
# You can always change the coefficients further as you wish

def RungeKutta_StaticArrayGlobal():
    # Do stuff

    # Just to check
    return numStages

更好的解决方案是定义一个以Runge Kutta系数为成员的cython类

优雅的解决方案:

cdef class RungeKutta_StaticArrayClass:
    cdef double* coeffs
    cdef int numStages
    def __cinit__(self):
        # Note that due to the static nature of self.coeffs, its elements
        # expire beyond the scope of this function    
        self.coeffs = [0.,0.,1.3]
        self.numStages = 3

    def GetnumStages(self):
        return self.numStages

    def Integrate(self):
        # Reset self.coeffs
        self.coeffs = [0.,0.,0.,0.,0.8,2.1]
        # Perform integration

关于设置元素的问题,让我们使用calloc而不是malloc修改自己的代码,使用动态分配的数组

动态分配的版本:

from libc.stdlib cimport calloc, free

ctypedef struct RKHelper:
    int numStages
    double* coeffs

def RungeKutta_DynamicArray():
    cdef:
        RKHelper firstRKMethod

    firstRKMethod.numStages = 3
    # Use calloc instead, it zero initialises the buffer, so you don't 
    # need to set the elements to zero within a loop
    firstRKMethod.coeffs = <double*> calloc(firstRKMethod.numStages,sizeof(double))

    # Set non-zero elements
    firstRKMethod.coeffs[2] = 1.3

    free(firstRKMethod.coeffs)

    # Just to check
    return firstRKMethod.numStages

让我们做一个有点荒谬的基准测试,以验证前两个示例中的数组是否真正是静态的(即没有运行时成本)

In[1]: print(RungeKutta_DynamicArray())
3
In[2]: print(RungeKutta_StaticArray())
3
In[3]: obj = RungeKutta_StaticArrayClass()
In[4]: print(obj.GetnumStages())
3

In[5]: %timeit RungeKutta_DynamicArray()
10000000 loops, best of 3: 65.2 ns per loop

In[6]: %timeit RungeKutta_StaticArray()
10000000 loops, best of 3: 25.2 ns per loop 

In[6]: %timeit RungeKutta_StaticArrayClass()
10000000 loops, best of 3: 49.6 ns per loop 

RungeKutta_StaticArray基本上接近无操作成本,这意味着阵列分配没有运行时损失.您可以选择在此函数中声明系数,时间仍然相同.尽管使用其成员和构造函数设置类的开销,RungeKutta_StaticArrayClass仍然比动态分配的版本更快.

点赞