]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/polynomial.hh
297bd6c8c15a1d57b7e72f0bb8575b41560e7ce5
[lilypond.git] / flower / include / polynomial.hh
1
2 /*
3  * poly.h -- routines for manipulation of polynomials in one var
4  *
5  * (c) 1993--2007 Han-Wen Nienhuys
6  */
7
8 #ifndef POLY_H
9 #define POLY_H
10
11 #include "std-vector.hh"
12 #include "arithmetic-operator.hh"
13 #include "real.hh"
14
15 /// structure for a polynomial in one var. 
16 struct Polynomial
17 {
18   /// degree of polynomial
19   int degree ()const;
20
21   /// coefficients 
22   vector<Real> coefs_;
23
24   // leading coef
25   Real &lc ();
26
27   // leading coef
28   Real lc () const;
29   void print () const;
30   Real eval (Real) const;
31   void print_sols (vector<Real>) const;
32   void check_sols (vector<Real>) const;
33   void check_sol (Real x) const;
34   static Polynomial multiply (const Polynomial &p1, const Polynomial &p2);
35   static Polynomial power (int exponent, const Polynomial &src);
36
37   /// chop low coefficients
38   void clean ();
39
40   /// eliminate #x#  close to  zero
41   void real_clean ();
42   void scalarmultiply (Real fact);
43   void operator *= (Real f) { scalarmultiply (f); }
44   void operator /= (Real f) { scalarmultiply (1 / f); }
45   void operator += (Polynomial const &p2);
46   void operator *= (Polynomial const &p2);
47   void operator -= (Polynomial const &p2);
48   Polynomial (Real a, Real b = 0.0);
49   Polynomial (){}
50   void set_negate (const Polynomial &src);
51
52   /// take the derivative
53   void differentiate ();
54   int set_mod (const Polynomial &u, const Polynomial &v);
55
56   void debug_clean ();
57
58   vector<Real> solve_quadric ()const;
59   vector<Real> solve_cubic ()const;
60   vector<Real> solve_linear ()const;
61
62   vector<Real> solve () const;
63 };
64
65 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, -);
66 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, +);
67 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, *);
68
69 inline Polynomial
70 operator * (Polynomial p, Real a)
71 {
72   p *= a;
73   return p;
74 }
75 inline Polynomial
76 operator * (Real a, Polynomial p)
77 {
78   p *= a;
79   return p;
80 }
81 #endif
82