arrays - 如何绘制数组的实部和虚部?

标签 arrays fortran gfortran

我编写了一个计算样本离散傅里叶变换的程序,在本例中我对正弦波进行采样。为了测试它,我需要绘制结果。但是,生成的数组充满了复数值。

那么如何提取这些数组元素的实部和虚部,然后根据索引绘制它们?

这是我的代码:

program DFT
implicit none
integer :: k, N, x, y, j, r, l, istat
integer, parameter :: dp = selected_real_kind(15,300)
real, allocatable,dimension(:) :: h
complex, allocatable, dimension(:) :: rst
complex, dimension(:,:), allocatable :: W
real(kind=dp) :: pi, z, P, A, i
pi = 3.14159265359
P = 2*pi
A = 1
!open file to write results to
open(unit=100, file="dft.dat", status='replace')

N = 10
!allocate arrays as length N, apart from W (NxN)


allocate(h(N))
allocate(rst(N))
allocate(W(-N/2:N/2,1:N))

pi = 3.14159265359
!loop to fill the sample containing array
do k=1,N
  h(k) = sin((2*k*pi)/N)
end do

!loop to fill the product matrix with values
do j = -N/2,N/2
do k = 1, N

    W(j,k) = EXP((2.0_dp*pi*cmplx(0.0_dp,1.0_dp)*j*k)/N)

end do
end do
!use of matmul command to multiply matrices
rst = matmul(W,h)
!print *, h, w


write(100,*) rst

end program

谢谢。

最佳答案

REAL 内在函数返回 Fortran 中复数的实部。它也是一个基本函数,因此对于复杂类型的数组,只需 REAL( array ) 将返回一个与包含所需结果的原始数组类型相同的实数数组。

AIMAG 内部函数返回 Fortran 中复数的虚部。它也是一个基本函数,因此对于复杂类型的数组,只需 AIMAG( array ) 就会返回一个与包含所需结果的原始数组类型相同的真实数组。

或者,在 Fortran 2003 中,后者 %re 和 %im 可分别用于访问复数变量的实部和虚部。关于其基本性质的评论再次适用。

这些很容易通过谷歌搜索找到,或者更好的是我认为每个 Fortran 程序员至少应该能够访问 Metcalf、Reid 和 Cohen 的《现代 Fortran 解释》的副本。

关于arrays - 如何绘制数组的实部和虚部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48998911/

相关文章:

c - 为 Atlas (os-x, macports) 制作动态库

gcc - 使用gfortran编译: undefined reference to iargc_

javascript - JS给对象赋值导致对象重复

正确地将文件内容按行存储到数组中,稍后打印数组内容

java - 在java数据模式中映射嵌套json

fortran - 用于获取复杂变量的实部和虚部的通用和特定函数

fortran - "%"在 Fortran 中意味着什么?

gcc - 为-Wimplicit-interface禁用-Werror

C:将表示数字位的 int 数组转换为单个 Int?

Fortran 2003 不允许此内部过程 [BESSEL_J0]