Introduction to Numerical Recipes in C

R. Sureshkumar and Gregory J. McRae

Department of Chemical Engineering
Massachusetts Institute of Technology
Cambridge, MA 02139

Table of Contents

  1. What are Numerical Recipes in C?
  2. Where are the source files on Athena?
  3. How do we use Numerical Recipes in C on Athena?
  4. Show examples of driver programs
  5. How can we compile and run driver programs?
  6. References
  7. Restrictions

[ 10.001 Home Page | MIT Home Page ]

What are Numerical Recipes in C?

Numerical Recipes in C is a collection (or a library) of C functions written by Press et al. (1992) which can be used for standard numerical applications. These applications include solution of systems of linear equations, matrix manipulations, evaluation of functions, solution of non-linear algebraic equations, solution of differential and integral equations and statistical analysis. The book by Press et al. has a listing and a short description of each one of the C functions.

The motivation in using libraries such as Numerical Recipes in C is the following. As a result of modeling a process/system of our interest, we typically arrive at a mathematical description of the process or system. If we can break up the mathematical problem into a set of standard mathematical problems whose numerical solution can be obtained through the use of functions available in libraries, we can save the substantial time and effort which go into the coding and debugging. Typically, for standard applications, the scientific computing libraries are robust enough to allow for their use in design/research work.

This document describes how to find the Numerical Recipes in C routines on Athena, how to compile them, and then link them into your own codes for use in solving practical problems.

Return to the Table of Contents.

Where are the sources files on Athena?

The source files for Numerical Recipes in C are located in an Athena locker. The following sequence of commands show you how to:

  1. Attach the NRC recipes locker to your account
  2. Change directory to the source files for NRC
  3. List the contents of the directory
  4. Display the C code for the Gauss Jordan program
athena%  add recipes     
athena%  cd /mit/recipes/src/recipes_c-ansi/recipes
athena%  ls *
athena%  more gaussj.c

(Please note that the characters "athena%" signifies the Athena prompt.)

A partial listing of the routines from the command ls * is

Makefile       ddpoly.c       hunt.c         pearsn.c.orig  rzextr.c
Makefile.bak   decchk.c       hunt.c.orig    period.c       savgol.c
Makefile.orig  df1dim.c       hypdrv.c       piksr2.c       scrsho.c
addint.c       dfour1.c       hypgeo.c       piksrt.c       select.c
airy.c         dfpmin.c       hypser.c       pinvs.c        selip.c

etc...
As a specific example the file gaussj.c provides a C function for solving systems of linear algebra problems of the form A x = b using Gauss-Jordan reduction with pivoting. Further details of the use of the algorithm itself and a listing can be seen in Chapter 2, p. 36 of Press et al. (1992). The first few lines of the file obtained with more gaussj.c is: gaussj.c Press et al. more gaussj.c

#include 
#define NRANSI
#include "nrutil.h"
#define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}

void gaussj(float **a, int n, float **b, int m)
{
        int *indxc,*indxr,*ipiv;
        int i,icol,irow,j,k,l,ll;
        float big,dum,pivinv,temp;

... etc. ....

Return to the Table of Contents.

How do we use Numerical Recipes in C on Athena?

In order to use a NRC function we need to write a driver program. A driver program is typically one in which we read the relevant data for the problems (in this case, matrix a, number of equations n, the number of right hand side vectors m, and the r.h.s. data for each right hand side in a matrix of dimension n*m), call the appropriate Numerical Recipes in C function (in this case gaussj) and do any post processing and printing of the results. In this case, the function gaussj can be called by reference, and if you refer to book, Press et al. (1992) you will find that after the call to the function, the matrix a has the inverse of the original (input) matrix and b contains the solution vectors.

Note: You may choose to get the source code and modify it, but the driver program route is more elegant, prone to less difficulties in debugging and more systematic, in my opinion. The next section of this document contains secveral examples of the use of driver programs.

Return to the Table of Contents.

Sample Driver Programs for Numerical Recipies in C

This section of the document shows two examples of the use of Numerical Recipes in C functions called from user written driver programs. The web links for the examples are:

The first example shows how to use the gaussj.c program to solve a system of linear equations of the form A x = b. A test file gauss.dat is also available to test the program.

The second example use the function trapzd.c to compute integrals. This example also illustrates how to pass functions as arguements to other functions.

Return to the Table of Contents.

How can we compile and run gauss_driver.c ?

We need to let the compiler know where the function gaussj can be found and how it can be linked. The UNIX consultants recommend the following, rather long, command.

/mit/cygnus/`machtype`bin/gcc -DANSI -I/mit/recipes/src/recipes_c-ansi/include 
-L/mit/recipes/`machtype`lib $* gauss_driver.c -lrecipes_c-ansi -lm 
|& grep -v nrutil

Once the compilation is succesful, type a.out to run. Because of the length of the command to compile and link the program it is easier to copy the above command into a file, say compile_gauss_driver, and assign it an executable status (chmod +x filename).

The different options in the very long command are explained as follows. The character sequence 'machtype' is used to match the machine types, i.e., to identify the correct version of the Numerical Recipes in C library for the machine you are working on. If you simply type machtype at the athena prompt, it returns info on the machine type, for example:

athena%   machtype
sun4
athena% 

In this particular case the command machtype indicated that the computer was a Sun machine i.e. "sun4".

The remaining flags are as folows:

The compiler tries to be helpful by pointing out that we are not using most of the lines in the nrutil programs, the grep suppresses these messages.

To avoid typing in the whole long command, use gcnr in /mit/10.001/Examples.

Return to the Table of Contents.

References

Press, W.H., Teukolsky, S.A., Vetterling, W.T. and Flannerty, B.P. (1992), Numerical Recipes in C: The Art of Scientific Computing, (2nd Edition), Cambridge University Press, New York.

This book is a recommended text for 10.001. In addition to the C codes themselves the book contains a wealth of very practical information about how to use the algorithms and interpret the results. The book should be a part of your professional library if you are involved in numerical modeling of any sort.

Return to the Table of Contents.

Restrictions

Users shall at all times protect the programs and all related technical information, data and materials supplied by licensor from transfer to non-Athena users. Users have the right to modify and enhance the program, however, any embedded portion of the Numerical Recipes software is subject to these restrictions.

Return to the Table of Contents.


[Table of Contents | 10.001 Home Page | MIT Home Page ]

This web page was generated on November 1, 1997 by Gregory J. McRae who can be contacted via mcrae@mit.edu. Copyright © 1997, Massachusetts Institute of Technology. All rights reserved.