Doc: CG intro -- mention lily-git.
[lilypond/mpolesky.git] / flower / include / polynomial.hh
blob54c38a8dbea13b84c308a589cb26d9ab691fbde5
2 /*
3 * poly.h -- routines for manipulation of polynomials in one var
5 * (c) 1993--2009 Han-Wen Nienhuys
6 */
8 #ifndef POLY_H
9 #define POLY_H
11 #include "std-vector.hh"
12 #include "arithmetic-operator.hh"
13 #include "real.hh"
15 /// structure for a polynomial in one var.
16 struct Polynomial
18 /// degree of polynomial
19 int degree ()const;
21 /// coefficients
22 vector<Real> coefs_;
24 // leading coef
25 Real &lc ();
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);
37 /// chop low coefficients
38 void clean ();
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);
52 /// take the derivative
53 void differentiate ();
54 int set_mod (const Polynomial &u, const Polynomial &v);
56 void debug_clean ();
58 vector<Real> solve_quadric ()const;
59 vector<Real> solve_cubic ()const;
60 vector<Real> solve_linear ()const;
62 vector<Real> solve () const;
65 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, -);
66 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, +);
67 IMPLEMENT_ARITHMETIC_OPERATOR (Polynomial, *);
69 inline Polynomial
70 operator * (Polynomial p, Real a)
72 p *= a;
73 return p;
75 inline Polynomial
76 operator * (Real a, Polynomial p)
78 p *= a;
79 return p;
81 #endif