C   27
NR utilities
Guest on 14th March 2023 12:04:44 AM


  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define NR_END 1
  5. #define FREE_ARG char*
  6.  
  7. void nrerror(char error_text[]);
  8.  
  9. double **dmatrix(long nrl, long nrh, long ncl, long nch)
  10. /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
  11. {
  12.         long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
  13.         double **m;
  14.  
  15.         /* allocate pointers to rows */
  16.         m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
  17.         if (!m) nrerror("allocation failure 1 in matrix()");
  18.         m += NR_END;
  19.         m -= nrl;
  20.  
  21.         /* allocate rows and set pointers to them */
  22.         m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
  23.         if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
  24.         m[nrl] += NR_END;
  25.         m[nrl] -= ncl;
  26.  
  27.         for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
  28.  
  29.         /* return pointer to array of pointers to rows */
  30.         return m;
  31. }
  32.  
  33.  
  34.  
  35. int **imatrix(long nrl, long nrh, long ncl, long nch)
  36. /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
  37. {
  38.         long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
  39.         int **m;
  40.  
  41.         /* allocate pointers to rows */
  42.         m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
  43.         if (!m) nrerror("allocation failure 1 in matrix()");
  44.         m += NR_END;
  45.         m -= nrl;
  46.  
  47.  
  48.         /* allocate rows and set pointers to them */
  49.         m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
  50.         if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
  51.         m[nrl] += NR_END;
  52.         m[nrl] -= ncl;
  53.  
  54.         for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
  55.  
  56.         /* return pointer to array of pointers to rows */
  57.         return m;
  58. }
  59.  
  60.  
  61. void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
  62. /* free a double matrix allocated by dmatrix() */
  63. {
  64.         free((FREE_ARG) (m[nrl]+ncl-NR_END));
  65.         free((FREE_ARG) (m+nrl-NR_END));
  66. }
  67.  
  68.  
  69.  
  70. void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
  71. /* free an int matrix allocated by imatrix() */
  72. {
  73.         free((FREE_ARG) (m[nrl]+ncl-NR_END));
  74.         free((FREE_ARG) (m+nrl-NR_END));
  75. }
  76.  
  77.  
  78. void nrerror(char error_text[])
  79. /* Numerical Recipes standard error handler */
  80. {
  81.         fprintf(stderr,"Numerical Recipes run-time error...\n");
  82.         fprintf(stderr,"%s\n",error_text);
  83.         fprintf(stderr,"...now exiting to system...\n");
  84.         exit(1);
  85. }

Raw Paste

Login or Register to edit or fork this paste. It's free.