Layout nitpicks for notime fragment option.
[lilypond.git] / lily / least-squares.cc
blob582e5b0dcb2bb655832512338f0fe57489f77cd8
1 /*
2 least-squares.cc -- implement minimise_least_squares
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "least-squares.hh"
11 #include "warn.hh"
13 void
14 minimise_least_squares (Real *coef, Real *offset,
15 vector<Offset> const &input)
17 Real sx = 0.0;
18 Real sy = 0.0;
19 Real sqx = 0.0;
20 Real sxy = 0.0;
22 for (vsize i = 0; i < input.size ();i++)
24 Real x = input[i][X_AXIS];
25 Real y = input[i][Y_AXIS];
26 sx += x;
27 sy += y;
28 sqx += sqr (x);
29 sxy += x*y;
32 int count = input.size ();
34 *coef = 0.0;
35 *offset = 0.;
37 Real den = (count * sqx - sqr (sx));
38 if (!count || !den)
40 programming_error ("minimise_least_squares (): Nothing to minimise\n"
41 "This means that vertical spacing is triggered\n"
42 "before line breaking\n");
43 *coef = 0.0;
44 *offset = count ? sy / count : 0.0;
46 else
48 *coef = (count * sxy - sx * sy) / den;
49 *offset = (sy - (*coef) * sx) / count;