--- trunk/src/math/Polynomial.hpp 2005/04/15 22:04:00 507 +++ trunk/src/math/Polynomial.hpp 2006/05/17 21:51:42 963 @@ -53,7 +53,7 @@ #include #include #include - +#include "config.h" namespace oopse { template ElemType pow(ElemType x, int N) { @@ -74,12 +74,15 @@ namespace oopse { class Polynomial { public: - + typedef Polynomial PolynomialType; typedef int ExponentType; typedef ElemType CoefficientType; typedef std::map PolynomialPairMap; typedef typename PolynomialPairMap::iterator iterator; typedef typename PolynomialPairMap::const_iterator const_iterator; + + Polynomial() {} + Polynomial(ElemType v) {setCoefficient(0, v);} /** * Calculates the value of this Polynomial evaluated at the given x value. * @return The value of this Polynomial evaluates at the given x value @@ -185,7 +188,39 @@ namespace oopse { size_t size() { return polyPairMap_.size(); } - + + PolynomialType& operator += (const PolynomialType& p) { + typename Polynomial::const_iterator i; + + for (i = p.begin(); i != p.end(); ++i) { + this->addCoefficient(i->first, i->second); + } + + return *this; + } + + PolynomialType& operator -= (const PolynomialType& p) { + typename Polynomial::const_iterator i; + for (i = p.begin(); i != p.end(); ++i) { + this->addCoefficient(i->first, -i->second); + } + return *this; + } + + PolynomialType& operator *= (const PolynomialType& p) { + typename Polynomial::const_iterator i; + typename Polynomial::const_iterator j; + + for (i = this->begin(); i !=this->end(); ++i) { + for (j = p.begin(); j !=p.end(); ++j) { + this->addCoefficient( i->first + j->first, i->second * j->second); + } + } + + return *this; + } + + private: PolynomialPairMap polyPairMap_; @@ -211,6 +246,30 @@ namespace oopse { return p; } + template + Polynomial operator *(const Polynomial& p, const ElemType v) { + typename Polynomial::const_iterator i; + Polynomial result; + + for (i = p.begin(); i !=p.end(); ++i) { + result.addCoefficient( i->first , i->second * v); + } + + return result; + } + + template + Polynomial operator *( const ElemType v, const Polynomial& p) { + typename Polynomial::const_iterator i; + Polynomial result; + + for (i = p.begin(); i !=p.end(); ++i) { + result.addCoefficient( i->first , i->second * v); + } + + return result; + } + /** * Generates and returns the sum of two given Polynomials. * @param p1 the first polynomial @@ -252,7 +311,7 @@ namespace oopse { /** * Tests if two polynomial have the same exponents - * @return true if these all of the exponents in these Polynomial are identical + * @return true if all of the exponents in these Polynomial are identical * @param p1 the first polynomial * @param p2 the second polynomial * @note this function does not compare the coefficient @@ -276,7 +335,7 @@ namespace oopse { return true; } - typedef Polynomial DoublePolynomial; + typedef Polynomial DoublePolynomial; } //end namespace oopse #endif //MATH_POLYNOMIAL_HPP