复变函数

时间:2016-07-18 13:30:07

标签: function fortran complex-numbers

我想创建一个函数FUN(x),它将x作为一个复杂变量的参数,但我没有做到。我搜索过但没有找到任何有用的信息。有谁能够帮我?

    program Console2

IMPLICIT REAL *8 (A-H,O-W) 
external FUN
complex:: b

b=(2,2)

print*,FUN(b)

end program Console2

  FUNCTION FUN (x)    
  IMPLICIT REAL *8 (A-H,O-W) 
  complex, intent(in) :: x 
  complex :: a
  a=(1,2)

  FUN=x+a
  RETURN 

END

2 个答案:

答案 0 :(得分:3)

由于隐式打字不是答案,所以这里的东西更接近好Fortran ...

program console2

  use, intrinsic :: iso_fortran_env
  ! this module defines portable kind type parameters incl real64

  implicit none
  ! so no errors arising from forgotten declarations or misunderstood
  ! implicit declarations

  complex(kind=real64):: b
  ! for a complex number each part will have the same size as a real64
  ! no trying to figure out complex*8 or complex*16 

  b=(2,2)

  print*,fun(b)

contains
  ! by 'containing' the function definition we get the compiler to check
  ! its interface at compile time; properly called *host-association* and in
  ! a larger program we might use a module instead

  complex(kind=real64) function fun (x)    
    complex(kind=real64), intent(in) :: x
    complex(kind=real64) :: a

    a=(1,2)
    fun=x+a

  end function fun

end program console2

答案 1 :(得分:0)

首先,避免隐式输入。这就是我的工作。细节将在稍后添加:

  program Console2

  ! good programming practice
  IMPLICIT NONE
  external FUN
  complex:: b
  complex:: FUN                                                                                                                              

  b=(2,2)

  print*,FUN(b)
  end program Console2

  COMPLEX FUNCTION FUN (x)
  IMPLICIT NONE
   complex, intent(in) :: x
   complex :: a
   a=(1.0,2.0)   ! a=(1,2)

   FUN=x+a
   RETURN
   END

还应该使用KIND参数。稍后我将通过更好的实践和更长的解释来升级它。但就目前而言,上述编辑应该可以解释你的错误。高性能标志刚刚更新答案的更多解释。