返回类型功能不匹配

时间:2014-11-14 15:06:05

标签: fortran

我想用函数对fortran90中的复杂矩阵进行对角化。这是我使用的功能

!==========================================================================
function inv(A,n)
  Implicit none
  integer :: n  
  complex*16, dimension(n,n):: A
  complex*16, dimension(n,n):: inv
  complex*16,allocatable,dimension(:)::WORK
  integer,allocatable,dimension(:)::IPIV
  integer i,j,info,error

  allocate(WORK(n),IPIV(n),stat=error)
  if (error.ne.0)then
    print *,"error:not enough memory"
    stop
  end if


  call ZGETRF(n,n,A,n,IPIV,info)
  if(info .eq. 0) then
    write(*,*)"succeded"
  else
   write(*,*)"failed"
  end if

  call ZGETRI(n,A,n,IPIV,WORK,n,info)
  if(info .eq. 0) then
    write(*,*)"succeded"
    inv=A
  else
   write(*,*)"failed"
  end if
  !deallocate(A,IPIV,WORK,stat=error)
  !if (error.ne.0)then
  !  print *,"error:fail to release"
  !  stop
  !end if
end function inv

我只是用

来调用它
Wmattemp=inv(Wmattemp,nsit)

其类型为

complex*16, allocatable :: Wmattemp(:,:)

但是当我用

编译时,我得到了这个错误
gfortran -fdefault-real-8 code.f90 -llapack

code.f90:217.19:

       Wmattemp=inv(Wmattemp,nsit)
               1
 Error: Return type mismatch of function 'inv' at (1) (INTEGER(4)/COMPLEX(8))
 code.f90:217.16:

       Wmattemp=inv(Wmattemp,nsit)
            1
Error: The reference to function 'inv' at (1) either needs an explicit INTERFACE or the rank is incorrect

我不是强盗专家所以我找不到问题所在。

现在我加入了主程序

complex*16, allocatable :: inv(:,:)

但我得到了这个错误

code.f90:217.13:

       A=inv(Wmattemp,nsit)
             1
Error: Array index at (1) must be of INTEGER type, found COMPLEX

1 个答案:

答案 0 :(得分:1)

由于您没有使用模块,因此需要使用接口块在主程序中声明函数inv的返回值:

program main
! [...]
interface inv
  function inv(A,n)
    integer :: n  
    complex*16, dimension(n,n):: A
    complex*16, dimension(n,n):: inv
  end function
end interface
! [...]
Wmattemp=inv(Wmattemp,nsit)