]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/polynomial.hh
* VERSION (MY_PATCH_LEVEL): make 1.7.0
[lilypond.git] / flower / include / polynomial.hh
1
2 /*
3  * poly.h -- routines for manipulation of polynomials in one var
4  *
5  * (c) 1993, 1996,1999 Han-Wen Nienhuys
6  */
7
8 #ifndef POLY_H
9 #define POLY_H
10
11 #include "array.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   Array<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 (Array<Real>) const ;
32   void check_sols (Array<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   Array<Real> solve_quadric ()const;
59   Array<Real> solve_cubic ()const;
60   Array<Real> solve_linear ()const;
61
62   Array<Real> solve () const;
63 };
64
65
66 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, - );
67 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, + );
68 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, * );
69
70 inline Polynomial
71 operator * (Polynomial p, Real a)
72 {
73   p *=a;
74   return p;
75 }
76 inline Polynomial
77 operator * (Real a,Polynomial p)
78 {
79   p *=a;
80   return p;
81 }
82 #endif
83