2015-05-27 13 views
6

Sto provando a usare SymPy per prelevare residui, in questo caso la funzione cotangente. Ho una funzione di integrazione():Errore: function() accetta almeno n argomenti (n dati)

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

che io chiamo così:

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) 

E continuo a ricevere l'errore integrate() takes at least 6 arguments (6 given) e io non so dove il mio problema è. Ho provato a riavviare il kernel.

risposta

12

Quando viene visualizzato un messaggio di errore che indica che Python non può contare gli argomenti, è in genere perché il numero di argomenti che hai passato è uguale al numero di argomenti richiesti, ma mancano alcuni argomenti richiesti e alcuni argomenti opzionali. In questo caso, si ha la seguente definizione:

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

e la seguente chiamata:

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

Se li in fila, vediamo

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

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

che vi state perdendo uno di lower, upper o t, ma poiché hai fornito exact, la segnalazione degli errori viene confusa.

Python 3 ha un messaggio di errore per più cose come questa:

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