Fortran 中的函数指针数组 [英] Function pointer arrays in Fortran

查看:68
本文介绍了Fortran 中的函数指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 Fortran 90 中创建函数指针,代码如下

I can create function pointers in Fortran 90, with code like

real, external :: f

然后使用 f 作为另一个函数/子程序的参数.但是如果我想要一个 array 函数指针呢?在 C 中我会做

and then use f as an argument to another function/subroutine. But what if I want an array of function pointers? In C I would just do

double (*f[])(int);

创建一个函数数组,返回双精度并接受一个整数参数.我试过最明显的,

to create an array of functions returning double and taking an integer argument. I tried the most obvious,

real, external, dimension(3) :: f

但 gfortran 不允许我混合 EXTERNAL 和 DIMENSION.有什么办法可以做我想做的事吗?(这里的上下文是一个求解微分方程组的程序,所以我可以输入方程而无需在我的子程序中有一百万个参数.)

but gfortran doesn't let me mix EXTERNAL and DIMENSION. Is there any way to do what I want? (The context for this is a program for solving a system of differential equations, so I could input the equations without having a million parameters in my subroutines.)

推荐答案

声明real, external :: f"并没有真正使f"变成一个完整的指针,因为你不能改变它指向的过程-- 它确实允许您将这个单个函数传递给另一个例程.所以您还需要指针"属性.Metcalf, Reid & 在Fortran 95/2003 解释"的第 267 页上有示例.Cohen - 谷歌搜索fortran 过程指针"将显示此页面.一个接近您的简单示例是真实的、外部的、指针 :: f_ptr".或者:程序(f),指针:: f_ptr".这是 Fortran 2003 的功能 -- http://gcc.gnu.org/wiki/Fortran2003http://gcc.gnu.org/wiki/ProcedurePointers 列出了 gfortran 中的部分支持,最好与 4.5.我不确定是否直接允许维度",但是您可以将过程分配给指针,这提供了很大的灵活性.您也可以将指针放入派生类型中,该派生类型可以制成数组.

The declaration "real, external :: f" doesn't really make "f" into a full pointer since you can't change the procedure that it points -- it does permit you to pass this single function to another routine., So you also need the "pointer" attribute. There are examples on page 267 of "Fortran 95/2003 explained" by Metcalf, Reid & Cohen -- a google search for "fortran procedure pointer" will bring up this page. A simple example close to yours is "real, external, pointer :: f_ptr". Alternatively: "procedure (f), pointer :: f_ptr". This is a Fortran 2003 feature -- http://gcc.gnu.org/wiki/Fortran2003 and http://gcc.gnu.org/wiki/ProcedurePointers lists partial support in gfortran, best with 4.5. I'm not sure whether "dimension" is directly allowed, but you can assign a procedure to a pointer, which provides a lot of flexibility. You can also put the pointer into a derived type, which could be made into an array.

这是一个适用于 gfortran 4.5 的代码示例:编辑 2:根据下面的评论注释掉一行.

here is a code example which works with gfortran 4.5: Edit 2: line commented out per comments below.

module ExampleFuncs

  implicit none

contains

function f1 (x)
  real :: f1
  real, intent (in) :: x

  f1 = 2.0 * x

  return
end function f1


function f2 (x)
   real :: f2
   real, intent (in) :: x

   f2 = 3.0 * x**2

   return
end function f2


function fancy (func, x)

   real :: fancy
   real, intent (in) :: x

   interface AFunc
      function func (y)
         real :: func
         real, intent (in) ::y
      end function func
   end interface AFunc

   fancy = func (x) + 3.3 * x

end function fancy

end module  ExampleFuncs

program test_proc_ptr

  use ExampleFuncs

  implicit none

  ! REMOVE: pointer :: func
  interface
     function func (z)
        real :: func
        real, intent (in) :: z
     end function func
  end interface

  procedure (func), pointer :: f_ptr => null ()

  type Contains_f_ptr
     procedure (func), pointer, nopass :: my_f_ptr
  end type Contains_f_ptr

  type (Contains_f_ptr), dimension (2) :: NewType


  f_ptr => f1
  write (*, *) f_ptr (2.0)
  write (*, *) fancy (f_ptr, 2.0)

  f_ptr => f2
  write (*, *) f_ptr (2.0)
  write (*, *) fancy (f_ptr, 2.0)

  NewType(1) % my_f_ptr => f1
  NewType(2) % my_f_ptr => f2

  write (*, *) NewType(1) % my_f_ptr (3.0), NewType(2) % my_f_ptr (3.0)

  stop

end program test_proc_ptr

这篇关于Fortran 中的函数指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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