]> git.donarmstrong.com Git - lilypond.git/blob - lily/least-squares.cc
Fix some bugs in the dynamic engraver and PostScript backend
[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--2006 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   int N = input.size ();
32
33   *coef = 0.0;
34   *offset = 0.;
35
36   Real den = (N * sqx - sqr (sx));
37   if (!N || !den)
38     {
39       programming_error ("minimise_least_squares ():  Nothing to minimise");
40       *coef = 0.0;
41       *offset = N ? sy / N : 0.0;
42     }
43   else
44     {
45       *coef = (N *sxy - sx * sy) / den;
46       *offset = (sy - (*coef) * sx) / N;
47     }
48 }
49