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

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/math.c,v 1.5 90/09/11 17:00:14 toy Exp $
  3.  * NAME
  4.  *    abs, ceil, floor, exp, cos, sin, tan, asin, acos, atan, gamma
  5.  *        - math function applied to the elements of a vector
  6.  *
  7.  * SYNOPSIS
  8.  *    abs [-H] [-P prec] [-c cols] [vectors...]
  9.  *    ceil [-H] [-P prec] [-c cols] [vectors...]
  10.  *    floor [-H] [-P prec] [-c cols] [vectors...]
  11.  *    exp [-H] [-P prec] [-c cols] [vectors...]
  12.  *    cos [-H] [-P prec] [-c cols] [vectors...]
  13.  *    sin [-H] [-P prec] [-c cols] [vectors...]
  14.  *    tan [-H] [-P prec] [-c cols] [vectors...]
  15.  *    asin [-H] [-P prec] [-c cols] [vectors...]
  16.  *    acos [-H] [-P prec] [-c cols] [vectors...]
  17.  *    atan [-H] [-P prec] [-c cols] [vectors...]
  18.  *    gamma [-H] [-P prec] [-c cols] [vectors...]
  19.  *
  20.  * DESCRIPTION
  21.  *    Output is the appropriate math function of the elements of
  22.  *    the specified vector(s).
  23.  *
  24.  * HISTORY
  25.  * $Log:    math.c,v $
  26.  * Revision 1.5  90/09/11  17:00:14  toy
  27.  * Forget to set the default precision.
  28.  * Error messages should print the vector name.
  29.  *
  30.  * Revision 1.4  90/09/04  17:40:32  toy
  31.  * Set default precision from the environment variable STAT_PREC.
  32.  * Use print_help_strings to print out help.
  33.  *
  34.  * Revision 1.3  90/09/01  17:42:16  toy
  35.  * Use default name if we have an unknown program name.
  36.  *
  37.  * Revision 1.2  90/09/01  17:01:58  toy
  38.  * Use message function to print out messages.
  39.  *
  40.  * Revision 1.1  90/09/01  14:00:45  toy
  41.  * Initial revision
  42.  *
  43.  */
  44.  
  45. #include <stdio.h>
  46. #include <math.h>
  47. #include <string.h>
  48.  
  49. #if    defined(__STDC__) || defined(__GNUC__)
  50. #include <stddef.h>
  51. #include <stdlib.h>
  52. #endif
  53.  
  54. #include "gps.h"
  55.  
  56. #define    DEF_PROGNAME    "[abs|ceil|floor|exp|trig|gamma]"
  57.  
  58. /*
  59.  * Figure out what the name of the gamma (really log gamma)
  60.  * function is.  Sun likes lgamma, but System V uses gamma.
  61.  */
  62.  
  63. #if    defined(sun) || defined(USE_LGAMMA)
  64. #define    GAMMA    lgamma
  65. #elif    defined(SYSV) || defined(USG)
  66. #define    GAMMA    gamma
  67. #else /* The default is gamma     */
  68. #define    GAMMA    gamma
  69. #endif
  70.  
  71. static const char RCSID[] = "@(#) $Id: math.c,v 1.5 90/09/11 17:00:14 toy Exp $";
  72.  
  73. extern int getopt ();
  74. extern int optind;
  75. extern char *optarg;
  76.  
  77. const char *progname;
  78. const char *vector_name;
  79.  
  80. double (*function) PARMS ((double));
  81.  
  82. #define    OPT_STRING    "HP:c:"
  83.  
  84. #if    defined(__STDC__) || defined(__GNUC__)
  85. void
  86. help (void)
  87. #else
  88. void
  89. help ()
  90. #endif
  91. {
  92.   static const char *help_strings[] =
  93.   {
  94.     "\t-H\tThis help\n",
  95.     "\t-P p\tSet number of significant digits to p.  If not specified\n",
  96.     "\t\tuse value of STAT_PREC from environment, if available.  Else\n",
  97.     "\t\tuse default.\n",
  98.     "\t-c c\tSet number of output elements per line to n\n",
  99.     NULL
  100.   };
  101.   (void) fprintf (stderr, "%s\n", RCSID);
  102.   (void) fprintf (stderr, "Usage:  %s [-H] [-P prec] [-c cols] [vector ...]\n", progname);
  103.   print_help_strings (help_strings);
  104.   (void) fprintf (stderr, "Apply the function `%s' to the specified vectors, or stdin.\n", progname);
  105. }
  106.  
  107.  
  108. #if    defined(__STDC__) || defined(__GNUC__)
  109. double
  110. identity (double x)
  111. #else
  112. double
  113. identity (x)
  114.      double x;
  115. #endif
  116. {
  117.   return x;
  118. }
  119.  
  120.  
  121. #if    defined(__STDC__) || defined(__GNUC__)
  122. double
  123. Gamma (double x)
  124. #else
  125. double
  126. Gamma (x)
  127.      double x;
  128. #endif
  129. {
  130.   double lg;
  131.   extern int signgam;
  132.  
  133.   lg = GAMMA (x);
  134.  
  135.   return signgam * exp (lg);
  136. }
  137.  
  138.  
  139. #if    defined(__STDC__) || defined(__GNUC__)
  140. void
  141. do_func (FILE * fp)
  142. #else
  143. void
  144. do_func (fp)
  145.      FILE *fp;
  146. #endif
  147. {
  148.   int rc;
  149.   int cnt;
  150.   double x;
  151.  
  152.   cnt = 0;
  153.   do
  154.     {
  155.       cnt++;
  156.       rc = get_number (fp, &x);
  157.       if (rc == 1)
  158.     {
  159.       print_number (stdout, (*function) (x));
  160.     }
  161.       else if (rc == 0)
  162.     {
  163.       message ("Error reading number (element %d) from file `%s'.\n",
  164.            cnt, vector_name);
  165.     }
  166.   } while (rc == 1);
  167. }
  168.  
  169.  
  170. int
  171. main (argc, argv)
  172.      int argc;
  173.      char *argv[];
  174. {
  175.   int option;
  176.   double total;
  177.  
  178.   progname = get_my_name (argv[0], DEF_PROGNAME);
  179.   set_def_prec ();
  180.  
  181.   /*
  182.    * From the program name figure out what function we
  183.    * want to run
  184.    */
  185.  
  186.   if (strcmp (progname, "abs") == 0)
  187.     {
  188.       function = fabs;
  189.     }
  190.   else if (strcmp (progname, "ceil") == 0)
  191.     {
  192.       function = ceil;
  193.     }
  194.   else if (strcmp (progname, "floor") == 0)
  195.     {
  196.       function = floor;
  197.     }
  198.   else if (strcmp (progname, "exp") == 0)
  199.     {
  200.       function = exp;
  201.     }
  202.   else if (strcmp (progname, "cos") == 0)
  203.     {
  204.       function = cos;
  205.     }
  206.   else if (strcmp (progname, "sin") == 0)
  207.     {
  208.       function = sin;
  209.     }
  210.   else if (strcmp (progname, "tan") == 0)
  211.     {
  212.       function = tan;
  213.     }
  214.   else if (strcmp (progname, "asin") == 0)
  215.     {
  216.       function = asin;
  217.     }
  218.   else if (strcmp (progname, "acos") == 0)
  219.     {
  220.       function = acos;
  221.     }
  222.   else if (strcmp (progname, "atan") == 0)
  223.     {
  224.       function = atan;
  225.     }
  226.   else if (strcmp (progname, "gamma") == 0)
  227.     {
  228.       function = Gamma;
  229.     }
  230.   else
  231.     {
  232.       message ("cannot determine desired function from program name\n\tUsing default identity function\n");
  233.       function = identity;
  234.       progname = "identity";
  235.     }
  236.  
  237.  
  238.   /*
  239.    * Read options
  240.    */
  241.  
  242.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  243.     {
  244.       switch (option)
  245.     {
  246.     case 'H':        /* Help                 */
  247.       help ();
  248.       exit (0);
  249.       break;
  250.     case 'P':        /* Precision             */
  251.       (void) set_precision (atoi (optarg));
  252.       break;
  253.     case 'c':        /* Number of columns         */
  254.       (void) set_max_columns (atoi (optarg));
  255.       break;
  256.     default:
  257.       break;
  258.     }            /* endswitch     */
  259.     }                /* endwhile     */
  260.  
  261.   /*
  262.    * If no files are listed, use stdin.  Otherwise use
  263.    * the given files
  264.    */
  265.  
  266.   if (optind == argc)
  267.     {
  268.       vector_name = "standard input";
  269.       do_func (stdin);
  270.     }
  271.   else
  272.     {
  273.       total = 0;
  274.       while (optind < argc)
  275.     {
  276.       if (freopen (argv[optind], "r", stdin) == NULL)
  277.         {
  278.           message ("Cannot open `%s'\n", argv[optind]);
  279.         }
  280.       else
  281.         {
  282.           vector_name = argv[optind];
  283.           do_func (stdin);
  284.         }
  285.       optind++;
  286.     }            /* endwhile     */
  287.     }                /* endif     */
  288.  
  289.   end_column (stdout);
  290.   return 0;
  291. }
  292.