python - 为什么 numba 和 numpy 在就地操作上表现不同?

标签 python numpy numba in-place

用 numba njit 修饰的函数,它是 jit(nopython=True) 的别名,会产生不同的结果来自 numpy 的就地操作(简单的 @jit(nopython=False) 也给出来自 的不同结果numpy):

In [1]: import numpy as np
        from numba import njit

        def npfun(arr):
            arr -= arr[3]

        @njit
        def jitfun(arr):
            arr -= arr[3]

        arr1 = np.ones((6,2))
        arr2 = arr1.copy()

        npfun(arr1)
        jitfun(arr2)

        arr1 == arr2

Out[1]: array([[ True,  True],
               [ True,  True],
               [ True,  True],
               [ True,  True],
               [False, False],
               [False, False]], dtype=bool)  

看起来 numpy 评估 rhs 并将其作为副本传递,而 numba 将 rhs 视为看法。这样做有什么技术原因吗?

numpy 1.13.3 numba 0.35

最佳答案

你正在做的操作:

arr -= arr[3]

曾经是 NumPy 中的未定义行为。它最近才被定义,在 NumPy 1.13.0 中, 发布 June 7th this year .新定义的行为总是表现得好像它复制了所有输入,尽管它会在检测到不需要时尝试避免实际制作副本。

看起来 Numba 目前并没有尝试模仿新行为,要么是因为它有多新,要么是因为 Numba 特定的问题。

关于python - 为什么 numba 和 numpy 在就地操作上表现不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47914320/

相关文章:

python - 如何用 dict pandas python 替换分组数据框

python - 替换OpenCV中多个RGB值的所有像素

函数名称和参数之间的 Python 方括号 : func[. ..](...)

Python Pandas : Multi Indexed columns DataFrame merging task

python - SQLAlchemy - 创建表模板

python - 向 seaborn 中的每个子图添加文本

python - 在python中读取和操作多个netcdf文件

python - 将大型稀疏矩阵转换为 COO 时出错

python - numba 中反射列表签名的问题

python - 错误: number of arguments and self