[英]How can I determine which variable to use for a defined function in python
I have defined this function : 我已经定义了这个功能:
def xintegral(x,k,z,xdot):
f=np.exp((x-xdot)**2*k/(2*z)*1j)
return f
And I want to insert this function in the place of func to this process: 我想在此过程中的func位置插入此函数:
def simpson(func,mini,maxi,N):
sum=0
h=(maxi-mini)/N
even=mini+2*h
odd=mini+h
for i in range(N//2-1):
sum+=func(even)*2
even+=2*h
for i in range(N//2):
sum+=func(odd)*4
odd+=2*h
sum=(sum+(func(mini)+func(maxi)))*h/3
return sum
I want to use as a func the xintegral with variable the xdot. 我想将xdot变量用作xintegral函数。 So I tried to write it like that : simpson(xintegral(x,k,z,xdot),mini,maxi,N) but I want the variable to be the xdot.
所以我试图这样写:simpson(xintegral(x,k,z,xdot),mini,maxi,N),但我希望变量是xdot。 The variables odd,mini,maxi can replace xdot?
变量奇数,最小,最大可以代替xdot吗? How can I do that?
我怎样才能做到这一点?
You can do this with partial application. 您可以使用部分应用程序执行此操作。 This will create a function
p_xintegral
that will take a single argument. 这将创建一个函数
p_xintegral
,该函数将使用单个参数。 That single argument will be assigned to xdot
with in xintegral
and x
, y
, and z
are determined at the time you do the partial application: 该单个参数将在
xintegral
分配给xdot
,并且x
, y
和z
在执行部分应用程序时确定:
from functools import partial
p_xintegral = partial(xintegral, x, y, z)
So you should then be able to call simpson(p_xintegral, mini, maxi, N)
. 因此,您应该能够调用
simpson(p_xintegral, mini, maxi, N)
。
I think this is what you want. 我想这就是你想要的。 Otherwise your f is not a function
否则你的f不是函数
def xintegral(x,k,z,xdot):
f=lambda j:np.exp((x-xdot)**2*k/(2*z)*1j)
return f
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.