scipy.integrate.quad on arrays of limits

筅森魡賤 提交于 2019-12-06 12:48:52

The integral of exp(-x*x) is a scaled version of the error function, so you can use scipy.special.erf to compute the integral. Given scalars a and b, the integral of your function from a to b is 0.5*np.sqrt(np.pi)*(erf(b) - erf(a)).

erf is a "ufunc", which means it handles array arguments. Given a_list and b_list, your calculation can be written as

total = 0.5*np.sqrt(np.pi)*(erf(b_list) - erf(a_list)).sum()

The function rho can also be handled with erf, by using the appropriate scaling:

g = np.sqrt(2)*H
total = g*rho0*0.5*np.sqrt(np.pi)*(erf(b_list/g) - erf(a_list/g)).sum()

Check this against your slow solution before relying on it. For some values, the subtraction of the erf functions can result in a significant loss of precision.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!