/* The functions in this program allow to allocate dynamically vectors and matrices. */ #include #include /* The function *dvector() allocates a vector of doubles with subscript range v[nl...nh]. In practical applications we use nl = 1. */ double *dvector(int nl, int nh) { double *v; v = (double *) malloc( (size_t) ((nh - nl + 1 + 1) * sizeof(double)) ); if (!v) fprintf(stderr, "\n allocation failure in dvector()" ); return v - nl + 1; } /* The function free_dvector() frees a vector of doubles allocated by dvector(). */ void free_dvector(double *v, int nl, int nh) { free( (FREE_ARG) (v + nl - 1) ); } /* The function *ivector() allocates a vector of integers with subscript range v[nl...nh]. In practical applications we use nl = 1. */ int *ivector(int nl, int nh) { int *v; v = (int *) malloc( (size_t) ((nh - nl + 1 + 1) * sizeof(int)) ); if (!v) fprintf(stderr, "\n allocation failure in ivector()" ); return v - nl + 1; } /* The function free_ivector() frees a vector of integers allocated by ivector(). */ void free_ivector(int *v, int nl, int nh) { free( (FREE_ARG) (v + nl - 1) ); } /* The function **dmatrix() allocates a double matrix with subscript range m[nrl...nrh][ncl...nch]. In practical applications we use nrl = ncl = 1. */ double **dmatrix(int nrl, int nrh, int ncl, int nch) { int i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1; double **m; /* allocate pointers to rows */ m = (double **) malloc( (size_t) ((nrow + 1) * sizeof(double*)) ); if (!m) fprintf(stderr, "\n allocation failure 1 in matrix()"); m += 1; m -= nrl; /* allocate rows and set pointers to them */ m[nrl] = (double *) malloc( (size_t) ((nrow * ncol + 1 ) * sizeof(double)) ); if (!m[nrl]) fprintf(stderr, "\n allocation failure 2 in matrix()"); m[nrl] += 1; m[nrl] -= ncl; for(i = nrl + 1; i <= nrh; i++ ) m[i] = m[i - 1] + ncol; /* return pointer to array of pointers to rows */ return m; } /* The function free_dmatrix() frees a double matrix allocated by dmatrix(). */ void free_dmatrix(double **m, int nrl, int nrh, int ncl, int nch) { free( (FREE_ARG) (m[nrl] + ncl - 1) ); free( (FREE_ARG) (m + nrl - 1) ); } /* The function **dcomp_matrix() allocates a dcomplex matrix with subscript range m[nrl...nrh][ncl...nch]. In practical applications we use nrl = ncl = 1. */ dcomplex **dcomp_matrix(int nrl, int nrh, int ncl, int nch) { int i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1; dcomplex **m; /* allocate pointers to rows */ m = (dcomplex **) malloc( (size_t) ((nrow + 1) * sizeof(dcomplex*)) ); if (!m) fprintf(stderr, "\n allocation failure 1 in matrix()"); m += 1; m -= nrl; /* allocate rows and set pointers to them */ m[nrl] = (dcomplex *) malloc( (size_t) ((nrow * ncol + 1 ) * sizeof(dcomplex)) ); if (!m[nrl]) fprintf(stderr, "\n allocation failure 2 in matrix()"); m[nrl] += 1; m[nrl] -= ncl; for(i = nrl + 1; i <= nrh; i++ ) m[i] = m[i - 1] + ncol; /* return pointer to array of pointers to rows */ return m; } /* The function free_dcomp_matrix() frees a dcomplex matrix allocated by dcomp_matrix(). */ void free_dcomp_matrix(dcomplex **m, int nrl, int nrh, int ncl, int nch) { free( (FREE_ARG) (m[nrl] + ncl - 1) ); free( (FREE_ARG) (m + nrl - 1) ); }