与“quad”的集成和“正交”来自 Python/SciPy

发布于 2024-12-04 14:25:08 字数 1574 浏览 0 评论 0原文

阅读,我认为“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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清引 2024-12-11 14:25:08

问题出在 return q(myfunc2, 0, 1, args=(y))[0] 行中,特别是在 args=(y) 部分。您想要的是 args=(y,) (注意 y 后面的逗号)或 args=[y]

问题是在 Python 中元组是用逗号而不是括号创建的。看:

>>> a = (1,)
>>> b = (1)
>>> print a, type(a)
(1,) <type 'tuple'>
>>> print b, type(b)
1 <type 'int'>

The problem is in the line return q(myfunc2, 0, 1, args=(y))[0], specifically in the args=(y) part. What you want is args=(y,) (notice the comma after y) or args=[y].

The issue is that in Python tuples are created with commas, not with parentheses. Look:

>>> a = (1,)
>>> b = (1)
>>> print a, type(a)
(1,) <type 'tuple'>
>>> print b, type(b)
1 <type 'int'>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文