comparison toolboxes/FullBNT-1.0.7/bnt/inference/static/@quickscore_inf_engine/private/nrutil.c @ 0:e9a9cd732c1e tip

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