此函数中的if语句在没有向量化的情况下有效吗?
def K(T0,z,v):
for i in range(len(T0)-1):
GDens[i+1]=(Dens[i+1]-Dens[i])/(z[i+1]-z[i])
for i in range(len(T0)):
B[i]=(((ws/Dens0)*k0)**2)*np.exp(-2*alfa*z[i])-((g/Dens0)*GDens[i])
for i in range(len(T0)):
if B[i]>0:
kz[i]=((0.05*h1)**2)*np.sqrt(B[i])+kmin
else:
kz[i]=kmin
kfinal=kz
return kfinal
答案 0 :(得分:2)
我不确定你在问什么,但这就是你如何对这段代码进行矢量化:
GDens = np.zeros_like(z)
GDens[:-1] = (Dens[1:] - Dens[:-1]) / (z[1:] - z[:-1])
B = (((ws/Dens0)*k0)**2)*np.exp(-2*alfa*z)-((g/Dens0)*GDens)
kz = np.where(B > 0, ((0.05*h1)**2)*np.sqrt(B)+kmin, kmin)