Fortran pros and cons (was Re: Coding style article with interesting section on white space)
beliavsky at aol.com
beliavsky at aol.com
Mon Jan 31 00:16:11 EST 2005
Michael Tobis wrote:
> beliavsky at aol.com wrote:
> > Michael Tobis wrote:
>
> > Fortran 90/95 is more expressive than Fortran 77 in many ways, as
> > described in ...
> > http://www.nr.com/CiP97.pdf .
> >
>
> > ... expresses more science per
> > line of code and per programming workday.
>
> The example shown on p 10 illustrates a shorter piece of code in f90
> than in f77, but it is not obviously more expressive or less complex.
> Arguably the f77 code is easier to write and maintain, even though it
> has more linefeeds in it, so I find the example far from compelling.
Ok, here are some simple examples of the greater expressiveness of
Fortran 95 compared to F77 or C for calculations involving arrays.
(1) To compute the sum of squares for each column of a matrix of the
positive elements, one can write in F90 just
isum = sum(imat**2,dim=1,mask=imat>0)
compared to
do j=1,ncol
isum(j) = 0
do i=1,nrows
if (imat(i,j) > 0) isum(j) = isum(j) + imat(i,j)**2
end do
end do
I think there is a similar Numeric Python one-liner using the sum and
compress functions. Array operations are not revolutionary (APL had
them in 1960s), but they are faster to write and later read.
(2) Suppose x and y are matrices of the same size and one wants to set
each element y(i,j) = f(x(i,j)) for some elemental (no side-effects)
function f. In Fortran 95, one can just write
y = f(x)
compared to
do j=1,ncol
do i=1,nrow
y(i,j) = f(x(i,j))
end do
end do
The ufunc of Numeric Python and the map of basic Python offer similar
functionality.
With Fortran 95 one can code numerical algorithms involving arrays in a
high-level manner similar to Python with Numeric/Numarray or Matlab,
while retaining the advantages (better performance, stand-alone
executables) and disadvantages (explicit variable declarations, no
scripting ability) of a compiled language with static typing. That's
all I am claiming.
More information about the Python-list
mailing list