source: Sophya/trunk/Poubelle/archTOI.old/nrutil.c@ 577

Last change on this file since 577 was 577, checked in by ansari, 26 years ago

SST

File size: 15.9 KB
RevLine 
[556]1#if defined(__STDC__) || defined(ANSI) || defined(NRANSI) /* ANSI */
2
3#include <stdio.h>
4#include <stddef.h>
5#include <stdlib.h>
6#define NR_END 1
7#define FREE_ARG char*
8
9void nrerror(char error_text[]);
10
11/*void nrerror(char error_text[])
12/* Numerical Recipes standard error handler */
13/*{
14 fprintf(stderr,"Numerical Recipes run-time error...\n");
15 fprintf(stderr,"%s\n",error_text);
16 fprintf(stderr,"...now exiting to system...\n");
17 exit(1);
18}*/
19
[577]20double *vector(long nl, long nh)
21/* allocate a double vector with subscript range v[nl..nh] */
[556]22{
[577]23 double *v;
[556]24
[577]25 v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
[556]26 if (!v) nrerror("allocation failure in vector()");
27 return v-nl+NR_END;
28}
29
30int *ivector(long nl, long nh)
31/* allocate an int vector with subscript range v[nl..nh] */
32{
33 int *v;
34
35 v=(int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int)));
36 if (!v) nrerror("allocation failure in ivector()");
37 return v-nl+NR_END;
38}
39
40unsigned char *cvector(long nl, long nh)
41/* allocate an unsigned char vector with subscript range v[nl..nh] */
42{
43 unsigned char *v;
44
45 v=(unsigned char *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(unsigned char)));
46 if (!v) nrerror("allocation failure in cvector()");
47 return v-nl+NR_END;
48}
49
50unsigned long *lvector(long nl, long nh)
51/* allocate an unsigned long vector with subscript range v[nl..nh] */
52{
53 unsigned long *v;
54
55 v=(unsigned long *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(long)));
56 if (!v) nrerror("allocation failure in lvector()");
57 return v-nl+NR_END;
58}
59
60double *dvector(long nl, long nh)
61/* allocate a double vector with subscript range v[nl..nh] */
62{
63 double *v;
64
65 v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
66 if (!v) nrerror("allocation failure in dvector()");
67 return v-nl+NR_END;
68}
69
[577]70double **matrix(long nrl, long nrh, long ncl, long nch)
71/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
[556]72{
73 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
[577]74 double **m;
[556]75
76 /* allocate pointers to rows */
[577]77 m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
[556]78 if (!m) nrerror("allocation failure 1 in matrix()");
79 m += NR_END;
80 m -= nrl;
81
82 /* allocate rows and set pointers to them */
[577]83 m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
[556]84 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
85 m[nrl] += NR_END;
86 m[nrl] -= ncl;
87
88 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
89
90 /* return pointer to array of pointers to rows */
91 return m;
92}
93
94double **dmatrix(long nrl, long nrh, long ncl, long nch)
95/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
96{
97 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
98 double **m;
99
100 /* allocate pointers to rows */
101 m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
102 if (!m) nrerror("allocation failure 1 in matrix()");
103 m += NR_END;
104 m -= nrl;
105
106 /* allocate rows and set pointers to them */
107 m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
108 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
109 m[nrl] += NR_END;
110 m[nrl] -= ncl;
111
112 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
113
114 /* return pointer to array of pointers to rows */
115 return m;
116}
117
118int **imatrix(long nrl, long nrh, long ncl, long nch)
119/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
120{
121 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
122 int **m;
123
124 /* allocate pointers to rows */
125 m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
126 if (!m) nrerror("allocation failure 1 in matrix()");
127 m += NR_END;
128 m -= nrl;
129
130
131 /* allocate rows and set pointers to them */
132 m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
133 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
134 m[nrl] += NR_END;
135 m[nrl] -= ncl;
136
137 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
138
139 /* return pointer to array of pointers to rows */
140 return m;
141}
142
[577]143double **submatrix(double **a, long oldrl, long oldrh, long oldcl, long oldch,
[556]144 long newrl, long newcl)
145/* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
146{
147 long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
[577]148 double **m;
[556]149
150 /* allocate array of pointers to rows */
[577]151 m=(double **) malloc((size_t) ((nrow+NR_END)*sizeof(double*)));
[556]152 if (!m) nrerror("allocation failure in submatrix()");
153 m += NR_END;
154 m -= newrl;
155
156 /* set pointers to rows */
157 for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
158
159 /* return pointer to array of pointers to rows */
160 return m;
161}
162
[577]163double **convert_matrix(double *a, long nrl, long nrh, long ncl, long nch)
164/* allocate a double matrix m[nrl..nrh][ncl..nch] that points to the matrix
[556]165declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
166and ncol=nch-ncl+1. The routine should be called with the address
167&a[0][0] as the first argument. */
168{
169 long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
[577]170 double **m;
[556]171
172 /* allocate pointers to rows */
[577]173 m=(double **) malloc((size_t) ((nrow+NR_END)*sizeof(double*)));
[556]174 if (!m) nrerror("allocation failure in convert_matrix()");
175 m += NR_END;
176 m -= nrl;
177
178 /* set pointers to rows */
179 m[nrl]=a-ncl;
180 for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
181 /* return pointer to array of pointers to rows */
182 return m;
183}
184
[577]185double ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
186/* allocate a double 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
[556]187{
188 long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
[577]189 double ***t;
[556]190
191 /* allocate pointers to pointers to rows */
[577]192 t=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double**)));
[556]193 if (!t) nrerror("allocation failure 1 in f3tensor()");
194 t += NR_END;
195 t -= nrl;
196
197 /* allocate pointers to rows and set pointers to them */
[577]198 t[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double*)));
[556]199 if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
200 t[nrl] += NR_END;
201 t[nrl] -= ncl;
202
203 /* allocate rows and set pointers to them */
[577]204 t[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(double)));
[556]205 if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
206 t[nrl][ncl] += NR_END;
207 t[nrl][ncl] -= ndl;
208
209 for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
210 for(i=nrl+1;i<=nrh;i++) {
211 t[i]=t[i-1]+ncol;
212 t[i][ncl]=t[i-1][ncl]+ncol*ndep;
213 for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
214 }
215
216 /* return pointer to array of pointers to rows */
217 return t;
218}
219
[577]220void free_vector(double *v, long nl, long nh)
221/* free a double vector allocated with vector() */
[556]222{
223 free((FREE_ARG) (v+nl-NR_END));
224}
225
226void free_ivector(int *v, long nl, long nh)
227/* free an int vector allocated with ivector() */
228{
229 free((FREE_ARG) (v+nl-NR_END));
230}
231
232void free_cvector(unsigned char *v, long nl, long nh)
233/* free an unsigned char vector allocated with cvector() */
234{
235 free((FREE_ARG) (v+nl-NR_END));
236}
237
238void free_lvector(unsigned long *v, long nl, long nh)
239/* free an unsigned long vector allocated with lvector() */
240{
241 free((FREE_ARG) (v+nl-NR_END));
242}
243
244void free_dvector(double *v, long nl, long nh)
245/* free a double vector allocated with dvector() */
246{
247 free((FREE_ARG) (v+nl-NR_END));
248}
249
[577]250void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
251/* free a double matrix allocated by matrix() */
[556]252{
253 free((FREE_ARG) (m[nrl]+ncl-NR_END));
254 free((FREE_ARG) (m+nrl-NR_END));
255}
256
257void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
258/* free a double matrix allocated by dmatrix() */
259{
260 free((FREE_ARG) (m[nrl]+ncl-NR_END));
261 free((FREE_ARG) (m+nrl-NR_END));
262}
263
264void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
265/* free an int matrix allocated by imatrix() */
266{
267 free((FREE_ARG) (m[nrl]+ncl-NR_END));
268 free((FREE_ARG) (m+nrl-NR_END));
269}
270
[577]271void free_submatrix(double **b, long nrl, long nrh, long ncl, long nch)
[556]272/* free a submatrix allocated by submatrix() */
273{
274 free((FREE_ARG) (b+nrl-NR_END));
275}
276
[577]277void free_convert_matrix(double **b, long nrl, long nrh, long ncl, long nch)
[556]278/* free a matrix allocated by convert_matrix() */
279{
280 free((FREE_ARG) (b+nrl-NR_END));
281}
282
[577]283void free_f3tensor(double ***t, long nrl, long nrh, long ncl, long nch,
[556]284 long ndl, long ndh)
[577]285/* free a double f3tensor allocated by f3tensor() */
[556]286{
287 free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
288 free((FREE_ARG) (t[nrl]+ncl-NR_END));
289 free((FREE_ARG) (t+nrl-NR_END));
290}
291
292#else /* ANSI */
293/* traditional - K&R */
294
295#include <stdio.h>
296#define NR_END 1
297#define FREE_ARG char*
298
299void nrerror(error_text)
300char error_text[];
301/* Numerical Recipes standard error handler */
302{
303 void exit();
304
305 fprintf(stderr,"Numerical Recipes run-time error...\n");
306 fprintf(stderr,"%s\n",error_text);
307 fprintf(stderr,"...now exiting to system...\n");
308 exit(1);
309}
310
[577]311double *vector(nl,nh)
[556]312long nh,nl;
[577]313/* allocate a double vector with subscript range v[nl..nh] */
[556]314{
[577]315 double *v;
[556]316
[577]317 v=(double *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(double)));
[556]318 if (!v) nrerror("allocation failure in vector()");
319 return v-nl+NR_END;
320}
321
322int *ivector(nl,nh)
323long nh,nl;
324/* allocate an int vector with subscript range v[nl..nh] */
325{
326 int *v;
327
328 v=(int *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(int)));
329 if (!v) nrerror("allocation failure in ivector()");
330 return v-nl+NR_END;
331}
332
333unsigned char *cvector(nl,nh)
334long nh,nl;
335/* allocate an unsigned char vector with subscript range v[nl..nh] */
336{
337 unsigned char *v;
338
339 v=(unsigned char *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(unsigned char)));
340 if (!v) nrerror("allocation failure in cvector()");
341 return v-nl+NR_END;
342}
343
344unsigned long *lvector(nl,nh)
345long nh,nl;
346/* allocate an unsigned long vector with subscript range v[nl..nh] */
347{
348 unsigned long *v;
349
350 v=(unsigned long *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(long)));
351 if (!v) nrerror("allocation failure in lvector()");
352 return v-nl+NR_END;
353}
354
355double *dvector(nl,nh)
356long nh,nl;
357/* allocate a double vector with subscript range v[nl..nh] */
358{
359 double *v;
360
361 v=(double *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(double)));
362 if (!v) nrerror("allocation failure in dvector()");
363 return v-nl+NR_END;
364}
365
[577]366double **matrix(nrl,nrh,ncl,nch)
[556]367long nch,ncl,nrh,nrl;
[577]368/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
[556]369{
370 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
[577]371 double **m;
[556]372
373 /* allocate pointers to rows */
[577]374 m=(double **) malloc((unsigned int)((nrow+NR_END)*sizeof(double*)));
[556]375 if (!m) nrerror("allocation failure 1 in matrix()");
376 m += NR_END;
377 m -= nrl;
378
379 /* allocate rows and set pointers to them */
[577]380 m[nrl]=(double *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(double)));
[556]381 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
382 m[nrl] += NR_END;
383 m[nrl] -= ncl;
384
385 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
386
387 /* return pointer to array of pointers to rows */
388 return m;
389}
390
391double **dmatrix(nrl,nrh,ncl,nch)
392long nch,ncl,nrh,nrl;
393/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
394{
395 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
396 double **m;
397
398 /* allocate pointers to rows */
399 m=(double **) malloc((unsigned int)((nrow+NR_END)*sizeof(double*)));
400 if (!m) nrerror("allocation failure 1 in matrix()");
401 m += NR_END;
402 m -= nrl;
403
404 /* allocate rows and set pointers to them */
405 m[nrl]=(double *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(double)));
406 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
407 m[nrl] += NR_END;
408 m[nrl] -= ncl;
409
410 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
411
412 /* return pointer to array of pointers to rows */
413 return m;
414}
415
416int **imatrix(nrl,nrh,ncl,nch)
417long nch,ncl,nrh,nrl;
418/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
419{
420 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
421 int **m;
422
423 /* allocate pointers to rows */
424 m=(int **) malloc((unsigned int)((nrow+NR_END)*sizeof(int*)));
425 if (!m) nrerror("allocation failure 1 in matrix()");
426 m += NR_END;
427 m -= nrl;
428
429
430 /* allocate rows and set pointers to them */
431 m[nrl]=(int *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(int)));
432 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
433 m[nrl] += NR_END;
434 m[nrl] -= ncl;
435
436 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
437
438 /* return pointer to array of pointers to rows */
439 return m;
440}
441
[577]442double **submatrix(a,oldrl,oldrh,oldcl,oldch,newrl,newcl)
443double **a;
[556]444long newcl,newrl,oldch,oldcl,oldrh,oldrl;
445/* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
446{
447 long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
[577]448 double **m;
[556]449
450 /* allocate array of pointers to rows */
[577]451 m=(double **) malloc((unsigned int) ((nrow+NR_END)*sizeof(double*)));
[556]452 if (!m) nrerror("allocation failure in submatrix()");
453 m += NR_END;
454 m -= newrl;
455
456 /* set pointers to rows */
457 for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
458
459 /* return pointer to array of pointers to rows */
460 return m;
461}
462
[577]463double **convert_matrix(a,nrl,nrh,ncl,nch)
464double *a;
[556]465long nch,ncl,nrh,nrl;
[577]466/* allocate a double matrix m[nrl..nrh][ncl..nch] that points to the matrix
[556]467declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
468and ncol=nch-ncl+1. The routine should be called with the address
469&a[0][0] as the first argument. */
470{
471 long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
[577]472 double **m;
[556]473
474 /* allocate pointers to rows */
[577]475 m=(double **) malloc((unsigned int) ((nrow+NR_END)*sizeof(double*)));
[556]476 if (!m) nrerror("allocation failure in convert_matrix()");
477 m += NR_END;
478 m -= nrl;
479
480 /* set pointers to rows */
481 m[nrl]=a-ncl;
482 for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
483 /* return pointer to array of pointers to rows */
484 return m;
485}
486
[577]487double ***f3tensor(nrl,nrh,ncl,nch,ndl,ndh)
[556]488long nch,ncl,ndh,ndl,nrh,nrl;
[577]489/* allocate a double 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
[556]490{
491 long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
[577]492 double ***t;
[556]493
494 /* allocate pointers to pointers to rows */
[577]495 t=(double ***) malloc((unsigned int)((nrow+NR_END)*sizeof(double**)));
[556]496 if (!t) nrerror("allocation failure 1 in f3tensor()");
497 t += NR_END;
498 t -= nrl;
499
500 /* allocate pointers to rows and set pointers to them */
[577]501 t[nrl]=(double **) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(double*)));
[556]502 if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
503 t[nrl] += NR_END;
504 t[nrl] -= ncl;
505
506 /* allocate rows and set pointers to them */
[577]507 t[nrl][ncl]=(double *) malloc((unsigned int)((nrow*ncol*ndep+NR_END)*sizeof(double)));
[556]508 if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
509 t[nrl][ncl] += NR_END;
510 t[nrl][ncl] -= ndl;
511
512 for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
513 for(i=nrl+1;i<=nrh;i++) {
514 t[i]=t[i-1]+ncol;
515 t[i][ncl]=t[i-1][ncl]+ncol*ndep;
516 for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
517 }
518
519 /* return pointer to array of pointers to rows */
520 return t;
521}
522
523void free_vector(v,nl,nh)
[577]524double *v;
[556]525long nh,nl;
[577]526/* free a double vector allocated with vector() */
[556]527{
528 free((FREE_ARG) (v+nl-NR_END));
529}
530
531void free_ivector(v,nl,nh)
532int *v;
533long nh,nl;
534/* free an int vector allocated with ivector() */
535{
536 free((FREE_ARG) (v+nl-NR_END));
537}
538
539void free_cvector(v,nl,nh)
540long nh,nl;
541unsigned char *v;
542/* free an unsigned char vector allocated with cvector() */
543{
544 free((FREE_ARG) (v+nl-NR_END));
545}
546
547void free_lvector(v,nl,nh)
548long nh,nl;
549unsigned long *v;
550/* free an unsigned long vector allocated with lvector() */
551{
552 free((FREE_ARG) (v+nl-NR_END));
553}
554
555void free_dvector(v,nl,nh)
556double *v;
557long nh,nl;
558/* free a double vector allocated with dvector() */
559{
560 free((FREE_ARG) (v+nl-NR_END));
561}
562
563void free_matrix(m,nrl,nrh,ncl,nch)
[577]564double **m;
[556]565long nch,ncl,nrh,nrl;
[577]566/* free a double matrix allocated by matrix() */
[556]567{
568 free((FREE_ARG) (m[nrl]+ncl-NR_END));
569 free((FREE_ARG) (m+nrl-NR_END));
570}
571
572void free_dmatrix(m,nrl,nrh,ncl,nch)
573double **m;
574long nch,ncl,nrh,nrl;
575/* free a double matrix allocated by dmatrix() */
576{
577 free((FREE_ARG) (m[nrl]+ncl-NR_END));
578 free((FREE_ARG) (m+nrl-NR_END));
579}
580
581void free_imatrix(m,nrl,nrh,ncl,nch)
582int **m;
583long nch,ncl,nrh,nrl;
584/* free an int matrix allocated by imatrix() */
585{
586 free((FREE_ARG) (m[nrl]+ncl-NR_END));
587 free((FREE_ARG) (m+nrl-NR_END));
588}
589
590void free_submatrix(b,nrl,nrh,ncl,nch)
[577]591double **b;
[556]592long nch,ncl,nrh,nrl;
593/* free a submatrix allocated by submatrix() */
594{
595 free((FREE_ARG) (b+nrl-NR_END));
596}
597
598void free_convert_matrix(b,nrl,nrh,ncl,nch)
[577]599double **b;
[556]600long nch,ncl,nrh,nrl;
601/* free a matrix allocated by convert_matrix() */
602{
603 free((FREE_ARG) (b+nrl-NR_END));
604}
605
606void free_f3tensor(t,nrl,nrh,ncl,nch,ndl,ndh)
[577]607double ***t;
[556]608long nch,ncl,ndh,ndl,nrh,nrl;
[577]609/* free a double f3tensor allocated by f3tensor() */
[556]610{
611 free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
612 free((FREE_ARG) (t[nrl]+ncl-NR_END));
613 free((FREE_ARG) (t+nrl-NR_END));
614}
615
616#endif /* ANSI */
Note: See TracBrowser for help on using the repository browser.