nrutil.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* CAUTION: This is the ANSI C (only) version of the Numerical Recipes
  2. utility file nrutil.c. Do not confuse this file with the same-named
  3. file nrutil.c that is supplied in the 'misc' subdirectory.
  4. *That* file is the one from the book, and contains both ANSI and
  5. traditional K&R versions, along with #ifdef macros to select the
  6. correct version. *This* file contains only ANSI C. */
  7. #include <stdio.h>
  8. #include <stddef.h>
  9. #include <stdlib.h>
  10. #define NR_END 1
  11. #define FREE_ARG char*
  12. #define REAL float
  13. void nrerror(char error_text[])
  14. /* Numerical Recipes standard error handler */
  15. {
  16. fprintf(stderr,"Numerical Recipes run-time error...\n");
  17. fprintf(stderr,"%s\n",error_text);
  18. fprintf(stderr,"...now exiting to system...\n");
  19. exit(1);
  20. }
  21. REAL *vector(long nl, long nh)
  22. /* allocate a REAL vector with subscript range v[nl..nh] */
  23. {
  24. REAL *v;
  25. v=(REAL *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(REAL)));
  26. if (!v) nrerror("allocation failure in vector()");
  27. return v-nl+NR_END;
  28. }
  29. int *ivector(long nl, long nh)
  30. /* allocate an int vector with subscript range v[nl..nh] */
  31. {
  32. int *v;
  33. v=(int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int)));
  34. if (!v) nrerror("allocation failure in ivector()");
  35. return v-nl+NR_END;
  36. }
  37. unsigned char *cvector(long nl, long nh)
  38. /* allocate an unsigned char vector with subscript range v[nl..nh] */
  39. {
  40. unsigned char *v;
  41. v=(unsigned char *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(unsigned char)));
  42. if (!v) nrerror("allocation failure in cvector()");
  43. return v-nl+NR_END;
  44. }
  45. unsigned long *lvector(long nl, long nh)
  46. /* allocate an unsigned long vector with subscript range v[nl..nh] */
  47. {
  48. unsigned long *v;
  49. v=(unsigned long *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(long)));
  50. if (!v) nrerror("allocation failure in lvector()");
  51. return v-nl+NR_END;
  52. }
  53. double *dvector(long nl, long nh)
  54. /* allocate a double vector with subscript range v[nl..nh] */
  55. {
  56. double *v;
  57. v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
  58. if (!v) nrerror("allocation failure in dvector()");
  59. return v-nl+NR_END;
  60. }
  61. REAL **matrix(long nrl, long nrh, long ncl, long nch)
  62. /* allocate a REAL matrix with subscript range m[nrl..nrh][ncl..nch] */
  63. {
  64. long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
  65. REAL **m;
  66. /* allocate pointers to rows */
  67. m=(REAL **) malloc((size_t)((nrow+NR_END)*sizeof(REAL*)));
  68. if (!m) nrerror("allocation failure 1 in matrix()");
  69. m += NR_END;
  70. m -= nrl;
  71. /* allocate rows and set pointers to them */
  72. m[nrl]=(REAL *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(REAL)));
  73. if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
  74. m[nrl] += NR_END;
  75. m[nrl] -= ncl;
  76. for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
  77. /* return pointer to array of pointers to rows */
  78. return m;
  79. }
  80. double **dmatrix(long nrl, long nrh, long ncl, long nch)
  81. /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
  82. {
  83. long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
  84. double **m;
  85. /* allocate pointers to rows */
  86. m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
  87. if (!m) nrerror("allocation failure 1 in matrix()");
  88. m += NR_END;
  89. m -= nrl;
  90. /* allocate rows and set pointers to them */
  91. m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
  92. if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
  93. m[nrl] += NR_END;
  94. m[nrl] -= ncl;
  95. for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
  96. /* return pointer to array of pointers to rows */
  97. return m;
  98. }
  99. int **imatrix(long nrl, long nrh, long ncl, long nch)
  100. /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
  101. {
  102. long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
  103. int **m;
  104. /* allocate pointers to rows */
  105. m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
  106. if (!m) nrerror("allocation failure 1 in matrix()");
  107. m += NR_END;
  108. m -= nrl;
  109. /* allocate rows and set pointers to them */
  110. m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
  111. if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
  112. m[nrl] += NR_END;
  113. m[nrl] -= ncl;
  114. for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
  115. /* return pointer to array of pointers to rows */
  116. return m;
  117. }
  118. REAL **submatrix(REAL **a, long oldrl, long oldrh, long oldcl, long oldch,
  119. long newrl, long newcl)
  120. /* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
  121. {
  122. long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
  123. REAL **m;
  124. /* allocate array of pointers to rows */
  125. m=(REAL **) malloc((size_t) ((nrow+NR_END)*sizeof(REAL*)));
  126. if (!m) nrerror("allocation failure in submatrix()");
  127. m += NR_END;
  128. m -= newrl;
  129. /* set pointers to rows */
  130. for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
  131. /* return pointer to array of pointers to rows */
  132. return m;
  133. }
  134. REAL **convert_matrix(REAL *a, long nrl, long nrh, long ncl, long nch)
  135. /* allocate a REAL matrix m[nrl..nrh][ncl..nch] that points to the matrix
  136. declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
  137. and ncol=nch-ncl+1. The routine should be called with the address
  138. &a[0][0] as the first argument. */
  139. {
  140. long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
  141. REAL **m;
  142. /* allocate pointers to rows */
  143. m=(REAL **) malloc((size_t) ((nrow+NR_END)*sizeof(REAL*)));
  144. if (!m) nrerror("allocation failure in convert_matrix()");
  145. m += NR_END;
  146. m -= nrl;
  147. /* set pointers to rows */
  148. m[nrl]=a-ncl;
  149. for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
  150. /* return pointer to array of pointers to rows */
  151. return m;
  152. }
  153. REAL ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
  154. /* allocate a REAL 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
  155. {
  156. long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
  157. REAL ***t;
  158. /* allocate pointers to pointers to rows */
  159. t=(REAL ***) malloc((size_t)((nrow+NR_END)*sizeof(REAL**)));
  160. if (!t) nrerror("allocation failure 1 in f3tensor()");
  161. t += NR_END;
  162. t -= nrl;
  163. /* allocate pointers to rows and set pointers to them */
  164. t[nrl]=(REAL **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(REAL*)));
  165. if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
  166. t[nrl] += NR_END;
  167. t[nrl] -= ncl;
  168. /* allocate rows and set pointers to them */
  169. t[nrl][ncl]=(REAL *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(REAL)));
  170. if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
  171. t[nrl][ncl] += NR_END;
  172. t[nrl][ncl] -= ndl;
  173. for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
  174. for(i=nrl+1;i<=nrh;i++) {
  175. t[i]=t[i-1]+ncol;
  176. t[i][ncl]=t[i-1][ncl]+ncol*ndep;
  177. for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
  178. }
  179. /* return pointer to array of pointers to rows */
  180. return t;
  181. }
  182. void free_vector(REAL *v, long nl, long nh)
  183. /* free a REAL vector allocated with vector() */
  184. {
  185. free((FREE_ARG) (v+nl-NR_END));
  186. }
  187. void free_ivector(int *v, long nl, long nh)
  188. /* free an int vector allocated with ivector() */
  189. {
  190. free((FREE_ARG) (v+nl-NR_END));
  191. }
  192. void free_cvector(unsigned char *v, long nl, long nh)
  193. /* free an unsigned char vector allocated with cvector() */
  194. {
  195. free((FREE_ARG) (v+nl-NR_END));
  196. }
  197. void free_lvector(unsigned long *v, long nl, long nh)
  198. /* free an unsigned long vector allocated with lvector() */
  199. {
  200. free((FREE_ARG) (v+nl-NR_END));
  201. }
  202. void free_dvector(double *v, long nl, long nh)
  203. /* free a double vector allocated with dvector() */
  204. {
  205. free((FREE_ARG) (v+nl-NR_END));
  206. }
  207. void free_matrix(REAL **m, long nrl, long nrh, long ncl, long nch)
  208. /* free a REAL matrix allocated by matrix() */
  209. {
  210. free((FREE_ARG) (m[nrl]+ncl-NR_END));
  211. free((FREE_ARG) (m+nrl-NR_END));
  212. }
  213. void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
  214. /* free a double matrix allocated by dmatrix() */
  215. {
  216. free((FREE_ARG) (m[nrl]+ncl-NR_END));
  217. free((FREE_ARG) (m+nrl-NR_END));
  218. }
  219. void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
  220. /* free an int matrix allocated by imatrix() */
  221. {
  222. free((FREE_ARG) (m[nrl]+ncl-NR_END));
  223. free((FREE_ARG) (m+nrl-NR_END));
  224. }
  225. void free_submatrix(REAL **b, long nrl, long nrh, long ncl, long nch)
  226. /* free a submatrix allocated by submatrix() */
  227. {
  228. free((FREE_ARG) (b+nrl-NR_END));
  229. }
  230. void free_convert_matrix(REAL **b, long nrl, long nrh, long ncl, long nch)
  231. /* free a matrix allocated by convert_matrix() */
  232. {
  233. free((FREE_ARG) (b+nrl-NR_END));
  234. }
  235. void free_f3tensor(REAL ***t, long nrl, long nrh, long ncl, long nch,
  236. long ndl, long ndh)
  237. /* free a REAL f3tensor allocated by f3tensor() */
  238. {
  239. free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
  240. free((FREE_ARG) (t[nrl]+ncl-NR_END));
  241. free((FREE_ARG) (t+nrl-NR_END));
  242. }