我正在尝试使用 jit 来编译我的代码。目前,除了 Numba 之外我唯一使用的库是 Numpy。当指定 nopython = True 时,它会抛出一堆 linspace 数组错误。我用一个非常简单的函数重新创建了错误:
@jit(nopython=True)
def func(Nt):
time = np.linspace(np.int_(0),np.int_(Nt-1),np.int_(Nt),dtype=np.int_)
return time
Nt = 10
func(Nt)
运行时,显示以下错误消息(见附图)。
我尝试了许多不同的排列,扰乱了参数的类型,但没有成功。我想做的就是制作一个 linspace 整数从 0 到 Nt - 1 的数组。有什么建议吗?
删除
dtype=
参数:
import numba
@numba.jit(nopython=True)
def func(Nt):
time = np.linspace(np.int_(0), np.int_(Nt - 1), np.int_(Nt))
return time
Nt = 10
print(func(Nt))
打印:
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
如果你想要整数,只需之后重新转换即可:
@numba.jit(nopython=True)
def func(Nt):
time = np.linspace(np.int_(0), np.int_(Nt - 1), np.int_(Nt)).astype("int_")
return time