Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...