]> git.donarmstrong.com Git - lilypond.git/blob - lily/least-squares.cc
Run `make grand-replace'.
[lilypond.git] / lily / least-squares.cc
1 /*
2   least-squares.cc -- implement minimise_least_squares
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "least-squares.hh"
10
11 #include "warn.hh"
12
13 void
14 minimise_least_squares (Real *coef, Real *offset,
15                         vector<Offset> const &input)
16 {
17   Real sx = 0.0;
18   Real sy = 0.0;
19   Real sqx = 0.0;
20   Real sxy = 0.0;
21
22   for (vsize i = 0; i < input.size ();i++)
23     {
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;
30     }
31
32   int count = input.size ();
33
34   *coef = 0.0;
35   *offset = 0.;
36
37   Real den = (count * sqx - sqr (sx));
38   if (!count || !den)
39     {
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;
45     }
46   else
47     {
48       *coef = (count * sxy - sx * sy) / den;
49       *offset = (sy - (*coef) * sx) / count;
50     }
51 }
52