正在加载图片...
1.2 Some C Conventions for Scientific Computing 19 But suppose that your vector of length 7,now call it a,is perversely a native C, zero-offset array (has range a [0..6]).Perhaps this is the case because you disagree with our aesthetic prejudices,Heaven help you!To use our recipe,do you have to copy a's contents element by element into another,unit-offset vector?No!Do you have to declare a new pointer aaa and set it equal to a-1?No!You simply invoke someroutine(a-1,7);.Then a[1],as seen from within our recipe,is actually a[o]as seen from your program.In other words,you can change conventions"on the fly"with just a couple of keystrokes. Forgive us for belaboring these points.We want to free you from the zero-offset thinking that C encourages but(as we see)does not require.A final liberating point is that the utility file nrutil.c,listed in full in Appendix B,includes functions for allocating (using malloc())arbitrary-offset vectors of arbitrary lengths.The nted for synopses of these functions are as follows: float *vector(long nl,long nh) Allocates a float vector with range [nl..nh] to any int *ivector(long nl,long nh) Allocates an int vector with range [nl..nh]. unsigned char *cvector(long nl,long nh) Allocates an unsigned char vector with range [nl..nh]. (North America server computer, to make one paper UnN电.t THE unsigned long *lvector(long nl,long nh) ART Allocates an unsigned long vector with range [nl..nh]. Programs double *dvector(long nl,long nh) Allocates a double vector with range [nl..nh]. strictly proh A typical use of the above utilities is the declaration float *b;followed by b=vector(1,7);,which makes the range b[1..7]come into existence and allows b to be passed to any function calling for a unit-offset vector. The file nrutil.c also contains the corresponding deallocation routines, 1988-1992 by Numerical Recipes OF SCIENTIFIC COMPUTING(ISBN void free_vector(float *v,long nl,long nh) void free_ivector(int *v,long nl,long nh) 10-621 void free_cvector(unsigned char *v,long nl,long nh) 43108 void free_lvector(unsigned long *v,long nl,long nh) void free_dvector(double *v,long nl,long nh) (outside North Software. with the typical use being free_vector(b,1,7);. Our recipes use the above utilities extensively for the allocation and deallocation Ame of vector workspace.We also commend them to you for use in your main programs or other procedures.Note that if you want to allocate vectors of length longer than 64k on an IBM PC-compatible computer,you should replace all occurrences of malloc in nrutil.c by your compiler's special-purpose memory allocation function.This applies also to matrix allocation,to be discussed next.1.2 Some C Conventions for Scientific Computing 19 Permission 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). But suppose that your vector of length 7, now call it a, is perversely a native C, zero-offset array (has range a[0..6]). Perhaps this is the case because you disagree with our aesthetic prejudices, Heaven help you! To use our recipe, do you have to copy a’s contents element by element into another, unit-offset vector? No! Do you have to declare a new pointer aaa and set it equal to a-1? No! You simply invoke someroutine(a-1,7);. Then a[1], as seen from within our recipe, is actually a[0] as seen from your program. In other words, you can change conventions “on the fly” with just a couple of keystrokes. Forgive us for belaboring these points. We want to free you from the zero-offset thinking that C encourages but (as we see) does not require. A final liberating point is that the utility file nrutil.c, listed in full in Appendix B, includes functions for allocating (using malloc()) arbitrary-offset vectors of arbitrary lengths. The synopses of these functions are as follows: float *vector(long nl, long nh) Allocates a float vector with range [nl..nh]. int *ivector(long nl, long nh) Allocates an int vector with range [nl..nh]. unsigned char *cvector(long nl, long nh) Allocates an unsigned char vector with range [nl..nh]. unsigned long *lvector(long nl, long nh) Allocates an unsigned long vector with range [nl..nh]. double *dvector(long nl, long nh) Allocates a double vector with range [nl..nh]. A typical use of the above utilities is the declaration float *b; followed by b=vector(1,7);, which makes the range b[1..7] come into existence and allows b to be passed to any function calling for a unit-offset vector. The file nrutil.c also contains the corresponding deallocation routines, void free_vector(float *v, long nl, long nh) void free_ivector(int *v, long nl, long nh) void free_cvector(unsigned char *v, long nl, long nh) void free_lvector(unsigned long *v, long nl, long nh) void free_dvector(double *v, long nl, long nh) with the typical use being free_vector(b,1,7);. Our recipes use the above utilities extensively for the allocation and deallocation of vector workspace. We also commend them to you for use in your main programs or other procedures. Note that if you want to allocate vectors of length longer than 64k on an IBM PC-compatible computer, you should replace all occurrences of malloc in nrutil.c by your compiler’s special-purpose memory allocation function. This applies also to matrix allocation, to be discussed next
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有