Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Interface 类型,该类型是实数或实数数组_Interface_Fortran - Fatal编程技术网

Interface 类型,该类型是实数或实数数组

Interface 类型,该类型是实数或实数数组,interface,fortran,Interface,Fortran,我有以下问题: 我有一个包含一些fortran函数的模块,它以2个(数学)函数作为输入。目前我这样做的方式是为输入函数定义一个接口。我的模块如下所示: module myModule implicit none save abstract interface subroutine functype1(x,f) import dp real(kind=dp), dimension(:),inte

我有以下问题:

我有一个包含一些fortran函数的模块,它以2个(数学)函数作为输入。目前我这样做的方式是为输入函数定义一个接口。我的模块如下所示:

module myModule
   implicit none
   save 
   
       abstract interface
        subroutine functype1(x,f) 
            import dp 
            real(kind=dp), dimension(:),intent(in) :: x
            real(kind=dp), dimension(:),intent(out):: f
        end subroutine

        subroutine functype2(x,M)
            import dp 
            real(kind=dp), dimension(:),intent(in) :: x 
            real(kind=dp), dimension(:,:),intent(out):: M
        end subroutine
    end interface

contains 

      function taylorExpansion(func,funcdiff)
          procedure(functype1) :: func ! The mathematical function f
          procedure(functype2) :: funcdiff ! The function evaluating the derivative/gradient of f
          
          ! ...
          ! (further implementation)
          ! ...
      end function
现在的问题是,我希望functype1具有某种通用性:有时输入只是一个实数,有时是一个实数数组。有可能顺利地做到这一点吗? (目前,我被迫将大小为1的数组传递给func1,而不是实数组,这相当笨拙)

另一种表述方式是:是否可以创建一个实数类型或实数数组类型?这样,两个调用都可以:

program testprog
     use myModule
     implicit none
     save 
     
     call taylorExpansion(xsquared,xdiff) ! functype1 can accept reals
     call taylorExpansion(vectornorm,vectornormdiff) ! functype1 can also accept vectors
contains 
     subroutine xsquared(x,fx)
          real(kind=dp) :: x
          real(kind=dp) :: fx
          fx = x**2
     end subroutine 

     subroutine twox(x,fx)
          real(kind=dp) :: x
          real(kind=dp) :: fx 
          fx = 2*x
     end subroutine 

     subroutine vectornorm(x,fx)
          real(kind=dp), dimension(:) :: x
          real(kind=dp) :: fx=0.0
          integer i
          
          do i = 1,size(x)
              fx = fx + x(i)**2
          end do
      end subroutine

     subroutine vectornormdiff(x,fx)
          real(kind=dp), dimension(:) :: x
          real(kind=dp), dimension(:) :: fx
          integer i
          
          do = 1,size(x)
             fx(i) = 2*x(i)
          end do
      end subroutine

end program




使子例程元素化能满足您的需要吗?不,不幸的是不能:如果函数将一个数组映射到一个数组,它就不能以元素方式应用,例如f([x1,x2])=[x1*x2,x2-3]伪参数的秩可以是,但如果您能在任何人推荐该方法之前提供您想要做的事情的更多细节,可能会更好,或者你也可以选择。myFunc实际上围绕函数f执行泰勒展开:对于实函数f,func1是函数求值,func2是导数函数求值。但f也可以是多变量函数:在这种情况下,func1仍然是一个函数求值,但是func2是函数的梯度。有一个微妙的概念需要澄清:在
taylorexpansion
接受
xsquared
vectornorm
的情况下,这不起作用,因为这两个子例程的特性不同。这与接受标量或实数参数的
xsquared
不同。您想知道如何更改
taylorexpansion
还是如何编写
xsquared\u或向量norm
(它接受标量和数组)?无论哪种方式,您几乎肯定不想要
real(kind=dp)::fx=0.0
vectornorm
中。