2 poly.cc -- routines for manipulation of polynomials in one var
4 (c) 1993--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #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];
32 Polynomial::multiply(const Polynomial & p1, const Polynomial & p2)
36 int deg= p1.degree () + p2.degree ();
37 for (int i = 0; i <= deg; i++)
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];
49 Polynomial::differentiate()
51 for (int i = 1; i<= degree (); i++)
53 coefs_[i-1] = coefs_[i] * i;
59 Polynomial::power(int exponent, const Polynomial & src)
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
70 dest = multiply(dest, base);
74 base = multiply(base, base);
81 static Real const FUDGE = 1e-8;
87 for (i = 0; i <= degree (); i++)
89 if (abs(coefs_[i]) < FUDGE)
93 while (degree () > 0 &&
94 (fabs (coefs_.top ()) < FUDGE * fabs (coefs_.top (1)))
101 Polynomial::add(const Polynomial & p1, const Polynomial & p2)
104 int tempord = p2.degree () >? p1.degree ();
105 for (int i = 0; i <= tempord; i++)
108 if (i <= p1.degree ())
109 temp += p1.coefs_[i];
110 if (i <= p2.degree ())
111 temp += p2.coefs_[i];
112 dest.coefs_.push (temp);
118 Polynomial::scalarmultiply(Real fact)
120 for (int i = 0; i <= degree (); i++)
125 Polynomial::subtract(const Polynomial & p1, const Polynomial & p2)
128 int tempord = p2.degree () >? p1.degree ();
130 for (int i = 0; i <= tempord; i++)
132 Real temp = 0.0; // can't store result directly.. a=a-b
133 if (i <= p1.degree ())
134 temp += p1.coefs_[i];
135 if (i <= p2.degree ())
136 temp -= p2.coefs_[i];
137 dest.coefs_.push (temp);
144 Polynomial::set_negate(const Polynomial & src)
146 for (int i = 0; i <= src.degree(); i++)
147 coefs_[i] = -src.coefs_[i];
152 Polynomial::set_mod(const Polynomial &u, const Polynomial &v)
157 for (int k = u.degree () - v.degree () - 1; k >= 0; k -= 2)
158 coefs_[k] = -coefs_[k];
160 for (int k = u.degree () - v.degree (); k >= 0; k--)
161 for (int j = v.degree () + k - 1; j >= k; j--)
162 coefs_[j] = -coefs_[j] - coefs_[v.degree () + k] * v.coefs_[j - k];
164 for (int k = u.degree () - v.degree (); k >= 0; k--)
165 for (int j = v.degree () + k - 1; j >= k; j--)
166 coefs_[j] -= coefs_[v.degree () + k] * v.coefs_[j - k];
169 int k = v.degree () - 1;
170 while (k >= 0 && coefs_[k] == 0.0)
173 coefs_.set_size(1+ ( (k < 0) ? 0 : k));
178 Polynomial::check_sol(Real x) const
185 if( abs(f) > abs(d) * FUDGE)
188 warning("x=%f is not a root of polynomial\n"
189 "f(x)=%f, f'(x)=%f \n", x, f, d); */
193 Polynomial::check_sols(Array<Real> roots) const
195 for (int i=0; i< roots.size (); i++)
199 Polynomial::Polynomial (Real a, Real b)
207 inline Real cubic_root(Real x)
210 return pow(x, 1.0/3.0) ;
212 return -pow(-x, 1.0/3.0);
224 Polynomial::solve_cubic()const
228 /* normal form: x^3 + Ax^2 + Bx + C = 0 */
229 Real A = coefs_[2] / coefs_[3];
230 Real B = coefs_[1] / coefs_[3];
231 Real C = coefs_[0] / coefs_[3];
234 * substitute x = y - A/3 to eliminate quadric term: x^3 +px + q = 0
238 Real p = 1.0 / 3 * (-1.0 / 3 * sq_A + B);
239 Real q = 1.0 / 2 * (2.0 / 27 * A * sq_A - 1.0 / 3 * A * B + C);
241 /* use Cardano's formula */
243 Real cb_p = p * p * p;
244 Real D = q * q + cb_p;
247 if (iszero(q)) { /* one triple solution */
251 } else { /* one single and one double solution */
252 Real u = cubic_root(-q);
257 } else if (D < 0) { /* Casus irreducibilis: three real solutions */
258 Real phi = 1.0 / 3 * acos(-q / sqrt(-cb_p));
259 Real t = 2 * sqrt(-p);
261 sol.push (t * cos(phi));
262 sol.push (-t * cos(phi + M_PI / 3));
263 sol.push ( -t * cos(phi - M_PI / 3));
264 } else { /* one real solution */
265 Real sqrt_D = sqrt(D);
266 Real u = cubic_root(sqrt_D - q);
267 Real v = -cubic_root(sqrt_D + q);
273 Real sub = 1.0 / 3 * A;
275 for (int i = sol.size (); i--;)
279 assert (fabs (eval (sol[i]) ) < 1e-8);
286 Polynomial::lc () const
294 return coefs_.top ();
298 Polynomial::degree ()const
300 return coefs_.size () -1;
303 all roots of quadratic eqn.
306 Polynomial::solve_quadric()const
309 /* normal form: x^2 + px + q = 0 */
310 Real p = coefs_[1] / (2 * coefs_[2]);
311 Real q = coefs_[0] / coefs_[2];
324 /* solve linear equation */
326 Polynomial::solve_linear()const
330 s.push ( -coefs_[0] / coefs_[1]);
336 Polynomial::solve () const
338 Polynomial * me = (Polynomial*) this;
344 return solve_linear ();
346 return solve_quadric ();
348 return solve_cubic ();
356 Polynomial:: operator *= (Polynomial const &p2)
358 *this = multiply (*this,p2);
362 Polynomial::operator += (Polynomial const &p)
364 *this = add( *this, p);
368 Polynomial::operator -= (Polynomial const &p)
370 *this = subtract(*this, p);