问题描述
许多 FFT 算法利用复数存储在数组中的实部和虚部交替.通过创建一个 COMPLEX 数组并将其传递给 FFT 例程,是否可以保证它可以转换为具有交替实部和虚部的 REAL 数组(大小为两倍)?
Many FFT algorithms take advantage of complex numbers stored with alternating real and imaginary part in the array. By creating a COMPLEX array and passing it to a FFT routine, is it guaranteed that it can be cast to a REAL array (of twice the size) with alternating real and imaginary components?
subroutine fft (data, n, isign)
dimension data(2*n)
do 1 i=1,2*n,2
data(i) = ..
data(i+1) = ..
1 continue
return
end
...
complex s(n)
call fft (s, n, 1)
...
(顺便说一句,维度数据(2*n)和说它是真实的一样吗?)
(and, btw, is dimension data(2*n) the same as saying it is a REAL?)
推荐答案
我之所以写这个答案,是因为经验告诉我,一旦我写出这样的答案,一位真正的 Fortran 专家就会涌入纠正我.
I'm only writing this answer because experience has taught me that as soon as I do write this sort of answer one of the real Fortran experts hereabouts piles in to correct me.
我认为当前的标准及其任何前身都没有明确声明 complex
将被实现为两个相邻的内存 reals
.但是,我认为这种实现是标准定义equivalence
和common
的必然结果.我想我从来没有遇到过一个 complex
没有被实现为一对 reals
的实现.
I don't think that the current standard, nor any of its predecessors, states explicitly that a complex
is to be implemented as two neighbouring-in-memory reals
. However, I think that this implementation is a necessary consequence of the standard's definitions of equivalence
and of common
. I don't think I have ever encountered an implementation in which a complex
was not implemented as a pair of reals
.
标准确实保证,尽管一个 complex
可以转换成一对 reals
.所以,给定一些定义:
The standard does guarantee, though that a complex
can be converted into a pair of reals
. So, given some definitions:
complex :: z
complex, dimension(4) :: zarr
real :: r1, r2
real, dimension(8) :: rarr
以下将满足您的期望
r1 = real(z)
r2 = aimag(z)
这两个功能都是基本的,这里有一个皱纹:
Both those functions are elemental and here comes a wrinkle:
real(zarr)
返回一个 4 元素的实数数组,就像
returns a 4-element array of reals, as does
aimag(zarr)
同时
[real(zarr), aimag(zarr)]
是一个 8 元素实数数组,其中 zarr
的实数部分后跟复数部分.也许
is an 8-element array of reals with the real parts of zarr
followed by the complex parts. Perhaps
rarr(1:8:2) = real(zarr)
rarr(2:8:2) = aimag(zarr)
对你来说没问题.不过,我不确定有什么更简洁的方法.
will be OK for you. I'm not sure of any neater way to do this though.
亚历山大不是唯一一个能够引用标准的人!他引用的部分让我想知道非默认复杂标量.所以我继续阅读,我认为他指向我们的部分的第 6 段是密切相关的
Alexander's not the only one able to quote the standard ! The part he quotes left me wondering about non-default complex scalars. So I read on, and I think that para 6 of the section he points us towards is germane
项目 (1)-(5) 中未指定的任何类型的非指针标量对象占用单个未指定的存储单元,每个存储单元都不同case 和每组类型参数的值,那是不同的第(4)项的未指定存储单元,
我认为这对这里的任何答案都没有任何影响.
I don't think that this has any impact at all on any of the answers here.
这篇关于fortran 中 COMPLEX 的存储是否保证为两个 REAL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!