One common use of Fortran's Equivalence is the following: A large
array of numeric values is made available to a subprogram as a
parameter. The array contains many different unrelated variables,
rather than a collection of repetitions of the same variable. It is
represented as an array to reduce the number of names that need to be
passed as parameters. Within the subprogram, a lengthy Equivalence
statement is used to create connotative names as aliases to the various
array elements, which increases the readability of the code of the
subprogram. Is this a good idea or not. What alternatives to aliasing
are available?
|
|
0
|
|
|
|
Reply
|
djlmunoz (1)
|
2/26/2005 8:35:23 PM |
|
djlmunoz@gmail.com wrote:
> One common use of Fortran's Equivalence is the following: A large
> array of numeric values is made available to a subprogram as a
> parameter. The array contains many different unrelated variables,
> rather than a collection of repetitions of the same variable. It is
> represented as an array to reduce the number of names that need to be
> passed as parameters. Within the subprogram, a lengthy Equivalence
> statement is used to create connotative names as aliases to the various
> array elements, which increases the readability of the code of the
> subprogram. Is this a good idea or not. What alternatives to aliasing
> are available?
>
You can't do this in standard Fortran. If by "parameter"
you mean argument, then you can't equivalence to it. Some
compiler might allow it as an extension, but you can't
count on it.
A more modern way (since 1990 or so) is to use a module.
module my_numbers
real, parameter :: pi = 3.14
real, parameter :: 2 = 2.718
integer, parameter :: one_over_fine = 137
real :: x = 0
end module my_numbers
....
subroutine whatever (...)
use my_numbers only: pi, x
This has lots of advantages (in my opinion). You can make
them be actual "parameters" in the Fortran use of the term,
compile time constants like "pi", or be ordinary variables
with meangingful names, like "x" ;) . You only need to
use the ones you actually want. If you add some more items
to the module, you don't have to change any existing code,
it won't see the new items (if you've used the only clause.
Dick Hendrickson
|
|
0
|
|
|
|
Reply
|
dick.hendrickson (1286)
|
2/26/2005 8:48:39 PM
|
|
Dick Hendrickson wrote:
>
>
> djlmunoz@gmail.com wrote:
>
>> One common use of Fortran's Equivalence is the following: A large
>> array of numeric values is made available to a subprogram as a
>> parameter. The array contains many different unrelated variables,
>> rather than a collection of repetitions of the same variable. It is
>> represented as an array to reduce the number of names that need to be
>> passed as parameters. Within the subprogram, a lengthy Equivalence
>> statement is used to create connotative names as aliases to the various
>> array elements, which increases the readability of the code of the
>> subprogram. Is this a good idea or not. What alternatives to aliasing
>> are available?
>>
>
> You can't do this in standard Fortran. If by "parameter"
> you mean argument, then you can't equivalence to it. Some
> compiler might allow it as an extension, but you can't
> count on it.
>
> A more modern way (since 1990 or so) is to use a module.
> module my_numbers
> real, parameter :: pi = 3.14
> real, parameter :: 2 = 2.718
> integer, parameter :: one_over_fine = 137
> real :: x = 0
> end module my_numbers
> ...
> subroutine whatever (...)
> use my_numbers only: pi, x
>
> This has lots of advantages (in my opinion). You can make
> them be actual "parameters" in the Fortran use of the term,
> compile time constants like "pi", or be ordinary variables
> with meangingful names, like "x" ;) . You only need to
> use the ones you actually want. If you add some more items
> to the module, you don't have to change any existing code,
> it won't see the new items (if you've used the only clause.
If the OP meant to say "argument" rather than "parameter", then the way
to achieve the goal is to define a derived type, and then pass a
variable of the derived type around:
module my_module
type my_type
real :: position
real :: velocity
integer :: n_particles
...
end type my_type
end module my_module
....
subroutine my_subroutine (a)
use my_module
type(my_type) :: a
...
This is the standard way in Fortran 90/95/2003 to aggregate a
non-homogeneous collection of variables.
cheers,
Rich
|
|
0
|
|
|
|
Reply
|
rhdt (1081)
|
2/26/2005 8:59:26 PM
|
|
djlmunoz@gmail.com wrote:
> One common use of Fortran's Equivalence is the following: A large
> array of numeric values is made available to a subprogram as a
> parameter. The array contains many different unrelated variables,
> rather than a collection of repetitions of the same variable. It is
> represented as an array to reduce the number of names that need to be
> passed as parameters. Within the subprogram, a lengthy Equivalence
> statement is used to create connotative names as aliases to the various
> array elements, which increases the readability of the code of the
> subprogram. Is this a good idea or not. What alternatives to aliasing
> are available?
>
In graphic package I once used there were many parameters. They were
all given symbolic names. The package included a subroutine to set
parameters in a specification array. So
CALL SETSPC ( SPCARY, "xstep", 1.0 ) (F77 six character names :-( )
would set the parameter "xstep" in the specification array "spcary"
to the value "1.0". There was a corresponding GETSPC to get the
value out of the specification array. They both had to agree that
xstep would be in position 17 (or whatever). The coding of these
utilities consisted of a fair number of IFs with character comparisons.
Same sort of notion except the user was not bothered by having to look
at the equivalence statement and could have as many different
specifications as were needed.
With a user defined type in a module this would now become
Graph_Spec%xstep = 1.0
in the user declared Graph_Spec. The "equivalence" and packaging
is in the user defined type.
That is one of the features of F90. There are those who refuse to
use these new fangled features because they never learned them
form their grandmothers. Others have to forgo modernity for the
sake of backwards compatibility with a large installed code base.
|
|
0
|
|
|
|
Reply
|
g.sande (1183)
|
2/26/2005 9:32:30 PM
|
|
Dick Hendrickson wrote:
>
>
> djlmunoz@gmail.com wrote:
>
>> One common use of Fortran's Equivalence is the following: A large
>> array of numeric values is made available to a subprogram as a
>> parameter. The array contains many different unrelated variables,
>> rather than a collection of repetitions of the same variable. It is
>> represented as an array to reduce the number of names that need to be
>> passed as parameters. Within the subprogram, a lengthy Equivalence
>> statement is used to create connotative names as aliases to the various
>> array elements, which increases the readability of the code of the
>> subprogram. Is this a good idea or not. What alternatives to aliasing
>> are available?
>>
>
> You can't do this in standard Fortran. If by "parameter"
> you mean argument, then you can't equivalence to it. Some
> compiler might allow it as an extension, but you can't
> count on it.
>
> A more modern way (since 1990 or so) is to use a module.
> module my_numbers
> real, parameter :: pi = 3.14
> real, parameter :: 2 = 2.718
Does this line do what it appears to do (redefine the constant 2 to 2.718)?
Gib
|
|
0
|
|
|
|
Reply
|
bogle (300)
|
2/27/2005 12:50:00 AM
|
|
<djlmunoz@gmail.com> wrote in message...
> One common use of Fortran's Equivalence is the following: A large
> array of numeric values is made available to a subprogram as a
> parameter. The array contains many different unrelated variables,
> rather than a collection of repetitions of the same variable. It is
> represented as an array to reduce the number of names that need to be
> passed as parameters. Within the subprogram, a lengthy Equivalence
> statement is used to create connotative names as aliases to the various
> array elements, which increases the readability of the code of the
> subprogram. Is this a good idea or not. What alternatives to aliasing
> are available?
I have seen this done when using a machine/OS API for reading files. SO
when reading a file the FREAD would use a logical array as the buffer. This
logical array was equivalenced to the field names with in each record of the
file being read.
Like someone said below in this thread. You can't directly equivalence to a
passed parameter inside a subroutine/function.
|
|
0
|
|
|
|
Reply
|
collector (4)
|
2/27/2005 2:15:48 AM
|
|
Gib Bogle wrote:
> Dick Hendrickson wrote:
> > You can't do this in standard Fortran. If by "parameter"
> > you mean argument, then you can't equivalence to it. Some
> > compiler might allow it as an extension, but you can't
> > count on it.
> >
> > A more modern way (since 1990 or so) is to use a module.
> > module my_numbers
> > real, parameter :: pi = 3.14
> > real, parameter :: 2 = 2.718
>
> Does this line do what it appears to do (redefine the constant 2 to 2.718)?
Only on a compiler that does really poor error-checking. What it should
do (for certain values of should that may or may not be "the standard
requires it") is produce an error message, as "2" is not a valid
parameter name.
In any case, I'm reasonably sure it was meant to be "e = 2.718", and is
simply a typo.
- Brooks
--
The "bmoses-nospam" address is valid; no unmunging needed.
|
|
0
|
|
|
|
Reply
|
bmoses-nospam (1258)
|
2/27/2005 2:46:44 AM
|
|
Gib Bogle wrote:
> Dick Hendrickson wrote:
>
>>
>>
>> djlmunoz@gmail.com wrote:
>>
>>> One common use of Fortran's Equivalence is the following: A large
>>> array of numeric values is made available to a subprogram as a
>>> parameter. The array contains many different unrelated variables,
>>> rather than a collection of repetitions of the same variable. It is
>>> represented as an array to reduce the number of names that need to be
>>> passed as parameters. Within the subprogram, a lengthy Equivalence
>>> statement is used to create connotative names as aliases to the various
>>> array elements, which increases the readability of the code of the
>>> subprogram. Is this a good idea or not. What alternatives to aliasing
>>> are available?
>>>
>>
>> You can't do this in standard Fortran. If by "parameter"
>> you mean argument, then you can't equivalence to it. Some
>> compiler might allow it as an extension, but you can't
>> count on it.
>>
>> A more modern way (since 1990 or so) is to use a module.
>> module my_numbers
>> real, parameter :: pi = 3.14
>> real, parameter :: 2 = 2.718
>
>
> Does this line do what it appears to do (redefine the constant 2 to 2.718)?
Nope, it's an error. I was aiming for the "e" key and missed.
Dick Hendrickson
>
> Gib
|
|
0
|
|
|
|
Reply
|
dick.hendrickson (1286)
|
2/27/2005 5:10:24 PM
|
|
Patrick Thrapp wrote:
> <djlmunoz@gmail.com> wrote in message...
>
>>One common use of Fortran's Equivalence is the following: A large
>>array of numeric values is made available to a subprogram as a
>>parameter. The array contains many different unrelated variables,
>>rather than a collection of repetitions of the same variable. It is
>>represented as an array to reduce the number of names that need to be
>>passed as parameters. Within the subprogram, a lengthy Equivalence
>>statement is used to create connotative names as aliases to the various
>>array elements, which increases the readability of the code of the
>>subprogram. Is this a good idea or not. What alternatives to aliasing
>>are available?
> I have seen this done when using a machine/OS API for reading files. SO
> when reading a file the FREAD would use a logical array as the buffer. This
> logical array was equivalenced to the field names with in each record of the
> file being read.
> Like someone said below in this thread. You can't directly equivalence to a
> passed parameter inside a subroutine/function.
You could easily copy a whole array to an EQUIVALENCEd array with one
DO loop, less code than a large number of assignments.
-- glen
|
|
0
|
|
|
|
Reply
|
gah (12236)
|
2/27/2005 10:06:04 PM
|
|
|
8 Replies
29 Views
(page loaded in 0.16 seconds)
Similiar Articles: Intel Fortran Complier + MPICH 1.2.6 (Beginner Question) - comp ...This might be not a MPI question. I upgraded my F77 compiler to Intel Fortran Compiler(IFC) for some F90 features. So.. I rebuilt the MPICH to set th... GUI: Fortran + Visual Basic - comp.lang.fortranOn 3/9/2010 2:29 AM, Abbas Fakhari wrote: > I want to generate a software ... As Steve L says, it's essentially not a Fortran question at all; it's simply an interface ... Fortran 95 equivalent of read(..., POS=...) - comp.lang.fortran ...Intel Fortran Complier + MPICH 1.2.6 (Beginner Question) - comp ... Fortran 95 equivalent of read(..., POS=...) - comp.lang.fortran ..... obvious what OP meant, see Intel's ... Help needed: read 3-dimensional array from a MAT-file in Fortran ...> > Question: How to read the MAT-file in Fortran F90 and store the data in a similar 3-dimensional array? I am, a novice of course, using Intel Visual Fortran Compiler on XP ... Fortran 77/90/95 free compiler - comp.lang.fortranWhere can I download I compiler for Fortran 77/90/95 (like Force 2.0 for Fortran 77) ? ... As I mentioned in a different thread, in reply to a similar question See www ... [Question] Install gcc -Arithmetic Exception (core dumped ...segmentation fault (SIGSEGV) - comp.lang.fortran [Question] Install gcc -Arithmetic Exception (core dumped ... segmentation fault (SIGSEGV) - comp.lang.fortran [Question ... Allocatable versus automatic arrays - comp.lang.fortranAllocatable versus automatic arrays - comp.lang.fortran Questions regarding derived data types with allocatable arrays ... Allocatable versus automatic arrays - comp.lang ... problem with mixed c and fortran code - comp.lang.fortran ...It's possible for someone who programs with great confidence in C to really struggle with Fortran and there are some questions about the level of indirection of dummy ... fortran compiler #5 - comp.lang.fortranHello, Question: Is the MS Visual C++ Digital Visual Fortran 6.0 compatible with Compaq Visual Fortran Professional Edition 6.6.0.? I am trying to ... MPI Program help - comp.parallel.mpiHelp needed: read 3-dimensional array from a MAT-file in Fortran ... ... Question on linking MPI programs with ifc - comp.parallel.mpi ... Intel Fortran Complier ... Fortran Questions - www.personal.psu.eduFortran Questions. Can you give us a complete list of the Fortran commands and what they do? Fortran Question - Page 2Computing & Technology > Programming & Comp Sci ... In the subrotine, if i want do a loop with matrix, how i define the matrix? I try ... I found the problem ... 7/18/2012 9:53:46 AM
|