Next: , Previous: , Up: Fortran interface   [Contents][Index]


3.3 Fortran sample program

Here follows sample program demonstrating use of library Fortran interface. Hopefully, comments throughout code will be enough for Fortran programmer to get acquainted with library usage. Basic functioning of program is equivalent to code presented for C programmer in Introduction sequence, except that textual representation of function derivative is not printed to standard output and this is avoided simply because of Fortran 77 ugly string handling. Following code is written in Fortran 77 with GNU Fortran 77 compiler extensions (most notable of these certainly is free form of source code).

! Program is demonstrating use of GNU libmatheval library of procedures
! for evaluating mathematical functions.
program evaluator
  implicit none

  ! Declarations of GNU libmatheval procedures used.
  integer*8 evaluator_create
  integer*8 evaluator_derivative_x
  double precision evaluator_evaluate_x
  external evaluator_destroy

  ! Size of input buffer.
  integer :: BUFFER_SIZE
  parameter(BUFFER_SIZE = 256)

  character(len = BUFFER_SIZE) :: buffer ! Input buffer.
  integer*8 :: f, f_prim ! Evaluators for function and function derivative.
  double precision :: x ! Variable x value.

  ! Read function.  Function has to be over variable x, or result may
  ! be undetermined.  Size of textual represenatation will be truncated
  ! here to BUFFER_SIZE characters, in real conditions one should 
  ! probably come with something smarter to avoid this limit.
  write (*, '(A)') 'f(x) = '
  read (*, '(A)') buffer

  ! Create evaluator for function.
  f = evaluator_create (buffer);
  if (f == 0) stop

  ! Create evaluator for function derivative.
  f_prim = evaluator_derivative_x (f);
  if (f_prim == 0) stop

  ! Read variable x value.
  write (*, '(A)') 'x = '
  read (*, *) x

  ! Calculate and print values of function and its derivative for given
  ! value of x.
  write (*,*) '  f (', x, ') = ', evaluator_evaluate_x (f, x)
  write (*,*) '  f'' (', x, ') = ', evaluator_evaluate_x (f_prim, x)

  ! Destroy evaluators.
  call evaluator_destroy (f)
  call evaluator_destroy (f_prim)
end program evaluator

Next: Fortran build process, Previous: Fortran convenience procedures, Up: Fortran interface   [Contents][Index]