如何解决有没有办法让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
其中xint
是p,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)
现在,如果th
,T
,B
中的一个是向量,则您的函数是向量值的,您将无法再使用quad
。我会研究quad_vec
或quadpy.quad
。 (quadpy是我的一个项目。)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。