簡體   English   中英

多個函數作為python中的參數

[英]multiple functions as arguments in python

我有以下問題:我有兩組數據(組T和組F)。 以及以下功能:

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 

現在,我要創建Y的曲面圖。 為此,我想將Y實現為python(numpy)函數,結果卻非常復雜,因為Y將其他函數作為輸入。

我的另一個想法是分別評估數據上的xBA並將結果存儲在numpy數組中。 有了這些,我也可以獲得函數Y的輸出,但是我不知道哪種方法更好地繪制數據,我真的很想知道如何將Y編寫為python函數。

非常感謝您的幫助

絕對有可能將功能用作其他功能的輸入參數。 用例可能看起來像:

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

我希望這有助於解決您的特定問題。

  1. def s(x,y,z,*args,*args2):會產生錯誤。

這是完全正常的(至少就我所知),每個函數僅允許一個可變長度的非關鍵字參數列表(必須將其准確標記為* args)。 因此,如果刪除星號(*),則實際上應該可以正確運行s。

  1. 關於最初的問題,您可以執行以下操作:

     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) 

正如您所看到的(可能已經根據您的評論進行了測試),您可以將函數用作參數。 只要您在“子”功能中不使用任何“如果”條件或其他非矢量化函數,頂級功能就應該已經矢量化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM