numpy.ndarray.astype 在 numba.jit 中不工作

numpy.ndarray.astype not working in numba.jit

各位程序员大家好!

我正在尝试从用 numba.jit:

修饰的函数中的复数中释放一个二维数组

没有 numba.jit 的功能(有效):

import numpy as np
import numba

def main():
    KAM = np.array([[ -106.66666667-0.j,    42.66666667+0.j,    42.66666667+0.j,     21.33333333+0.j,     0.        +0.j],
                   [   42.66666667+0.j,   -42.66666667-0.j,     0.        +0.j,      0.        +0.j,     0.        +0.j],
                   [   42.66666667+0.j,     0.        +0.j,   -42.66666667-0.j,      0.        +0.j,     0.        +0.j],
                   [   21.33333333+0.j,     0.        +0.j,     0.        +0.j,  -1088.        -0.j,  1066.66666667+0.j],
                   [    0.        +0.j,     0.        +0.j,     0.        +0.j,   1066.66666667+0.j, -1066.66666667-0.j]])

    KAM = KAM.astype(float)
    print(KAM)

if __name__ == '__main__':
    main()

输出:

C:\Users\Artur\Anaconda\python.exe C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py
C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py:11: ComplexWarning: Casting complex values to real discards the imaginary part
  KAM = KAM.astype(float)
[[ -106.66666667    42.66666667    42.66666667    21.33333333
      0.        ]
 [   42.66666667   -42.66666667     0.             0.
      0.        ]
 [   42.66666667     0.           -42.66666667     0.
      0.        ]
 [   21.33333333     0.             0.         -1088.
   1066.66666667]
 [    0.             0.             0.          1066.66666667
  -1066.66666667]]

Process finished with exit code 0

numba.jit装饰的函数:

import numpy as np
import numba

@numba.jit(nopython=True)
def main():
    KAM = np.array([[ -106.66666667-0.j,    42.66666667+0.j,    42.66666667+0.j,     21.33333333+0.j,     0.        +0.j],
                   [   42.66666667+0.j,   -42.66666667-0.j,     0.        +0.j,      0.        +0.j,     0.        +0.j],
                   [   42.66666667+0.j,     0.        +0.j,   -42.66666667-0.j,      0.        +0.j,     0.        +0.j],
                   [   21.33333333+0.j,     0.        +0.j,     0.        +0.j,  -1088.        -0.j,  1066.66666667+0.j],
                   [    0.        +0.j,     0.        +0.j,     0.        +0.j,   1066.66666667+0.j, -1066.66666667-0.j]])

    KAM = KAM.astype(float)
    print(KAM)

if __name__ == '__main__':
    main()

输出:

C:\Users\Artur\Anaconda\python.exe C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py
Traceback (most recent call last):
  File "C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py", line 16, in <module>
    main()
  File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 401, in _compile_for_args
    error_rewrite(e, 'typing')
  File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 344, in error_rewrite
    reraise(type(e), e, None)
  File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\utils.py", line 80, in reraise
    raise value.with_traceback(tb)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of BoundFunction(array.astype for array(complex128, 2d, C)) with parameters (Function(<class 'float'>))
 * parameterized
[1] During: resolving callee type: BoundFunction(array.astype for array(complex128, 2d, C))
[2] During: typing of call at C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py (12)


File "test2.py", line 12:
def main():
    <source elided>

    KAM = KAM.astype(float)
    ^


Process finished with exit code 1

虽然错误消息中提到了复数值,但这个问题似乎不是由复数值引起的。 运行 KAM 中没有复数的相同代码导致相同的错误:

import numpy as np
import numba

@numba.jit(nopython=True)
def main():
    KAM = np.array([[ -106.66666667,    42.66666667,    42.66666667,     21.33333333,     0.        ],
                   [   42.66666667,   -42.66666667,     0.        ,      0.        ,     0.        ],
                   [   42.66666667,     0.        ,   -42.66666667,      0.        ,     0.        ],
                   [   21.33333333,     0.        ,     0.        ,  -1088.        ,  1066.66666667],
                   [    0.        ,     0.        ,     0.        ,   1066.66666667, -1066.66666667]])

    KAM = KAM.astype(float)
    print(KAM)

if __name__ == '__main__':
    main()

输出:

C:\Users\Artur\Anaconda\python.exe C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py
Traceback (most recent call last):
  File "C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py", line 22, in <module>
    main()
  File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 401, in _compile_for_args
    error_rewrite(e, 'typing')
  File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 344, in error_rewrite
    reraise(type(e), e, None)
  File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\utils.py", line 80, in reraise
    raise value.with_traceback(tb)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of BoundFunction(array.astype for array(float64, 2d, C)) with parameters (Function(<class 'float'>))
 * parameterized
[1] During: resolving callee type: BoundFunction(array.astype for array(float64, 2d, C))
[2] During: typing of call at C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py (18)


File "test2.py", line 18:
def main():
    <source elided>

    KAM = KAM.astype(float)
    ^


Process finished with exit code 1

不要使用 .astype(float) 将复数转换为实数。

使用 np.real(arr) 获取实部,如果需要,使用 np.imag(arr) 获取虚部。如果您想要复数的大小,请使用 np.abs(arr)。全部使用 numba.