gpt4 book ai didi

python - 错误 : function() takes at least n arguments (n given)

转载 作者:太空狗 更新时间:2023-10-29 20:42:04 24 4
gpt4 key购买 nike

我正在尝试使用 SymPy 获取残差,在本例中为余切函数。我有一个 integrate() 函数:

import sympy as sy
import numpy as np

def integrate(f, z, gamma, t, lower, upper, exact=True):
'''
Integrate f(z) along the contour gamma(t): [lower, upper] --> C

INPUTS:
f - A SymPy expression. Should represent a function from C to C.
z - A SymPy symbol. Should be the variable of f.
gamma - A SymPy expression. Should represent a function from [lower, upper] to C.
t - A SymPy symbol. Should be the variable of gamma.
lower - The lower bound for the domain of gamma.
upper - The upper bound for the domain of gamma.

RETURN:
A complex number.
'''
integrand = f.subs(z, gamma)*sy.diff(gamma, t)
ans = sy.integrate(integrand, (t, lower, upper))
if exact:
return sy.simplify(ans)
if ~exact:
return sy.N(sy.simplify(ans))

我这样调用它:

def cot_res(n):
"""Return the residue of the cotangent function at n*pi/2."""
z, t = sy.symbols('z t')
f = sy.cot(z)
gamma = n*np.pi/2 + sy.exp(1j*t)
return 1/(2*np.pi*1j)*integrate(f, z, gamma, 0, 2*sy.pi, exact=True)

for i in xrange(10):
print i/2., cot_res(i)

而且我一直收到错误 integrate() takes at least 6 arguments (6 given) 并且我不确定我的问题出在哪里。我试过重启内核。

最佳答案

当您收到一条错误消息,指出 Python 无法计算参数时,这通常是因为您传递的参数数量等于所需参数的数量,但您缺少一些必需参数并包括一些可选参数争论。在这种情况下,您有以下定义:

def integrate(f, z, gamma, t, lower, upper, exact=True):

和下面的调用:

integrate(f, z, gamma, 0, 2*sy.pi, exact=True)

如果我们把它们排成一行,我们会看到

def integrate(f, z, gamma, t, lower, upper, exact=True):

integrate(f, z, gamma, 0, 2*sy.pi, exact=True)

您缺少 lower 之一, upper , 或 t , 但因为你提供了 exact ,错误报告变得困惑。

Python 3 对于这样的事情有更好的错误消息:

>>> def f(a, b=0): pass
...
>>> f(b=1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() missing 1 required positional argument: 'a'

关于python - 错误 : function() takes at least n arguments (n given),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30473468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com