home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / graphics-0.17 / dist-stat / mean.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-13  |  5.1 KB  |  245 lines

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/mean.c,v 1.4 90/09/10 12:22:11 toy Exp $
  3.  * NAME
  4.  *    mean - mean of a vector
  5.  *
  6.  * SYNOPSIS
  7.  *    mean [-H] [-P prec] [-c cols] [vectors...]
  8.  *
  9.  * DESCRIPTION
  10.  *    Output is the possibly trimmed mean of the elements of
  11.  *    the specified vector(s).
  12.  *
  13.  * HISTORY
  14.  * $Log:    mean.c,v $
  15.  * Revision 1.4  90/09/10  12:22:11  toy
  16.  * Forgot to print out help strings.
  17.  * Fixed error in function declaration when not using
  18.  * ANSI or GNU C.
  19.  *
  20.  * Revision 1.3  90/09/04  17:40:34  toy
  21.  * Set default precision from the environment variable STAT_PREC.
  22.  * Use print_help_strings to print out help.
  23.  *
  24.  * Revision 1.2  90/09/02  17:14:04  toy
  25.  * Use read_vector routine instead of do_read.
  26.  *
  27.  * Revision 1.1  90/09/01  16:53:35  toy
  28.  * Initial revision
  29.  *
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include <math.h>
  34. #include <string.h>
  35. #include <assert.h>
  36.  
  37. #if    defined(__STDC__) || defined(__GNUC__)
  38. #include <stddef.h>
  39. #include <stdlib.h>
  40. #else
  41. #include <malloc.h>
  42. #endif
  43.  
  44. #include "gps.h"
  45.  
  46. #define    DEF_PROGNAME    "mean"
  47.  
  48. static const char RCSID[] = "@(#) $Id: mean.c,v 1.4 90/09/10 12:22:11 toy Exp $";
  49.  
  50. extern int getopt ();
  51. extern int optind;
  52. extern char *optarg;
  53.  
  54. const char *progname;
  55.  
  56. #define    OPT_STRING    "HP:c:f:p:n:"
  57.  
  58. void
  59. help ()
  60. {
  61.   static const char *help_strings[] =
  62.   {
  63.     "\t-H\tThis help\n",
  64.     "\t-P p\tSet number of significant digits to p.  If not specified\n",
  65.     "\t\tuse value of STAT_PREC from environment, if available.  Else\n",
  66.     "\t\tuse default.\n",
  67.     "\t-c c\tSet number of output elements per line to n\n",
  68.     "\t-f f\tTrim (1/f)*r elements from each end, where r\n",
  69.     "\t\tis the rank of the vector\n",
  70.     "\t-p p\tTrim p*r elements, where 0 <= p < 0.5\n",
  71.     "\t-n n\tTrim n elements\n",
  72.     "Compute the mean of the given vector.  Elements may be optionally\n",
  73.     "trimmed from each end before computing the mean.\n",
  74.     "\n",
  75.     "A separate mean is printed for each specified vector\n",
  76.     NULL
  77.   };
  78.  
  79.   (void) fprintf (stderr, "%s\n", RCSID);
  80.   (void) fprintf (stderr, "Usage:  %s [-H] [-P prec] [-c cols] [vector ...]\n", progname);
  81.   print_help_strings (help_strings);
  82. }
  83.  
  84. #if    defined(__STDC__) || defined(__GNUC__)
  85. void
  86. do_mean (double *vector, int rank, int ntrim)
  87. #else
  88. void
  89. do_mean (vector, rank, ntrim)
  90.      double *vector;
  91.      int rank;
  92.      int ntrim;
  93. #endif
  94. {
  95.   int k;
  96.   double sum;
  97.  
  98.   /*
  99.    * Trim off the appropriate number of elements from
  100.    * both ends of the vector
  101.    */
  102.  
  103.   rank -= 2 * ntrim;
  104.   vector += ntrim;
  105.  
  106.   if (rank > 0)
  107.     {
  108.       sum = 0;
  109.       for (k = 0; k < rank; k++)
  110.     {
  111.       sum += *vector++;
  112.     }
  113.       print_number (stdout, sum / rank);
  114.     }                /* endif     */
  115. }
  116.  
  117.  
  118.  
  119. int
  120. main (argc, argv)
  121.      int argc;
  122.      char *argv[];
  123. {
  124.   int option;
  125.   int errcnt;
  126.   int got_f_opt;
  127.   int got_p_opt;
  128.   int got_n_opt;
  129.   int rank;
  130.   int trim_number;
  131.   double trim_frac;
  132.   double trim_percent;
  133.   double *vector;
  134.  
  135.   progname = get_my_name (argv[0], DEF_PROGNAME);
  136.   set_def_prec ();
  137.  
  138.   /*
  139.    * Read options
  140.    */
  141.  
  142.   errcnt = 0;
  143.   got_f_opt = got_p_opt = got_n_opt = FALSE;
  144.   trim_number = 0;
  145.  
  146.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  147.     {
  148.       switch (option)
  149.     {
  150.     case 'H':        /* Help                 */
  151.       help ();
  152.       exit (0);
  153.       break;
  154.     case 'P':        /* Precision             */
  155.       (void) set_precision (atoi (optarg));
  156.       break;
  157.     case 'c':        /* Number of output columsn     */
  158.       (void) set_max_columns (atoi (optarg));
  159.       break;
  160.     case 'f':        /* Fraction to trim         */
  161.       got_f_opt = TRUE;
  162.       trim_frac = atof (optarg);
  163.       break;
  164.     case 'p':        /* Percentage to trim         */
  165.       got_p_opt = TRUE;
  166.       trim_percent = atof (optarg);
  167.       break;
  168.     case 'n':        /* Number to trim         */
  169.       got_n_opt = TRUE;
  170.       trim_number = atoi (optarg);
  171.       break;
  172.     default:
  173.       break;
  174.     }            /* endswitch     */
  175.     }                /* endwhile     */
  176.  
  177.   if ((got_f_opt && got_p_opt) || (got_f_opt && got_n_opt) || (got_p_opt && got_n_opt))
  178.     {
  179.       message ("-f, -p, and -n options are mutually exclusive\n");
  180.       errcnt++;
  181.     }
  182.   if (got_f_opt && (trim_frac <= 1))
  183.     {
  184.       message ("trim fraction must be greater than 1\n");
  185.       errcnt++;
  186.     }
  187.   if (got_p_opt && ((trim_percent <= 0) || (trim_percent >= 0.5)))
  188.     {
  189.       message ("trim percentage must be between 0 and .5\n");
  190.       errcnt++;
  191.     }
  192.   if (errcnt > 0)
  193.     {
  194.       exit (1);
  195.     }
  196.  
  197.   /*
  198.    * If no files are listed, use stdin.  Otherwise use
  199.    * the given files
  200.    */
  201.  
  202.   if (optind == argc)
  203.     {
  204.       vector = read_vector (stdin, &rank);
  205.       if (got_f_opt)
  206.     {
  207.       trim_number = rank / trim_frac + 0.5;
  208.     }
  209.       else if (got_p_opt)
  210.     {
  211.       trim_number = rank * trim_percent + 0.5;
  212.     }
  213.       do_mean (vector, rank, trim_number);
  214.       free (vector);
  215.     }
  216.   else
  217.     {
  218.       while (optind < argc)
  219.     {
  220.       if (freopen (argv[optind], "r", stdin) == NULL)
  221.         {
  222.           message ("Cannot open `%s'\n", argv[optind]);
  223.         }
  224.       else
  225.         {
  226.           vector = read_vector (stdin, &rank);
  227.           if (got_f_opt)
  228.         {
  229.           trim_number = rank / trim_frac + 0.5;
  230.         }
  231.           else if (got_p_opt)
  232.         {
  233.           trim_number = rank * trim_percent + 0.5;
  234.         }
  235.           do_mean (vector, rank, trim_number);
  236.           free (vector);
  237.         }
  238.       optind++;
  239.     }            /* endwhile     */
  240.     }                /* endif     */
  241.  
  242.   end_column (stdout);
  243.   return 0;
  244. }
  245.