lilypond-0.1.59
[lilypond.git] / lily / leastsquares.cc
blob0f1568f29f82f44f2ac714b2e1d064666a685dbd
1 #include "leastsquares.hh"
3 void
4 Least_squares::OK() const
6 assert (input.size() > 1);
7 Real dx = 0.0;
8 for (int i=1; i < input.size(); i++)
9 dx += abs (input[i-1].x() - input[i].x ());
11 assert (dx);
14 void
15 Least_squares::minimise (Real &coef, Real &offset)
17 OK();
18 Real sx = 0.0;
19 Real sy = 0.0;
20 Real sqx =0.0;
21 Real sxy = 0.0;
23 for (int i=0; i < input.size();i++)
25 Real x=input[i].x();
26 Real y = input[i].y();
27 sx += x;
28 sy += y;
29 sqx += sqr (x);
30 sxy += x*y;
32 int N = input.size();
35 coef = (N * sxy - sx*sy)/(N*sqx - sqr (sx));
36 offset = (sy - coef * sx)/N;