Fortran 77中的等值(实际和复杂变量) [英] Equivalence in fortran 77 (real and complex variable)

查看:378
本文介绍了Fortran 77中的等值(实际和复杂变量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是代码的一部分:

  REAL * 8 DATA1(0:N-1)
COMPLEX * 16 DATA2(0:N / 2-1)
EQUIVALENCE(DATA1,DATA2)
...
...
CALL FFT(DATA1,N / 2,-1)



基本上,FFT子程序是一维复数到复数的FFT引擎。在子程序中有一些置换和矩阵向量的乘法运算。

代码稍后以这种方式调用DATA2:



< pre $ K0 = DATA2(0)
K1 = DCONJG(DATA2(0))

任何人都可以告诉我为什么使用EQUIVALENCE()语句?我的假设是DATA1,它是REAL,被更改为DATA2,这是COMPLEX变量,其中某些更改是在FFT子例程上执行的。但是如果是这样的话,DATA2的虚部怎么样?因为FFT子程序只包含REAL变量。为什么DATA1和DATA2的数组大小不同?



我无法在这个论坛找到任何满足我的问题的答案。感谢您的回答。它会帮助我很多。

解决方案>

equivalence 是Fortran存储的两个特性之一 - 实体的关联。 (另一个是 common 块,我将在这里保持沉默的话题)。 等价语句声明在其参数列表中命名的实体共享相同的存储位置。在这种情况下, data1 data2 共享相同的内存位置。

如果您有一个检查内存位置的工具,并将它指向 data1 ,您会看到如下所示的内容:

  + ---------- + ---------- + --- ------- + ---------- + ---------- + ---------- + 
| | | | | |
| data1(0)| data1(1)| data1(2)| data1(3)| data1(4)| data1(...
| | | | | |
+ ---------- + ---------- + -------- - + ---------- + ---------- + ---------- +

将相同的工具指向 data2 ,您会看到类似这样的内容

  + ---------- + ---------- + ---------- + ---------- + ---------- + ---------- + 
| | |
| data2(0)| data2(1)| data2(....
| re im | re im | re im
+ ---------- + ---------- + ---------- + ---------- + ---------- + ---------- +

但'真相'更像是

  + ---------- + ---------- + ---------- + --------- -  + ---------- + ---------- + 
| | |
| data1(0)data1(1)| data1(2)data1 (3)| data1(4)data1(...
| | |
| data2(0)| data2(1)| data2(....
| re im | re im | re im
+ ---------- + ---------- + ---- ------ + ---------- + ---------- + ---------- +

data1(0) data2(0) data1(1) data2(0)等等。



这是等价的应用程序之一,偶尔会遇到这种情况 - 可以在 complex reals 对之间切换,但不限于此类型转换,没有什么可说的,你不能等价整数和实数,或任何其他类型。



偶尔还会看到的另一个用途是使用>等价,用于将数组从一个等级重新映射到另一个等级。例如,给定

 整数,dimension(3,2):: array2 
integer,di mension(6):: array1



  equivalence(array1(1),array2(1,1))

可以将相同的元素视为属于rank-2数组或属于rank-1数组以满足程序需求。

> equivalence 在这些日子里一般都会被忽略,现在的大多数已经被使用的功能可以通过现代Fortran更安全地完成。更多的,你可能会关心看我的答案 Fortran中的COMPLEX存储保证是两个REAL?


I am trying to understand Fortran 77 code but stumbled on EQUIVALENCE() statement on the code.

Here is part of the code:

REAL*8 DATA1(0:N-1)
COMPLEX*16 DATA2(0:N/2-1)
EQUIVALENCE(DATA1, DATA2)
...
...
CALL FFT(DATA1, N/2, -1)

Basically FFT subroutine is a one-dimensional complex-to-complex FFT engine. There are some permutation and matrix-vector multiplication on the subroutine.

The code calls DATA2 later in this manner:

K0=DATA2(0)
K1=DCONJG(DATA2(0))

can anyone give me clue about why EQUIVALENCE() statement is used? My assumption is DATA1 which is REAL, being changed to DATA2, which is COMPLEX variable with some changed performed on FFT subroutine. But if it is so, how about the imaginary part of DATA2? Because the FFT subroutine only contains REAL variable. And why the array size of DATA1 and DATA2 is different?

I can not find any answer in this forum which satisfy my question. Thanks for your answers. It would help me a lot.

解决方案

equivalence is one of Fortran's two features for storage-association of entities. (The other is common blocks on which topic I will remain silent here). The equivalence statement declares that the entities named in its argument list share the same storage locations. In this case data1 and data2 share the same memory locations.

If you have a tool for inspecting memory locations and point it at data1 you'll see something like this:

+----------+----------+----------+----------+----------+----------+
|          |          |          |          |          |
| data1(0) | data1(1) | data1(2) | data1(3) | data1(4) | data1(...
|          |          |          |          |          |
+----------+----------+----------+----------+----------+----------+

Point the same tool at data2 and you'll see something like this

+----------+----------+----------+----------+----------+----------+
|                     |                     |                     
|        data2(0)     |        data2(1)     |        data2(....         
|   re          im    |   re          im    |   re          im    
+----------+----------+----------+----------+----------+----------+

but the 'truth' is rather more like

+----------+----------+----------+----------+----------+----------+
|                     |                     |          
| data1(0)  data1(1)  | data1(2)   data1(3) | data1(4)   data1(...
|                     |                     |                     
|        data2(0)     |        data2(1)     |        data2(....         
|   re          im    |   re          im    |   re          im    
+----------+----------+----------+----------+----------+----------+

data1(0) is at the same location as the real component of data2(0). data1(1) is the imaginary component of data2(0), and so forth.

This is one of the applications of equivalence which one still occasionally comes across -- being able to switch between viewing data as complex or as pairs of reals. However, it's not confined to this kind of type conversion, there's nothing to say you can't equivalence integers and reals, or any other types.

Another use one occasionally still sees is the use of equivalence for remapping arrays from one rank to another. For example, given

integer, dimension(3,2) :: array2
integer, dimension(6)   :: array1

and

equivalence(array1(1),array2(1,1))

the same elements can be treated as belonging to a rank-2 array or to a rank-1 array to suit the program's needs.

equivalence is generally frowned upon these days, most of what it has been used for can be done more safely with modern Fortran. For more, you might care to look at my answer to Is the storage of COMPLEX in fortran guaranteed to be two REALs?

这篇关于Fortran 77中的等值(实际和复杂变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆