]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/polynomial.hh
Run grand-replace (issue 3765)
[lilypond.git] / flower / include / polynomial.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1993--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21  * polynomial.hh -- routines for manipulation of polynomials in one var
22  */
23
24 #ifndef POLY_H
25 #define POLY_H
26
27 #include "std-vector.hh"
28 #include "arithmetic-operator.hh"
29 #include "real.hh"
30
31 /// structure for a polynomial in one var.
32 struct Polynomial
33 {
34   /// degree of polynomial
35   ssize_t degree ()const;
36
37   /// coefficients
38   vector<Real> coefs_;
39
40   // leading coef
41   Real &lc ();
42
43   // leading coef
44   Real lc () const;
45   void print () const;
46   Real eval (Real) const;
47   Real minmax (Real, Real, bool) const;
48   void print_sols (vector<Real>) const;
49   void check_sols (vector<Real>) const;
50   void check_sol (Real x) const;
51   static Polynomial multiply (const Polynomial &p1, const Polynomial &p2);
52   static Polynomial power (int exponent, const Polynomial &src);
53
54   /// chop low coefficients
55   void clean ();
56
57   /// eliminate #x#  close to  zero
58   void real_clean ();
59   void scalarmultiply (Real fact);
60   void operator *= (Real f) { scalarmultiply (f); }
61   void operator /= (Real f) { scalarmultiply (1 / f); }
62   void operator += (Polynomial const &p2);
63   void operator *= (Polynomial const &p2);
64   void operator -= (Polynomial const &p2);
65   Polynomial (Real a, Real b = 0.0);
66   Polynomial () {}
67   void set_negate (const Polynomial &src);
68
69   /// take the derivative
70   void differentiate ();
71   int set_mod (const Polynomial &u, const Polynomial &v);
72
73   void debug_clean ();
74
75   vector<Real> solve_quadric ()const;
76   vector<Real> solve_cubic ()const;
77   vector<Real> solve_linear ()const;
78
79   vector<Real> solve () const;
80 };
81
82 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, -);
83 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, +);
84 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, *);
85
86 inline Polynomial
87 operator * (Polynomial p, Real a)
88 {
89   p *= a;
90   return p;
91 }
92 inline Polynomial
93 operator * (Real a, Polynomial p)
94 {
95   p *= a;
96   return p;
97 }
98 #endif
99