gpt4 book ai didi

python - 在 Cython 中使用 lambda 函数时出错

转载 作者:太空狗 更新时间:2023-10-30 00:15:32 24 4
gpt4 key购买 nike

我正在尝试使用 Cython 来加速一段代码。当我使用 lambda 函数时,Cython 给出了一个错误,显示为“Expected an identifier or literal”。据我所知,lambda 函数应该在 Cython 0.13 中得到支持。我在这一点上不正确吗?如果它们确实受到支持,我是否需要做一些除了我在这里实现它们之外的事情?

def f(e_1, e_2, rho):
"""Bivariate Normal pdf with mean zero, unit variances, and correlation coefficient rho."""
return (1.0 / (2.0 * pi * sqrt(1 - rho**2))) * exp(-(1.0 / (2*(1 - rho**2))) * (e_1**2 + e_2**2 - 2*rho*e_1*e_2))

def P_zero(b_10, b_11, b_20, b_21, rho, gamma, x):
"""Returns the probability of observing zero entrants in a market by numerically
integrating out the unobserved firm-specific profit shocks."""
h_z = lambda e_1: -inf
g_z = lambda e_1: -b_10 - b_11*x[0] - gamma*x[1]
I = lambda e_1, e_2: f(e_1, e_2, rho)
return dblquad(I, -inf, (-b_20 - b_21*x[0] - gamma*x[2]), h_z, g_z)[0]

最佳答案

在我看来,除非你定义了 ,否则你应该将 h_z = lambda e_1: -inf 更改为 h_z = lambda e_1: -float('inf') inf 在别的地方。

关于python - 在 Cython 中使用 lambda 函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4295684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com