4
#if defined(__STDC__) || defined(ANSI) || defined(NRANSI) /* ANSI */
12
#define FREE_ARG char*
14
void nrerror(char error_text[])
15
/* Numerical Recipes standard error handler */
17
fprintf(stderr,"Numerical Recipes run-time error...\n");
18
fprintf(stderr,"%s\n",error_text);
19
fprintf(stderr,"...now exiting to system...\n");
23
float *vector(long nl, long nh)
24
/* allocate a float vector with subscript range v[nl..nh] */
28
v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
29
if (!v) nrerror("allocation failure in vector()");
33
int *ivector(long nl, long nh)
34
/* allocate an int vector with subscript range v[nl..nh] */
38
v=(int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int)));
39
if (!v) nrerror("allocation failure in ivector()");
43
unsigned char *cvector(long nl, long nh)
44
/* allocate an unsigned char vector with subscript range v[nl..nh] */
48
v=(unsigned char *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(unsigned char)));
49
if (!v) nrerror("allocation failure in cvector()");
53
unsigned long *lvector(long nl, long nh)
54
/* allocate an unsigned long vector with subscript range v[nl..nh] */
58
v=(unsigned long *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(long)));
59
if (!v) nrerror("allocation failure in lvector()");
63
double *dvector(long nl, long nh)
64
/* allocate a double vector with subscript range v[nl..nh] */
67
v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
68
if (!v) nrerror("allocation failure in dvector()");
72
float **matrix(long nrl, long nrh, long ncl, long nch)
73
/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
75
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
78
/* allocate pointers to rows */
79
m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
80
if (!m) nrerror("allocation failure 1 in matrix()");
84
/* allocate rows and set pointers to them */
85
m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
86
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
90
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
92
/* return pointer to array of pointers to rows */
96
double **dmatrix(long nrl, long nrh, long ncl, long nch)
97
/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
99
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
102
/* allocate pointers to rows */
103
m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
104
if (!m) nrerror("allocation failure 1 in matrix()");
108
/* allocate rows and set pointers to them */
109
m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
110
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
114
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
116
/* return pointer to array of pointers to rows */
120
int **imatrix(long nrl, long nrh, long ncl, long nch)
121
/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
123
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
126
/* allocate pointers to rows */
127
m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
128
if (!m) nrerror("allocation failure 1 in matrix()");
133
/* allocate rows and set pointers to them */
134
m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
135
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
139
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
141
/* return pointer to array of pointers to rows */
145
float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
146
long newrl, long newcl)
147
/* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
149
long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
152
/* allocate array of pointers to rows */
153
m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
154
if (!m) nrerror("allocation failure in submatrix()");
158
/* set pointers to rows */
159
for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
161
/* return pointer to array of pointers to rows */
165
float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch)
166
/* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
167
declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
168
and ncol=nch-ncl+1. The routine should be called with the address
169
&a[0][0] as the first argument. */
171
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
174
/* allocate pointers to rows */
175
m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
176
if (!m) nrerror("allocation failure in convert_matrix()");
180
/* set pointers to rows */
182
for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
183
/* return pointer to array of pointers to rows */
187
float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
188
/* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
190
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
193
/* allocate pointers to pointers to rows */
194
t=(float ***) malloc((size_t)((nrow+NR_END)*sizeof(float**)));
195
if (!t) nrerror("allocation failure 1 in f3tensor()");
199
/* allocate pointers to rows and set pointers to them */
200
t[nrl]=(float **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float*)));
201
if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
205
/* allocate rows and set pointers to them */
206
t[nrl][ncl]=(float *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(float)));
207
if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
208
t[nrl][ncl] += NR_END;
211
for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
212
for(i=nrl+1;i<=nrh;i++) {
214
t[i][ncl]=t[i-1][ncl]+ncol*ndep;
215
for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
218
/* return pointer to array of pointers to rows */
222
void free_vector(float *v, long nl, long nh)
223
/* free a float vector allocated with vector() */
225
free((FREE_ARG) (v+nl-NR_END));
228
void free_ivector(int *v, long nl, long nh)
229
/* free an int vector allocated with ivector() */
231
free((FREE_ARG) (v+nl-NR_END));
234
void free_cvector(unsigned char *v, long nl, long nh)
235
/* free an unsigned char vector allocated with cvector() */
237
free((FREE_ARG) (v+nl-NR_END));
240
void free_lvector(unsigned long *v, long nl, long nh)
241
/* free an unsigned long vector allocated with lvector() */
243
free((FREE_ARG) (v+nl-NR_END));
246
void free_dvector(double *v, long nl, long nh)
247
/* free a double vector allocated with dvector() */
249
free((FREE_ARG) (v+nl-NR_END));
252
void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
253
/* free a float matrix allocated by matrix() */
255
free((FREE_ARG) (m[nrl]+ncl-NR_END));
256
free((FREE_ARG) (m+nrl-NR_END));
259
void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
260
/* free a double matrix allocated by dmatrix() */
262
free((FREE_ARG) (m[nrl]+ncl-NR_END));
263
free((FREE_ARG) (m+nrl-NR_END));
266
void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
267
/* free an int matrix allocated by imatrix() */
269
free((FREE_ARG) (m[nrl]+ncl-NR_END));
270
free((FREE_ARG) (m+nrl-NR_END));
273
void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch)
274
/* free a submatrix allocated by submatrix() */
276
free((FREE_ARG) (b+nrl-NR_END));
279
void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch)
280
/* free a matrix allocated by convert_matrix() */
282
free((FREE_ARG) (b+nrl-NR_END));
285
void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
287
/* free a float f3tensor allocated by f3tensor() */
289
free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
290
free((FREE_ARG) (t[nrl]+ncl-NR_END));
291
free((FREE_ARG) (t+nrl-NR_END));
295
/* traditional - K&R */
299
#define FREE_ARG char*
301
void nrerror(error_text)
303
/* Numerical Recipes standard error handler */
307
fprintf(stderr,"Numerical Recipes run-time error...\n");
308
fprintf(stderr,"%s\n",error_text);
309
fprintf(stderr,"...now exiting to system...\n");
315
/* allocate a float vector with subscript range v[nl..nh] */
319
v=(float *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(float)));
320
if (!v) nrerror("allocation failure in vector()");
326
/* allocate an int vector with subscript range v[nl..nh] */
330
v=(int *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(int)));
331
if (!v) nrerror("allocation failure in ivector()");
335
unsigned char *cvector(nl,nh)
337
/* allocate an unsigned char vector with subscript range v[nl..nh] */
341
v=(unsigned char *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(unsigned char)));
342
if (!v) nrerror("allocation failure in cvector()");
346
unsigned long *lvector(nl,nh)
348
/* allocate an unsigned long vector with subscript range v[nl..nh] */
352
v=(unsigned long *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(long)));
353
if (!v) nrerror("allocation failure in lvector()");
357
double *dvector(nl,nh)
359
/* allocate a double vector with subscript range v[nl..nh] */
363
v=(double *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(double)));
364
if (!v) nrerror("allocation failure in dvector()");
368
float **matrix(nrl,nrh,ncl,nch)
369
long nch,ncl,nrh,nrl;
370
/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
372
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
375
/* allocate pointers to rows */
376
m=(float **) malloc((unsigned int)((nrow+NR_END)*sizeof(float*)));
377
if (!m) nrerror("allocation failure 1 in matrix()");
381
/* allocate rows and set pointers to them */
382
m[nrl]=(float *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(float)));
383
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
387
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
389
/* return pointer to array of pointers to rows */
393
double **dmatrix(nrl,nrh,ncl,nch)
394
long nch,ncl,nrh,nrl;
395
/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
397
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
400
/* allocate pointers to rows */
401
m=(double **) malloc((unsigned int)((nrow+NR_END)*sizeof(double*)));
402
if (!m) nrerror("allocation failure 1 in matrix()");
406
/* allocate rows and set pointers to them */
407
m[nrl]=(double *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(double)));
408
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
412
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
414
/* return pointer to array of pointers to rows */
418
int **imatrix(nrl,nrh,ncl,nch)
419
long nch,ncl,nrh,nrl;
420
/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
422
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
425
/* allocate pointers to rows */
426
m=(int **) malloc((unsigned int)((nrow+NR_END)*sizeof(int*)));
427
if (!m) nrerror("allocation failure 1 in matrix()");
432
/* allocate rows and set pointers to them */
433
m[nrl]=(int *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(int)));
434
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
438
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
440
/* return pointer to array of pointers to rows */
444
float **submatrix(a,oldrl,oldrh,oldcl,oldch,newrl,newcl)
446
long newcl,newrl,oldch,oldcl,oldrh,oldrl;
447
/* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
449
long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
452
/* allocate array of pointers to rows */
453
m=(float **) malloc((unsigned int) ((nrow+NR_END)*sizeof(float*)));
454
if (!m) nrerror("allocation failure in submatrix()");
458
/* set pointers to rows */
459
for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
461
/* return pointer to array of pointers to rows */
465
float **convert_matrix(a,nrl,nrh,ncl,nch)
467
long nch,ncl,nrh,nrl;
468
/* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
469
declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
470
and ncol=nch-ncl+1. The routine should be called with the address
471
&a[0][0] as the first argument. */
473
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
476
/* allocate pointers to rows */
477
m=(float **) malloc((unsigned int) ((nrow+NR_END)*sizeof(float*)));
478
if (!m) nrerror("allocation failure in convert_matrix()");
482
/* set pointers to rows */
484
for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
485
/* return pointer to array of pointers to rows */
489
float ***f3tensor(nrl,nrh,ncl,nch,ndl,ndh)
490
long nch,ncl,ndh,ndl,nrh,nrl;
491
/* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
493
long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
496
/* allocate pointers to pointers to rows */
497
t=(float ***) malloc((unsigned int)((nrow+NR_END)*sizeof(float**)));
498
if (!t) nrerror("allocation failure 1 in f3tensor()");
502
/* allocate pointers to rows and set pointers to them */
503
t[nrl]=(float **) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(float*)));
504
if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
508
/* allocate rows and set pointers to them */
509
t[nrl][ncl]=(float *) malloc((unsigned int)((nrow*ncol*ndep+NR_END)*sizeof(float)));
510
if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
511
t[nrl][ncl] += NR_END;
514
for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
515
for(i=nrl+1;i<=nrh;i++) {
517
t[i][ncl]=t[i-1][ncl]+ncol*ndep;
518
for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
521
/* return pointer to array of pointers to rows */
525
void free_vector(v,nl,nh)
528
/* free a float vector allocated with vector() */
530
free((FREE_ARG) (v+nl-NR_END));
533
void free_ivector(v,nl,nh)
536
/* free an int vector allocated with ivector() */
538
free((FREE_ARG) (v+nl-NR_END));
541
void free_cvector(v,nl,nh)
544
/* free an unsigned char vector allocated with cvector() */
546
free((FREE_ARG) (v+nl-NR_END));
549
void free_lvector(v,nl,nh)
552
/* free an unsigned long vector allocated with lvector() */
554
free((FREE_ARG) (v+nl-NR_END));
557
void free_dvector(v,nl,nh)
560
/* free a double vector allocated with dvector() */
562
free((FREE_ARG) (v+nl-NR_END));
565
void free_matrix(m,nrl,nrh,ncl,nch)
567
long nch,ncl,nrh,nrl;
568
/* free a float matrix allocated by matrix() */
570
free((FREE_ARG) (m[nrl]+ncl-NR_END));
571
free((FREE_ARG) (m+nrl-NR_END));
574
void free_dmatrix(m,nrl,nrh,ncl,nch)
576
long nch,ncl,nrh,nrl;
577
/* free a double matrix allocated by dmatrix() */
579
free((FREE_ARG) (m[nrl]+ncl-NR_END));
580
free((FREE_ARG) (m+nrl-NR_END));
583
void free_imatrix(m,nrl,nrh,ncl,nch)
585
long nch,ncl,nrh,nrl;
586
/* free an int matrix allocated by imatrix() */
588
free((FREE_ARG) (m[nrl]+ncl-NR_END));
589
free((FREE_ARG) (m+nrl-NR_END));
592
void free_submatrix(b,nrl,nrh,ncl,nch)
594
long nch,ncl,nrh,nrl;
595
/* free a submatrix allocated by submatrix() */
597
free((FREE_ARG) (b+nrl-NR_END));
600
void free_convert_matrix(b,nrl,nrh,ncl,nch)
602
long nch,ncl,nrh,nrl;
603
/* free a matrix allocated by convert_matrix() */
605
free((FREE_ARG) (b+nrl-NR_END));
608
void free_f3tensor(t,nrl,nrh,ncl,nch,ndl,ndh)
610
long nch,ncl,ndh,ndl,nrh,nrl;
611
/* free a float f3tensor allocated by f3tensor() */
613
free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
614
free((FREE_ARG) (t[nrl]+ncl-NR_END));
615
free((FREE_ARG) (t+nrl-NR_END));