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

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/var.c,v 1.3 90/09/04 17:40:53 toy Exp $
  3.  * NAME
  4.  *    var - variance of the elements of a vector
  5.  *
  6.  * SYNOPSIS
  7.  *    var [-HB] [-P prec] [-c cols] [vectors...]
  8.  *
  9.  * DESCRIPTION
  10.  *    Output is the variance of the elements of the specified vector(s).
  11.  *
  12.  * BUGS
  13.  *    The entire vector is read into memory to compute the variance.
  14.  *    This is done for accuracy (but is it really necessary?).
  15.  *
  16.  * HISTORY
  17.  * $Log:    var.c,v $
  18.  * Revision 1.3  90/09/04  17:40:53  toy
  19.  * Set default precision from the environment variable STAT_PREC.
  20.  * Use print_help_strings to print out help.
  21.  *
  22.  * Revision 1.2  90/09/02  17:14:28  toy
  23.  * Use read_vector routine instead of do_read.
  24.  *
  25.  * Revision 1.1  90/09/02  15:44:55  toy
  26.  * Initial revision
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <math.h>
  32. #include <string.h>
  33. #include <assert.h>
  34.  
  35. #if    defined(__STDC__) || defined(__GNUC__)
  36. #include <stddef.h>
  37. #include <stdlib.h>
  38. #else
  39. #include <malloc.h>
  40. #endif
  41.  
  42. #include "gps.h"
  43.  
  44. #define    DEF_PROGNAME    "var"
  45.  
  46. static const char RCSID[] = "@(#) $Id: var.c,v 1.3 90/09/04 17:40:53 toy Exp $";
  47.  
  48. extern int getopt ();
  49. extern int optind;
  50. extern char *optarg;
  51.  
  52. const char *progname;
  53.  
  54. #define    OPT_STRING    "HBP:c:"
  55.  
  56. void
  57. help ()
  58. {
  59.   static const char *help_strings[] =
  60.   {
  61.     "\t-H\tThis help\n",
  62.     "\t-B\tUse unbiased variance estimate\n",
  63.     "\t-P p\tSet number of significant digits to p\n",
  64.     "\t-c c\tSet number of output elements per line to n\n",
  65.     "The variance of each vector is printed.\n",
  66.     NULL
  67.   };
  68.  
  69.   (void) fprintf (stderr, "%s\n", RCSID);
  70.   (void) fprintf (stderr, "Usage:  %s [-H] [-P prec] [-c cols] [vector ...]\n", progname);
  71.   print_help_strings (help_strings);
  72. }
  73.  
  74. #if    defined(__STDC__) || defined(__GNUC__)
  75. void
  76. do_var (double *vector, int rank, int biased)
  77. #else
  78. void
  79. do_var (vector, rank, biased)
  80.      double *vector;
  81.      int rank;
  82.      int biased;
  83. #endif
  84. {
  85.   int k;
  86.   double mean;
  87.   double var;
  88.   double *ptr;
  89.  
  90.   /*
  91.    * First compute the mean
  92.    */
  93.  
  94.   mean = 0;
  95.   ptr = vector;
  96.   for (k = 0; k < rank; k++)
  97.     {
  98.       mean += *ptr++;
  99.     }                /* endfor     */
  100.   mean /= (double) rank;
  101.  
  102.   /*
  103.    * Now compute variance
  104.    */
  105.  
  106.   var = 0;
  107.   ptr = vector;
  108.   for (k = 0; k < rank; k++)
  109.     {
  110.       double diff;
  111.       diff = *ptr++ - mean;
  112.       var += diff * diff;
  113.     }                /* endfor     */
  114.  
  115.   var /= (biased) ? rank : rank - 1;
  116.   print_number (stdout, var);
  117. }
  118.  
  119.  
  120.  
  121. int
  122. main (argc, argv)
  123.      int argc;
  124.      char *argv[];
  125. {
  126.   int option;
  127.   int errcnt;
  128.   int rank;
  129.   int biased;
  130.   double *vector;
  131.  
  132.   progname = get_my_name (argv[0], DEF_PROGNAME);
  133.   set_def_prec ();
  134.  
  135.   errcnt = 0;
  136.   biased = TRUE;
  137.   (void) set_max_columns (1);
  138.  
  139.   /*
  140.    * Read options
  141.    */
  142.  
  143.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  144.     {
  145.       switch (option)
  146.     {
  147.     case 'H':        /* Help                 */
  148.       help ();
  149.       exit (0);
  150.       break;
  151.     case 'P':        /* Precision             */
  152.       (void) set_precision (atoi (optarg));
  153.       break;
  154.     case 'c':        /* Number of output columns     */
  155.       (void) set_max_columns (atoi (optarg));
  156.       break;
  157.     case 'B':        /* Use unbiased estimate     */
  158.       biased = FALSE;
  159.       break;
  160.     default:
  161.       break;
  162.     }            /* endswitch     */
  163.     }                /* endwhile     */
  164.  
  165.   if (errcnt > 0)
  166.     {
  167.       exit (1);
  168.     }
  169.  
  170.   /*
  171.    * If no files are listed, use stdin.  Otherwise use
  172.    * the given files
  173.    */
  174.  
  175.   if (optind == argc)
  176.     {
  177.       vector = read_vector (stdin, &rank);
  178.       do_var (vector, rank, biased);
  179.       free (vector);
  180.     }
  181.   else
  182.     {
  183.       while (optind < argc)
  184.     {
  185.       if (freopen (argv[optind], "r", stdin) == NULL)
  186.         {
  187.           message ("Cannot open `%s'\n", argv[optind]);
  188.         }
  189.       else
  190.         {
  191.           vector = read_vector (stdin, &rank);
  192.           do_var (vector, rank, biased);
  193.           free (vector);
  194.         }
  195.       optind++;
  196.     }            /* endwhile     */
  197.     }                /* endif     */
  198.  
  199.   end_column (stdout);
  200.   return 0;
  201. }
  202.