Equivalence in fortran 77 (real and complex variab

2019-09-11 02:25发布

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.

1条回答
我只想做你的唯一
2楼-- · 2019-09-11 03:11

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?

查看更多
登录 后发表回答