Numerical Recipes C Code Free Download
Buy Numerical Recipes with Source Code CD-ROM 3rd Edition: The Art of Scientific Computing on Amazon.com FREE SHIPPING on qualified orders. In the past, the Numerical Recipes book was considered the gold standard reference for numerical algorithms. The earliest Fortran Edition was followed by editions in C and C and others, bringing it then more up-to-date. Through these, it provided reference code for the state-of-the-art algorithms of the day.
|
|
Home|Submit Code|Top Code Search|Last Code Search|Privacy Policy|Link to Us|Contact |
Introduction to Numerical Recipes in C
R. Sureshkumar and Gregory J. McRae
Department of Chemical EngineeringMassachusetts Institute of Technology
Cambridge, MA 02139
Table of Contents
[ 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:
- Attach the NRC recipes locker to your account
- Change directory to the source files for NRC
- List the contents of the directory
- Display the C code for the Gauss Jordan program
(Please note that the characters 'athena%' signifies the Athena prompt.)
A partial listing of the routines from the command ls * is 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.cPress et al.more gaussj.c
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.
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:
In this particular case the command machtype indicated that the computer was a Sun machine i.e. 'sun4'.
The remaining flags are as folows:
- -D to define a macro (in this case the directive is to use ANSI C.
- -I where to look for include files in this case in the directory /mit/recipes/src/recipes_c-ansi/include.
- -L where to look for libraries.
- -l to link a library.
- $* to get all the command line arguements.
- |& pipes the warning messages through the grep command.
- grep -v nrutil finds warnings referring to nrutil and deletes them.
The compiler tries to be helpful by pointing out that we are not usingmost 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
Numerical Recipes In C++ Download
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.
Numerical Recipes Code Download
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.