Numba在 Python中编译函数时出错
我在用Python编写一个薛定谔方程的模拟,但在使用Numba库中的 @njit来优化函数时遇到了问题。
这是代码。我只是想求解一个离散微分方程。
import numpy as np
from scipy.integrate import solve_ivp as ivp
from numba import njit
m = 1/2 #Un. Geom.
h_bar = 1 #Unidades geometrizadas
infty = 5000 #Esto sirve como infinito
# =============================================================================
# # Parámetros
# =============================================================================
z0 = 0
zf = 1
omega = 1
t0 = 0
tf = 1
a = 1
b = 3
m = 50
# =============================================================================
# # Funciones
# =============================================================================
def f(z):
return 2**0.5*np.sin(2*np.pi*z)
def Ll(a,b,t):
return a + b*t
def dLl(b):
return b
def Ls(a,b,t):
return a + b*np.sin(omega*t)
def dLs(b,t):
return omega*b*np.cos(omega*t)
@njit
def res(t,phi):
global h
dphi = np.zeros_like(z, dtype = complex) #recordar que para dphi0 y dphi-1 son ceros
for i in range(0,m):
if i != 0 and i != m-1:
#print(i,len(dphi))
#print('phi_i= ', phi[i], 'phi_{i-1}= ', phi[i-1], 'phi_{i+1}= ',phi[i+1],'||',dphi[i])
dphi[i] = 1/h**2*1j/Ll(a,b,t)**2*(phi[i+1]-2*phi[i]+phi[i-1]) + 1/2/h*i*dLl(b)/Ll(a,b,t)*(phi[i+1] - phi[i-1])
# dphi[i] = 1/h**2*1j/L**2*(phi[i+1]-2*phi[i]+phi[i-1]) + 1/2/h*i*dLs(b,t)/Ls(a,b,t)(phi[i+1] - phi[i-1]) #esta es para el sin
else:
if i == 0:
print(i,phi[i])
dphi[i] = 1/h**2*1j/Ll(a,b,t)**2*(phi[i+1]-2*phi[i]+ 0) + 1/2/h*i*dLl(b)/Ll(a,b,t)*(phi[i+1] - 0)
if i == m-1:
print(i,phi[i])
dphi[i] = 1/h**2*1j/Ll(a,b,t)**2*( 0 -2*phi[i]+phi[i-1]) + 1/2/h*i*dLl(b)/Ll(a,b,t)*( 0 - phi[i-1])
print(dphi)
return dphi
# =============================================================================
# # Resolución numérica
# Esto se hace con ivp
# =============================================================================
h = 1/m
z = np.linspace(z0+h,zf-h,m)
z = np.transpose(z)
print('tamaño z = ', len(z))
y0 = f(z)
t = np.linspace(t0,tf,m)
sol = ivp(res,(t0,tf), y0, t_eval=t)
print(sol.y)
我的函数在没有 @njit的情况下可以正常工作,但当我取消注释时,得到的结果是 "Cannot determine Numba type of
在对所有被调用的函数实现 @njit之后,我得到这个信息:
TypingError: No implementation of function Function(<function zeros_like at 0x000001EAA78FB160>) found for signature:
zeros_like(readonly array(float64, 1d, C), dtype=Literal[str](complex128))
There are 2 candidate implementations:
- Of which 2 did not match due to:
Overload of function 'zeros_like': File: numba\core\typing\npydecl.py: Line 525.
With argument(s): '(readonly array(float64, 1d, C), dtype=unicode_type)':
No match.
During: resolving callee type: Function(<function zeros_like at 0x000001EAA78FB160>)
During: typing of call at C:\...\untitled0.py (66)
我一直在查阅Numba的文档,据说函数np.zeros_like只包含包中的前两个参数(我现在只用这两个),但我仍然收到这个信息。我试着在函数中引入z,但这也不起作用。
解决方案
正如你在问题的编辑中所述,首先需要用 njit 给函数 L1 和 dL1 进行装饰。
使用numba 0.62.1时我得到一个不同的错误:
There are 2 candidate implementations:
- Of which 2 did not match due to:
Overload in function 'ol_np_zeros_like': File: numba/np/arrayobj.py: Line 4539.
With argument(s): '(readonly array(float64, 1d, C), dtype=Function(<class 'complex'>))':
Rejected as the implementation raised a specific error:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function empty_like>) found for signature:
>>> empty_like(readonly array(float64, 1d, C), dtype=Function(<class 'complex'>))
无论哪种情况,错误都指向对 numpy.zeros_like 的使用,特别是 dtype 参数。
这可以通过把第 dphi = np.zeros_like(z, dtype = complex) 行中的 complex 改为 "complex" 或 np.complex128 来修复。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。