我正在使用numba
0.34.0和numpy
1.13.1。一个小例子如下所示:
import numpy as np
from numba import jit
@jit(nopython=True)
def initial(veh_size):
t = np.linspace(0, (100 - 1) * 30, 100, dtype=np.int32)
t0 = np.linspace(0, (veh_size - 1) * 30, veh_size, dtype=np.int32)
return t0
initial(100)
t
和t0
行都有相同的错误消息。
错误消息:
numba.errors.InternalError: [1] During: resolving callee type: Function(<function linspace at 0x000001F977678C80>) [2] During: typing of call at ***/test1.py (6)
答案 0 :(得分:4)
因为np.linspace
的numba版本不接受dtype
参数(source: numba 0.34 documentation):
2.7.3.3。其他功能
支持以下顶级功能:
[...]
numpy.linspace()
(仅限3个参数形式)[...]
您需要使用astype
将其转换为nopython-numba函数:
import numpy as np
from numba import jit
@jit(nopython=True)
def initial(veh_size):
t = np.linspace(0, (100 - 1) * 30, 100).astype(np.int32)
t0 = np.linspace(0, (veh_size - 1) * 30, veh_size).astype(np.int32)
return t0
initial(100)
或者只是不要在nopython-numba函数中使用np.linspace
并将其作为参数传递。这避免了一个临时数组,我怀疑numbas np.linspace
比NumPys更快。