lilypond-1.3.69
[lilypond.git] / lily / least-squares.cc
blob737cbd6d58743ffc83326504e9c490e9a2a3bc9e
1 /*
2 least-squares.cc -- implement minimise_least_squares
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "least-squares.hh"
11 #include "warn.hh"
14 void
15 minimise_least_squares (Real * coef, Real * offset,
16 Array<Offset> input)
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_AXIS];
26 Real y = input[i][Y_AXIS];
27 sx += x;
28 sy += y;
29 sqx += sqr (x);
30 sxy += x*y;
32 int N = input.size();
34 *coef =0.0;
35 *offset =0.;
37 Real den = (N*sqx - sqr (sx));
38 if (!N || !den)
39 programming_error ("minimise_least_squares(): Nothing to minimise");
41 *coef = (N * sxy - sx*sy)/den;
42 *offset = (sy - (*coef) * sx)/N;