Use scalar instead of embedded_scm for context mod overrides.
[lilypond/mpolesky.git] / lily / least-squares.cc
blob2e9382f503326140895b8a3319000f3dccd935e6
1 /*
2 least-squares.cc -- implement minimise_least_squares
4 source file of the GNU LilyPond music typesetter
6 (c) 1996--2009 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;