如何将一组函数存储到Fortran数组中 [英] How to store a set of functions into a Fortran array
本文介绍了如何将一组函数存储到Fortran数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们知道,函数名称可以视为其他子例程传入/传出的参数.我想知道我们是否有任何技巧可以将函数列表保存到数组中,然后将其传入和传出进行处理.
As we know that function name can be treated as parameters to pass in/out by other subroutines. I wonder if we have any tricks to save a list of functions into an array, which would be passed in and out for process.
!------例如.在某个地方,我们可以设置任何数组
!-------for example. At somewhere we set any array
type(Idonotknow)::Farray(N)
然后设置值:
Farray(1)%any=>fun1
Farray(2)%any=>fun2
...
Farray(N)%any=>funN
其中fun1,fun2 ... funN类似于
where fun1,fun2...funN are something like
Function fun1(input)
implicit none
statements
End Function
最后我们可以给他们打电话
Finally we can call them
do i = 1, N
Call F(i)%any(input)
enddo
推荐答案
首先,您应该创建一个仅包含不传递任何参数的过程指针的类型.然后创建该类型的数组.
Firstly you should create a type that contain only a procedure pointer that doesn't pass any argument. and then create an array of that type.
示例:
program test_func_array
implicit none
type pp
procedure(func) ,pointer ,nopass :: f =>null()
end type pp
interface
function func(x)
real :: func
real, intent (in) :: x
end function func
end interface
type(pp) :: func_array(4)
func_array(1)%f => exp
func_array(2)%f => tan
func_array(3)%f => cos
func_array(4)%f => sin
print*,func_array(1)%f(1.)
print*,func_array(2)%f(1.)
print*,func_array(3)%f(0.)
print*,func_array(4)%f(0.)
end program test_func_array
这篇关于如何将一组函数存储到Fortran数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文