和一个带两个参数的函数
实函数fun(x,a)实数,意图(in):: x,a现在对于运行时固定的 a ,我想通过 fun 至处理函数。所以理想情况下,我想调用类似于
调用处理函数(fun(:,a = a0),input = myinput,输出= myoutput)Fortran2003特性 gfortran-5 支持?
当然,我可以在处理函数中插入一个可选的伪参数 a $ (x)或 f(x,c $ c>)并调用 f a)取决于子程序正文中的 present(a)。但是改变子程序并不优雅。
解决方案在Fortran 2008中,您可以将内部函数作为参数传递并且gfortran支持它。(), $ b $调用处理函数(wrapper,input = myinput,output = myoutput)包含真实函数包装(x)真实,意图(in):: x 包装= fun(x,a0) end函数结束子程序
顺便说一句,我会远离外部它是邪恶的,使用接口块。
I have a subroutine (minimal example)
subroutine treatfunction(f,input,output) external, real::f real, intent(in):: input real, intent(out):: output output = f(input) + f(1.0) ! i.e. f has only one argument end subroutineand a function with two arguments
real function fun(x,a) real,intent(in)::x,aNow for a given a fixed at runtime, I want to pass fun to treatfunction. So ideally, I would want to call something like
call treatfunction(fun(:,a=a0), input=myinput, output=myoutput)What is the most elegant way of doing this with the Fortran2003 features gfortran-5 supports?
Of course, I could insert an optional dummy argument a in treatfunction and call f either with f(x) or f(x,a) depending on present(a) in the subroutine's body. But changing the subroutine is not elegant.
解决方案In Fortran 2008 you can pass internal functions as arguments and gfortran supports it.
subroutine calling() a0 = ... call treatfunction(wrapper, input=myinput, output=myoutput) contains real function wrapper(x) real, intent(in) :: x wrapper = fun(x,a0) end function end subroutineBTW, I would stay away from external it is evil, use interface blocks.
更多推荐
如何将一个具有多个参数的函数传递给一个只有一个参数的子程序?
发布评论