错误:function()至少接受n个参数(给定n个) [英] Error: function() takes at least n arguments (n given)

查看:73
本文介绍了错误:function()至少接受n个参数(给定n个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SymPy去除残差,在这种情况下为余切函数.我有一个integration()函数:

I'm trying to use SymPy to take residues, in this case the cotangent function. I've got an integrate() function:

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),但我不确定我的问题在哪里.我尝试重新启动内核.

And I keep getting the error integrate() takes at least 6 arguments (6 given) and I'm not sure where my problem is. I have tried restarting the kernel.

推荐答案

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

When you get an error message that indicates Python can't count arguments, it's generally because the number of arguments you've passed is equal to the number of required arguments, but you're missing some required arguments and including some optional arguments. In this case, you have the following definition:

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

和以下呼叫:

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

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

If we line them up, we see

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

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

您遗漏了loweruppert之一,但是由于提供了exact,因此错误报告令人困惑.

that you're missing one of lower, upper, or t, but because you've supplied exact, the error reporting gets confused.

Python 3对于以下情况具有更好的错误消息:

Python 3 has a better error message for things like this:

>>> 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'

这篇关于错误:function()至少接受n个参数(给定n个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆