有没有办法让scipy.integrate.quad接受args中的数组?

问题描述

我有一个功能:

def xx(th,T,B):
    f = integrate.quad(xint,np.inf,args = (th,B))[0]
    a = v(th)*f
    return a

其中xintp,th,B的函数的函数。前述所有功能均正常运行;然后应将xx(th,B)集成到th上,其他变量是单个数字。

运行此命令时,我得到:TypeError: only size-1 arrays can be converted to Python scalars,因为th是一个数组而不是单个数字。

我尝试使用lambda函数以及dblquad在同一计算中完成两个积分,但没有任何效果。牢记限制,因此尝试避免for循环,是否有任何办法让integrate.quad接受数组参数?

运行时的跟踪:

run file.py
  Traceback (most recent call last):

  File "file.py",line 280,in <module>
  file()

  File "file.py",line 240,in sctif
  axes[0,2].plot(th,xx(th,10,10))

  File "file.py",line 166,in xx
  f,_ = integrate.quad(xint,100,B))

  File "/home/caitlin/anaconda3/lib/python3.7/site-packages/scipy/integrate/quadpack.py",line 352,in quad
points)

  File "/home/caitlin/anaconda3/lib/python3.7/site-packages/scipy/integrate/quadpack.py",line 463,in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)

TypeError: only size-1 arrays can be converted to Python scalars`

  

解决方法

我从没真正获得args选项的用处。恕我直言,如果您定义仅接受一个参数的函数,则可能会通过包装以下代码来使代码更清晰:

th = 2.0
T = 1.0
B = 3.0

def xint(x):
    return th * x ** B / T

f,_ = integrate.quad(xint,np.inf)

现在,如果thTB中的一个是向量,则您的函数是向量值的,您将无法再使用quad。我会研究quad_vecquadpy.quad。 (quadpy是我的一个项目。)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...