Mar*_*ham 8 python arguments sympy
我正在尝试使用SymPy来获取残留物,在这种情况下是余切函数.我有一个integrate()函数:
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))
Run Code Online (Sandbox Code Playgroud)
这是我所说的:
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)
Run Code Online (Sandbox Code Playgroud)
而且我一直收到错误integrate() takes at least 6 arguments (6 given),我不确定我的问题在哪里.我试过重启内核.
use*_*ica 16
当您收到一条错误消息,表明Python无法计算参数时,通常是因为您传递的参数数量等于所需参数的数量,但您缺少一些必需的参数并包含一些可选参数.在这种情况下,您有以下定义:
def integrate(f, z, gamma, t, lower, upper, exact=True):
Run Code Online (Sandbox Code Playgroud)
以及以下电话:
integrate(f, z, gamma, 0, 2*sy.pi, exact=True)
Run Code Online (Sandbox Code Playgroud)
如果我们排队,我们会看到
def integrate(f, z, gamma, t, lower, upper, exact=True):
integrate(f, z, gamma, 0, 2*sy.pi, exact=True)
Run Code Online (Sandbox Code Playgroud)
那你缺少的一个lower,upper或者t,而是因为你提供的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'
Run Code Online (Sandbox Code Playgroud)