/*
poly.cc -- routines for manipulation of polynomials in one var
- (c) 1993--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+ (c) 1993--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
*/
#include <math.h>
}
-Polynomial
-Polynomial::add (const Polynomial & p1, const Polynomial & p2)
+
+void
+Polynomial::operator += (Polynomial const &p)
{
- Polynomial dest;
- int tempord = p2.degree () >? p1.degree ();
- for (int i = 0; i <= tempord; i++)
- {
- Real temp = 0.0;
- if (i <= p1.degree ())
- temp += p1.coefs_[i];
- if (i <= p2.degree ())
- temp += p2.coefs_[i];
- dest.coefs_.push (temp);
- }
- return dest;
+ while (degree () < p.degree())
+ coefs_.push (0.0);
+
+ for (int i = 0; i <= p.degree(); i++)
+ coefs_[i] += p.coefs_[i];
}
+
+void
+Polynomial::operator -= (Polynomial const &p)
+{
+ while (degree () < p.degree())
+ coefs_.push (0.0);
+
+ for (int i = 0; i <= p.degree(); i++)
+ coefs_[i] -= p.coefs_[i];
+}
+
+
void
Polynomial::scalarmultiply (Real fact)
{
coefs_[i] *= fact;
}
-Polynomial
-Polynomial::subtract (const Polynomial & p1, const Polynomial & p2)
-{
- Polynomial dest;
- int tempord = p2.degree () >? p1.degree ();
-
- for (int i = 0; i <= tempord; i++)
- {
- Real temp = 0.0; // can't store result directly.. a=a-b
- if (i <= p1.degree ())
- temp += p1.coefs_[i];
- if (i <= p2.degree ())
- temp -= p2.coefs_[i];
- dest.coefs_.push (temp);
- }
- return dest;
-
-}
+
void
Polynomial::set_negate (const Polynomial & src)
/* use Cardano's formula */
- Real cb_p = p * p * p;
- Real D = q * q + cb_p;
+ Real cb = p * p * p;
+ Real D = q * q + cb;
if (iszero (D)) {
if (iszero (q)) { /* one triple solution */
sol.push (-u);
}
} else if (D < 0) { /* Casus irreducibilis: three real solutions */
- Real phi = 1.0 / 3 * acos (-q / sqrt (-cb_p));
+ Real phi = 1.0 / 3 * acos (-q / sqrt (-cb));
Real t = 2 * sqrt (-p);
sol.push (t * cos (phi));
*this = multiply (*this,p2);
}
-void
-Polynomial::operator += (Polynomial const &p)
-{
- *this = add ( *this, p);
-}
-void
-Polynomial::operator -= (Polynomial const &p)
-{
- *this = subtract (*this, p);
-}