Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

lsutil.c

Go to the documentation of this file.
00001 /*
00002   lsutil.c
00003 
00004   Least squares linear fit code
00005 
00006   MODIFICATIONS
00007 
00008   28-Jun-2000 WPS added __attribute__((unused)) to ident.
00009 
00010   */
00011 
00012 /* ident tag */
00013 #ifndef __GNUC__
00014 #ifndef __attribute__
00015 #define __attribute__(x)
00016 #endif
00017 #endif
00018 
00019 static char __attribute__((unused)) ident[] = "$Id: lsutil.c,v 1.3 2000/10/27 20:34:42 terrylr Exp $";
00020 
00021 static double sum(double *x, int n)
00022 {
00023   double retval = 0.0;
00024 
00025   n--;
00026   while (n >= 0)
00027     {
00028       retval += x[n];
00029       n--;
00030     }
00031 
00032   return retval;
00033 }
00034 
00035 static double sumSq(double *x, int n)
00036 {
00037   double retval = 0.0;
00038 
00039   n--;
00040   while (n >= 0)
00041     {
00042       retval += x[n]*x[n];
00043       n--;
00044     }
00045 
00046   return retval;
00047 }
00048 
00049 static double sumProd(double *x, double *y, int n)
00050 {
00051   double retval = 0.0;
00052 
00053   n--;
00054   while (n >= 0)
00055     {
00056       retval += x[n]*y[n];
00057       n--;
00058     }
00059 
00060   return retval;
00061 }
00062 
00063 /* returns the sum of (x[i] - d) y[i]*/
00064 static double sumProdOD(double *x, double *y, int n, double d)
00065 {
00066   double retval = 0.0;
00067 
00068   n--;
00069   while (n >= 0)
00070     {
00071       retval += (x[n]-d)*y[n];
00072       n--;
00073     }
00074 
00075   return retval;
00076 }
00077 
00078 static double sumSqOD(double *x, int n, double d)
00079 {
00080 
00081   double retval = 0.0;
00082 
00083   n--;
00084   while (n >= 0)
00085     {
00086       retval += (x[n]-d)*(x[n]-d);
00087       n--;
00088     }
00089 
00090   return retval;
00091 }
00092 
00093 /*
00094   given arrays of points x, y, and number of points n,
00095   computes the least squares best fit line y = a x + b,
00096   putting the coefficients in the passed pointer args.
00097 
00098   Returns 0 if OK, -1 if only one point, or a = inf
00099   */
00100 int lsCoeff(double *x, double *y, int n, double *a, double *b)
00101 {
00102   double sumX;
00103   double sumY;
00104   double sumXY;
00105   double sumXSq;
00106   double denom;
00107 
00108   if (n <= 1)
00109     {
00110       return -1;
00111     }
00112 
00113   sumX = sum(x, n);
00114   sumY = sum(y, n);
00115   sumXY = sumProd(x, y, n);
00116   sumXSq = sumSq(x, n);
00117 
00118   denom = n*sumXSq - sumX*sumX;
00119   if (denom == 0.0)
00120     {
00121       return -1;
00122     }
00123   *a = (n*sumXY - sumX*sumY)/denom;
00124   *b = (sumY - (*a)*sumX)/n;
00125 
00126   return 0;
00127 }
00128 
00129 /*
00130   given arrays of points x, y, number of points n, and the
00131   point d where y must be 0, computes the least squares best
00132   fit line y = a (x - d), putting the coefficient in the passed
00133   pointer arg a.
00134 
00135   Returns 0 if OK, -1 if only one point, or a = inf
00136   */
00137 int lsCoeffOD(double *x, double *y, int n, double d, double *a)
00138 {
00139   double sumXODY;
00140   double sumXODSq;
00141 
00142   if (n <= 1)
00143     {
00144       return -1;
00145     }
00146 
00147   sumXODY = sumProdOD(x, y, n, d);
00148   sumXODSq = sumSqOD(x, n, d);
00149 
00150   if (sumXODSq == 0.0)
00151     {
00152       return -1;
00153     }
00154 
00155   *a = sumXODY/sumXODSq;
00156 
00157   return 0;
00158 }

Generated on Sun Dec 2 15:27:41 2001 for EMC by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001