This page contains links to the code samples that are part of or
referred to by our book.
The samples are in a wide range of different languages, including C,
C++, Fortran (mostly Fortran 90), and Java, although some are things
like makefile's,
or sed
scripts. Use at your own risk. They have been tested, and
we have tried to make sure that they are error free, but as with most
numerical code, accuracy and reliability cannot be taken for granted
even for a code that is "correct".
Our intention is that the codes provided here are more-or-less
complete, rather than short selections as shown in the book itself.
If you notice any problems with the codes provided here, please let
us know.
- Part I: Numerical Software
- Chapter 1: Why numerical software?
- Chapter 2: Scientific computation and Numerical Analysis
- Implementation of tanh(x)
with overflow problems (tanh1.c);
how to implement it without overflow problems (tanh2.c)
- Don't use floating point numbers to control for loops (bad-loop.c)
- NaN (Not-a-Number) test in Fortran 90 (nan-test.f90)
- Example of setting rounding modes (C99 standard) under gcc (fp-round.c)
- Standard one-pass and two-pass algorithms for mean and
variance (mean_stddev1.c)
- Four ways of computing (exp(z)-1)/z: (exp-eg.c)
- An unstable recurrence (unstable-recurrence.c)
- Chapter 3: Priorities
- Chapter 4: Famous disasters
- Chapter 5: Exercises
- Part II: Developing Software
- Chapter 6: Basics of computer organization
- Chapter 7: Software design
- Code for copying an array (copy-array1.c), and an
"improved" one using pointer arithmetic that often performs worse (copy-array2.c)
- Different loop orderings are equivalent, but perform
differently (mat-mult.c) (The
first is better. See the later chapter on "Architecture and
Performance".)
- One-relative (Fortran) vs. zero-relative (C/C++) indexing:
solution of triangular linear systems one-relative (Lsolve.f90) and
zero-relative (Lsolve1.c)
- Variable names: matrix multiplication: succinct (matmul1.c) or verbose (matmul2.c)
- Spline data structures and interfaces in C (spline.h)
- "Expert" (expert.c) vs.
"non-expert" (non-expert.c)
interfaces
- Optional arguments in Fortan 90 (ifaces-opt.f90)
- Hardwired vs. flexible: different ways of implementing the
trapezoidal method -- an exercise showing the importance of passing
functions (trapezoidal.c)
- Simple Unix scripts for generating documentation from
structured C++ comments (doc-gen-eg.sh)
- Routine for skipping comments (of the form #...\n) in data
files together with test (skipcomment.c),
and a test data file to try it out on (skip.dat)
- Chapter 8: Data structures
- Spline data structures and interfaces in C (spline.h)
- Matrix creation/allocation in C: naive (create_matrix1.c),
better (create_matrix2.c),
dangerous unless you use special syntax (create_matrix3.c).
And in Java (create_matrix.java)
- Creating/allocating a 3-D array in C (create_3d_array.c)
- Function environment issues:
- Reverse communication (integrate_rc_if.f90)
which includes testing code, and is based on this code for trapezoidal
rule (integrate.f90)
- Passing pointers in C (integrate_pp.c, qp_func.c, qp_integerate.c)
- Using interfaces in Java (Func.java,
Quadratic.java, FuncTest.java, Trapezoidal.java, quad-test.java)
- Using abstract classes in C++ (Func.h, quadratic.h, trapezoidal.cpp, trapezoidal.h)
- Compositors and efficient use of operator overloading in C++ (matvec.cpp)
- Use of templates in C++: Fixed Dimension Vectors: standard
implementation (FDVec.h),
template specializations (FDVec-spec.h),
and a recursive template for addition (rec-templ.h, rec-templ.cpp)
- Chapter 9: Deisgn for testing and debugging
- Checkpoint macro in C (checkpoint.h)
- Output routine for spline data structure (spline_print.c) and some
sample output (test1.out);
output from a routine that dumps out extra information such as memory
addresses (test1b.out)
- Input routine for spline data structure (new.h, spline_read.c)
- Spline files (spline.h,
utility.h, spline.c, spline_eval.c, spline_eval.h)
- Test routines for spline functions up to spline evaluation
but not spline construction (spline_test1.c,
spline_test2.c, spline_test3.c, spline_test4.c , spline_test5_inner.c)
- Error and exception handling
- Performance testing of ODE codes in C++ (ODEfunc.h, ode-perf.c)
- Chapter 10: Exercises
- Part III: Efficiency in time, efficiency in memory
- Chapter 11: Be algorithm aware
- Chapter 12: Computer architecture and efficiency
- Dot product routines: standard and with loops unrolled (dotprod.c)
- LAPACK general LU routines from LAPACK 3E (sgetrf.f90, sgetf2.f90). Note that sgetf2.f90
is the standard (unblocked) version.
- Chapter 13: Global vs. local optimization
- Chapter 14: Grabbing memory when you need it
- Chapter 15: Memory bugs and leaks A number of these examples are of things
you should not do!!!
- Example of code that allows overwriting memory (overwrite.c) and what its
output can look like (overwrite.out)
-- AVOID!
- Example of code that results in dangling pointers (dangle.cc) -- AVOID!
- Shallow vs. deep copying of data structures in Fortran 90 (deepvsshallow.f90)
- Examples of codes for adding 3-D vectors:
- ... which returns a pointer to a local variable (add_3d_vec1.cc) -- AVOID!
- ... which uses an output argument to put the result in (add_3d_vec2.cc)
- ... which allocates the result and returns it (add_3d_vec3.cc) but which is
vulnerable to memory leaks as it allocates a new vector each time it is
called
- ... which has an output argument that is allocated if it is
NULL on entry (add_3d_vec4.cc)
- Part IV: Tools
- Chapter 16: Sources of scientific software
- Chapter 17: Unix tools
- Part V: Design examples
- Chapter 18: Cubic spline function library
- Main spine library (splines.f90),
which needs the set_precision module in (my_wp.f90) and the tridiagonal
matrix library (tridiag.f90).
Note that basic testing code is incorporated into the splinesand tridiag
modules so that only a very simple main program is needed for testing.
- Performance testing code (splpt.f90)
- Chapter 19: Multigrid algorithms