lilypond-0.0.32
[lilypond.git] / src / leastsquares.cc
blobcf9ec478e494b50923f067bd624f0bc7c73e56ae
1 #include "leastsquares.hh"
2 void
3 Least_squares::OK() const
5 assert( input.size() > 1 );
6 Real dx = 0.0;
7 for (int i=1; i < input.size(); i++)
8 dx += abs (input[i-1].x - input[i].x);
10 assert(dx);
12 void
13 Least_squares::minimise(Real &coef, Real &offset)
15 OK();
16 Real sx = 0.0;
17 Real sy = 0.0;
18 Real sqx =0.0;
19 Real sxy = 0.0;
21 for (int i=0; i < input.size();i++) {
22 Real x=input[i].x;
23 Real y = input[i].y;
24 sx += x;
25 sy += y;
26 sqx += sqr(x);
27 sxy += x*y;
29 int N = input.size();
32 coef = (N * sxy - sx*sy )/(N*sqx - sqr(sx));
33 offset = (sy - coef * sx)/N;