如何将 Numba“@vectorize”ufunc 与结构化 Numpy 数组一起使用?

How can I use Numba "@vectorize" ufunc with a structured Numpy array?

我无法将矢量化 ufunc 获取到 运行。常规 @njit 工作正常,@vectorize documentation 表明向量化装饰器与 njit 相同。我 运行宁 Windows 10,如果这有影响的话

演示程序如下。从下面的输出中,我们可以看到 njit 函数 运行s 没有发生意外,并且向量化函数存在类型错误。

import sys
import numpy
import numba

Structured = numpy.dtype([("a", numpy.int32), ("b", numpy.float64)])
numba_dtype = numba.from_dtype(Structured)

@numba.njit([numba.float64(numba_dtype)])
def jitted(x):
    x['b'] = 17.5
    return 18.

@numba.vectorize([numba.float64(numba_dtype)], target="cpu", nopython=True)
def vectorized(x):
    x['b'] = 17.5
    return 12.1

print('python version = ', sys.implementation.version)    
print('numpy version = ', numpy.__version__)
print('numba version = ', numba.__version__)
for struct in numpy.empty((3,), dtype=Structured):
    print(jitted(struct))

print(vectorized(numpy.empty((3,), dtype=Structured)))

输出为

python version = sys.version_info(major=3, minor=7, micro=1, releaselevel='final', serial=0)
numpy version = 1.17.3
numba version = 0.48.0
18.0
18.0
18.0
Traceback (most recent call last): File "scratch.py", line 49, in
print(vectorized(numpy.empty((3,), dtype=Structured))) TypeError: ufunc 'vectorized' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

貌似不支持,已经转成feature request