Information Technology Reference
In-Depth Information
added that new method to the library. This is a key point in handling large pieces of
numerical software. One solution is to employ
object-oriented programming
.Inthe
following we assume that the reader is familiar with object-oriented programming
from a basic course in Java or C
CC
.
In object-oriented programming we represent the numerical algorithms by
objects
. Objects contain both data and functions (operating on the object data and
external data). We introduce a pseudo-code notation for objects:
class Trapezoidal
data:
a, b, f, n
methods:
integrate()
init(a, b, f, n)
This pseudo code defines the contents of objects of type
Trapezoidal
. Each such
object will contain the data
a
,
b
,
f
,and
n
(with obvious meaning in the present exam-
ple) and a function called
integrate
, for carrying out the numerical integration,
and another function
init
, for initializing the data members.
We create a class for each integration method, say,
Trapezoidal
,
Simpson
,
and so on. Each class is
derived
from a general base (or super) class called
Integration
. The base class defines the set of legal methods that can be applied to
all integration methods and it also declares the set of data that can be shared among
various integration methods (typically
a
,
b
,
f
,and
n
in our example). As soon as
we have created an object representing a particular method, say,
Simpson
, we can
treat this object as being of type
Integration
in the rest of the code. This means
that we hide the information that we are working with Simpson's rule; all we can
see is a general numerical integration object. When we want to integrate a function,
we have an object (say)
i
and just call
i
's
integrate
method, without arguments.
The magic of object-oriented programming is that the program knows the type of
integration object we actually have (say,
Simpson
) and calls the
integrate
method
in the appropriate subclass (here
Simpson
's
integrate
method). Since the object
type can be treated as “identical” for all methods, and since all
integrate
methods
take the same argument list (empty in this case), the application code for integrating
a function is independent of the method we use.
At this stage it might be encouraging to realize that the outlined object-oriented
programming approach has wide applications. Numerical software is basically a col-
lection of different types of numerical methods (integration, the solution of ODEs,
the solution of nonlinear equations, the solution of linear systems of equations, etc.)
and a “main program” calling up a combination of methods to solve a specific prob-
lem. A certain type of numerical method has many different algorithms. Employing
the idea of object-oriented programming, each type of numerical method is imple-
mented as a class hierarchy, where the different algorithms are realized as different
subclasses. In this way, we will have a class hierarchy for numerical integration,
ODE solvers, nonlinear equation solvers, and so on. When an ODE solver needs a
nonlinear equation solver as part of the algorithm, it can work with a general (base
class) interface to all methods for nonlinear equations. The code applying these
methods does not need to distinguish between bisection or Newton's method; just