]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/polynomial.hh
1724ab82a21cd956f816f1348c446319cbfaac7b
[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 <sys/types.h>
28 #include "std-vector.hh"
29 #include "arithmetic-operator.hh"
30 #include "real.hh"
31
32 /// structure for a polynomial in one var.
33 struct Polynomial
34 {
35   /// degree of polynomial
36   ssize_t degree ()const;
37
38   /// coefficients
39   vector<Real> coefs_;
40
41   // leading coef
42   Real &lc ();
43
44   // leading coef
45   Real lc () const;
46   void print () const;
47   Real eval (Real) const;
48   Real minmax (Real, Real, bool) const;
49   void print_sols (vector<Real>) const;
50   void check_sols (vector<Real>) const;
51   void check_sol (Real x) const;
52   static Polynomial multiply (const Polynomial &p1, const Polynomial &p2);
53   static Polynomial power (int exponent, const Polynomial &src);
54
55   /// chop low coefficients
56   void clean ();
57
58   /// eliminate #x#  close to  zero
59   void real_clean ();
60   void scalarmultiply (Real fact);
61   void operator *= (Real f) { scalarmultiply (f); }
62   void operator /= (Real f) { scalarmultiply (1 / f); }
63   void operator += (Polynomial const &p2);
64   void operator *= (Polynomial const &p2);
65   void operator -= (Polynomial const &p2);
66   Polynomial (Real a, Real b = 0.0);
67   Polynomial () {}
68   void set_negate (const Polynomial &src);
69
70   /// take the derivative
71   void differentiate ();
72   int set_mod (const Polynomial &u, const Polynomial &v);
73
74   void debug_clean ();
75
76   vector<Real> solve_quadric ()const;
77   vector<Real> solve_cubic ()const;
78   vector<Real> solve_linear ()const;
79
80   vector<Real> solve () const;
81 };
82
83 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, -);
84 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, +);
85 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, *);
86
87 inline Polynomial
88 operator * (Polynomial p, Real a)
89 {
90   p *= a;
91   return p;
92 }
93 inline Polynomial
94 operator * (Real a, Polynomial p)
95 {
96   p *= a;
97   return p;
98 }
99 #endif
100