
/*
 * Copyright (c) 2001 BBNT Solutions LLC
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice and this permission
 * appear in all copies and in supporting documentation, and that the
 * name of BBN Technologies not be used in advertising or publicity
 * pertaining to distribution of the software without specific,
 * written prior permission.  BBN makes no representations about the
 * suitability of this software for any purposes.  It is provided "AS
 * IS" without express or implied warranties.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE     80

unsigned long n = 0;
double sumx = 0.0, sumy = 0.0;
double sumxy = 0.0;
double sumxsq = 0.0;


void usage (progname)
char *progname;
{
    fprintf (stderr,"usage: %s file(1) [file(2-n)]\n", progname);
    fprintf (stderr,"  (use \"-\" for stdin)\n");
    exit (1);
}


void slurp_file (filename)
char *filename;
{
    FILE *fp;
    char line [MAX_LINE];
    char *num;
    double x, y;
    unsigned long lineno = 0;

    if (!strcmp (filename,"-"))
	fp = stdin;
    else if ((fp = fopen (filename,"r")) == NULL)
    {
	fprintf (stderr,"cannot open %s for reading\n", filename);
	exit (1);
    }
    while (fgets (line,MAX_LINE,fp) != NULL)
    {
	lineno++;
	n++;
	if ((num = strtok (line," ")) == NULL)
	{
	    fprintf (stdout,"error in line %ld of file %s\n", lineno, 
		     filename);
	    exit (1);
	}
	else
	    x = atof (num);
	if ((num = strtok (NULL," ")) == NULL)
	{
	    y = x;
	    x = n;
	}
	else
	    y = atof (num);
	sumx += x;
	sumy += y;
	sumxy += (x * y);
	sumxsq += (x * x);
    }
    if (strcmp (filename,"-"))
	fclose (fp);
}


void parseargs (argc,argv)
int argc;
char *argv [];
{
    int i;

    if (argc < 2)
	usage (argv [0]);
    for (i = 1; i < argc; i++)
    {
	if (!strcmp (argv [i],"-h"))
	    usage (argv [0]);
	if (!strcmp (argv [i],"-v") || !strcmp (argv [i],"--version"))
	{
	    fprintf (stdout,"\nlesq v%s\n", VERSION);
	    fprintf (stdout,"Mark Allman (mallman@icir.org)\n\n");
	    exit (1);
	}
	if (argv [i] [0] == '-' && (strlen (argv [i]) > 1))
	    usage (argv [0]);
    }
    for (i = 1; i < argc; i++)
	/* if we add options we need to check to make sure each argument
	   in here is a filename */
	slurp_file (argv [i]);
}


void calc_lesq ()
{
    double mean_x, mean_y;
    double b0, b1;

    mean_x = (1.0 / n) * sumx;
    mean_y = (1.0 / n) * sumy;
    b1 = (sumxy - (n * mean_x * mean_y)) / (sumxsq - (n * mean_x * mean_x));
    b0 = mean_y - (b1 * mean_x);
    fprintf (stdout,"mean_x = %f\n", mean_x);
    fprintf (stdout,"mean_y = %f\n", mean_y);
    fprintf (stdout,"sum(xy) = %f\n", sumxy);
    fprintf (stdout,"sum(x^2) = %f\n", sumxsq);
    fprintf (stdout,"b0 = %f\n", b0);
    fprintf (stdout,"b1 = %f\n", b1);
    fprintf (stdout,"final: y = %f + %fx\n", b0, b1);
}


int main (argc,argv)
int argc;
char *argv [];
{
    parseargs (argc,argv);
    calc_lesq ();
    exit (0);
}
