2 poly.cc -- routines for manipulation of polynomials in one var
4 (c) 1993--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 #include "polynomial.hh"
12 Een beter milieu begint bij uzelf. Hergebruik!
15 This was ripped from Rayce, a raytracer I once wrote.
19 Polynomial::eval (Real x)const
24 for (int i = coefs_.size (); i--;)
25 p = x * p + coefs_[i];
31 Polynomial::multiply (const Polynomial &p1, const Polynomial &p2)
35 int deg = p1.degree () + p2.degree ();
36 for (int i = 0; i <= deg; i++)
39 for (int j = 0; j <= i; j++)
40 if (i - j <= p2.degree () && j <= p1.degree ())
41 dest.coefs_.top () += p1.coefs_[j] * p2.coefs_[i - j];
48 Polynomial::differentiate ()
50 for (int i = 1; i <= degree (); i++)
51 coefs_[i - 1] = coefs_[i] * i;
56 Polynomial::power (int exponent, const Polynomial &src)
59 Polynomial dest (1), base (src);
62 classic int power. invariant: src^exponent = dest * src ^ e
63 greetings go out to Lex Bijlsma & Jaap vd Woude */
68 dest = multiply (dest, base);
74 base = multiply (base, base);
81 static Real const FUDGE = 1e-8;
87 We only do relative comparisons. Absolute comparisons break down in
90 && (fabs (coefs_.top ()) < FUDGE *fabs (coefs_.top (1))
96 Polynomial::operator += (Polynomial const &p)
98 while (degree () < p.degree ())
101 for (int i = 0; i <= p.degree (); i++)
102 coefs_[i] += p.coefs_[i];
106 Polynomial::operator -= (Polynomial const &p)
108 while (degree () < p.degree ())
111 for (int i = 0; i <= p.degree (); i++)
112 coefs_[i] -= p.coefs_[i];
116 Polynomial::scalarmultiply (Real fact)
118 for (int i = 0; i <= degree (); i++)
123 Polynomial::set_negate (const Polynomial &src)
125 for (int i = 0; i <= src.degree (); i++)
126 coefs_[i] = -src.coefs_[i];
131 Polynomial::set_mod (const Polynomial &u, const Polynomial &v)
137 for (int k = u.degree () - v.degree () - 1; k >= 0; k -= 2)
138 coefs_[k] = -coefs_[k];
140 for (int k = u.degree () - v.degree (); k >= 0; k--)
141 for (int j = v.degree () + k - 1; j >= k; j--)
142 coefs_[j] = -coefs_[j] - coefs_[v.degree () + k] * v.coefs_[j - k];
147 for (int k = u.degree () - v.degree (); k >= 0; k--)
148 for (int j = v.degree () + k - 1; j >= k; j--)
149 coefs_[j] -= coefs_[v.degree () + k] * v.coefs_[j - k];
152 int k = v.degree () - 1;
153 while (k >= 0 && coefs_[k] == 0.0)
156 coefs_.set_size (1+ ((k < 0) ? 0 : k));
161 Polynomial::check_sol (Real x) const
164 Polynomial p (*this);
168 if (abs (f) > abs (d) * FUDGE)
171 warning ("x=%f is not a root of polynomial\n"
172 "f (x)=%f, f' (x)=%f \n", x, f, d); */
176 Polynomial::check_sols (Array<Real> roots) const
178 for (int i = 0; i < roots.size (); i++)
179 check_sol (roots[i]);
182 Polynomial::Polynomial (Real a, Real b)
190 inline Real cubic_root (Real x)
193 return pow (x, 1.0 / 3.0);
195 return -pow (-x, 1.0 / 3.0);
207 Polynomial::solve_cubic ()const
211 /* normal form: x^3 + Ax^2 + Bx + C = 0 */
212 Real A = coefs_[2] / coefs_[3];
213 Real B = coefs_[1] / coefs_[3];
214 Real C = coefs_[0] / coefs_[3];
217 * substitute x = y - A/3 to eliminate quadric term: x^3 +px + q = 0
221 Real p = 1.0 / 3 * (-1.0 / 3 * sq_A + B);
222 Real q = 1.0 / 2 * (2.0 / 27 * A *sq_A - 1.0 / 3 * A *B + C);
224 /* use Cardano's formula */
231 if (iszero (q)) { /* one triple solution */
236 else { /* one single and one double solution */
237 Real u = cubic_root (-q);
243 else if (D < 0) { /* Casus irreducibilis: three real solutions */
244 Real phi = 1.0 / 3 * acos (-q / sqrt (-cb));
245 Real t = 2 * sqrt (-p);
247 sol.push (t * cos (phi));
248 sol.push (-t * cos (phi + M_PI / 3));
249 sol.push (-t * cos (phi - M_PI / 3));
251 else { /* one real solution */
252 Real sqrt_D = sqrt (D);
253 Real u = cubic_root (sqrt_D - q);
254 Real v = -cubic_root (sqrt_D + q);
260 Real sub = 1.0 / 3 * A;
262 for (int i = sol.size (); i--;)
267 assert (fabs (eval (sol[i])) < 1e-8);
275 Polynomial::lc () const
277 return coefs_.top ();
283 return coefs_.top ();
287 Polynomial::degree ()const
289 return coefs_.size () -1;
292 all roots of quadratic eqn.
295 Polynomial::solve_quadric ()const
298 /* normal form: x^2 + px + q = 0 */
299 Real p = coefs_[1] / (2 * coefs_[2]);
300 Real q = coefs_[0] / coefs_[2];
314 /* solve linear equation */
316 Polynomial::solve_linear ()const
320 s.push (-coefs_[0] / coefs_[1]);
325 Polynomial::solve () const
327 Polynomial *me = (Polynomial *) this;
333 return solve_linear ();
335 return solve_quadric ();
337 return solve_cubic ();
344 Polynomial::operator *= (Polynomial const &p2)
346 *this = multiply (*this, p2);