1 | // Dominique YVON, CEA/DAPNIA/SPP 02/2000
|
---|
2 |
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <stddef.h>
|
---|
5 | #include <stdlib.h>
|
---|
6 |
|
---|
7 | #include "numrecipes.h"
|
---|
8 |
|
---|
9 | // En tete du fichier nrutil.h
|
---|
10 |
|
---|
11 | static float sqrarg;
|
---|
12 | #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 : sqrarg*sqrarg)
|
---|
13 |
|
---|
14 | static double dsqrarg;
|
---|
15 | #define DSQR(a) ((dsqrarg=(a)) == 0.0 ? 0.0 : dsqrarg*dsqrarg)
|
---|
16 |
|
---|
17 | static double dmaxarg1,dmaxarg2;
|
---|
18 | #define DMAX(a,b) (dmaxarg1=(a),dmaxarg2=(b),(dmaxarg1) > (dmaxarg2) ?\
|
---|
19 | (dmaxarg1) : (dmaxarg2))
|
---|
20 |
|
---|
21 | static double dminarg1,dminarg2;
|
---|
22 | #define DMIN(a,b) (dminarg1=(a),dminarg2=(b),(dminarg1) < (dminarg2) ?\
|
---|
23 | (dminarg1) : (dminarg2))
|
---|
24 |
|
---|
25 | static float maxarg1,maxarg2;
|
---|
26 | #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1) > (maxarg2) ?\
|
---|
27 | (maxarg1) : (maxarg2))
|
---|
28 |
|
---|
29 | static float minarg1,minarg2;
|
---|
30 | #define FMIN(a,b) (minarg1=(a),minarg2=(b),(minarg1) < (minarg2) ?\
|
---|
31 | (minarg1) : (minarg2))
|
---|
32 |
|
---|
33 | static long lmaxarg1,lmaxarg2;
|
---|
34 | #define LMAX(a,b) (lmaxarg1=(a),lmaxarg2=(b),(lmaxarg1) > (lmaxarg2) ?\
|
---|
35 | (lmaxarg1) : (lmaxarg2))
|
---|
36 |
|
---|
37 | static long lminarg1,lminarg2;
|
---|
38 | #define LMIN(a,b) (lminarg1=(a),lminarg2=(b),(lminarg1) < (lminarg2) ?\
|
---|
39 | (lminarg1) : (lminarg2))
|
---|
40 |
|
---|
41 | static int imaxarg1,imaxarg2;
|
---|
42 | #define IMAX(a,b) (imaxarg1=(a),imaxarg2=(b),(imaxarg1) > (imaxarg2) ?\
|
---|
43 | (imaxarg1) : (imaxarg2))
|
---|
44 |
|
---|
45 | static int iminarg1,iminarg2;
|
---|
46 | #define IMIN(a,b) (iminarg1=(a),iminarg2=(b),(iminarg1) < (iminarg2) ?\
|
---|
47 | (iminarg1) : (iminarg2))
|
---|
48 |
|
---|
49 | #define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
|
---|
50 |
|
---|
51 |
|
---|
52 | #define NR_END 1
|
---|
53 | #define FREE_ARG char*
|
---|
54 |
|
---|
55 |
|
---|
56 | void NumRecipes::nrerror(char error_text[])
|
---|
57 | /* Numerical Recipes standard error handler */
|
---|
58 | {
|
---|
59 | fprintf(stderr,"Numerical Recipes run-time error...\n");
|
---|
60 | fprintf(stderr,"%s\n",error_text);
|
---|
61 | fprintf(stderr,"...now exiting to system...\n");
|
---|
62 | exit(1);
|
---|
63 | }
|
---|
64 |
|
---|
65 | float* NumRecipes::vector(long nl, long nh)
|
---|
66 | /* allocate a float vector with subscript range v[nl..nh] */
|
---|
67 | {
|
---|
68 | float *v;
|
---|
69 |
|
---|
70 | v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
|
---|
71 | if (!v) nrerror("allocation failure in vector()");
|
---|
72 | return v-nl+NR_END;
|
---|
73 | }
|
---|
74 |
|
---|
75 | int* NumRecipes::ivector(long nl, long nh)
|
---|
76 | /* allocate an int vector with subscript range v[nl..nh] */
|
---|
77 | {
|
---|
78 | int *v;
|
---|
79 |
|
---|
80 | v=(int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int)));
|
---|
81 | if (!v) nrerror("allocation failure in ivector()");
|
---|
82 | return v-nl+NR_END;
|
---|
83 | }
|
---|
84 |
|
---|
85 | unsigned char* NumRecipes::cvector(long nl, long nh)
|
---|
86 | /* allocate an unsigned char vector with subscript range v[nl..nh] */
|
---|
87 | {
|
---|
88 | unsigned char *v;
|
---|
89 |
|
---|
90 | v=(unsigned char *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(unsigned char)));
|
---|
91 | if (!v) nrerror("allocation failure in cvector()");
|
---|
92 | return v-nl+NR_END;
|
---|
93 | }
|
---|
94 |
|
---|
95 | unsigned long * NumRecipes::lvector(long nl, long nh)
|
---|
96 | /* allocate an unsigned long vector with subscript range v[nl..nh] */
|
---|
97 | {
|
---|
98 | unsigned long *v;
|
---|
99 |
|
---|
100 | v=(unsigned long *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(long)));
|
---|
101 | if (!v) nrerror("allocation failure in lvector()");
|
---|
102 | return v-nl+NR_END;
|
---|
103 | }
|
---|
104 |
|
---|
105 | double* NumRecipes::dvector(long nl, long nh)
|
---|
106 | /* allocate a double vector with subscript range v[nl..nh] */
|
---|
107 | {
|
---|
108 | double *v;
|
---|
109 |
|
---|
110 | v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
|
---|
111 | if (!v) nrerror("allocation failure in dvector()");
|
---|
112 | return v-nl+NR_END;
|
---|
113 | }
|
---|
114 |
|
---|
115 | float ** NumRecipes::matrix(long nrl, long nrh, long ncl, long nch)
|
---|
116 | /* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
|
---|
117 | {
|
---|
118 | long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
|
---|
119 | float **m;
|
---|
120 |
|
---|
121 | /* allocate pointers to rows */
|
---|
122 | m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
|
---|
123 | if (!m) nrerror("allocation failure 1 in matrix()");
|
---|
124 | m += NR_END;
|
---|
125 | m -= nrl;
|
---|
126 |
|
---|
127 | /* allocate rows and set pointers to them */
|
---|
128 | m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
|
---|
129 | if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
|
---|
130 | m[nrl] += NR_END;
|
---|
131 | m[nrl] -= ncl;
|
---|
132 |
|
---|
133 | for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
|
---|
134 |
|
---|
135 | /* return pointer to array of pointers to rows */
|
---|
136 | return m;
|
---|
137 | }
|
---|
138 |
|
---|
139 | double ** NumRecipes::dmatrix(long nrl, long nrh, long ncl, long nch)
|
---|
140 | /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
|
---|
141 | {
|
---|
142 | long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
|
---|
143 | double **m;
|
---|
144 |
|
---|
145 | /* allocate pointers to rows */
|
---|
146 | m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
|
---|
147 | if (!m) nrerror("allocation failure 1 in matrix()");
|
---|
148 | m += NR_END;
|
---|
149 | m -= nrl;
|
---|
150 |
|
---|
151 | /* allocate rows and set pointers to them */
|
---|
152 | m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
|
---|
153 | if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
|
---|
154 | m[nrl] += NR_END;
|
---|
155 | m[nrl] -= ncl;
|
---|
156 |
|
---|
157 | for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
|
---|
158 |
|
---|
159 | /* return pointer to array of pointers to rows */
|
---|
160 | return m;
|
---|
161 | }
|
---|
162 |
|
---|
163 | int ** NumRecipes::imatrix(long nrl, long nrh, long ncl, long nch)
|
---|
164 | /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
|
---|
165 | {
|
---|
166 | long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
|
---|
167 | int **m;
|
---|
168 |
|
---|
169 | /* allocate pointers to rows */
|
---|
170 | m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
|
---|
171 | if (!m) nrerror("allocation failure 1 in matrix()");
|
---|
172 | m += NR_END;
|
---|
173 | m -= nrl;
|
---|
174 |
|
---|
175 |
|
---|
176 | /* allocate rows and set pointers to them */
|
---|
177 | m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
|
---|
178 | if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
|
---|
179 | m[nrl] += NR_END;
|
---|
180 | m[nrl] -= ncl;
|
---|
181 |
|
---|
182 | for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
|
---|
183 |
|
---|
184 | /* return pointer to array of pointers to rows */
|
---|
185 | return m;
|
---|
186 | }
|
---|
187 |
|
---|
188 | float** NumRecipes::submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
|
---|
189 | long newrl, long newcl)
|
---|
190 | /* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
|
---|
191 | {
|
---|
192 | long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
|
---|
193 | float **m;
|
---|
194 |
|
---|
195 | /* allocate array of pointers to rows */
|
---|
196 | m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
|
---|
197 | if (!m) nrerror("allocation failure in submatrix()");
|
---|
198 | m += NR_END;
|
---|
199 | m -= newrl;
|
---|
200 |
|
---|
201 | /* set pointers to rows */
|
---|
202 | for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
|
---|
203 |
|
---|
204 | /* return pointer to array of pointers to rows */
|
---|
205 | return m;
|
---|
206 | }
|
---|
207 |
|
---|
208 | float** NumRecipes::convert_matrix(float *a, long nrl, long nrh, long ncl, long nch)
|
---|
209 | /* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
|
---|
210 | declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
|
---|
211 | and ncol=nch-ncl+1. The routine should be called with the address
|
---|
212 | &a[0][0] as the first argument. */
|
---|
213 | {
|
---|
214 | long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
|
---|
215 | float **m;
|
---|
216 |
|
---|
217 | /* allocate pointers to rows */
|
---|
218 | m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
|
---|
219 | if (!m) nrerror("allocation failure in convert_matrix()");
|
---|
220 | m += NR_END;
|
---|
221 | m -= nrl;
|
---|
222 |
|
---|
223 | /* set pointers to rows */
|
---|
224 | m[nrl]=a-ncl;
|
---|
225 | for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
|
---|
226 | /* return pointer to array of pointers to rows */
|
---|
227 | return m;
|
---|
228 | }
|
---|
229 |
|
---|
230 | float *** NumRecipes::f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
|
---|
231 | /* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
|
---|
232 | {
|
---|
233 | long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
|
---|
234 | float ***t;
|
---|
235 |
|
---|
236 | /* allocate pointers to pointers to rows */
|
---|
237 | t=(float ***) malloc((size_t)((nrow+NR_END)*sizeof(float**)));
|
---|
238 | if (!t) nrerror("allocation failure 1 in f3tensor()");
|
---|
239 | t += NR_END;
|
---|
240 | t -= nrl;
|
---|
241 |
|
---|
242 | /* allocate pointers to rows and set pointers to them */
|
---|
243 | t[nrl]=(float **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float*)));
|
---|
244 | if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
|
---|
245 | t[nrl] += NR_END;
|
---|
246 | t[nrl] -= ncl;
|
---|
247 |
|
---|
248 | /* allocate rows and set pointers to them */
|
---|
249 | t[nrl][ncl]=(float *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(float)));
|
---|
250 | if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
|
---|
251 | t[nrl][ncl] += NR_END;
|
---|
252 | t[nrl][ncl] -= ndl;
|
---|
253 |
|
---|
254 | for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
|
---|
255 | for(i=nrl+1;i<=nrh;i++) {
|
---|
256 | t[i]=t[i-1]+ncol;
|
---|
257 | t[i][ncl]=t[i-1][ncl]+ncol*ndep;
|
---|
258 | for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
|
---|
259 | }
|
---|
260 |
|
---|
261 | /* return pointer to array of pointers to rows */
|
---|
262 | return t;
|
---|
263 | }
|
---|
264 |
|
---|
265 | void NumRecipes::free_vector(float *v, long nl, long nh)
|
---|
266 | /* free a float vector allocated with vector() */
|
---|
267 | {
|
---|
268 | free((FREE_ARG) (v+nl-NR_END));
|
---|
269 | }
|
---|
270 |
|
---|
271 | void NumRecipes::free_ivector(int *v, long nl, long nh)
|
---|
272 | /* free an int vector allocated with ivector() */
|
---|
273 | {
|
---|
274 | free((FREE_ARG) (v+nl-NR_END));
|
---|
275 | }
|
---|
276 |
|
---|
277 | void NumRecipes::free_cvector(unsigned char *v, long nl, long nh)
|
---|
278 | /* free an unsigned char vector allocated with cvector() */
|
---|
279 | {
|
---|
280 | free((FREE_ARG) (v+nl-NR_END));
|
---|
281 | }
|
---|
282 |
|
---|
283 | void NumRecipes::free_lvector(unsigned long *v, long nl, long nh)
|
---|
284 | /* free an unsigned long vector allocated with lvector() */
|
---|
285 | {
|
---|
286 | free((FREE_ARG) (v+nl-NR_END));
|
---|
287 | }
|
---|
288 |
|
---|
289 | void NumRecipes::free_dvector(double *v, long nl, long nh)
|
---|
290 | /* free a double vector allocated with dvector() */
|
---|
291 | {
|
---|
292 | free((FREE_ARG) (v+nl-NR_END));
|
---|
293 | }
|
---|
294 |
|
---|
295 | void NumRecipes::free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
|
---|
296 | /* free a float matrix allocated by matrix() */
|
---|
297 | {
|
---|
298 | free((FREE_ARG) (m[nrl]+ncl-NR_END));
|
---|
299 | free((FREE_ARG) (m+nrl-NR_END));
|
---|
300 | }
|
---|
301 |
|
---|
302 | void NumRecipes::free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
|
---|
303 | /* free a double matrix allocated by dmatrix() */
|
---|
304 | {
|
---|
305 | free((FREE_ARG) (m[nrl]+ncl-NR_END));
|
---|
306 | free((FREE_ARG) (m+nrl-NR_END));
|
---|
307 | }
|
---|
308 |
|
---|
309 | void NumRecipes::free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
|
---|
310 | /* free an int matrix allocated by imatrix() */
|
---|
311 | {
|
---|
312 | free((FREE_ARG) (m[nrl]+ncl-NR_END));
|
---|
313 | free((FREE_ARG) (m+nrl-NR_END));
|
---|
314 | }
|
---|
315 |
|
---|
316 | void NumRecipes::free_submatrix(float **b, long nrl, long nrh, long ncl, long nch)
|
---|
317 | /* free a submatrix allocated by submatrix() */
|
---|
318 | {
|
---|
319 | free((FREE_ARG) (b+nrl-NR_END));
|
---|
320 | }
|
---|
321 |
|
---|
322 | void NumRecipes::free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch)
|
---|
323 | /* free a matrix allocated by convert_matrix() */
|
---|
324 | {
|
---|
325 | free((FREE_ARG) (b+nrl-NR_END));
|
---|
326 | }
|
---|
327 |
|
---|
328 | void NumRecipes::free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
|
---|
329 | long ndl, long ndh)
|
---|
330 | /* free a float f3tensor allocated by f3tensor() */
|
---|
331 | {
|
---|
332 | free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
|
---|
333 | free((FREE_ARG) (t[nrl]+ncl-NR_END));
|
---|
334 | free((FREE_ARG) (t+nrl-NR_END));
|
---|
335 | }
|
---|
336 |
|
---|
337 | //_____________________________ Fichier hunt.c _________________________________________
|
---|
338 |
|
---|
339 | void NumRecipes::hunt(float xx[], unsigned long n, float x, unsigned long *jlo)
|
---|
340 | {
|
---|
341 | unsigned long jm,jhi,inc;
|
---|
342 | int ascnd;
|
---|
343 |
|
---|
344 | ascnd=(xx[n] > xx[1]);
|
---|
345 | if (*jlo <= 0 || *jlo > n) {
|
---|
346 | *jlo=0;
|
---|
347 | jhi=n+1;
|
---|
348 | } else {
|
---|
349 | inc=1;
|
---|
350 | if (x >= xx[*jlo] == ascnd) {
|
---|
351 | if (*jlo == n) return;
|
---|
352 | jhi=(*jlo)+1;
|
---|
353 | while (x >= xx[jhi] == ascnd) {
|
---|
354 | *jlo=jhi;
|
---|
355 | inc += inc;
|
---|
356 | jhi=(*jlo)+inc;
|
---|
357 | if (jhi > n) {
|
---|
358 | jhi=n+1;
|
---|
359 | break;
|
---|
360 | }
|
---|
361 | }
|
---|
362 | } else {
|
---|
363 | if (*jlo == 1) {
|
---|
364 | *jlo=0;
|
---|
365 | return;
|
---|
366 | }
|
---|
367 | jhi=(*jlo)--;
|
---|
368 | while (x < xx[*jlo] == ascnd) {
|
---|
369 | jhi=(*jlo);
|
---|
370 | inc <<= 1;
|
---|
371 | if (inc >= jhi) {
|
---|
372 | *jlo=0;
|
---|
373 | break;
|
---|
374 | }
|
---|
375 | else *jlo=jhi-inc;
|
---|
376 | }
|
---|
377 | }
|
---|
378 | }
|
---|
379 | while (jhi-(*jlo) != 1) {
|
---|
380 | jm=(jhi+(*jlo)) >> 1;
|
---|
381 | if (x > xx[jm] == ascnd)
|
---|
382 | *jlo=jm;
|
---|
383 | else
|
---|
384 | jhi=jm;
|
---|
385 | }
|
---|
386 | }
|
---|
387 | // __________________________Fichier polint.c___________________________________
|
---|
388 |
|
---|
389 | void NumRecipes::polint(float xa[], float ya[], int n, float x, float *y, float *dy)
|
---|
390 | {
|
---|
391 | int i,m,ns=1;
|
---|
392 | float den,dif,dift,ho,hp,w;
|
---|
393 | float *c,*d;
|
---|
394 |
|
---|
395 | dif=fabs(x-xa[1]);
|
---|
396 | c=vector(1,n);
|
---|
397 | d=vector(1,n);
|
---|
398 | for (i=1;i<=n;i++) {
|
---|
399 | if ( (dift=fabs(x-xa[i])) < dif) {
|
---|
400 | ns=i;
|
---|
401 | dif=dift;
|
---|
402 | }
|
---|
403 | c[i]=ya[i];
|
---|
404 | d[i]=ya[i];
|
---|
405 | }
|
---|
406 | *y=ya[ns--];
|
---|
407 | for (m=1;m<n;m++) {
|
---|
408 | for (i=1;i<=n-m;i++) {
|
---|
409 | ho=xa[i]-x;
|
---|
410 | hp=xa[i+m]-x;
|
---|
411 | w=c[i+1]-d[i];
|
---|
412 | if ( (den=ho-hp) == 0.0) nrerror("Error in routine polint");
|
---|
413 | den=w/den;
|
---|
414 | d[i]=hp*den;
|
---|
415 | c[i]=ho*den;
|
---|
416 | }
|
---|
417 | *y += (*dy=(2*ns < (n-m) ? c[ns+1] : d[ns--]));
|
---|
418 | }
|
---|
419 | free_vector(d,1,n);
|
---|
420 | free_vector(c,1,n);
|
---|
421 |
|
---|
422 | }
|
---|
423 |
|
---|
424 | // ________________________Fichier polin2.c_______________________________________
|
---|
425 |
|
---|
426 | void NumRecipes::polin2(float x1a[], float x2a[], float **ya, int m, int n, float x1,
|
---|
427 | float x2, float *y, float *dy)
|
---|
428 | {
|
---|
429 |
|
---|
430 | int j;
|
---|
431 | float *ymtmp;
|
---|
432 |
|
---|
433 | ymtmp=vector(1,m);
|
---|
434 | for (j=1;j<=m;j++) {
|
---|
435 | polint(x2a,ya[j],n,x2,&ymtmp[j],dy);
|
---|
436 | }
|
---|
437 | polint(x1a,ymtmp,m,x1,y,dy);
|
---|
438 | free_vector(ymtmp,1,m);
|
---|
439 | } |
---|