在Fortran中将多个变量的外部函数作为一个变量的函数传递 [英] Passing external function of multiple variables as a function of one variable in Fortran

查看:915
本文介绍了在Fortran中将多个变量的外部函数作为一个变量的函数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 QUADPACK 中使用例程来执行数值积分。例程期望函数作为 REAL,EXTERNAL 传递,所以我没有使用指针或其他任何东西的自由。



是否可以将函数 f(x,a,b,...)作为函数 f(x )对于只有x函数的例程?就像 MATLAB 中用 @(x)f(x,a,b,...)

解决方案

您无法直接在Fortran中使用类似的技巧。您也不能在Fortran中返回闭包。只需写一个包装。

pre $函数wrap_f(x)结果(res)
...
res = f(a, b,...)
结束函数

它可以是内部函数或模块函数,通过主机关联得到 a b ,或者它可以使用包含 a b



如果您想将函数作为实际参数传递,是Fortran 2003中的一个内部过程,但仅限于Fortran 2008中。但它适用于最新版本的gfortran和ifort。为了更好的便携性使用模块。


I'm trying to use routines in QUADPACK to perform numerical integration. The routines expect functions to be passed as REAL,EXTERNAL, so I don't have the liberty of using pointers or whatever else.

Is it possible to alias a function f(x,a,b,...) as being a function f(x) for the routine that expects a function of x only? Much like what one would accomplish in MATLAB with @(x)f(x,a,b,...).

解决方案

You cannot make similar tricks with functions in Fortran directly. You also cannot return a closure in Fortran. Just write a wrapper.

function wrap_f(x) result(res)
  ...
  res = f(a,b,...)
end function

It can be an internal or module function and get a and b by the host association or it can use the module containing a and b.

If you want to pass the function as an actual argument, it cannot be an internal procedure in up to Fortran 2003, but only in Fortran 2008. But it works in recent versions of gfortran and ifort. For better portability use a module.

这篇关于在Fortran中将多个变量的外部函数作为一个变量的函数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆