在迭代3D数组

时间:2016-09-11 13:44:28

标签: python numpy numba

我有一个3D数组(n,3,2)用于保存三个2D矢量的组,我正在迭代它们:

import numpy as np
for x in np.zeros((n,2,3), dtype=np.float64):
     print(x) # for example

正常numpy这个工作正常,但当我在

中包装有问题的函数时
 @numba.jit(nopython=True)

我收到如下错误。

numba.errors.LoweringError: Failed at nopython (nopython mode backend)
iterating over 3D array
File "paint.py", line 111
[1] During: lowering "$77.2 = iternext(value=$phi77.1)" at paint.py (111)

供参考,实际代码为here

1 个答案:

答案 0 :(得分:5)

看起来这只是没有实现。

In [13]: @numba.njit
    ...: def f(v):
    ...:     for x in v:
    ...:         y = x
    ...:     return y

In [14]: f(np.zeros((2,2,2)))
NotImplementedError                       Traceback (most recent call last)
<snip>
LoweringError: Failed at nopython (nopython mode backend)
iterating over 3D array
File "<ipython-input-13-788b4772d1d9>", line 3
[1] During: lowering "$7.2 = iternext(value=$phi7.1)" at <ipython-input-13-788b4772d1d9> (3)

如果使用索引循环,似乎可以正常工作。

In [15]: @numba.njit
    ...: def f(v):
    ...:     for i in range(len(v)):
    ...:         y = v[i]
    ...:     return y

In [16]: f(np.zeros((2,2,2)))
Out[16]: 
array([[ 0.,  0.],
       [ 0.,  0.]])