1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | #include <stdio.h> |
9 | #include <stddef.h> |
10 | #include <stdlib.h> |
11 | #define NR_END1 1 |
12 | #define FREE_ARGchar* char* |
13 | |
14 | void nrerror(char error_text[]) |
15 | |
16 | { |
17 | fprintf(stderrstderr,"Numerical Recipes run-time error...\n"); |
18 | fprintf(stderrstderr,"%s\n",error_text); |
19 | fprintf(stderrstderr,"...now exiting to system...\n"); |
20 | exit(1); |
21 | } |
22 | |
23 | float *vector(long nl, long nh) |
24 | |
25 | { |
26 | float *v; |
27 | |
28 | v=(float *)malloc((size_t) ((nh-nl+1+NR_END1)*sizeof(float))); |
29 | if (!v) nrerror("allocation failure in vector()"); |
30 | return v-nl+NR_END1; |
31 | } |
32 | |
33 | int *ivector(long nl, long nh) |
34 | |
35 | { |
36 | int *v; |
37 | |
38 | v=(int *)malloc((size_t) ((nh-nl+1+NR_END1)*sizeof(int))); |
39 | if (!v) nrerror("allocation failure in ivector()"); |
40 | return v-nl+NR_END1; |
41 | } |
42 | |
43 | unsigned char *cvector(long nl, long nh) |
44 | |
45 | { |
46 | unsigned char *v; |
47 | |
48 | v=(unsigned char *)malloc((size_t) ((nh-nl+1+NR_END1)*sizeof(unsigned char))); |
49 | if (!v) nrerror("allocation failure in cvector()"); |
50 | return v-nl+NR_END1; |
51 | } |
52 | |
53 | unsigned long *lvector(long nl, long nh) |
54 | |
55 | { |
56 | unsigned long *v; |
57 | |
58 | v=(unsigned long *)malloc((size_t) ((nh-nl+1+NR_END1)*sizeof(long))); |
59 | if (!v) nrerror("allocation failure in lvector()"); |
60 | return v-nl+NR_END1; |
61 | } |
62 | |
63 | double *dvector(long nl, long nh) |
64 | |
65 | { |
66 | double *v; |
67 | |
68 | v=(double *)malloc((size_t) ((nh-nl+1+NR_END1)*sizeof(double))); |
69 | if (!v) nrerror("allocation failure in dvector()"); |
70 | return v-nl+NR_END1; |
71 | } |
72 | |
73 | float **matrix(long nrl, long nrh, long ncl, long nch) |
74 | |
75 | { |
76 | long i, nrow=nrh-nrl+1,ncol=nch-ncl+1; |
77 | float **m; |
78 | |
79 | |
80 | m=(float **) malloc((size_t)((nrow+NR_END1)*sizeof(float*))); |
81 | if (!m) nrerror("allocation failure 1 in matrix()"); |
82 | m += NR_END1; |
83 | m -= nrl; |
84 | |
85 | |
86 | m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END1)*sizeof(float))); |
87 | if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); |
88 | m[nrl] += NR_END1; |
89 | m[nrl] -= ncl; |
90 | |
91 | for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol; |
92 | |
93 | |
94 | return m; |
95 | } |
96 | |
97 | double **dmatrix(long nrl, long nrh, long ncl, long nch) |
98 | |
99 | { |
100 | long i, nrow=nrh-nrl+1,ncol=nch-ncl+1; |
101 | double **m; |
102 | |
103 | |
104 | m=(double **) malloc((size_t)((nrow+NR_END1)*sizeof(double*))); |
| |
105 | if (!m) nrerror("allocation failure 1 in matrix()"); |
| 2 | | Assuming 'm' is non-null | |
|
| |
106 | m += NR_END1; |
107 | m -= nrl; |
108 | |
109 | |
110 | m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END1)*sizeof(double))); |
| 4 | | Memory is never released; potential leak of memory pointed to by 'm' |
|
111 | if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); |
112 | m[nrl] += NR_END1; |
113 | m[nrl] -= ncl; |
114 | |
115 | for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol; |
116 | |
117 | |
118 | return m; |
119 | } |
120 | |
121 | int **imatrix(long nrl, long nrh, long ncl, long nch) |
122 | |
123 | { |
124 | long i, nrow=nrh-nrl+1,ncol=nch-ncl+1; |
125 | int **m; |
126 | |
127 | |
128 | m=(int **) malloc((size_t)((nrow+NR_END1)*sizeof(int*))); |
129 | if (!m) nrerror("allocation failure 1 in matrix()"); |
130 | m += NR_END1; |
131 | m -= nrl; |
132 | |
133 | |
134 | |
135 | m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END1)*sizeof(int))); |
136 | if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); |
137 | m[nrl] += NR_END1; |
138 | m[nrl] -= ncl; |
139 | |
140 | for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol; |
141 | |
142 | |
143 | return m; |
144 | } |
145 | |
146 | float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch, |
147 | long newrl, long newcl) |
148 | |
149 | { |
150 | long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl; |
151 | float **m; |
152 | |
153 | |
154 | m=(float **) malloc((size_t) ((nrow+NR_END1)*sizeof(float*))); |
155 | if (!m) nrerror("allocation failure in submatrix()"); |
156 | m += NR_END1; |
157 | m -= newrl; |
158 | |
159 | |
160 | for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol; |
161 | |
162 | |
163 | return m; |
164 | } |
165 | |
166 | float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch) |
167 | |
168 | |
169 | |
170 | |
171 | { |
172 | long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1; |
173 | float **m; |
174 | |
175 | |
176 | m=(float **) malloc((size_t) ((nrow+NR_END1)*sizeof(float*))); |
177 | if (!m) nrerror("allocation failure in convert_matrix()"); |
178 | m += NR_END1; |
179 | m -= nrl; |
180 | |
181 | |
182 | m[nrl]=a-ncl; |
183 | for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol; |
184 | |
185 | return m; |
186 | } |
187 | |
188 | float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh) |
189 | |
190 | { |
191 | long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1; |
192 | float ***t; |
193 | |
194 | |
195 | t=(float ***) malloc((size_t)((nrow+NR_END1)*sizeof(float**))); |
196 | if (!t) nrerror("allocation failure 1 in f3tensor()"); |
197 | t += NR_END1; |
198 | t -= nrl; |
199 | |
200 | |
201 | t[nrl]=(float **) malloc((size_t)((nrow*ncol+NR_END1)*sizeof(float*))); |
202 | if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()"); |
203 | t[nrl] += NR_END1; |
204 | t[nrl] -= ncl; |
205 | |
206 | |
207 | t[nrl][ncl]=(float *) malloc((size_t)((nrow*ncol*ndep+NR_END1)*sizeof(float))); |
208 | if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()"); |
209 | t[nrl][ncl] += NR_END1; |
210 | t[nrl][ncl] -= ndl; |
211 | |
212 | for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep; |
213 | for(i=nrl+1;i<=nrh;i++) { |
214 | t[i]=t[i-1]+ncol; |
215 | t[i][ncl]=t[i-1][ncl]+ncol*ndep; |
216 | for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep; |
217 | } |
218 | |
219 | |
220 | return t; |
221 | } |
222 | |
223 | void free_vector(float *v, long nl, long nh) |
224 | |
225 | { |
226 | free((FREE_ARGchar*) (v+nl-NR_END1)); |
227 | } |
228 | |
229 | void free_ivector(int *v, long nl, long nh) |
230 | |
231 | { |
232 | free((FREE_ARGchar*) (v+nl-NR_END1)); |
233 | } |
234 | |
235 | void free_cvector(unsigned char *v, long nl, long nh) |
236 | |
237 | { |
238 | free((FREE_ARGchar*) (v+nl-NR_END1)); |
239 | } |
240 | |
241 | void free_lvector(unsigned long *v, long nl, long nh) |
242 | |
243 | { |
244 | free((FREE_ARGchar*) (v+nl-NR_END1)); |
245 | } |
246 | |
247 | void free_dvector(double *v, long nl, long nh) |
248 | |
249 | { |
250 | free((FREE_ARGchar*) (v+nl-NR_END1)); |
251 | } |
252 | |
253 | void free_matrix(float **m, long nrl, long nrh, long ncl, long nch) |
254 | |
255 | { |
256 | free((FREE_ARGchar*) (m[nrl]+ncl-NR_END1)); |
257 | free((FREE_ARGchar*) (m+nrl-NR_END1)); |
258 | } |
259 | |
260 | void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch) |
261 | |
262 | { |
263 | free((FREE_ARGchar*) (m[nrl]+ncl-NR_END1)); |
264 | free((FREE_ARGchar*) (m+nrl-NR_END1)); |
265 | } |
266 | |
267 | void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch) |
268 | |
269 | { |
270 | free((FREE_ARGchar*) (m[nrl]+ncl-NR_END1)); |
271 | free((FREE_ARGchar*) (m+nrl-NR_END1)); |
272 | } |
273 | |
274 | void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch) |
275 | |
276 | { |
277 | free((FREE_ARGchar*) (b+nrl-NR_END1)); |
278 | } |
279 | |
280 | void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch) |
281 | |
282 | { |
283 | free((FREE_ARGchar*) (b+nrl-NR_END1)); |
284 | } |
285 | |
286 | void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch, |
287 | long ndl, long ndh) |
288 | |
289 | { |
290 | free((FREE_ARGchar*) (t[nrl][ncl]+ndl-NR_END1)); |
291 | free((FREE_ARGchar*) (t[nrl]+ncl-NR_END1)); |
292 | free((FREE_ARGchar*) (t+nrl-NR_END1)); |
293 | } |