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