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
Function 复变函数_Function_Fortran_Complex Numbers - Fatal编程技术网

Function 复变函数

Function 复变函数,function,fortran,complex-numbers,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,

我想创建一个函数
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

因为隐式类型不是答案,这里有一个更接近优秀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

因为隐式类型不是答案,这里有一个更接近优秀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

首先,避免隐式键入。这是我的工作。稍后将添加详细信息:

  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参数。稍后,我将使用更好的实践和更长的解释来升级此功能。但是现在,上面的编辑应该可以解释你的错误。高性能标记只是更新了答案的更多解释。

首先,避免隐式键入。这是我的工作。稍后将添加详细信息:

  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参数。稍后,我将使用更好的实践和更长的解释来升级此功能。但是现在,上面的编辑应该可以解释你的错误。High Performance Mark刚刚更新了答案,提供了更多的解释。

您的问题是结果是实数(正如您隐式声明的那样)还是其他什么?输出只是实数,缺少复杂部分。如果您希望
有趣
复杂,您必须将其声明为复杂,而不是
真实*8
。在主程序和函数中都添加
complex fun
。啊哈,我知道了,通过将隐式实数更改为隐式复数来解决问题,谢谢。您的问题是结果是实数(正如您隐式声明的那样)还是其他什么?输出只是实数,缺少复杂部分。如果希望
fun
变得复杂,则必须将其声明为复杂,而不是
real*8
。在主程序和函数中都加入
complex fun
。啊哈,我明白了,通过将隐式实数改为隐式复数来解决问题,谢谢。