Home : Programming

Fortran

"I don't know what the language of the year 2000 will look like, but I know it will be called Fortran." -- Tony Hoare

Arrays

By convention, Fortran arrays are indexed from 1 and up.

FGSL

GSL is the Gnu Scientific Library. FGSL is the Fortran interface to it.

Setting up FGSL 0.9.4 + GSL 1.15 + Slackware 13.37. There can be library incompatability problems, so I compiled GSL and FGSL from source. Compilation was straightforward and obvious. Suppose you have a file stats.f90:

program stats
  use fgsl 
  implicit none
  real(fgsl_double) :: data(5) = (/17.2D0, 18.1D0, 16.5D0, 18.3D0, 12.6D0 /)
  real(fgsl_double) :: mean, variance, largest, smallest

  mean     = fgsl_stats_mean(data, 1_fgsl_size_t, 5_fgsl_size_t)
  variance = fgsl_stats_variance(data, 1_fgsl_size_t, 5_fgsl_size_t)
  largest  = fgsl_stats_max(data, 1_fgsl_size_t, 5_fgsl_size_t)
  smallest = fgsl_stats_min(data, 1_fgsl_size_t, 5_fgsl_size_t)
  
  write(6, '(''The dataset is '',5(F9.5))') data
  write(6, '(''The sample mean is '',F9.5)') mean
  write(6, '(''The estimated variance is '',F9.5)') variance
  write(6, '(''The largest value is '',F9.5)') largest
  write(6, '(''The smallest value is '',F9.5)') smallest
end program stats
Compile it using
g95 -cpp --intrinsic-modules-path /usr/include/g95 \
-o stats stats.f90 -lfgsl_g95 \
-L/usr/local/lib -lgsl -lgslcblas 

Internal read

If you want to convert from a string to a number, you need to perform a "Fotran internal read", like so:
program fir ! "fortran internal read"
  real v
  character*20 :: inp
  inp = "666.666"
  read(inp, fmt = *), v
  print *, v
end program fir

Links to other sites

  • Advanced Fortran 90 - quite extensive
  • flibs - a collection of fortran modules on Sourceforge
  • scope - scoping rules at the module and subroutine level
    Author:  Mark Carter
    Created: 22-Nov-2011
    Updated: 05-Feb-2012