我想在Fortran中創建function composition。這個想法是如果我有f(x)和g(x),我想找到f(g(x))。 Python的語法很簡單Better Function Composition in Python。Fortran中的函數組合
下面是一個示例Fortran代碼:
module test10
implicit none
contains
function f5x (x)
real :: f5x
real, intent (in) :: x
f5x = 5.00*x
end function f5x
function f10x (x)
real :: f10x
real, intent (in) :: x
f10x = 10.00*x
end function f10x
end module test10
program call_test10
use test10
implicit none
real :: val1, val2, input
interface
real function fx_new (y)
real, intent (in) :: y
end function fx_new
end interface
input=1.0
write (*,*) 'Invoking f5x'
val1 = f5x(1.0)
write (*,*) val1
write (*,*) 'Invoking f10x'
val1 = f10x(1.0)
write (*,*) val1
write (*,*) 'Invoking f10x(f5x)'
procedure (fx_new), pointer :: ptr1 => f5x
procedure (fx_new), pointer :: ptr2 => f10x
val2 = ptr2(ptr1)
write (*,*) val2
end program call_test10
中的陳述和val1 = f5x(1.0)
val1 = f10x(1.0)
本身運行。但是當涉及到函數組合時,我不知道如何在Fortran中實現這個功能。我想評估f10x(f5x),然後想要給x賦值。有任何想法嗎?
如果我有一個模塊文件和函數返回類型,參數的數目和參數匹配的類型,可以Fortran編譯器決定哪些函數包含的文件來執行?我想知道的另一件事是我可以擺脫界面(通過使用程序指針或其他)?執行外部函數的接口的概念對我來說看起來很混亂,所以如果可能的話,請提供一種無需接口即可執行外部函數的方法。我知道關鍵字external
的,但它是爲那些更早版本(雖然它繼續在新標準的工作)。
AS過程指針在新標準(2003年,2008年,...),我會用Fortran標準2008
封鎖在fortran中是不可能的。 – wey273824
我不認爲你可以完成這個任務,而不必訴諸於通過'iso_c_binding'中的過程'c_f_pointer'實現的用戶定義派生數據類型與指針類型綁定過程和C語言風格的轉換。無論如何,函數組合違反了Fortran中如何傳遞參數。請參閱:https://software.intel.com/zh-cn/blogs/2009/03/31/doctor-fortran-in-ive-come-here-here-for-an-argument – jlokimlin