Home > Archive > Fortran > May 2005 > equivalence
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Daniel 2005-05-17, 8:59 am |
| Hi!
I was gven a source code like this:
DIMENSION A(3,3), B(3,3), C(3,3), U(6)
EQUIVALENCE ( A(1), C(1) ),( U(1), B(1) )
My compiler won't work with the equivalence statement.
I'd like to work around it, but I am not quite sure what this
equvalence stetement
here actually does, on which array elements it refers to...
Can anyone explain?
| |
| Michael Metcalf 2005-05-17, 8:59 am |
|
"Daniel" <dalboz@gmx.net> wrote in message
news:1116327175.134413.205710@z14g2000cwz.googlegroups.com...
> Hi!
>
> I was gven a source code like this:
>
> DIMENSION A(3,3), B(3,3), C(3,3), U(6)
>
> EQUIVALENCE ( A(1), C(1) ),( U(1), B(1) )
>
> My compiler won't work with the equivalence statement.
> I'd like to work around it, but I am not quite sure what this
> equvalence stetement
> here actually does, on which array elements it refers to...
>
> Can anyone explain?
>
The compiler is correct, it should be:
EQUIVALENCE ( A(1,1), C(1,1) ),( U(1), B(1,1) )
where each array reference has the correct rank. The statement sets up a
mapping between the pairs of arrays listed; they then share the same
(common) storage.
Regards,
Mike Metcalf
| |
| David Frank 2005-05-17, 3:59 pm |
|
"Michael Metcalf" <metcalfmetcalf@compuserve.com> wrote in message
news:d6cjjt$j4j$05$1@news.t-online.com...
>
>
>
> The compiler is correct, it should be:
>
> EQUIVALENCE ( A(1,1), C(1,1) ),( U(1), B(1,1) )
>
And more readable imo is EQUIVALENCE (A,C), (U,B)
| |
| Daniel 2005-05-17, 3:59 pm |
| so this is not some strange kind of statement meaning something like:
(forgive this pseudocode)
EQUIVALENCE (A (1,"all elements"), C (1,"all elements")) , ( U (1), B
(1, "all elements"))
?
| |
| Duane Bozarth 2005-05-17, 3:59 pm |
| Daniel wrote:
>
> so this is not some strange kind of statement meaning something like:
>
> (forgive this pseudocode)
>
> EQUIVALENCE (A (1,"all elements"), C (1,"all elements")) , ( U (1), B
> (1, "all elements"))
>
> ?
No (whatever your pseudocode is supposed to represent)...
The EQUIVALENCE simply aligns the subsequent elements of the arrays
beginning at the element explicitly given.
| |
| David Frank 2005-05-17, 3:59 pm |
|
"Daniel" <dalboz@gmx.net> wrote in message
news:1116332707.035488.55640@g14g2000cwa.googlegroups.com...
> so this is not some strange kind of statement meaning something like:
>
> (forgive this pseudocode)
>
> EQUIVALENCE (A (1,"all elements"), C (1,"all elements")) , ( U (1), B
> (1, "all elements"))
>
> ?
>
In your example it was apparent all arrays have base indices = 1
so my syntax: EQUIVALENCE (A,C), (U,B)
sets A array's starting address to C array's starting address, ditto U and
B arrays.
Then the usual Fortran array ordering takes over..
Test it out for yourself.
| |
| Dick Hendrickson 2005-05-17, 3:59 pm |
|
Michael Metcalf wrote:
> "Daniel" <dalboz@gmx.net> wrote in message
> news:1116327175.134413.205710@z14g2000cwz.googlegroups.com...
>
>
>
> The compiler is correct, it should be:
>
> EQUIVALENCE ( A(1,1), C(1,1) ),( U(1), B(1,1) )
>
> where each array reference has the correct rank. The statement sets up a
> mapping between the pairs of arrays listed; they then share the same
> (common) storage.
The relatively old FORTRAN 66 standard allowed the number of
subscripts in an EQUIVALENCE list to either be the same as
the rank of the array or to be 1.
If only one subscript was given, it was "as if" the array
element successor function was used to map the array to a
one dimension array. For some reason, this feature was
removed in the FORTRAN 77 standard.
Back in the good old days, (before optimization was very
good, when machines had few registers, and integer multiply
took a long time) it was a reasonably common extension
to allow multidimensional arrays to be referenced with
only one subscript (which often was out of bounds) as
a way to linearize whole array processing in a single
DO loop and gain significant efficiency.
Dick Hendrickson
>
> Regards,
>
> Mike Metcalf
>
>
| |
| glen herrmannsfeldt 2005-05-17, 8:58 pm |
| Dick Hendrickson wrote:
[color=darkred]
[color=darkred]
[color=darkred]
(snip)
[color=darkred]
> The relatively old FORTRAN 66 standard allowed the number of
> subscripts in an EQUIVALENCE list to either be the same as
> the rank of the array or to be 1.
> If only one subscript was given, it was "as if" the array
> element successor function was used to map the array to a
> one dimension array. For some reason, this feature was
> removed in the FORTRAN 77 standard.
It does seem that it was removed in Fortran 77. However,
as someone else mentioned, an array name representing the
first element of the array is still allowed.
While the appropriate number of subscripts are required, I
don't (yet) see the restriction that they be within the
bounds declared. One could possibly, using sequence association,
EQUIVALENCE to array elements. It might be that
REAL A(10,10),B(2)
EQUIVALENCE (A(99,1),B)
would still work. I wouldn't recommend using it, though.
> Back in the good old days, (before optimization was very
> good, when machines had few registers, and integer multiply
> took a long time) it was a reasonably common extension
> to allow multidimensional arrays to be referenced with
> only one subscript (which often was out of bounds) as
> a way to linearize whole array processing in a single
> DO loop and gain significant efficiency.
This feature is still in new Fortran standards. That is,
for assumed size arrays, one may use a dummy argument array
with a different number of subscripts, usually one. This
allows one to write array processing routines independent of
the number of dimensions specified.
Given that case is still allowed, I wonder why the EQUIVALENCE
case was removed.
-- glen
| |
| Ken Fairfield 2005-05-17, 8:58 pm |
| Daniel wrote:
> Hi!
>
> I was gven a source code like this:
>
> DIMENSION A(3,3), B(3,3), C(3,3), U(6)
>
> EQUIVALENCE ( A(1), C(1) ),( U(1), B(1) )
Try:
EQUIVALENCE ( A(1,1), C(1,1) ), ( U(1), B(1,1))
> My compiler won't work with the equivalence statement.
> I'd like to work around it, but I am not quite sure what this
> equvalence stetement
> here actually does, on which array elements it refers to...
Compiler make and version, host O/S, and exact compiler messages
are very helpful in diagnosing problems. However, the EQUIVALENCE
statement you showed should make most compilers give an error
message because of the way A, B and C were specified (wrongly) in
that statement.
> Can anyone explain?
This is "storage association". It makes the array A and the array
C use the same storage locations, such that a change to, e.g., C(2,1)
changes the value of A(2,1) to the same value (these amount to aliases
for the same storage). Similarly, U(5) refers to the same storage as
B(2,2), etc.
Regards, Ken
--
I don't speak for Intel, Intel doesn't speak for me...
Ken Fairfield
D1C Automation VMS System Support
who: kenneth dot h dot fairfield
where: intel dot com
| |
| Richard E Maine 2005-05-17, 8:58 pm |
| In article <w4OdnY76T9Au0RffRVn-uw@comcast.com>,
glen herrmannsfeldt <gah@ugcs.caltech.edu> wrote:
[about equivalence]
> While the appropriate number of subscripts are required, I
> don't (yet) see the restriction that they be within the
> bounds declared.
That's probably because you are looking in the wrong place. I bet you
are looking under equivalence. It isn't there because it is much more
general restriction than that. But it isn't too hard to trace starting
from there.
In the f77 standard (because that seems to be the topical version here),
you can see in the section on equivalence (8.2.1) that one of the things
allowed in an equivalence is an "array element name" (F90 doesn't call
things like this "names", but I'll stick to f77 for now). "Array element
name" is defined in the eponymous section 5.3, where we see that it
involves "subscript expressions". "Subscript expression is defined in
5.4.2." The second para of 5.4.2 is the limitation in question.
> Given that case is still allowed, I wonder why the EQUIVALENCE
> case was removed.
Beats me. That was done in the change from f66 to f77, which predates my
involvement in standards activities. I was programming in Fortran then,
but didn't know much of anything about standards (a failing that I
apparently shared with most programers of the era).
--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
| |
| glen herrmannsfeldt 2005-05-17, 8:58 pm |
| Richard E Maine wrote:
> In article <w4OdnY76T9Au0RffRVn-uw@comcast.com>,
> glen herrmannsfeldt <gah@ugcs.caltech.edu> wrote:
>
> [about equivalence]
>
[color=darkred]
> That's probably because you are looking in the wrong place. I bet you
> are looking under equivalence. It isn't there because it is much more
> general restriction than that. But it isn't too hard to trace starting
> from there.
(snip)
> "Array element
> name" is defined in the eponymous section 5.3, where we see that it
> involves "subscript expressions". "Subscript expression is defined in
> 5.4.2." The second para of 5.4.2 is the limitation in question.
OK, I was expecting a separate one for the constant subscripts required
for EQUIVALENCE, instead of the general one used in expressions.
It seems that they just add the restriction that the subscripts be
constant as part of EQUIVALENCE.
-- glen
|
|
|
|
|