wolffd@0: /* CAUTION: This is the ANSI C (only) version of the Numerical Recipes wolffd@0: utility file nrutil.c. Do not confuse this file with the same-named wolffd@0: file nrutil.c that is supplied in the 'misc' subdirectory. wolffd@0: *That* file is the one from the book, and contains both ANSI and wolffd@0: traditional K&R versions, along with #ifdef macros to select the wolffd@0: correct version. *This* file contains only ANSI C. */ wolffd@0: wolffd@0: #include wolffd@0: #include wolffd@0: #include wolffd@0: #define NR_END 1 wolffd@0: #define FREE_ARG char* wolffd@0: wolffd@0: void nrerror(char error_text[]) wolffd@0: /* Numerical Recipes standard error handler */ wolffd@0: { wolffd@0: fprintf(stderr,"Numerical Recipes run-time error...\n"); wolffd@0: fprintf(stderr,"%s\n",error_text); wolffd@0: fprintf(stderr,"...now exiting to system...\n"); wolffd@0: exit(1); wolffd@0: } wolffd@0: wolffd@0: float *vector(long nl, long nh) wolffd@0: /* allocate a float vector with subscript range v[nl..nh] */ wolffd@0: { wolffd@0: float *v; wolffd@0: wolffd@0: v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float))); wolffd@0: if (!v) nrerror("allocation failure in vector()"); wolffd@0: return v-nl+NR_END; wolffd@0: } wolffd@0: wolffd@0: int *ivector(long nl, long nh) wolffd@0: /* allocate an int vector with subscript range v[nl..nh] */ wolffd@0: { wolffd@0: int *v; wolffd@0: wolffd@0: v=(int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int))); wolffd@0: if (!v) nrerror("allocation failure in ivector()"); wolffd@0: return v-nl+NR_END; wolffd@0: } wolffd@0: wolffd@0: unsigned char *cvector(long nl, long nh) wolffd@0: /* allocate an unsigned char vector with subscript range v[nl..nh] */ wolffd@0: { wolffd@0: unsigned char *v; wolffd@0: wolffd@0: v=(unsigned char *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(unsigned char))); wolffd@0: if (!v) nrerror("allocation failure in cvector()"); wolffd@0: return v-nl+NR_END; wolffd@0: } wolffd@0: wolffd@0: unsigned long *lvector(long nl, long nh) wolffd@0: /* allocate an unsigned long vector with subscript range v[nl..nh] */ wolffd@0: { wolffd@0: unsigned long *v; wolffd@0: wolffd@0: v=(unsigned long *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(long))); wolffd@0: if (!v) nrerror("allocation failure in lvector()"); wolffd@0: return v-nl+NR_END; wolffd@0: } wolffd@0: wolffd@0: double *dvector(long nl, long nh) wolffd@0: /* allocate a double vector with subscript range v[nl..nh] */ wolffd@0: { wolffd@0: double *v; wolffd@0: wolffd@0: v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double))); wolffd@0: if (!v) nrerror("allocation failure in dvector()"); wolffd@0: return v-nl+NR_END; wolffd@0: } wolffd@0: wolffd@0: float **matrix(long nrl, long nrh, long ncl, long nch) wolffd@0: /* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */ wolffd@0: { wolffd@0: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1; wolffd@0: float **m; wolffd@0: wolffd@0: /* allocate pointers to rows */ wolffd@0: m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*))); wolffd@0: if (!m) nrerror("allocation failure 1 in matrix()"); wolffd@0: m += NR_END; wolffd@0: m -= nrl; wolffd@0: wolffd@0: /* allocate rows and set pointers to them */ wolffd@0: m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float))); wolffd@0: if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); wolffd@0: m[nrl] += NR_END; wolffd@0: m[nrl] -= ncl; wolffd@0: wolffd@0: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol; wolffd@0: wolffd@0: /* return pointer to array of pointers to rows */ wolffd@0: return m; wolffd@0: } wolffd@0: wolffd@0: double **dmatrix(long nrl, long nrh, long ncl, long nch) wolffd@0: /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */ wolffd@0: { wolffd@0: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1; wolffd@0: double **m; wolffd@0: wolffd@0: /* allocate pointers to rows */ wolffd@0: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*))); wolffd@0: if (!m) nrerror("allocation failure 1 in matrix()"); wolffd@0: m += NR_END; wolffd@0: m -= nrl; wolffd@0: wolffd@0: /* allocate rows and set pointers to them */ wolffd@0: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double))); wolffd@0: if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); wolffd@0: m[nrl] += NR_END; wolffd@0: m[nrl] -= ncl; wolffd@0: wolffd@0: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol; wolffd@0: wolffd@0: /* return pointer to array of pointers to rows */ wolffd@0: return m; wolffd@0: } wolffd@0: wolffd@0: int **imatrix(long nrl, long nrh, long ncl, long nch) wolffd@0: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */ wolffd@0: { wolffd@0: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1; wolffd@0: int **m; wolffd@0: wolffd@0: /* allocate pointers to rows */ wolffd@0: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*))); wolffd@0: if (!m) nrerror("allocation failure 1 in matrix()"); wolffd@0: m += NR_END; wolffd@0: m -= nrl; wolffd@0: wolffd@0: wolffd@0: /* allocate rows and set pointers to them */ wolffd@0: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int))); wolffd@0: if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); wolffd@0: m[nrl] += NR_END; wolffd@0: m[nrl] -= ncl; wolffd@0: wolffd@0: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol; wolffd@0: wolffd@0: /* return pointer to array of pointers to rows */ wolffd@0: return m; wolffd@0: } wolffd@0: wolffd@0: float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch, wolffd@0: long newrl, long newcl) wolffd@0: /* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */ wolffd@0: { wolffd@0: long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl; wolffd@0: float **m; wolffd@0: wolffd@0: /* allocate array of pointers to rows */ wolffd@0: m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*))); wolffd@0: if (!m) nrerror("allocation failure in submatrix()"); wolffd@0: m += NR_END; wolffd@0: m -= newrl; wolffd@0: wolffd@0: /* set pointers to rows */ wolffd@0: for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol; wolffd@0: wolffd@0: /* return pointer to array of pointers to rows */ wolffd@0: return m; wolffd@0: } wolffd@0: wolffd@0: float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch) wolffd@0: /* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix wolffd@0: declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1 wolffd@0: and ncol=nch-ncl+1. The routine should be called with the address wolffd@0: &a[0][0] as the first argument. */ wolffd@0: { wolffd@0: long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1; wolffd@0: float **m; wolffd@0: wolffd@0: /* allocate pointers to rows */ wolffd@0: m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*))); wolffd@0: if (!m) nrerror("allocation failure in convert_matrix()"); wolffd@0: m += NR_END; wolffd@0: m -= nrl; wolffd@0: wolffd@0: /* set pointers to rows */ wolffd@0: m[nrl]=a-ncl; wolffd@0: for(i=1,j=nrl+1;i