For Programmers: Free Programming Magazines  


Home > Archive > Fortran > February 2005 > fortran question









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]

 

Author fortran question
djlmunoz@gmail.com

2005-02-26, 8:58 pm

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?

Dick Hendrickson

2005-02-26, 8:58 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

Rich Townsend

2005-02-26, 8:58 pm

Dick Hendrickson wrote:
>
>
> djlmunoz@gmail.com 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
> 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
Gordon Sande

2005-02-26, 8:58 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.




Gib Bogle

2005-02-27, 3:58 am

Dick Hendrickson wrote:

>
>
> djlmunoz@gmail.com 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)?

Gib
Patrick Thrapp

2005-02-27, 3:58 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.


Brooks Moses

2005-02-27, 3:58 am

Gib Bogle wrote:
> Dick Hendrickson wrote:
>
> 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.
Dick Hendrickson

2005-02-27, 4:00 pm



Gib Bogle wrote:
> Dick Hendrickson wrote:
>
>
>
> 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


glen herrmannsfeldt

2005-02-27, 8:58 pm

Patrick Thrapp wrote:

> <djlmunoz@gmail.com> wrote in message...
>
[color=darkred]
> 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com