【发布时间】:2020-03-05 05:05:08
【问题描述】:
我有以下代码
@njit
def ss(w,g,sm):
kb = ( (α/(r*(1+τk)))**((1-γ)/(1-γ - α)) )* \
( (γ/(w*(1+τn)))**(γ/(1-γ-α)) )* \
(smat*(1-τo))**(1/(1-γ-α)) ## ss capital
nb = (1+τk)*r*γ/((1+τn)*w*α)*kb ## ss labor
πb = (1-τo)*sm*(kb**α)*(nb**γ)- (1+τn)*w*nb-(1+τk)*r*kb-cf #ss profit
W = πb/(1-0.0196) ## error in the code
for i in range(ns):
for j in range(nτ):
xb[i,j] = 1 if W[i,j]>=0 else xb[i,j]
we = sum(W*g*xb) - ce
return we
据我所知它应该可以工作,但我不断收到以下错误
TypingError: Invalid use of Function(<built-in function setitem>) with argument(s) of type(s): (readonly array(float64, 2d, C), tuple(int64 x 2), float64)
* parameterized
In definition 0:
All templates rejected with literals.
In definition 1:
All templates rejected without literals.
In definition 2:
All templates rejected with literals.
In definition 3:
All templates rejected without literals.
In definition 4:
All templates rejected with literals.
In definition 5:
All templates rejected without literals.
In definition 6:
我仍在寻找解决 python 的方法。我知道这是因为我正在使用 njit,但究竟是什么部分导致了它?如果我删除 for 循环,它可以正常工作,但我想知道 for 循环中的什么可能导致这种情况?
可重现的例子
import numpy as np
from numba import njit
class rep:
def __init__(self, A = 1, B=2, C = 3, D = 4):
self.A, self.B,self.C, self.D = A,B,C,D
def op(cls):
A,B,C,D = cls.A, cls.B,cls.C, cls.D
xb = np.zeros([10,10])
@njit
def rep1(w,g,sm):
kb = A*3 + B*2
nb = C*3 - D*w
pib = sm*kb - nb
W = pib/4
for i in range(10):
for j in range(10):
xb[i,j] = 1 if W[i,j]>=0 else xb[i,j]
we = np.sum(w*xb*g)
return we
return rep1
g = np.zeros([10,10])
sm = np.ones([10,10])
w = 1
r = rep()
rep1 = op(r)
print(rep1(w,g,sm))
【问题讨论】:
-
几乎不可能回答一个有很多变量的问题,而没有它们是什么。 (Arrays-> which shape, Tuples,...必须重新编译,这可能是不想要的)
-
添加了可重现的示例。所有数组都是 numpy 数组。错误似乎来自 for 循环,因为 xb 是在函数外部定义的。但即使我在里面定义它仍然给我同样的错误。它仅在 xb 是参数时才有效,但我的印象是 njit 可以在 numpy 数组上工作,即使它们不是参数。
标签: python loops for-loop jit numba