简体   繁体   English

多个函数作为python中的参数

[英]multiple functions as arguments in python

I have the following problem: I have two sets of data (set T and set F). 我有以下问题:我有两组数据(组T和组F)。 And the following functions: 以及以下功能:

x(T) = arctan(T-c0), A(x(T)) =  arctan(x(T) -c1),    
B(x(T)) =  arctan(x(T) -c2) 
and Y(x(t),F) = ((A(x(t)) - B(x(t)))/2 - A(x(t))arctan(F-c3) +  B(x(t))arctan(F-c4))
# where c0,c1,c2,c3,c4 are constants 

Now I want to create a surface plot of Y . 现在,我要创建Y的曲面图。 And for that I would like to implement Y as a python (numpy) function what turns out to be quite complicated, because Y takes other functions as input. 为此,我想将Y实现为python(numpy)函数,结果却非常复杂,因为Y将其他函数作为输入。

Another idea of mine was to evaluate x , B and A on the data separately and store the results in numpy arrays. 我的另一个想法是分别评估数据上的xBA并将结果存储在numpy数组中。 With those I also could get the output of the function Y , but I don't know which way is better in order to plot the data and I really would like to know how to write Y as a python function. 有了这些,我也可以获得函数Y的输出,但是我不知道哪种方法更好地绘制数据,我真的很想知道如何将Y编写为python函数。

Thank you very much for your help 非常感谢您的帮助

It is absolutely possible to use functions as input parameters to other functions. 绝对有可能将功能用作其他功能的输入参数。 A use case could look like: 用例可能看起来像:

def plus_one(standard_input_parameter_like_int):
    return standard_input_parameter_like_int + 1

def apply_function(function_as_input, standard_input_parameter):
    return function_as_input(standard_input_parameter)

if(__name__ == '__main__'):
    print(apply_function(plus_one, 1))

I hope that helps to solve your specific problem. 我希望这有助于解决您的特定问题。

  1. [...] somethin like def s(x,y,z,*args,*args2): will yield an error. def s(x,y,z,*args,*args2):会产生错误。

This is perfectly normal as (at least as far as I know) there is only one variable length non-keyword argument list allowed per function (that has to be exactly labeled as *args). 这是完全正常的(至少就我所知),每个函数仅允许一个可变长度的非关键字参数列表(必须将其准确标记为* args)。 So if you remove the asterisks (*) you should actually be able to run s properly. 因此,如果删除星号(*),则实际上应该可以正确运行s。

  1. Regarding your initial question you could do something like: 关于最初的问题,您可以执行以下操作:

     c = [0.2,-0.2,0,0,0,0] def x(T): return np.arctan(Tc[0]) def A(xfunc,T): return np.arctan(xfunc(T) - c[1]) def B(xfunc,T): return np.arctan(xfunc(T) - c[2]) def Y(xfunc,Afunc,Bfunc,t,f): return (Afunc(xfunc,t) - Bfunc(xfunc,t))/2.0 - Afunc(xfunc,t) * np.arctan(f - c[3]) + Bfunc(xfunc,t)*np.arctan(fc[4]) _tSet = np.linspace(-1,1,20) _fSet = np.arange(-1,1,20) print Y(x,A,B,_tSet,_fSet) 

As you can see (and probably already tested by yourself judging from your comment) you can use functions as arguments. 正如您所看到的(可能已经根据您的评论进行了测试),您可以将函数用作参数。 And as long as you don't use any 'if' conditions or other non-vectorized functions in your 'sub'-functions the top-level function should already be vectorized. 只要您在“子”功能中不使用任何“如果”条件或其他非矢量化函数,顶级功能就应该已经矢量化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM