[英]Error when using lambda function with Cython
I am trying to use Cython to speed up a piece of code. 我正在尝试使用Cython来加速一段代码。 Cython is giving an error that reads "Expected an identifier or literal" when I use lambda functions.
当我使用lambda函数时,Cython会给出一个错误,上面写着“Expected a identifier或literal”。 As far as I can tell, lambda functions are meant to be supported in Cython 0.13.
据我所知,lambda函数在Cython 0.13中得到支持。 Am I incorrect on this point?
我在这一点上不正确吗? If they are, indeed, supported, do I need to do something other than what I have here to implement them?
如果它们确实受到支持,我是否需要做一些其他事情而不是我在这里实施它们?
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: -float('inf')
更改h_z = lambda e_1: -inf
,除非你在其他地方定义了inf
。
I'm able to compile the below simplified Cython code and run it fine using Cython 0.14.1 on OS X 10.6.6. 我能够编译下面简化的Cython代码并使用OS X 10.6.6上的Cython 0.14.1运行它。 I don't know the details as to why it doesn't work on 0.13.
我不知道为什么它在0.13上不起作用的细节。 The easiest solution is to upgrade Cython, if possible.
最简单的解决方案是尽可能升级Cython。
def f(e_1, e_2, rho):
return e_1 + e_2 + rho
def dummy(a, b, c, d, e):
return [a(1,2) + b + c + d(1) + e(3)]
def P_zero(b_10, b_11, b_20, b_21, rho, gamma, x):
h_z = lambda e_1: -1000
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 dummy(I, -1000, (-b_20 - b_21 * x[0] - gamma * x[2]), h_z, g_z)[0]
print P_zero(1, 2, 3, 4, 5, 6, [6, 7, 8])
# outputs "-2122"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.