]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/leastsquares.cc
release: 1.3.17
[lilypond.git] / lily / leastsquares.cc
index ab718a97f76bca922a7deed174a2836cbdb00469..6856c625b30893f12e3ee7b51736ab9cb9bff83c 100644 (file)
@@ -1,18 +1,20 @@
 #include "leastsquares.hh"
+#include "warn.hh"
+
 void
 Least_squares::OK() const
 {
   assert (input.size() > 1);
   Real dx = 0.0;
   for (int i=1; i < input.size(); i++)
-       dx += abs (input[i-1].x() - input[i].x ());
+    dx += abs (input[i-1][X_AXIS] - input[i][X_AXIS]);
 
   assert (dx);
 }
+
 void
 Least_squares::minimise (Real &coef, Real &offset)
 {
-  OK();
   Real sx = 0.0;
   Real sy = 0.0;
   Real sqx =0.0;
@@ -20,17 +22,21 @@ Least_squares::minimise (Real &coef, Real &offset)
 
   for (int i=0; i < input.size();i++) 
     {
-       Real x=input[i].x();
-       Real y = input[i].y();
-       sx += x;
-       sy += y;
-       sqx += sqr (x);
-       sxy += x*y;
+      Real x=input[i][X_AXIS];
+      Real y = input[i][Y_AXIS];
+      sx += x;
+      sy += y;
+      sqx += sqr (x);
+      sxy += x*y;
     }
   int N = input.size();
-  
 
-  coef = (N * sxy - sx*sy)/(N*sqx - sqr (sx));
+  coef =0.0;
+  offset =0.;
+  
+  Real den = (N*sqx - sqr (sx));
+  if (!N || !den)
+    programming_error ("Least_squares::minimise():  Nothing to minimise");
+  coef = (N * sxy - sx*sy)/den;
   offset = (sy - coef * sx)/N;
-       
 }