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
8
9 #define NR_END 1
10 #define FREE_ARG char*
11
12 float sqrarg;
13 double dsqrarg;
14 double dmaxarg1, dmaxarg2;
15 double dminarg1, dminarg2;
16 float maxarg1, maxarg2;
17 float minarg1, minarg2;
18 long lmaxarg1, lmaxarg2;
19 long lminarg1, lminarg2;
20 int imaxarg1, imaxarg2;
21 int iminarg1, iminarg2;
22
23
24 /* allocate a float vector with subscript range v[nl..nh] */
vector(int nl,int nh)25 float *vector(int nl, int nh)
26 {
27 float *v;
28
29 v = (float *)G_malloc(((nh - nl + 1 + NR_END) * sizeof(float)));
30
31 return v - nl + NR_END;
32 }
33
34
35 /* allocate an int vector with subscript range v[nl..nh] */
ivector(int nl,int nh)36 int *ivector(int nl, int nh)
37 {
38 int *v;
39
40 v = (int *)G_malloc(((nh - nl + 1 + NR_END) * sizeof(int)));
41
42 return v - nl + NR_END;
43 }
44
45
46 /* allocate an unsigned char vector with subscript range v[nl..nh] */
cvector(int nl,int nh)47 unsigned char *cvector(int nl, int nh)
48 {
49 unsigned char *v;
50
51 v = (unsigned char *)
52 G_malloc(((nh - nl + 1 + NR_END) * sizeof(unsigned char)));
53
54 return v - nl + NR_END;
55 }
56
57
58 /* allocate an unsigned long vector with subscript range v[nl..nh] */
lvector(int nl,int nh)59 unsigned long *lvector(int nl, int nh)
60 {
61 unsigned long *v;
62
63 v = (unsigned long *)G_malloc(((nh - nl + 1 + NR_END) * sizeof(long)));
64
65 return v - nl + NR_END;
66 }
67
68
69 /* allocate a double vector with subscript range v[nl..nh] */
dvector(int nl,int nh)70 double *dvector(int nl, int nh)
71 {
72 double *v;
73
74 v = (double *)G_malloc(((nh - nl + 1 + NR_END) * sizeof(double)));
75
76 return v - nl + NR_END;
77 }
78
79
80 /* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
matrix(int nrl,int nrh,int ncl,int nch)81 float **matrix(int nrl, int nrh, int ncl, int nch)
82 {
83 int i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
84 float **m;
85
86 /* allocate pointers to rows */
87 m = (float **)G_malloc(((nrow + NR_END) * sizeof(float *)));
88 m += NR_END;
89 m -= nrl;
90
91 /* allocate rows and set pointers to them */
92 m[nrl] = (float *)G_malloc(((nrow * ncol + NR_END) * sizeof(float)));
93 m[nrl] += NR_END;
94 m[nrl] -= ncl;
95
96 for (i = nrl + 1; i <= nrh; i++)
97 m[i] = m[i - 1] + ncol;
98
99 /* return pointer to array of pointers to rows */
100 return m;
101 }
102
103
104 /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
dmatrix(int nrl,int nrh,int ncl,int nch)105 double **dmatrix(int nrl, int nrh, int ncl, int nch)
106 {
107 int i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
108 double **m;
109
110 /* allocate pointers to rows */
111 m = (double **)G_malloc(((nrow + NR_END) * sizeof(double *)));
112 m += NR_END;
113 m -= nrl;
114
115 /* allocate rows and set pointers to them */
116 m[nrl] = (double *)G_malloc(((nrow * ncol + NR_END) * sizeof(double)));
117 m[nrl] += NR_END;
118 m[nrl] -= ncl;
119
120 for (i = nrl + 1; i <= nrh; i++)
121 m[i] = m[i - 1] + ncol;
122
123 /* return pointer to array of pointers to rows */
124 return m;
125 }
126
127
128 /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
imatrix(int nrl,int nrh,int ncl,int nch)129 int **imatrix(int nrl, int nrh, int ncl, int nch)
130 {
131 int i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
132 int **m;
133
134 /* allocate pointers to rows */
135 m = (int **)G_malloc(((nrow + NR_END) * sizeof(int *)));
136 m += NR_END;
137 m -= nrl;
138
139
140 /* allocate rows and set pointers to them */
141 m[nrl] = (int *)G_malloc(((nrow * ncol + NR_END) * sizeof(int)));
142 m[nrl] += NR_END;
143 m[nrl] -= ncl;
144
145 for (i = nrl + 1; i <= nrh; i++)
146 m[i] = m[i - 1] + ncol;
147
148 /* return pointer to array of pointers to rows */
149 return m;
150 }
151
152
153 /* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
submatrix(float ** a,int oldrl,int oldrh,int oldcl,int oldch,int newrl,int newcl)154 float **submatrix(float **a, int oldrl, int oldrh, int oldcl, int oldch,
155 int newrl, int newcl)
156 {
157 int i, j, nrow = oldrh - oldrl + 1, ncol = oldcl - newcl;
158 float **m;
159
160 /* allocate array of pointers to rows */
161 m = (float **)G_malloc(((nrow + NR_END) * sizeof(float *)));
162 m += NR_END;
163 m -= newrl;
164
165 /* set pointers to rows */
166 for (i = oldrl, j = newrl; i <= oldrh; i++, j++)
167 m[j] = a[i] + ncol;
168
169 /* return pointer to array of pointers to rows */
170 return m;
171 }
172
173
174 /* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
175 declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
176 and ncol=nch-ncl+1. The routine should be called with the address
177 &a[0][0] as the first argument. */
convert_matrix(float * a,int nrl,int nrh,int ncl,int nch)178 float **convert_matrix(float *a, int nrl, int nrh, int ncl, int nch)
179 {
180 int i, j, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
181 float **m;
182
183 /* allocate pointers to rows */
184 m = (float **)G_malloc(((nrow + NR_END) * sizeof(float *)));
185 m += NR_END;
186 m -= nrl;
187
188 /* set pointers to rows */
189 m[nrl] = a - ncl;
190 for (i = 1, j = nrl + 1; i < nrow; i++, j++)
191 m[j] = m[j - 1] + ncol;
192
193 /* return pointer to array of pointers to rows */
194 return m;
195 }
196
197
198 /* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
f3tensor(int nrl,int nrh,int ncl,int nch,int ndl,int ndh)199 float ***f3tensor(int nrl, int nrh, int ncl, int nch, int ndl, int ndh)
200 {
201 int i, j, nrow = nrh - nrl + 1, ncol = nch - ncl + 1, ndep =
202 ndh - ndl + 1;
203 float ***t;
204
205 /* allocate pointers to pointers to rows */
206 t = (float ***)G_malloc(((nrow + NR_END) * sizeof(float **)));
207 t += NR_END;
208 t -= nrl;
209
210 /* allocate pointers to rows and set pointers to them */
211 t[nrl] = (float **)G_malloc(((nrow * ncol + NR_END) * sizeof(float *)));
212 t[nrl] += NR_END;
213 t[nrl] -= ncl;
214
215 /* allocate rows and set pointers to them */
216 t[nrl][ncl] =
217 (float *)G_malloc(((nrow * ncol * ndep + NR_END) * sizeof(float)));
218 t[nrl][ncl] += NR_END;
219 t[nrl][ncl] -= ndl;
220
221 for (j = ncl + 1; j <= nch; j++)
222 t[nrl][j] = t[nrl][j - 1] + ndep;
223 for (i = nrl + 1; i <= nrh; i++) {
224 t[i] = t[i - 1] + ncol;
225 t[i][ncl] = t[i - 1][ncl] + ncol * ndep;
226
227 for (j = ncl + 1; j <= nch; j++)
228 t[i][j] = t[i][j - 1] + ndep;
229 }
230
231 /* return pointer to array of pointers to rows */
232 return t;
233 }
234
235
236 /* free a float vector allocated with vector() */
free_vector(float * v,int nl,int nh)237 void free_vector(float *v, int nl, int nh)
238 {
239 G_free((FREE_ARG) (v + nl - NR_END));
240 }
241
242
243 /* free an int vector allocated with ivector() */
free_ivector(int * v,int nl,int nh)244 void free_ivector(int *v, int nl, int nh)
245 {
246 G_free((FREE_ARG) (v + nl - NR_END));
247 }
248
249
250 /* free an unsigned char vector allocated with cvector() */
free_cvector(unsigned char * v,int nl,int nh)251 void free_cvector(unsigned char *v, int nl, int nh)
252 {
253 G_free((FREE_ARG) (v + nl - NR_END));
254 }
255
256
257 /* free an unsigned long vector allocated with lvector() */
free_lvector(unsigned long * v,int nl,int nh)258 void free_lvector(unsigned long *v, int nl, int nh)
259 {
260 G_free((FREE_ARG) (v + nl - NR_END));
261 }
262
263
264 /* free a double vector allocated with dvector() */
free_dvector(double * v,int nl,int nh)265 void free_dvector(double *v, int nl, int nh)
266 {
267 G_free((FREE_ARG) (v + nl - NR_END));
268 }
269
270
271 /* free a float matrix allocated by matrix() */
free_matrix(float ** m,int nrl,int nrh,int ncl,int nch)272 void free_matrix(float **m, int nrl, int nrh, int ncl, int nch)
273 {
274 G_free((FREE_ARG) (m[nrl] + ncl - NR_END));
275 G_free((FREE_ARG) (m + nrl - NR_END));
276 }
277
278
279 /* free a double matrix allocated by dmatrix() */
free_dmatrix(double ** m,int nrl,int nrh,int ncl,int nch)280 void free_dmatrix(double **m, int nrl, int nrh, int ncl, int nch)
281 {
282 G_free((FREE_ARG) (m[nrl] + ncl - NR_END));
283 G_free((FREE_ARG) (m + nrl - NR_END));
284 }
285
286
287 /* free an int matrix allocated by imatrix() */
free_imatrix(int ** m,int nrl,int nrh,int ncl,int nch)288 void free_imatrix(int **m, int nrl, int nrh, int ncl, int nch)
289 {
290 G_free((FREE_ARG) (m[nrl] + ncl - NR_END));
291 G_free((FREE_ARG) (m + nrl - NR_END));
292 }
293
294
295 /* free a submatrix allocated by submatrix() */
free_submatrix(float ** b,int nrl,int nrh,int ncl,int nch)296 void free_submatrix(float **b, int nrl, int nrh, int ncl, int nch)
297 {
298 G_free((FREE_ARG) (b + nrl - NR_END));
299 }
300
301
302 /* free a matrix allocated by convert_matrix() */
free_convert_matrix(float ** b,int nrl,int nrh,int ncl,int nch)303 void free_convert_matrix(float **b, int nrl, int nrh, int ncl, int nch)
304 {
305 G_free((FREE_ARG) (b + nrl - NR_END));
306 }
307
308
309 /* free a float f3tensor allocated by f3tensor() */
free_f3tensor(float *** t,int nrl,int nrh,int ncl,int nch,int ndl,int ndh)310 void free_f3tensor(float ***t, int nrl, int nrh, int ncl, int nch,
311 int ndl, int ndh)
312 {
313 G_free((FREE_ARG) (t[nrl][ncl] + ndl - NR_END));
314 G_free((FREE_ARG) (t[nrl] + ncl - NR_END));
315 G_free((FREE_ARG) (t + nrl - NR_END));
316 }
317