【问题标题】:Error: function() takes at least n arguments (n given)错误:function() 至少需要 n 个参数(n 给定)
【发布时间】:2017-09-03 02:19:40
【问题描述】:

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

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 arguments sympy


    【解决方案1】:

    当您收到指示 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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-08
      • 2013-08-03
      • 1970-01-01
      • 2017-10-15
      • 2014-09-20
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多