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

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

问题描述

我正在尝试使用 SymPy 取余数,在这种情况下是余切函数.我有一个集成()函数:

import sympy as sy将 numpy 导入为 npdef 积分(f,z,伽玛,t,下,上,精确=真):'''沿等高线 gamma(t) 对 f(z) 进行积分:[lower, upper] -->C输入:f - SymPy 表达式.应该代表一个从 C 到 C 的函数.z - SymPy 符号.应该是 f 的变量.gamma - SymPy 表达式.应该代表一个从 [lower, upper] 到 C 的函数.t - SymPy 符号.应该是 gamma 的变量.下 - 伽马域的下限.upper - 伽马域的上限.返回:一个复数.'''被积函数 = f.subs(z, gamma)*sy.diff(gamma, t)ans = sy.integrate(被积函数,(t,下,上))如果确切:返回 sy.simplify(ans)如果~精确:返回 sy.N(sy.simplify(ans))

我这样称呼它:

def cot_res(n):"""返回余切函数在 n*pi/2 处的余数."""z, t = sy.symbols('z t')f = sy.cot(z)伽玛 = n*np.pi/2 + sy.exp(1j*t)返回 1/(2*np.pi*1j)*integrate(f, z, gamma, 0, 2*sy.pi,exact=True)对于 xrange(10) 中的 i:打印 i/2., cot_res(i)

而且我不断收到错误 integrate() 需要至少 6 个参数(给出 6 个参数),我不确定我的问题出在哪里.我试过重新启动内核.

解决方案

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

def 积分(f,z,gamma,t,下,上,精确=真):

以及以下调用:

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

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

def 积分(f,z,gamma,t,下,上,精确=真):积分(f,z,伽玛,0,2*sy.pi,精确=真)

您缺少 loweruppert 之一,但因为您提供了 exact,错误报告变得混乱.

Python 3 有一个更好的错误信息:

<预><代码>>>>def f(a, b=0):通过...>>>f(b=1)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:f() 缺少 1 个必需的位置参数:'a'

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

which I am calling thusly:

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)

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.

解决方案

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):

and the following call:

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)

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

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天全站免登陆