找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2168|回复: 0

[求助] 打算用@njit加速函数,但是报错,求大神指点!!急!!

0

主题

0

帖子

0

积分

贫民

积分
0
Red10 发表于 2020-3-21 09:38:18 | 显示全部楼层 |阅读模式
本帖最后由 Red10 于 2020-3-21 09:44 编辑

如题,打算用@njit加速python脚本,但是总是运行出错。主要问题有两个:1、不识别besselj函数;2、不识别复数1j。下面是我的code和错误信息,请大神过目并指点!!

@njit()
def cal(D_ccd, r_ccd, r_probe0, E_illum, E_cen, theta, objpixel, f, krzp, krzpin, rzp_in, kf2, rzp2,
        rzpin2, pre_c**):
    """
                 ***********************************************************************
                 Function name : cal
                 Description   : This function is designed for calculate the E_illum.
                 Argument    : D_ccd,r_ccd,r_probe0,E_illum,E_cen,theta,objpixel,f,krzp,krzpin,rzp_in,kf2,rzp2,rzpin2,pre_c**
                 Date        : 2020/2/3
                 ***********************************************************************
    """
for ss in range(0, D_ccd):
        # for ss in range(0, 3):
x = ss - r_ccd
        for tt in range(0,D_ccd):
            # for tt in range(0, 3):
y = tt - r_ccd
            r = math.sqrt(x * x + y * y)
            if r > r_probe0:
                E_illum[ss, tt] = 0
            elif r == 0:
                E_illum[ss, tt] = E_cen
            else:
                theta = r * objpixel / f
                x1 = krzp * theta
                x2 = krzpin * theta
                if rzp_in == 0:
                    E_illum[ss, tt] = pre_c** * cmath.exp(
                        1j * kf2 * theta * theta) * rzp2 * mpmath.besselj(1, x1) / x1
                else:
                    E_illum[ss, tt] = pre_c** * cmath.exp(1j * kf2 * theta * theta) * (
                            (rzp2 * mpmath.besselj(1, x1) / x1) - (
                            rzpin2 * mpmath.besselj(1, x2) / x2))
    ccd = E_illum
    return ccd



[size=13.0667px]Exception in thread Thread-1:Traceback (most recent call last):  [size=13.0667px]  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\threading.py", line 916, in _bootstrap_inner
[size=13.0667px]    self.run()  
[size=13.0667px]  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\threading.py", line 864, in run
[size=13.0667px]    self._target(*self._args, **self._kwargs)
[size=13.0667px]  File "G:/ptycho/PyPIE-v1.0.7_test/PyPIE_main.py", line 748, in iteration
[size=13.0667px]    self.generate_probe()
[size=13.0667px]  File "G:/ptycho/PyPIE-v1.0.7_test/PyPIE_main.py", line 609, in generate_probe
[size=13.0667px]    self.rzpin2, self.pre_c**)
[size=13.0667px]  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\numba\dispatcher.py", line 344, in _compile_for_args
[size=13.0667px]    reraise(type(e), e, None)
[size=13.0667px]  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\numba\six.py", line 658, in reraise
[size=13.0667px]    raise value.with_traceback(tb)
[size=13.0667px]

[size=13.0667px]numba.errors.TypingError: Failed at nopython (nopython frontend)
[size=13.0667px]Unknown attribute 'besselj' of type Module(<module 'mpmath' from 'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Anaconda3_64\\lib\\site-packages\\mpmath\\__init__.py'>)

[size=13.0667px]File "PyPIE_main.py", line 81:def cal(D_ccd, r_ccd, r_probe0, E_illum, E_cen, theta, objpixel, f, krzp, krzpin, rzp_in, kf2, rzp2,
[size=13.0667px]    <source elided>
[size=13.0667px]                    E_illum[ss, tt] = pre_c** * cmath.exp(
[size=13.0667px]                        1j * kf2 * theta * theta) * rzp2 * mpmath.besselj(1, x1) / x1
[size=13.0667px]                        ^
[size=13.0667px][1] During: typing of get attribute at G:/ptycho/PyPIE-v1.0.7_test/PyPIE_main.py (81)
[size=13.0667px]

[size=13.0667px]

[size=13.0667px]File "PyPIE_main.py", line 81:def cal(D_ccd, r_ccd, r_probe0, E_illum, E_cen, theta, objpixel, f, krzp, krzpin, rzp_in, kf2, rzp2,
[size=13.0667px]    <source elided>
[size=13.0667px]                    E_illum[ss, tt] = pre_c** * cmath.exp(
[size=13.0667px]                        1j * kf2 * theta * theta) * rzp2 * mpmath.besselj(1, x1) / x1
[size=13.0667px]                        ^


[size=13.0667px]This is not usually a problem with Numba itself but instead often caused bythe use of unsupported features or an issue in resolving types.To see Python/NumPy features supported by the latest release of Numba visit:
[size=13.0667px]http://numba.pydata.org/numba-doc/dev/reference/pysupported.html
[size=13.0667px]and
[size=13.0667px]http://numba.pydata.org/numba-doc/dev/reference/numpysupported.html
[size=13.0667px]For more information about typing errors and how to debug them visit:
[size=13.0667px]http://numba.pydata.org/numba-doc/latest/user/troubleshoot.html#my-code-doesn-t-compile
[size=13.0667px]If you think your code should work with Numba, please report the error messageand traceback, along with a minimal reproducer at:
[size=13.0667px]https://github.com/numba/numba/issues/new

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表