首页 > 解决方案 > 有谁知道为什么我的函数在另一个函数中调用它时会转换为类型元组?

问题描述

我编写了以下函数来计算系统触发中单个反应的概率,并将系统触发中每个反应的概率输出为数组:

def propensity_calc(LHS, popul_num, stoch_rate):
    propensity = np.zeros(len(LHS))
    for row in range(len(LHS)):
            a = stoch_rate[row]     
            for i in range(len(popul_num)):
                if (popul_num[i] >= LHS[row, i]):       
                    binom_rxn = binom(popul_num[i], LHS[row, i])
                    a = a*binom_rxn
                else:
                    a = 0
                    break
            propensity[row] = a     
    return propensity

此函数的输入是 3 个数组,每个数组具有popul_num每个反应物的离散分子数,LHS是一个 2D 数组,其中包含每个反应物之间的比率,并且stoch_rate是每个反应的速率。

我现在想在不同的函数中使用 scipy.misc.derivative 方法调用该函数来计算偏导数:

def time_step_calc(popul_num, state_change_array, a0):
    # equation 22:
    expctd_net_change = a0*state_change_array
    print("expected net change:\n", expctd_net_change)
    # equation 24 partial differentiation:
    for x in range(len(popul_num)):
        part_propensity_diff = derivative(func=propensity_calc(LHS, popul_num, stoch_rate), x0=popul_num[x])    <-- Error here, propensity_calc is converted into a tuple object?
        print("partial diff:\n", type(part_propensity_diff))
        # popul_num = numpy.ndarray --> derivative function needs a float to work
    # equation 26:
    t_step = epsi*a0 / sum(expctd_net_change*part_propensity_diff)
    delta_t = optimize.fmin(t_step, 0.00012) # take the minimum value of the partial derivative
    print("calculated delta_t:\n", delta_t)
    return delta_t

但是当我使用 propensity_calc 函数提供函数参数时,指示的行会引发以下错误TypeError: 'tuple' object is not callable

我不明白元组是从哪里来的,或者为什么 propensity_calc 被转换成它?

编辑:继承人回溯

File "C:\Users\Mike\AppData\Local\Programs\Python\Python38-32\lib\site-packages\scipy\misc\common.py", line 119, in derivative
val += weights[k]*func(x0+(k-ho)*dx,*args)
TypeError: 'tuple' object is not callable

有什么建议么?干杯

标签: pythonfunctionscipy

解决方案


当你说:

func=propensity_calc(LHS, popul_num, stoch_rate)

我不相信你需要

func=

propensity_calc(LHS, popul_num, stoch_rate)调用 propensity_calc

func=propensity_calc(LHS, popul_num, stoch_rate)定义一个元组,或不可变的对象序列。想一想您无法更改的对象数组。

这里有更多关于元组的信息:https ://www.tutorialspoint.com/python/python_tuples.htm


推荐阅读