2015-09-27 78 views
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产生以下错误:

Error 1 error #6634: The shape matching rules of actual arguments and dummy arguments have been violated. [FUNCTION1]

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

由于方便,我使用Visual Studio 2013。真正的作品必须在虚拟机上使用Compaq Visual Fortran 6编译,据我所知,它只能兼容fortran90。

回答

2

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

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

procedure(Function1) :: RandomFunction 

改为(Fortran 2003)。

+0

非常感谢!它并没有立即执行这个伎俩,而是完全把我推向了正确的方向。 – Oshibai