错误:function()至少需要n个参数(给定n)

时间:2015-05-27 04:54:06

标签: 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),而且我不确定我的问题在哪里。我试过重启内核。

1 个答案:

答案 0 :(得分:16)

当您收到一条错误消息,表明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)

您遗失了loweruppert中的一个,但由于您提供了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'