【发布时间】:2016-07-26 00:49:12
【问题描述】:
我想在 Fortran 中创建一个 function composition。这个想法是如果我有 f(x) 和 g(x),我想找到 f(g(x))。在pythonBetter Function Composition in Python中语法很简单。
这是一个示例 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,但它适用于更早的版本(尽管它继续适用于较新的标准)。
由于过程指针采用较新的标准(2003、2008、...),我将使用 Fortran 标准 2008。
【问题讨论】:
-
在 fortran 中无法关闭。
-
如果不借助具有指针类型绑定过程的用户定义派生数据类型和通过
c_f_pointerin @987654329 中的过程实现的 C 语言样式转换,我认为您无法完成此任务@。无论如何,函数组合违反了在 Fortran 中传递参数的方式。见:software.intel.com/en-us/blogs/2009/03/31/…
标签: fortran function-composition