home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / BarlaA / sw / OLD / Simo / SVM_mono / nrutil.c < prev    next >
C/C++ Source or Header  |  2002-06-25  |  10KB  |  327 lines

  1. /* CAUTION: This is the ANSI C (only) version of the Numerical Recipes
  2.    utility file nrutil.c.  Do not confuse this file with the same-named
  3.    file nrutil.c that is supplied in the same subdirectory or archive
  4.    as the header file nrutil.h.  *That* file contains both ANSI and
  5.    traditional K&R versions, along with #ifdef macros to select the
  6.    correct version.  *This* file contains only ANSI C.               */
  7.  
  8. #include <stdio.h>
  9. #include <stddef.h>
  10. #include <stdlib.h>
  11. #define NR_END 1
  12. #define FREE_ARG char*
  13.  
  14. void nrerror(char error_text[])
  15. /* Numerical Recipes standard error handler */
  16. {
  17.     fprintf(stderr,"Numerical Recipes run-time error...\n");
  18.     fprintf(stderr,"%s\n",error_text);
  19.     fprintf(stderr,"...now exiting to system...\n");
  20.     exit(1);
  21. }
  22.  
  23. float *vector(long nl, long nh)
  24. /* allocate a float vector with subscript range v[nl..nh] */
  25. {
  26.     float *v;
  27.  
  28.     v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
  29.     if (!v) nrerror("allocation failure in vector()");
  30.     return v-nl+NR_END;
  31. }
  32.  
  33. int *ivector(long nl, long nh)
  34. /* allocate an int vector with subscript range v[nl..nh] */
  35. {
  36.     int *v;
  37.  
  38.     v=(int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int)));
  39.     if (!v) nrerror("allocation failure in ivector()");
  40.     return v-nl+NR_END;
  41. }
  42.  
  43. unsigned char *cvector(long nl, long nh)
  44. /* allocate an unsigned char vector with subscript range v[nl..nh] */
  45. {
  46.     unsigned char *v;
  47.  
  48.     v=(unsigned char *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(unsigned char)));
  49.     if (!v) nrerror("allocation failure in cvector()");
  50.     return v-nl+NR_END;
  51. }
  52.  
  53. unsigned long *lvector(long nl, long nh)
  54. /* allocate an unsigned long vector with subscript range v[nl..nh] */
  55. {
  56.     unsigned long *v;
  57.  
  58.     v=(unsigned long *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(long)));
  59.     if (!v) nrerror("allocation failure in lvector()");
  60.     return v-nl+NR_END;
  61. }
  62.  
  63. double *dvector(long nl, long nh)
  64. /* allocate a double vector with subscript range v[nl..nh] */
  65. {
  66.     double *v;
  67.  
  68.     v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
  69.     if (!v) nrerror("allocation failure in dvector()");
  70.     return v-nl+NR_END;
  71. }
  72.  
  73. float **matrix(long nrl, long nrh, long ncl, long nch)
  74. /* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
  75. {
  76.     long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
  77.     float **m;
  78.  
  79.     /* allocate pointers to rows */
  80.     m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
  81.     if (!m) nrerror("allocation failure 1 in matrix()");
  82.     m += NR_END;
  83.     m -= nrl;
  84.  
  85.     /* allocate rows and set pointers to them */
  86.     m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
  87.     if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
  88.     m[nrl] += NR_END;
  89.     m[nrl] -= ncl;
  90.  
  91.     for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
  92.  
  93.     /* return pointer to array of pointers to rows */
  94.     return m;
  95. }
  96.  
  97. double **dmatrix(long nrl, long nrh, long ncl, long nch)
  98. /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
  99. {
  100.     long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
  101.     double **m;
  102.  
  103.     /* allocate pointers to rows */
  104.     m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
  105.     if (!m) nrerror("allocation failure 1 in matrix()");
  106.     m += NR_END;
  107.     m -= nrl;
  108.  
  109.     /* allocate rows and set pointers to them */
  110.     m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
  111.     if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
  112.     m[nrl] += NR_END;
  113.     m[nrl] -= ncl;
  114.  
  115.     for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
  116.  
  117.     /* return pointer to array of pointers to rows */
  118.     return m;
  119. }
  120.  
  121. int **imatrix(long nrl, long nrh, long ncl, long nch)
  122. /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
  123. {
  124.     long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
  125.     int **m;
  126.  
  127.     /* allocate pointers to rows */
  128.     m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
  129.     if (!m) nrerror("allocation failure 1 in matrix()");
  130.     m += NR_END;
  131.     m -= nrl;
  132.  
  133.  
  134.     /* allocate rows and set pointers to them */
  135.     m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
  136.     if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
  137.     m[nrl] += NR_END;
  138.     m[nrl] -= ncl;
  139.  
  140.     for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
  141.  
  142.     /* return pointer to array of pointers to rows */
  143.     return m;
  144. }
  145.  
  146. unsigned char **cmatrix(long nrl, long nrh, long ncl, long nch)
  147. /* allocate an unsigned char matrix with subscript range m[nrl..nrh][ncl..nch] */
  148. {
  149.     long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
  150.     unsigned char **m;
  151.  
  152.     /* allocate pointers to rows */
  153.     m=(unsigned char **) malloc((size_t)((nrow+NR_END)*sizeof(unsigned char*)));
  154.     if (!m) nrerror("allocation failure 1 in cmatrix()");
  155.     m += NR_END;
  156.     m -= nrl;
  157.  
  158.  
  159.     /* allocate rows and set pointers to them */
  160.     m[nrl]=(unsigned char *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(unsigned char)));
  161.     if (!m[nrl]) nrerror("allocation failure 2 in cmatrix()");
  162.     m[nrl] += NR_END;
  163.     m[nrl] -= ncl;
  164.  
  165.     for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
  166.  
  167.     /* return pointer to array of pointers to rows */
  168.     return m;
  169. }
  170.  
  171. float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
  172.     long newrl, long newcl)
  173. /* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
  174. {
  175.     long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
  176.     float **m;
  177.  
  178.     /* allocate array of pointers to rows */
  179.     m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
  180.     if (!m) nrerror("allocation failure in submatrix()");
  181.     m += NR_END;
  182.     m -= newrl;
  183.  
  184.     /* set pointers to rows */
  185.     for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
  186.  
  187.     /* return pointer to array of pointers to rows */
  188.     return m;
  189. }
  190.  
  191. float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch)
  192. /* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
  193. declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
  194. and ncol=nch-ncl+1. The routine should be called with the address
  195. &a[0][0] as the first argument. */
  196. {
  197.     long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
  198.     float **m;
  199.  
  200.     /* allocate pointers to rows */
  201.     m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
  202.     if (!m) nrerror("allocation failure in convert_matrix()");
  203.     m += NR_END;
  204.     m -= nrl;
  205.  
  206.     /* set pointers to rows */
  207.     m[nrl]=a-ncl;
  208.     for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
  209.     /* return pointer to array of pointers to rows */
  210.     return m;
  211. }
  212.  
  213. unsigned char ***c3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
  214. /* allocate an unsigned char 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
  215. {
  216.     long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
  217.     unsigned char ***t;
  218.  
  219.     /* allocate pointers to pointers to rows */
  220.     t=(unsigned char ***) malloc((size_t)((nrow+NR_END)*sizeof(unsigned char**)));
  221.     if (!t) nrerror("allocation failure 1 in c3tensor()");
  222.     t += NR_END;
  223.     t -= nrl;
  224.  
  225.     /* allocate pointers to rows and set pointers to them */
  226.     t[nrl]=(unsigned char **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(unsigned char*)));
  227.     if (!t[nrl]) nrerror("allocation failure 2 in c3tensor()");
  228.     t[nrl] += NR_END;
  229.     t[nrl] -= ncl;
  230.  
  231.     /* allocate rows and set pointers to them */
  232.     t[nrl][ncl]=(unsigned char *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(unsigned char)));
  233.     if (!t[nrl][ncl]) nrerror("allocation failure 3 in c3tensor()");
  234.     t[nrl][ncl] += NR_END;
  235.     t[nrl][ncl] -= ndl;
  236.  
  237.     for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
  238.     for(i=nrl+1;i<=nrh;i++) {
  239.         t[i]=t[i-1]+ncol;
  240.         t[i][ncl]=t[i-1][ncl]+ncol*ndep;
  241.         for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
  242.     }
  243.  
  244.     /* return pointer to array of pointers to rows */
  245.     return t;
  246. }
  247.  
  248. void free_vector(float *v, long nl, long nh)
  249. /* free a float vector allocated with vector() */
  250. {
  251.     free((FREE_ARG) (v+nl-NR_END));
  252. }
  253.  
  254. void free_ivector(int *v, long nl, long nh)
  255. /* free an int vector allocated with ivector() */
  256. {
  257.     free((FREE_ARG) (v+nl-NR_END));
  258. }
  259.  
  260. void free_cvector(unsigned char *v, long nl, long nh)
  261. /* free an unsigned char vector allocated with cvector() */
  262. {
  263.     free((FREE_ARG) (v+nl-NR_END));
  264. }
  265.  
  266. void free_lvector(unsigned long *v, long nl, long nh)
  267. /* free an unsigned long vector allocated with lvector() */
  268. {
  269.     free((FREE_ARG) (v+nl-NR_END));
  270. }
  271.  
  272. void free_dvector(double *v, long nl, long nh)
  273. /* free a double vector allocated with dvector() */
  274. {
  275.     free((FREE_ARG) (v+nl-NR_END));
  276. }
  277.  
  278. void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
  279. /* free a float matrix allocated by matrix() */
  280. {
  281.     free((FREE_ARG) (m[nrl]+ncl-NR_END));
  282.     free((FREE_ARG) (m+nrl-NR_END));
  283. }
  284.  
  285. void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
  286. /* free a double matrix allocated by dmatrix() */
  287. {
  288.     free((FREE_ARG) (m[nrl]+ncl-NR_END));
  289.     free((FREE_ARG) (m+nrl-NR_END));
  290. }
  291.  
  292. void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
  293. /* free an int matrix allocated by imatrix() */
  294. {
  295.     free((FREE_ARG) (m[nrl]+ncl-NR_END));
  296.     free((FREE_ARG) (m+nrl-NR_END));
  297. }
  298.  
  299. void free_cmatrix(unsigned char **m, long nrl, long nrh, long ncl, long nch)
  300. /* free an unsigned char matrix allocated by cmatrix() */
  301. {
  302.     free((FREE_ARG) (m[nrl]+ncl-NR_END));
  303.     free((FREE_ARG) (m+nrl-NR_END));
  304. }
  305.  
  306. void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch)
  307. /* free a submatrix allocated by submatrix() */
  308. {
  309.     free((FREE_ARG) (b+nrl-NR_END));
  310. }
  311.  
  312. void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch)
  313. /* free a matrix allocated by convert_matrix() */
  314. {
  315.     free((FREE_ARG) (b+nrl-NR_END));
  316. }
  317.  
  318. void free_c3tensor(unsigned char ***t, long nrl, long nrh, long ncl, long nch,
  319.     long ndl, long ndh)
  320. /* free an unsigned char c3tensor allocated by c3tensor() */
  321. {
  322.     free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
  323.     free((FREE_ARG) (t[nrl]+ncl-NR_END));
  324.     free((FREE_ARG) (t+nrl-NR_END));
  325. }
  326. /* (C) Copr. 1986-92 Numerical Recipes Software ].5J425##1-4#B%(|#5+. */
  327.