2 poly.cc -- routines for manipulation of polynomials in one var
4 (c) 1993--1999 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 && fabs (coefs_
.top ()) < FUDGE
* fabs (coefs_
.top (1)))
99 Polynomial::add(const Polynomial
& p1
, const Polynomial
& p2
)
102 int tempord
= p2
.degree () >? p1
.degree ();
103 for (int i
= 0; i
<= tempord
; i
++)
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
);
116 Polynomial::scalarmultiply(Real fact
)
118 for (int i
= 0; i
<= degree (); i
++)
123 Polynomial::subtract(const Polynomial
& p1
, const Polynomial
& p2
)
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
);
142 Polynomial::set_negate(const Polynomial
& src
)
144 for (int i
= 0; i
<= src
.degree(); i
++)
145 coefs_
[i
] = -src
.coefs_
[i
];
150 Polynomial::set_mod(const Polynomial
&u
, const Polynomial
&v
)
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
];
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)
171 coefs_
.set_size(1+ ( (k
< 0) ? 0 : k
));
176 Polynomial::check_sol(Real x
) const
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); */
191 Polynomial::check_sols(Array
<Real
> roots
) const
193 for (int i
=0; i
< roots
.size (); i
++)
197 Polynomial::Polynomial (Real a
, Real b
)
205 inline Real
cubic_root(Real x
)
208 return pow(x
, 1.0/3.0) ;
210 return -pow(-x
, 1.0/3.0);
222 Polynomial::solve_cubic()const
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
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
;
245 if (iszero(q
)) { /* one triple solution */
249 } else { /* one single and one double solution */
250 Real u
= cubic_root(-q
);
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
);
271 Real sub
= 1.0 / 3 * A
;
273 for (int i
= sol
.size (); i
--;)
277 assert (fabs (eval (sol
[i
]) ) < 1e-8);
284 Polynomial::lc () const
292 return coefs_
.top ();
296 Polynomial::degree ()const
298 return coefs_
.size () -1;
301 all roots of quadratic eqn.
304 Polynomial::solve_quadric()const
307 /* normal form: x^2 + px + q = 0 */
308 Real p
= coefs_
[1] / (2 * coefs_
[2]);
309 Real q
= coefs_
[0] / coefs_
[2];
322 /* solve linear equation */
324 Polynomial::solve_linear()const
328 s
.push ( -coefs_
[0] / coefs_
[1]);
334 Polynomial::solve () const
336 Polynomial
* me
= (Polynomial
*) this;
342 return solve_linear ();
344 return solve_quadric ();
346 return solve_cubic ();
354 Polynomial:: operator *= (Polynomial
const &p2
)
356 *this = multiply (*this,p2
);
360 Polynomial::operator += (Polynomial
const &p
)
362 *this = add( *this, p
);
366 Polynomial::operator -= (Polynomial
const &p
)
368 *this = subtract(*this, p
);