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

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

问题描述

我可以在Fortran 90的创建函数指针,用code像

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

real, external :: f

然后用˚F作为参数传递给另一个函数/子程序。但是,如果我想要的东西的阵列的函数指针?在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并不让我混外部和尺寸。有没有办法做我想要什么? (此上下文是解决微分方程组系统的程序,所以我输入的公式没有在我的子程序有一百万的参数。)

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.)

推荐答案

的声明真正的,外部:: F并没有真正F成为一个完整的指针,因为你不能改变它指向的程序 - 它允许你这个单一的功能传递给另一个例程,所以你还需要在指针属性。还有的的Fortran 95/2003解释说,通过梅特卡夫,里德和放大器267页中的实例;科恩 - 谷歌搜索FORTRAN程序指针将弹出页面。接近你一个简单的例子是真实的,外部的,指针:: f_ptr。或者:过程(F),指针:: f_ptr。这是Fortran 2003的功能 - http://gcc.gnu.org/wiki/Fortran2003 并的http://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工作的code例如:
编辑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天全站免登陆