[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 |
|
---|
| 9 | void 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 |
|
---|
| 20 | float *vector(long nl, long nh)
|
---|
| 21 | /* allocate a float vector with subscript range v[nl..nh] */
|
---|
| 22 | {
|
---|
| 23 | float *v;
|
---|
| 24 |
|
---|
| 25 | v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
|
---|
| 26 | if (!v) nrerror("allocation failure in vector()");
|
---|
| 27 | return v-nl+NR_END;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | int *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 |
|
---|
| 40 | unsigned 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 |
|
---|
| 50 | unsigned 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 |
|
---|
| 60 | double *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 |
|
---|
| 70 | float **matrix(long nrl, long nrh, long ncl, long nch)
|
---|
| 71 | /* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
|
---|
| 72 | {
|
---|
| 73 | long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
|
---|
| 74 | float **m;
|
---|
| 75 |
|
---|
| 76 | /* allocate pointers to rows */
|
---|
| 77 | m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
|
---|
| 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 */
|
---|
| 83 | m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
|
---|
| 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 |
|
---|
| 94 | double **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 |
|
---|
| 118 | int **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 |
|
---|
| 143 | float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
|
---|
| 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;
|
---|
| 148 | float **m;
|
---|
| 149 |
|
---|
| 150 | /* allocate array of pointers to rows */
|
---|
| 151 | m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
|
---|
| 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 |
|
---|
| 163 | float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch)
|
---|
| 164 | /* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
|
---|
| 165 | declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
|
---|
| 166 | and 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;
|
---|
| 170 | float **m;
|
---|
| 171 |
|
---|
| 172 | /* allocate pointers to rows */
|
---|
| 173 | m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
|
---|
| 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 |
|
---|
| 185 | float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
|
---|
| 186 | /* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
|
---|
| 187 | {
|
---|
| 188 | long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
|
---|
| 189 | float ***t;
|
---|
| 190 |
|
---|
| 191 | /* allocate pointers to pointers to rows */
|
---|
| 192 | t=(float ***) malloc((size_t)((nrow+NR_END)*sizeof(float**)));
|
---|
| 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 */
|
---|
| 198 | t[nrl]=(float **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float*)));
|
---|
| 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 */
|
---|
| 204 | t[nrl][ncl]=(float *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(float)));
|
---|
| 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 |
|
---|
| 220 | void free_vector(float *v, long nl, long nh)
|
---|
| 221 | /* free a float vector allocated with vector() */
|
---|
| 222 | {
|
---|
| 223 | free((FREE_ARG) (v+nl-NR_END));
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | void 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 |
|
---|
| 232 | void 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 |
|
---|
| 238 | void 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 |
|
---|
| 244 | void 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 |
|
---|
| 250 | void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
|
---|
| 251 | /* free a float matrix allocated by matrix() */
|
---|
| 252 | {
|
---|
| 253 | free((FREE_ARG) (m[nrl]+ncl-NR_END));
|
---|
| 254 | free((FREE_ARG) (m+nrl-NR_END));
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | void 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 |
|
---|
| 264 | void 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 |
|
---|
| 271 | void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch)
|
---|
| 272 | /* free a submatrix allocated by submatrix() */
|
---|
| 273 | {
|
---|
| 274 | free((FREE_ARG) (b+nrl-NR_END));
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch)
|
---|
| 278 | /* free a matrix allocated by convert_matrix() */
|
---|
| 279 | {
|
---|
| 280 | free((FREE_ARG) (b+nrl-NR_END));
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
|
---|
| 284 | long ndl, long ndh)
|
---|
| 285 | /* free a float f3tensor allocated by f3tensor() */
|
---|
| 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 |
|
---|
| 299 | void nrerror(error_text)
|
---|
| 300 | char 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 |
|
---|
| 311 | float *vector(nl,nh)
|
---|
| 312 | long nh,nl;
|
---|
| 313 | /* allocate a float vector with subscript range v[nl..nh] */
|
---|
| 314 | {
|
---|
| 315 | float *v;
|
---|
| 316 |
|
---|
| 317 | v=(float *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(float)));
|
---|
| 318 | if (!v) nrerror("allocation failure in vector()");
|
---|
| 319 | return v-nl+NR_END;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | int *ivector(nl,nh)
|
---|
| 323 | long 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 |
|
---|
| 333 | unsigned char *cvector(nl,nh)
|
---|
| 334 | long 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 |
|
---|
| 344 | unsigned long *lvector(nl,nh)
|
---|
| 345 | long 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 |
|
---|
| 355 | double *dvector(nl,nh)
|
---|
| 356 | long 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 |
|
---|
| 366 | float **matrix(nrl,nrh,ncl,nch)
|
---|
| 367 | long nch,ncl,nrh,nrl;
|
---|
| 368 | /* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
|
---|
| 369 | {
|
---|
| 370 | long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
|
---|
| 371 | float **m;
|
---|
| 372 |
|
---|
| 373 | /* allocate pointers to rows */
|
---|
| 374 | m=(float **) malloc((unsigned int)((nrow+NR_END)*sizeof(float*)));
|
---|
| 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 */
|
---|
| 380 | m[nrl]=(float *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(float)));
|
---|
| 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 |
|
---|
| 391 | double **dmatrix(nrl,nrh,ncl,nch)
|
---|
| 392 | long 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 |
|
---|
| 416 | int **imatrix(nrl,nrh,ncl,nch)
|
---|
| 417 | long 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 |
|
---|
| 442 | float **submatrix(a,oldrl,oldrh,oldcl,oldch,newrl,newcl)
|
---|
| 443 | float **a;
|
---|
| 444 | long 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;
|
---|
| 448 | float **m;
|
---|
| 449 |
|
---|
| 450 | /* allocate array of pointers to rows */
|
---|
| 451 | m=(float **) malloc((unsigned int) ((nrow+NR_END)*sizeof(float*)));
|
---|
| 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 |
|
---|
| 463 | float **convert_matrix(a,nrl,nrh,ncl,nch)
|
---|
| 464 | float *a;
|
---|
| 465 | long nch,ncl,nrh,nrl;
|
---|
| 466 | /* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
|
---|
| 467 | declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
|
---|
| 468 | and 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;
|
---|
| 472 | float **m;
|
---|
| 473 |
|
---|
| 474 | /* allocate pointers to rows */
|
---|
| 475 | m=(float **) malloc((unsigned int) ((nrow+NR_END)*sizeof(float*)));
|
---|
| 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 |
|
---|
| 487 | float ***f3tensor(nrl,nrh,ncl,nch,ndl,ndh)
|
---|
| 488 | long nch,ncl,ndh,ndl,nrh,nrl;
|
---|
| 489 | /* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
|
---|
| 490 | {
|
---|
| 491 | long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
|
---|
| 492 | float ***t;
|
---|
| 493 |
|
---|
| 494 | /* allocate pointers to pointers to rows */
|
---|
| 495 | t=(float ***) malloc((unsigned int)((nrow+NR_END)*sizeof(float**)));
|
---|
| 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 */
|
---|
| 501 | t[nrl]=(float **) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(float*)));
|
---|
| 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 */
|
---|
| 507 | t[nrl][ncl]=(float *) malloc((unsigned int)((nrow*ncol*ndep+NR_END)*sizeof(float)));
|
---|
| 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 |
|
---|
| 523 | void free_vector(v,nl,nh)
|
---|
| 524 | float *v;
|
---|
| 525 | long nh,nl;
|
---|
| 526 | /* free a float vector allocated with vector() */
|
---|
| 527 | {
|
---|
| 528 | free((FREE_ARG) (v+nl-NR_END));
|
---|
| 529 | }
|
---|
| 530 |
|
---|
| 531 | void free_ivector(v,nl,nh)
|
---|
| 532 | int *v;
|
---|
| 533 | long nh,nl;
|
---|
| 534 | /* free an int vector allocated with ivector() */
|
---|
| 535 | {
|
---|
| 536 | free((FREE_ARG) (v+nl-NR_END));
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | void free_cvector(v,nl,nh)
|
---|
| 540 | long nh,nl;
|
---|
| 541 | unsigned char *v;
|
---|
| 542 | /* free an unsigned char vector allocated with cvector() */
|
---|
| 543 | {
|
---|
| 544 | free((FREE_ARG) (v+nl-NR_END));
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | void free_lvector(v,nl,nh)
|
---|
| 548 | long nh,nl;
|
---|
| 549 | unsigned long *v;
|
---|
| 550 | /* free an unsigned long vector allocated with lvector() */
|
---|
| 551 | {
|
---|
| 552 | free((FREE_ARG) (v+nl-NR_END));
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | void free_dvector(v,nl,nh)
|
---|
| 556 | double *v;
|
---|
| 557 | long nh,nl;
|
---|
| 558 | /* free a double vector allocated with dvector() */
|
---|
| 559 | {
|
---|
| 560 | free((FREE_ARG) (v+nl-NR_END));
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | void free_matrix(m,nrl,nrh,ncl,nch)
|
---|
| 564 | float **m;
|
---|
| 565 | long nch,ncl,nrh,nrl;
|
---|
| 566 | /* free a float matrix allocated by matrix() */
|
---|
| 567 | {
|
---|
| 568 | free((FREE_ARG) (m[nrl]+ncl-NR_END));
|
---|
| 569 | free((FREE_ARG) (m+nrl-NR_END));
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | void free_dmatrix(m,nrl,nrh,ncl,nch)
|
---|
| 573 | double **m;
|
---|
| 574 | long 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 |
|
---|
| 581 | void free_imatrix(m,nrl,nrh,ncl,nch)
|
---|
| 582 | int **m;
|
---|
| 583 | long 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 |
|
---|
| 590 | void free_submatrix(b,nrl,nrh,ncl,nch)
|
---|
| 591 | float **b;
|
---|
| 592 | long nch,ncl,nrh,nrl;
|
---|
| 593 | /* free a submatrix allocated by submatrix() */
|
---|
| 594 | {
|
---|
| 595 | free((FREE_ARG) (b+nrl-NR_END));
|
---|
| 596 | }
|
---|
| 597 |
|
---|
| 598 | void free_convert_matrix(b,nrl,nrh,ncl,nch)
|
---|
| 599 | float **b;
|
---|
| 600 | long nch,ncl,nrh,nrl;
|
---|
| 601 | /* free a matrix allocated by convert_matrix() */
|
---|
| 602 | {
|
---|
| 603 | free((FREE_ARG) (b+nrl-NR_END));
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | void free_f3tensor(t,nrl,nrh,ncl,nch,ndl,ndh)
|
---|
| 607 | float ***t;
|
---|
| 608 | long nch,ncl,ndh,ndl,nrh,nrl;
|
---|
| 609 | /* free a float f3tensor allocated by f3tensor() */
|
---|
| 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 */
|
---|