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
。啊哈,我明白了,通过将隐式实数改为隐式复数来解决问题,谢谢。