]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/polynomial.hh
patch::: 1.3.136.jcn3
[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   static Polynomial add (const Polynomial & p1, const Polynomial & p2);
43   void scalarmultiply (Real fact);
44   void operator *= (Real f) { scalarmultiply (f); }
45   void operator /= (Real f) { scalarmultiply (1/f); }
46   void operator += (Polynomial const &p2);
47   void operator *= (Polynomial const &p2);
48   void operator -= (Polynomial const &p2);
49   Polynomial (Real a, Real b =0.0);
50   Polynomial (){}
51   static Polynomial subtract (const Polynomial & p1, const Polynomial & p2);
52   void set_negate (const Polynomial & src);
53     
54   /// take the derivative
55   void differentiate ();
56   int set_mod (const Polynomial &u, const Polynomial &v);
57
58   void debug_clean ();
59
60   Array<Real> solve_quadric ()const;
61   Array<Real> solve_cubic ()const;
62   Array<Real> solve_linear ()const;
63
64   Array<Real> solve () const;
65 };
66
67
68 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, - );
69 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, + );
70 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, * );
71
72 inline Polynomial
73 operator * (Polynomial p, Real a)
74 {
75   p *=a;
76   return p;
77 }
78 inline Polynomial
79 operator * (Real a,Polynomial p)
80 {
81   p *=a;
82   return p;
83 }
84 #endif
85