将多个变量的外部函数作为Fortran中一个变量的函数进行传递

我正在尝试使用例程QUADPACK来执行数值积分。例程希望函数以形式传递REAL,EXTERNAL,所以我没有使用指针或其他任何东西的自由。

是否可以将函数别名f(x,a,b,...)f(x)仅期望x 函数的例程的函数?就像什么人会在完成MATLAB@(x)f(x,a,b,...)


互换的青春
浏览 729回答 2
2回答

紫衣仙女

您不能直接使用Fortran中的函数来做类似的技巧。您也无法在Fortran中返回闭包。只是写一个包装。function wrap_f(x) result(res)  ...  res = f(a,b,...)end function它可以是内部函数或模块函数,并可以通过主机关联获取a和b,也可以使用包含a和的模块b。如果要将函数作为实际参数传递,则在Fortran 2003之前的版本中,它不能是内部过程,而只能在Fortran 2008中。它只能在gfortran和ifort的最新版本中使用。为了更好的可移植性,请使用模块。

慕运维8079593

我可以为这个问题显示一个很好的解决方案。我也是MATLAB的前用户,切换到FORTRAN时会错过函数句柄哈哈。我以这种方式解决了您的问题:module     private     public    :: f , g     real(kind=RP)   :: a0,b0,c0,...     contains         function f(x,a,b,c,d,...)                implicit none               real(kind=RP)  :: x,a,b,c,d,...               real(kind=RP)  :: f!              Here you define your function               f = ...         end function f         function g(x)               implicit none               real(kind=RP)  :: x , g!              Here you call "f" function with the frozen variables *0               g = f(x,a0,b0,c0,...)         end function g!        We said that parameters were private !     (to avoid to be modified from the outside, which can be dangerous, !     so we define functions to set their values         subroutine setValues(a,b,c,...)               implicit none               real(kind=RP)   :: a,b,c,...               a0 = a               b0 = b               c0 = c          end subroutine setValuesend module 
打开App,查看更多内容
随时随地看视频慕课网APP