正在加载图片...
Appendix B: Utility Routines Non-Copyright Notice:This Appendix and its utility routines are herewith g placed into the public domain.Anyone may copy them freely for any purpose. We of course accept no liability whatsoever for any such use. BpL one) NUMERICAL The routines listed below are used by many of the Recipes in this book.The first routine,nrerror,is invoked to terminate program execution-with an appropriate message-when a fatal error is encountered.The other routines are used to allocate 、发2i。> and deallocate memory for vectors and matrices,as explained in detail in 81.2.All memory allocation and deallocation in Numerical Recipes in C is done using these C:THEA routines as intermediaries.Therefore,if you want to allocate memory in some Press. different way,you need change only these routines,not the programs themselves. On the diskette.these routines are in the file nrutil.c.Also listed here is the header file nrutil.h.Besides the declarations for the routines in nrutil.h,it contains several macros that are used throughout the book.The ideas behind some of these macros were described in 81.2 小 6 Memory Allocation:Advanced Topics Two issues regarding vector and matrix memory allocation are worth discussing here: first,a "back-door"compatibility between our matrices and those used by other C software; 92y second,the question of whether our pointer arithmetic for unit-offset vectors and matrices violates the ANSI C standard. 1.Back-door compatibility.As explained in $1.2,we always allocate matrices via the Recipes scheme "pointer to an array of pointers to rows"(shown in Figure 1.2.1).With this scheme, once storage for a matrix is allocated,the top level pointer(the "name"of the matrix)can be used to store a matrix of any size whose dimensions are less than or equal to the size of 99 the dimensions allocated.The only time that the allocated size ever again becomes relevant is when the matrix storage is finally deallocated. In this scheme,the rows of a matrix need not be stored in physically contiguous storage; Software. each row has its own pointer addressing it(see Figure 1.2.1).However,the allocation routines printed below in fact (and by design)allocate an entire matrix as one contiguous block,via a single call to malloc().This "back-door"fact allows you to use matrices created with America). our routines directly with other software:The address of the first element in a matrix **a (usually &a[1][1]if a has been allocated with a statement like a=matrix(1,m,1,n);but possibly &a [o][o]if a was created with zero-offsets)is guaranteed to point to the beginning of a contiguous block containing the full physical matrix,stored by rows.This address can be used as an argument to other software.(Note that this other software will generally also need to know the physical size of the matrix,or at least the number of columns. 2.Unit-offser vectors.In $1.2,we described how a unit-offset vector bb[1..4]could be obtained from a zero-offset array b[0.3]by the pointer arithmetic operation bb=b-1.Since bb points to one location before b,bb[1]addresses the same element as b [o],and so on. 940Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machine￾readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). Appendix B: Utility Routines Non-Copyright Notice: This Appendix and its utility routines are herewith placed into the public domain. Anyone may copy them freely for any purpose. We of course accept no liability whatsoever for any such use. The routines listed below are used by many of the Recipes in this book. The first routine, nrerror, is invoked to terminate program execution — with an appropriate message — when a fatal error is encountered. The other routines are used to allocate and deallocate memory for vectors and matrices, as explained in detail in §1.2. All memory allocation and deallocation in Numerical Recipes in C is done using these routines as intermediaries. Therefore, if you want to allocate memory in some different way, you need change only these routines, not the programs themselves. On the diskette, these routines are in the file nrutil.c. Also listed here is the header file nrutil.h. Besides the declarations for the routines in nrutil.h, it contains several macros that are used throughout the book. The ideas behind some of these macros were described in §1.2. Memory Allocation: Advanced Topics Two issues regarding vector and matrix memory allocation are worth discussing here: first, a “back-door” compatibility between our matrices and those used by other C software; second, the question of whether our pointer arithmetic for unit-offset vectors and matrices violates the ANSI C standard. 1. Back-door compatibility. As explained in §1.2, we always allocate matrices via the scheme “pointer to an array of pointers to rows” (shown in Figure 1.2.1). With this scheme, once storage for a matrix is allocated, the top level pointer (the “name” of the matrix) can be used to store a matrix of any size whose dimensions are less than or equal to the size of the dimensions allocated. The only time that the allocated size ever again becomes relevant is when the matrix storage is finally deallocated. In this scheme, the rows of a matrix need not be stored in physically contiguous storage; each row has its own pointer addressing it (see Figure 1.2.1). However, the allocation routines printed below in fact (and by design) allocate an entire matrix as one contiguous block, via a single call to malloc(). This “back-door” fact allows you to use matrices created with our routines directly with other software: The address of the first element in a matrix **a (usually &a[1][1] if a has been allocated with a statement like a=matrix(1,m,1,n); but possibly &a[0][0] if a was created with zero-offsets) is guaranteed to point to the beginning of a contiguous block containing the full physical matrix, stored by rows. This address can be used as an argument to other software. (Note that this other software will generally also need to know the physical size of the matrix, or at least the number of columns.) 2. Unit-offset vectors. In §1.2, we described how a unit-offset vector bb[1..4] could be obtained from a zero-offset array b[0..3] by the pointer arithmetic operation bb=b-1. Since bb points to one location before b, bb[1] addresses the same element as b[0], and so on. 940
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有