Numerical Computing with Modern Fortran
Richard J. Hanson and Tim Hopkins
Chapter 5: Object-Oriented Programming for Numerical Applications
These routines execute a classical zero finding numerical algorithm of Richard Brent. The algorithm is executed in four ways, all using the same example of finding a zero of the function f(x) = x*(x*x-2.0d0)-5.0 on the interval [2,3]. Some concepts of Object-Oriented computing are illustrated in the source codes.
Four executables may be generated from the makefile
- testOriginal uses zeroinOrig.f, testOriginal.f90.
A Fortran 90 main program and function subprogram call the original code.
- testType uses brentTypes.f90, testType.f90.
This module contains a derived type for the required interval and error tolerance, and the zero finding algorithm as a module subprogram. The user function is an external subprogram that is located at the end of the main program.
- testClass uses set_precision.f90, brentClass.f90, brentExtraArgs.f90,
These modules, brentClass and brentExtraArgs, extend a derived type to a user class. The zero solving algorithm is packaged in brentClass and uses the contents of the user class to call the user function, packaged in brentExtraArgs.
- testClassFinalize uses set_precision.f90, brentClass.f90,
The program testClassFinalize uses modules brentExtraArgsFinalize and brentClass. The derived type brentArgs is extended to a user class argument that has a finalization routine which is called as the class argument dies or is deallocated. In this example the evaluation abscissa and associated function values are written to an external file, EvaluationList. This file is summarized and closed in the finalization routine, close_evaluation_file.
Sample output from testOriginal, testType and testClass
Root = 2.0945514809
Sample output from testClassFinalize
Root of f(x) = x*(x*x-2.0d0)-param = 0, is: 2.0945514809 The number of function evaluations required: 8 Output from the finalization routine: Number of function evaluations: 8 The file EvaluationList is closed.
Sample content of the file EvaluationList generated by testClassFinalize
2.0000000000 -1.0000000000 3.0000000000 16.0000000000 2.5000000000 5.6250000000 2.0754716981 -0.2106772705 2.0948885338 0.0037627021 2.0945478335 -0.0000407172 2.0945514809 -0.0000000077 2.0945514883 0.0000000754