首页 文章

使用数组值函数作为另一个函数的参数

提问于
浏览
1

我已将问题从更复杂的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和英特尔Fortran进行编译会产生以下错误:

错误1错误#6634:违反了实际参数和伪参数的形状匹配规则 . [FUNCTION1]

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

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

1 回答

  • 2

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

    这里不要使用 external . 它与数组值函数等高级功能不兼容 . 通常, external 仅适用于FORTRAN 77样式的旧程序 . 创建一个接口块(请参阅链接)或尝试

    procedure(Function1) :: RandomFunction
    

    相反(Fortran 2003) .

相关问题