lilypond-1.3.18
[lilypond.git] / flower / polynomial.cc
blob5a231ca6b2665aa524c2ee5603f759d734e41f74
1 /*
2 poly.cc -- routines for manipulation of polynomials in one var
4 (c) 1993--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
5 */
7 #include <math.h>
9 #include "polynomial.hh"
12 Een beter milieu begint bij uzelf. Hergebruik!
15 This was ripped from Rayce, a raytracer I once wrote.
18 Real
19 Polynomial::eval (Real x)const
21 Real p = 0.0;
23 // horner's scheme
24 for (int i = coefs_.size (); i--; )
25 p = x * p + coefs_[i];
27 return p;
31 Polynomial
32 Polynomial::multiply(const Polynomial & p1, const Polynomial & p2)
34 Polynomial dest;
36 int deg= p1.degree () + p2.degree ();
37 for (int i = 0; i <= deg; i++)
39 dest.coefs_.push (0);
40 for (int j = 0; j <= i; j++)
41 if (i - j <= p2.degree () && j <= p1.degree ())
42 dest.coefs_.top () += p1.coefs_[j] * p2.coefs_[i - j];
45 return dest;
48 void
49 Polynomial::differentiate()
51 for (int i = 1; i<= degree (); i++)
53 coefs_[i-1] = coefs_[i] * i;
55 coefs_.pop ();
58 Polynomial
59 Polynomial::power(int exponent, const Polynomial & src)
61 int e = exponent;
62 Polynomial dest(1), base(src);
64 // classicint power. invariant: src^exponent = dest * src ^ e
65 // greetings go out to Lex Bijlsma & Jaap vd Woude
66 while (e > 0)
68 if (e % 2)
70 dest = multiply(dest, base);
71 e--;
72 } else
74 base = multiply(base, base);
75 e /= 2;
78 return dest;
81 static Real const FUDGE = 1e-8;
83 void
84 Polynomial::clean()
86 int i;
87 for (i = 0; i <= degree (); i++)
89 if (abs(coefs_[i]) < FUDGE)
90 coefs_[i] = 0.0;
93 while (degree () > 0 && fabs (coefs_.top ()) < FUDGE * fabs (coefs_.top (1)))
94 coefs_.pop ();
98 Polynomial
99 Polynomial::add(const Polynomial & p1, const Polynomial & p2)
101 Polynomial dest;
102 int tempord = p2.degree () >? p1.degree ();
103 for (int i = 0; i <= tempord; i++)
105 Real temp = 0.0;
106 if (i <= p1.degree ())
107 temp += p1.coefs_[i];
108 if (i <= p2.degree ())
109 temp += p2.coefs_[i];
110 dest.coefs_.push (temp);
112 return dest;
115 void
116 Polynomial::scalarmultiply(Real fact)
118 for (int i = 0; i <= degree (); i++)
119 coefs_[i] *= fact;
122 Polynomial
123 Polynomial::subtract(const Polynomial & p1, const Polynomial & p2)
125 Polynomial dest;
126 int tempord = p2.degree () >? p1.degree ();
128 for (int i = 0; i <= tempord; i++)
130 Real temp = 0.0; // can't store result directly.. a=a-b
131 if (i <= p1.degree ())
132 temp += p1.coefs_[i];
133 if (i <= p2.degree ())
134 temp -= p2.coefs_[i];
135 dest.coefs_.push (temp);
137 return dest;
141 void
142 Polynomial::set_negate(const Polynomial & src)
144 for (int i = 0; i <= src.degree(); i++)
145 coefs_[i] = -src.coefs_[i];
148 /// mod of #u/v#
149 int
150 Polynomial::set_mod(const Polynomial &u, const Polynomial &v)
152 (*this) = u;
154 if (v.lc() < 0.0) {
155 for (int k = u.degree () - v.degree () - 1; k >= 0; k -= 2)
156 coefs_[k] = -coefs_[k];
158 for (int k = u.degree () - v.degree (); k >= 0; k--)
159 for (int j = v.degree () + k - 1; j >= k; j--)
160 coefs_[j] = -coefs_[j] - coefs_[v.degree () + k] * v.coefs_[j - k];
161 } else {
162 for (int k = u.degree () - v.degree (); k >= 0; k--)
163 for (int j = v.degree () + k - 1; j >= k; j--)
164 coefs_[j] -= coefs_[v.degree () + k] * v.coefs_[j - k];
167 int k = v.degree () - 1;
168 while (k >= 0 && coefs_[k] == 0.0)
169 k--;
171 coefs_.set_size(1+ ( (k < 0) ? 0 : k));
172 return degree();
175 void
176 Polynomial::check_sol(Real x) const
178 Real f=eval(x);
179 Polynomial p(*this);
180 p.differentiate();
181 Real d = p.eval(x);
183 if( abs(f) > abs(d) * FUDGE)
186 warning("x=%f is not a root of polynomial\n"
187 "f(x)=%f, f'(x)=%f \n", x, f, d); */
190 void
191 Polynomial::check_sols(Array<Real> roots) const
193 for (int i=0; i< roots.size (); i++)
194 check_sol(roots[i]);
197 Polynomial::Polynomial (Real a, Real b)
199 coefs_.push (a);
200 if (b)
201 coefs_.push (b);
204 /* cubic root. */
205 inline Real cubic_root(Real x)
207 if (x > 0.0)
208 return pow(x, 1.0/3.0) ;
209 else if (x < 0.0)
210 return -pow(-x, 1.0/3.0);
211 else
212 return 0.0;
215 static bool
216 iszero (Real r)
218 return !r;
221 Array<Real>
222 Polynomial::solve_cubic()const
224 Array<Real> sol;
226 /* normal form: x^3 + Ax^2 + Bx + C = 0 */
227 Real A = coefs_[2] / coefs_[3];
228 Real B = coefs_[1] / coefs_[3];
229 Real C = coefs_[0] / coefs_[3];
232 * substitute x = y - A/3 to eliminate quadric term: x^3 +px + q = 0
235 Real sq_A = A * A;
236 Real p = 1.0 / 3 * (-1.0 / 3 * sq_A + B);
237 Real q = 1.0 / 2 * (2.0 / 27 * A * sq_A - 1.0 / 3 * A * B + C);
239 /* use Cardano's formula */
241 Real cb_p = p * p * p;
242 Real D = q * q + cb_p;
244 if (iszero(D)) {
245 if (iszero(q)) { /* one triple solution */
246 sol.push (0);
247 sol.push (0);
248 sol.push (0);
249 } else { /* one single and one double solution */
250 Real u = cubic_root(-q);
252 sol.push (2 * u);
253 sol.push (-u);
255 } else if (D < 0) { /* Casus irreducibilis: three real solutions */
256 Real phi = 1.0 / 3 * acos(-q / sqrt(-cb_p));
257 Real t = 2 * sqrt(-p);
259 sol.push (t * cos(phi));
260 sol.push (-t * cos(phi + M_PI / 3));
261 sol.push ( -t * cos(phi - M_PI / 3));
262 } else { /* one real solution */
263 Real sqrt_D = sqrt(D);
264 Real u = cubic_root(sqrt_D - q);
265 Real v = -cubic_root(sqrt_D + q);
267 sol.push ( u + v);
270 /* resubstitute */
271 Real sub = 1.0 / 3 * A;
273 for (int i = sol.size (); i--;)
275 sol[i] -= sub;
277 assert (fabs (eval (sol[i]) ) < 1e-8);
280 return sol;
283 Real
284 Polynomial::lc () const
286 return coefs_.top();
289 Real&
290 Polynomial::lc ()
292 return coefs_.top ();
296 Polynomial::degree ()const
298 return coefs_.size () -1;
301 all roots of quadratic eqn.
303 Array<Real>
304 Polynomial::solve_quadric()const
306 Array<Real> sol;
307 /* normal form: x^2 + px + q = 0 */
308 Real p = coefs_[1] / (2 * coefs_[2]);
309 Real q = coefs_[0] / coefs_[2];
311 Real D = p * p - q;
313 if (D>0) {
314 D = sqrt(D);
316 sol.push ( D - p);
317 sol.push ( -D - p);
319 return sol;
322 /* solve linear equation */
323 Array<Real>
324 Polynomial::solve_linear()const
326 Array<Real> s;
327 if (coefs_[1])
328 s.push ( -coefs_[0] / coefs_[1]);
329 return s;
333 Array<Real>
334 Polynomial::solve () const
336 Polynomial * me = (Polynomial*) this;
337 me->clean ();
339 switch (degree ())
341 case 1:
342 return solve_linear ();
343 case 2:
344 return solve_quadric ();
345 case 3:
346 return solve_cubic ();
348 assert (false);
349 Array<Real> s;
350 return s;
353 void
354 Polynomial:: operator *= (Polynomial const &p2)
356 *this = multiply (*this,p2);
359 void
360 Polynomial::operator += (Polynomial const &p)
362 *this = add( *this, p);
365 void
366 Polynomial::operator -= (Polynomial const &p)
368 *this = subtract(*this, p);