【问题标题】:Use array valued function as argument to another function使用数组值函数作为另一个函数的参数
【发布时间】:2015-12-24 22:45:11
【问题描述】:

我已将一个问题从更复杂的 f90 代码转换为以下内容:

module userfunctions

implicit none

contains

    function Function1(Argument,ArgumentSize)

        ! Input
        integer, intent(in)                         ::      ArgumentSize
        real,dimension(ArgumentSize),intent(in)     ::      Argument

        ! Output
        real,dimension(ArgumentSize)                ::      Function1    

        ! Local
        integer                 ::      i

        Function1 = Argument

        ! Random operation on argument, resembling generic vector function
        do i=1,ArgumentSize

            Function1(i) = Function1(i)**2

        end do

    end function Function1


    function Function2(RandomFunction,Argument,ArgumentSize)


    ! Input
    integer, intent(in)                         ::      ArgumentSize
    real,dimension(ArgumentSize), intent(in)    ::      Argument

    ! Array-type function of dimension ArgumentSize
    real,external                       ::      RandomFunction

    ! Evaluate RandomFunction to
    real,dimension(ArgumentSize)       ::      Value

    ! Output
    real                                ::      Function2

    ! assign evaluation of RandomFunction to Value
    Value = RandomFunction(Argument,ArgumentSize)

    Function2 = dot_product(Value,Value)

    end function Function2

end module userfunctions



    program Fortran_Console_002

        use userfunctions

        implicit none

        real                    ::      Result1
        real,dimension(6)       ::      Vector1

        Vector1 = 2

        Result1 = Function2(Function1,Vector1,6)

        write(*,*) Result1


    end program Fortran_Console_002

结果应该是“96”。使用 Visual Studio 2013 和 Intel Fortran 编译它会产生以下错误:

错误1错误#6634:实际参数的形状匹配规则和 虚拟参数已被违反。 [功能1]

在实际上下文中,我确实需要将数组值函数从子程序传递给模块中定义的函数(非线性函数的求解器,它将函数作为参数)。我确实知道如何为标量值函数执行此操作,但未能将其应用于数组。

为了方便,我使用 Visual Studio 2013。真正的部分必须在虚拟机上使用 Compaq Visual Fortran 6 编译,据我所知,该虚拟机仅兼容 fortran90。

【问题讨论】:

    标签: arrays fortran parameter-passing fortran90


    【解决方案1】:

    有关一般规则,请参阅How to pass subroutine names as arguments in Fortran?

    不要在这里使用external。它与数组值函数等高级特性不兼容。通常,external 仅适用于 FORTRAN 77 风格的旧程序。做一个接口块(见链接)或尝试

       procedure(Function1) :: RandomFunction
    

    改为(Fortran 2003)。

    【讨论】:

    • 非常感谢!它并没有立即奏效,但完全把我推向了正确的方向。
    猜你喜欢
    • 2018-11-13
    • 2020-10-04
    • 2022-01-11
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2018-07-30
    • 2019-02-19
    相关资源
    最近更新 更多