numpy.ndarray.astype 在 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:UsersArturAnacondapython.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:UsersArturAnacondapython.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:UsersArturAnacondalibsite-packagesnumbacoredispatcher.py", line 401, in _compile_for_args
error_rewrite(e, 'typing')
File "C:UsersArturAnacondalibsite-packagesnumbacoredispatcher.py", line 344, in error_rewrite
reraise(type(e), e, None)
File "C:UsersArturAnacondalibsite-packagesnumbacoreutils.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:UsersArturAnacondapython.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:UsersArturAnacondalibsite-packagesnumbacoredispatcher.py", line 401, in _compile_for_args
error_rewrite(e, 'typing')
File "C:UsersArturAnacondalibsite-packagesnumbacoredispatcher.py", line 344, in error_rewrite
reraise(type(e), e, None)
File "C:UsersArturAnacondalibsite-packagesnumbacoreutils.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.

最新更新