与“quad”的集成和“正交”来自 Python/SciPy
阅读此和,我认为“quad”和“quadrature”应该是可以互换的*,至少在语法上。奇怪的是,它们似乎不是:
from scipy.integrate import quad as q
#from scipy.integrate import quadrature as q
def myfunc(x):
return x
def integr():
return q(myfunc, 0, 1)[0]
print integr()
def myfunc2(x, y):
return x + y
def integr2(y):
return q(myfunc2, 0, 1, args=(y))[0]
#return q(myfunc2, 0, 1, args=[y])[0]
print integr2(10)
...该示例对于“quad”运行良好,但对于“quadrature”则不然 - 我最终得到:
Traceback (most recent call last):
File "./test.py", line 38, in <module>
print integr2(10)
File "./test.py", line 36, in integr2
return q(myfunc2, 0, 1, args=(y))[0]
File "/usr/lib/python2.6/dist-packages/scipy/integrate/quadrature.py", line 136, in quadrature
newval = fixed_quad(vfunc, a, b, (), n)[0]
File "/usr/lib/python2.6/dist-packages/scipy/integrate/quadrature.py", line 48, in fixed_quad
return (b-a)/2.0*sum(w*func(y,*args),0), None
File "/usr/lib/python2.6/dist-packages/scipy/integrate/quadrature.py", line 77, in vfunc
return func(x, *args)
TypeError: myfunc2() argument after * must be a sequence, not int
我必须将 args 元组切换到列表(参见 integr2 中的注释行)即使文档说它应该是一个元组。看来这就是翻译抱怨的……(对吧?)
这是故意的吗?或者我做错了什么?最后,我希望能够随后选择集成方法,而不必更改太多其余代码。
*其实我真的不知道如何在两者之间做出选择。我确实理解高斯求积和自适应求积之间的区别,但我不知道“自适应高斯求积”是什么意思 - 节点数量是否适应,如果是的话如何!?
After reading this and that, it occurs to me that both "quad" and "quadrature" should be interchangeable*, atleast syntax-wise. Strangely it does seem they are not:
from scipy.integrate import quad as q
#from scipy.integrate import quadrature as q
def myfunc(x):
return x
def integr():
return q(myfunc, 0, 1)[0]
print integr()
def myfunc2(x, y):
return x + y
def integr2(y):
return q(myfunc2, 0, 1, args=(y))[0]
#return q(myfunc2, 0, 1, args=[y])[0]
print integr2(10)
... the example runs fine for "quad", but not for "quadrature" - I end up with:
Traceback (most recent call last):
File "./test.py", line 38, in <module>
print integr2(10)
File "./test.py", line 36, in integr2
return q(myfunc2, 0, 1, args=(y))[0]
File "/usr/lib/python2.6/dist-packages/scipy/integrate/quadrature.py", line 136, in quadrature
newval = fixed_quad(vfunc, a, b, (), n)[0]
File "/usr/lib/python2.6/dist-packages/scipy/integrate/quadrature.py", line 48, in fixed_quad
return (b-a)/2.0*sum(w*func(y,*args),0), None
File "/usr/lib/python2.6/dist-packages/scipy/integrate/quadrature.py", line 77, in vfunc
return func(x, *args)
TypeError: myfunc2() argument after * must be a sequence, not int
I have to switch the args tuple to a list (cf. commented line in integr2) even though the documentation says it should be a tuple. It seemed this is what the interpreter complains about ... (right?)
Is this intended? Or am I doing something wrong? In the end I'd like to be able to choose integration methods afterwards without having to change too much of the rest of the code.
*Actually I don't really get how to choose between the two. I do understand the difference between Gaussian quadrature and adaptive quadrature, but I don't know what "adaptive Gaussian quadrature" is supposed to mean - is the number of nodes adapted, if so how!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在
return q(myfunc2, 0, 1, args=(y))[0]
行中,特别是在args=(y)
部分。您想要的是args=(y,)
(注意y
后面的逗号)或args=[y]
。问题是在 Python 中元组是用逗号而不是括号创建的。看:
The problem is in the line
return q(myfunc2, 0, 1, args=(y))[0]
, specifically in theargs=(y)
part. What you want isargs=(y,)
(notice the comma aftery
) orargs=[y]
.The issue is that in Python tuples are created with commas, not with parentheses. Look: