Should I convert FORTRAN code to C?
Peter Shenkin
shenkin at cubsun.BIO.COLUMBIA.EDU
Thu Jun 30 00:15:56 AEST 1988
A few points concerning the everpresent comparisons between C and Fortran.
(I like *both*!!!) Anyway, my first point has to do with recursion. The
second with EQUIVALENCE. If you're bored already, hit "n".
In article <817 at garth.UUCP> you write:
>However convenient it might be, recursion is not strictly necessary in
>practical programs....
POINT 1:
I used to agree, and most people agree. Most textbook examples of recursion
-- eg, calculation of factorial(n) -- would be much clearer and faster, in
practice if coded using simple iteration. But I've come across a class of
problem which can only be generally handled through recursion, I think. I'd
be intrigued if someone can come up with a Fortran solution. (Mail it to me,
and I'll post a summary to comp.lang.fortran.)
This class involves, conceptually, nested DO-loops, when the depth of the nest
is unknown at compile time. For example, the following C program generates
grid points within a hypercube, given the number of degrees of freedom
(dimensionality) of the space, and the number of gridpoints (number of levels)
along each coordinate. Turns out I need to do more complicated versions of
this sort of thing all the time in numerical work.
NB: This *can* be coded in Fortran by setting up the maximum number of needed
DO-loops and escaping early, if need be. But this is unwieldy, at best , unless
this maximum is small. And here (if one malloc()'s -- see my note on MAXDF)
a maximum need not be specified.
/*****************************************************************************/
/* grid.c: generate grid points in a hypercube */
/* compile by saying, "cc -o grid grid.c"
/* invoke by saying, eg, "grid 4 2", to generate the vertices, only, of
* a 4-D hypercube.
*/
#define MAXDF 100
/* max number of degrees of freedom -- not really needed,
* since we could malloc() value[] in grdgen() */
main(argc,argv)
int argc;
char *argv[];
{
int df,nlev; /* number of deg. of freedom, number levels per df */
df= atoi(argv[1]);
nlev= atoi(argv[2]);
printf("df=%d; nlev=%d\n",df,nlev);
grdgen(df,nlev);
}
/*****************************************************************************/
grdgen(df,nlev)
int df; /* no. of degrees of freedom */
int nlev; /* no. of levels each df */
{
static int begin=1; /* ==0 if func was called by itself */
static int totaldf; /* total no. of degrees of freedom (1st call)*/
static int value[MAXDF]; /* to hold current values of all df's */
int i,j;
if(begin)
totaldf=df;
if(--df ==0) { /* iterate through last df and print values */
for(i=0; i<nlev; ++i) {
value[df] = i;
for(j=0; j<totaldf; ++j) {
printf("%d ",value[j]); }
printf("\n");
}
begin=1;
}
else
for(i=0; i<nlev; ++i) {
value[df] = i;
begin = 0;
grdgen(df,nlev);
}
}
/*****************************************************************************/
POINT 2:
>>>Apparently some Fortran programmers equivalence different typed arrays to
>>>create structures (shudder).
>
>Some fortran programmers use the EQUIVALENCE statement to adjust the
>beginning address of different arrays with different types so that
>they map their elements into interleaved storage.
Generally it's the same storage, not interleaved storage, and it's not to
create structures. (The conceptual equivalent (not real equivalent -- there
is none) of structures in standard Fortran is labeled COMMON.) EQUIVALENCE
is sometimes used to be able to reuse declared (C programmers would say
"defined") storage for different purposes in different places. The conceptual
equivalent of this in C is the union.
--
*******************************************************************************
Peter S. Shenkin, Department of Biological Sciences, Columbia University,
New York, NY 10027 Tel: (212) 280-5517 (work); (212) 829-5363 (home)
shenkin at cubsun.bio.columbia.edu shenkin%cubsun.bio.columbia.edu at cuvma.BITNET
More information about the Comp.lang.c
mailing list