Information Technology Reference
In-Depth Information
Based on this simple optimization example, and our experience with scien-
tific software development in general, we can provide some guidelines on how to
implement numerical methods:
1. Use an algorithm that is easy to understand and that can easily be looked up in
the literature.
2. Create a computer code that is as close to the algorithm as possible, such that
a “one-to-one correspondence” can be checked by reading the code and the
algorithm side by side.
3. Test the implementation on a simple problem where the exact answer is known
(such as f 2 .x/ D 1 Cx in the previous example).
4. Be careful with hand optimizations before the code is verified.
5. As soon as some hand optimizations are implemented, compile the code with
compiler optimization turned on, run the original and the hand-optimized code
in a relevant application, check that the results are equivalent, and then compare
timings.
Our recommended rule of thumb is to avoid optimization in the early stages of
software development since non-optimized code is usually easier to understand and
hence easier to debug. If CPU time consumption of the implemented algorithm is a
great concern, one can proceed with rewriting the algorithm for optimization. Hav-
ing the safe, non-optimized version at hand, it is much easier to verify the usually
trickier optimized version. We agree with the famous computer scientist Donald
Knuth, who said that “premature optimization is the root of all evil”.
We should emphasize that there are numerous examples, the first one appear-
ing already in Sect. 6.1.4 , especially in advanced scientific computing problems,
where a human is better than a compiler to perform optimizations. Most often, how-
ever, the human's contribution to faster code is to choose a more efficient numerical
method, whereas the benefit of the compiler is to arrange the sequence of arithmetic
operations in an optimal manner.
6.1.4
Developing Algorithms for Simpson's Rule
Let us discuss another example regarding the derivation and implementation of algo-
rithms for numerical integration. Simpson's rule for numerical integration reads (see
Project 1.7.2)
Z b
1
6 f.x i / ;
n X
6 f.x i1 /C 4
6 f.x i 2 /C 1
f.x/dx h
(6.3)
a
iD1
where
D 1
h D b a
n
x i D a C ih ;
2 .x i1 C x i /;
:
i 2
Simpson's rule is more accurate than the trapezoidal rule, in the sense that it requires
less computer power to calculate an integral with a certain accuracy. The task now
is to implement Simpson's rule.
 
Search WWH ::




Custom Search