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

标签 python arguments sympy

我正在尝试使用 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/

相关文章:

python - 如何在 Python 中获取目录中的文件名列表

python - Django模板如何使用变量查找字典值

python - 仅根据其所在行业选择股票

python - 将 "-h"参数传递给子脚本

shell - 使用 nohup 运行带参数的 shell 脚本

python - 确定 python 分数是否可以具有等效的小数

python - sympy 的高级索引?

通过JPype和numpy将Java类型转换为Python

javascript - 如何在 JavaScript 中检查参数是否是对象(而不是数组)

python - matplotlib pyplot.show : Invalid RGBA