import numpy as np
from scipy import integrate as integrate
### From the definition of the LOS integral
def LOS_integration(rs,r_vir,r_p): #### radius in kpc
rho = lambda l: np.exp(1 - np.log(1+np.sqrt(l**2 + r_p**2)/rs)/(np.sqrt(l**2 + r_p**2)/rs))
result = integrate.quad(rho,0,np.sqrt(r_vir**2-r_p**2),epsabs=1.49e-08, epsrel=1.49e-08)
return result[0]
integration_vec = np.vectorize(LOS_integration) ### vectorize the function
### convert LOS integration to radius integration
def LOS_integration1(rs,r_vir,r_p): #### radius in kpc
rho = lambda r: np.exp(1 - np.log(1+r/rs)/(r/rs)) * r/np.sqrt(r**2-r_p**2)
### r/np.sqrt(r**2-r_p**2) is the factor convert from LOS integration to radius integration
result = integrate.quad(rho,r_p,r_vir,epsabs=1.49e-08, epsrel=1.49e-08)
return result[0]
integration1_vec = np.vectorize(LOS_integration1)
我有下面的实现(假设密度分布
rho = exp(1-log(1+r/rs)/(r/rs))
):第一种方法要快得多,因为它不需要处理
r/np.sqrt(r**2-r_p**2)
中的奇点。在相关问题 更多 >
编程相关推荐