nrutil.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #include <stdio.h>
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4. #include <grass/gis.h>
  5. #include <grass/glocale.h>
  6. #include "nrutil.h"
  7. #define NR_END 1
  8. #define FREE_ARG char*
  9. float sqrarg;
  10. double dsqrarg;
  11. double dmaxarg1, dmaxarg2;
  12. double dminarg1, dminarg2;
  13. float maxarg1, maxarg2;
  14. float minarg1, minarg2;
  15. long lmaxarg1, lmaxarg2;
  16. long lminarg1, lminarg2;
  17. int imaxarg1, imaxarg2;
  18. int iminarg1, iminarg2;
  19. /* allocate a float vector with subscript range v[nl..nh] */
  20. float *vector(int nl, int nh)
  21. {
  22. float *v;
  23. v = (float *)G_malloc(((nh - nl + 1 + NR_END) * sizeof(float)));
  24. return v - nl + NR_END;
  25. }
  26. /* allocate an int vector with subscript range v[nl..nh] */
  27. int *ivector(int nl, int nh)
  28. {
  29. int *v;
  30. v = (int *)G_malloc(((nh - nl + 1 + NR_END) * sizeof(int)));
  31. return v - nl + NR_END;
  32. }
  33. /* allocate an unsigned char vector with subscript range v[nl..nh] */
  34. unsigned char *cvector(int nl, int nh)
  35. {
  36. unsigned char *v;
  37. v = (unsigned char *)
  38. G_malloc(((nh - nl + 1 + NR_END) * sizeof(unsigned char)));
  39. return v - nl + NR_END;
  40. }
  41. /* allocate an unsigned long vector with subscript range v[nl..nh] */
  42. unsigned long *lvector(int nl, int nh)
  43. {
  44. unsigned long *v;
  45. v = (unsigned long *)G_malloc(((nh - nl + 1 + NR_END) * sizeof(long)));
  46. return v - nl + NR_END;
  47. }
  48. /* allocate a double vector with subscript range v[nl..nh] */
  49. double *dvector(int nl, int nh)
  50. {
  51. double *v;
  52. v = (double *)G_malloc(((nh - nl + 1 + NR_END) * sizeof(double)));
  53. return v - nl + NR_END;
  54. }
  55. /* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
  56. float **matrix(int nrl, int nrh, int ncl, int nch)
  57. {
  58. int i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
  59. float **m;
  60. /* allocate pointers to rows */
  61. m = (float **)G_malloc(((nrow + NR_END) * sizeof(float *)));
  62. m += NR_END;
  63. m -= nrl;
  64. /* allocate rows and set pointers to them */
  65. m[nrl] = (float *)G_malloc(((nrow * ncol + NR_END) * sizeof(float)));
  66. m[nrl] += NR_END;
  67. m[nrl] -= ncl;
  68. for (i = nrl + 1; i <= nrh; i++)
  69. m[i] = m[i - 1] + ncol;
  70. /* return pointer to array of pointers to rows */
  71. return m;
  72. }
  73. /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
  74. double **dmatrix(int nrl, int nrh, int ncl, int nch)
  75. {
  76. int i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
  77. double **m;
  78. /* allocate pointers to rows */
  79. m = (double **)G_malloc(((nrow + NR_END) * sizeof(double *)));
  80. m += NR_END;
  81. m -= nrl;
  82. /* allocate rows and set pointers to them */
  83. m[nrl] = (double *)G_malloc(((nrow * ncol + NR_END) * sizeof(double)));
  84. m[nrl] += NR_END;
  85. m[nrl] -= ncl;
  86. for (i = nrl + 1; i <= nrh; i++)
  87. m[i] = m[i - 1] + ncol;
  88. /* return pointer to array of pointers to rows */
  89. return m;
  90. }
  91. /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
  92. int **imatrix(int nrl, int nrh, int ncl, int nch)
  93. {
  94. int i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
  95. int **m;
  96. /* allocate pointers to rows */
  97. m = (int **)G_malloc(((nrow + NR_END) * sizeof(int *)));
  98. m += NR_END;
  99. m -= nrl;
  100. /* allocate rows and set pointers to them */
  101. m[nrl] = (int *)G_malloc(((nrow * ncol + NR_END) * sizeof(int)));
  102. m[nrl] += NR_END;
  103. m[nrl] -= ncl;
  104. for (i = nrl + 1; i <= nrh; i++)
  105. m[i] = m[i - 1] + ncol;
  106. /* return pointer to array of pointers to rows */
  107. return m;
  108. }
  109. /* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
  110. float **submatrix(float **a, int oldrl, int oldrh, int oldcl, int oldch,
  111. int newrl, int newcl)
  112. {
  113. int i, j, nrow = oldrh - oldrl + 1, ncol = oldcl - newcl;
  114. float **m;
  115. /* allocate array of pointers to rows */
  116. m = (float **)G_malloc(((nrow + NR_END) * sizeof(float *)));
  117. m += NR_END;
  118. m -= newrl;
  119. /* set pointers to rows */
  120. for (i = oldrl, j = newrl; i <= oldrh; i++, j++)
  121. m[j] = a[i] + ncol;
  122. /* return pointer to array of pointers to rows */
  123. return m;
  124. }
  125. /* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
  126. declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
  127. and ncol=nch-ncl+1. The routine should be called with the address
  128. &a[0][0] as the first argument. */
  129. float **convert_matrix(float *a, int nrl, int nrh, int ncl, int nch)
  130. {
  131. int i, j, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
  132. float **m;
  133. /* allocate pointers to rows */
  134. m = (float **)G_malloc(((nrow + NR_END) * sizeof(float *)));
  135. m += NR_END;
  136. m -= nrl;
  137. /* set pointers to rows */
  138. m[nrl] = a - ncl;
  139. for (i = 1, j = nrl + 1; i < nrow; i++, j++)
  140. m[j] = m[j - 1] + ncol;
  141. /* return pointer to array of pointers to rows */
  142. return m;
  143. }
  144. /* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
  145. float ***f3tensor(int nrl, int nrh, int ncl, int nch, int ndl, int ndh)
  146. {
  147. int i, j, nrow = nrh - nrl + 1, ncol = nch - ncl + 1, ndep =
  148. ndh - ndl + 1;
  149. float ***t;
  150. /* allocate pointers to pointers to rows */
  151. t = (float ***)G_malloc(((nrow + NR_END) * sizeof(float **)));
  152. t += NR_END;
  153. t -= nrl;
  154. /* allocate pointers to rows and set pointers to them */
  155. t[nrl] = (float **)G_malloc(((nrow * ncol + NR_END) * sizeof(float *)));
  156. t[nrl] += NR_END;
  157. t[nrl] -= ncl;
  158. /* allocate rows and set pointers to them */
  159. t[nrl][ncl] =
  160. (float *)G_malloc(((nrow * ncol * ndep + NR_END) * sizeof(float)));
  161. t[nrl][ncl] += NR_END;
  162. t[nrl][ncl] -= ndl;
  163. for (j = ncl + 1; j <= nch; j++)
  164. t[nrl][j] = t[nrl][j - 1] + ndep;
  165. for (i = nrl + 1; i <= nrh; i++) {
  166. t[i] = t[i - 1] + ncol;
  167. t[i][ncl] = t[i - 1][ncl] + ncol * ndep;
  168. for (j = ncl + 1; j <= nch; j++)
  169. t[i][j] = t[i][j - 1] + ndep;
  170. }
  171. /* return pointer to array of pointers to rows */
  172. return t;
  173. }
  174. /* free a float vector allocated with vector() */
  175. void free_vector(float *v, int nl, int nh)
  176. {
  177. G_free((FREE_ARG) (v + nl - NR_END));
  178. }
  179. /* free an int vector allocated with ivector() */
  180. void free_ivector(int *v, int nl, int nh)
  181. {
  182. G_free((FREE_ARG) (v + nl - NR_END));
  183. }
  184. /* free an unsigned char vector allocated with cvector() */
  185. void free_cvector(unsigned char *v, int nl, int nh)
  186. {
  187. G_free((FREE_ARG) (v + nl - NR_END));
  188. }
  189. /* free an unsigned long vector allocated with lvector() */
  190. void free_lvector(unsigned long *v, int nl, int nh)
  191. {
  192. G_free((FREE_ARG) (v + nl - NR_END));
  193. }
  194. /* free a double vector allocated with dvector() */
  195. void free_dvector(double *v, int nl, int nh)
  196. {
  197. G_free((FREE_ARG) (v + nl - NR_END));
  198. }
  199. /* free a float matrix allocated by matrix() */
  200. void free_matrix(float **m, int nrl, int nrh, int ncl, int nch)
  201. {
  202. G_free((FREE_ARG) (m[nrl] + ncl - NR_END));
  203. G_free((FREE_ARG) (m + nrl - NR_END));
  204. }
  205. /* free a double matrix allocated by dmatrix() */
  206. void free_dmatrix(double **m, int nrl, int nrh, int ncl, int nch)
  207. {
  208. G_free((FREE_ARG) (m[nrl] + ncl - NR_END));
  209. G_free((FREE_ARG) (m + nrl - NR_END));
  210. }
  211. /* free an int matrix allocated by imatrix() */
  212. void free_imatrix(int **m, int nrl, int nrh, int ncl, int nch)
  213. {
  214. G_free((FREE_ARG) (m[nrl] + ncl - NR_END));
  215. G_free((FREE_ARG) (m + nrl - NR_END));
  216. }
  217. /* free a submatrix allocated by submatrix() */
  218. void free_submatrix(float **b, int nrl, int nrh, int ncl, int nch)
  219. {
  220. G_free((FREE_ARG) (b + nrl - NR_END));
  221. }
  222. /* free a matrix allocated by convert_matrix() */
  223. void free_convert_matrix(float **b, int nrl, int nrh, int ncl, int nch)
  224. {
  225. G_free((FREE_ARG) (b + nrl - NR_END));
  226. }
  227. /* free a float f3tensor allocated by f3tensor() */
  228. void free_f3tensor(float ***t, int nrl, int nrh, int ncl, int nch,
  229. int ndl, int ndh)
  230. {
  231. G_free((FREE_ARG) (t[nrl][ncl] + ndl - NR_END));
  232. G_free((FREE_ARG) (t[nrl] + ncl - NR_END));
  233. G_free((FREE_ARG) (t + nrl - NR_END));
  234. }