如何通过功能的阵列在FORTRAN? [英] How to pass function in array in fortran?

查看:190
本文介绍了如何通过功能的阵列在FORTRAN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理在FORTRAN符号变量函数的方法
我有一个通过定义的数组到另一个功能问题。下面是示例code。

!保存为文件test5.f08

 模块TEST5    隐无
    包含    函数f1(ARR1)结果(sum_arr)
        真实的,尺寸(:),分配的,意图(在):: ARR1
        真实的,尺寸(3):: ARR2
        真正:: sum_arr
        整数::指数        sum_arr = 0
        ARR2 = [4,5,6]        做指数= 1,3
            ARR1(指数)=指数!------ Q2 -----------
            sum_arr = sum_arr +(ARR1(指数)* ARR2(指数)+ ARR2(指数))
        做到底    结束函数f1前端模块TEST5

主程序调用上述模块:

!保存为文件test5_call.f08

 程序test5_call
    使用TEST5
    隐无    整数::ñ
    真实的,分配的,尺寸(:) ::数组1
    真正:: XVAR,答案    分配(数组1(3))
    数组1 = [XVAR,XVAR * 2,* XVAR XVAR]!--------- ------- Q1    答案= F1(数组1)
    打印*的数组元素=的答案,答案程序结束test5_call

作为编译
gfortran test5.f08 test5_call.f08 -o test5_call

在主程序线Q1,我定义包含数学EX pression $数组[X,X ^ 2,X ^ 3] $我想通过这个阵列模块TEST5运作F1。在行Q2(在模块TEST5函数f1),我要评估这个数组前pression。因此,当指数= 2 ARR1 = [2,2 ^ 2,2 ^ 3]

基本上,我想通过数组(符号变量)前pression到一个函数。和评估功能,其中前pression将获得价值除权pression。

这code运行,但没有阵列功能的作用。任何帮助,如何做到这一点?

我想在这个搜索,但没有得到任何信息。在C语言中,我发现了一个链接你怎么了传递函数作为参数用C?。由于我没有关于C多少知识,我不知道在这个问题的技术是否应该被使用。


解决方案

你的present code做:

XVAR 只是一个实数

数组1 = [XVAR,XVAR * 2,XVAR * XVAR] 是只有三个实数的数组,该值将取决于<$ C的当前值$ C> XVAR 这是在您的code不确定的。

真实的,尺寸(:),分配的,意图(在):: ARR1 又是几个实数只是一个分配数组。请注意,您不必可分配在这里都没有。


如何做你打算什么:

首先,你应该学习如何通过功能可言,看在其他

<一个href=\"https://stackoverflow.com/questions/32809769/how-to-pass-subroutine-names-as-arguments-in-fortran\">How通过子程序名作为参数的Fortran?

这样,你可以通过一个函数。我建议你​​从那里开始,并宣布你的 F1 接受三个独立的功能

 函数f1(func1的,FUNC2,FUNC3)结果(sum_arr)
  抽象接口
    实函数arg_func(X)
      真实的,意图(中):: X
    最终功能
  结束接口  过程(arg_func):: func1的,FUNC2,FUNC3

您只了解后如何工作和之后你有信心使用过程参数一般你可以开始考虑使用数组。

您必须定义包含一个程序指针的容器和使用这些容器的阵列。这是一个高级的主题,将其在Fortran语言 函数指针数组p>

Method of dealing with symbolic variable function in fortran I have question about passing a defined array into another function. Here is sample code.

!file saved as test5.f08

module test5

    implicit none
    contains

    function f1 (arr1) result (sum_arr)
        real, dimension (:), allocatable, intent (in):: arr1 
        real, dimension (3):: arr2
        real :: sum_arr
        integer :: index

        sum_arr=0
        arr2 = [4,5,6]

        do index= 1,3
            arr1(index) = index  !------Q2-----------
            sum_arr = sum_arr + ( arr1(index)*arr2(index) + arr2(index))
        end do

    end function f1

end module test5

Main program calling above module:

!file saved as test5_call.f08

program test5_call
    use test5
    implicit none

    integer :: n
    real,  allocatable, dimension (:) :: array1
    real::xvar, answer

    allocate (array1(3))
    array1 = [xvar,xvar*2,xvar*xvar] !---------Q1-------

    answer = f1(array1)
    print *,"Answer of array elements  = ", answer

end program test5_call

Compiling as gfortran test5.f08 test5_call.f08 -o test5_call

In the line Q1 in the main program, I am defining an array containing math expression $[x, x^2, X^3]$ I want to pass this array to function f1 in module test5. In line Q2 (in function f1 in module test5), I want to evaluate this array expression. So when index=2, arr1 = [2, 2^2, 2^3].

Basically I want to pass array with (symbolic variable) expression to a function. And evaluate the expression in the function, where the expression will receive values.

This code runs, but there is no effect of array function. Any help how to do this?

I tried searching on this, but didn't get any information. In C, I found a link How do you pass a function as a parameter in C? . As I do not have much knowledge about C, I am not sure whether the technique in the question should be used.

解决方案

What your present code is doing:

xvar is just a real number

array1 = [xvar,xvar*2,xvar*xvar] is just an array of three real numbers, the value will depend on the current value of xvar which is undefined in your code.

real, dimension (:), allocatable, intent (in):: arr1 is again just an allocatable array of several real numbers. Note, that you do not need allocatable here at all.


How to do what you intended:

First, you should study how to pass functions at all, see among others

How to pass subroutine names as arguments in Fortran?

That way you can pass one function. I would suggest you to start there and declare your f1 to accept three separate functions

function f1 (func1, func2, func3) result (sum_arr)
  abstract interface
    real function arg_func(x)
      real, intent(in) :: x
    end function
  end interface

  procedure(arg_func) :: func1, func2, func3

Only after you understand how this work and after you are confident using procedure arguments in general you can start thinking about using an array.

You would have to define a container which contains a procedure pointer and use an array of these containers. It is an advanced topic, leave it for the future Function pointer arrays in Fortran

这篇关于如何通过功能的阵列在FORTRAN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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