--- trunk/src/math/Polynomial.hpp 2006/01/30 22:25:27 876 +++ trunk/src/math/Polynomial.hpp 2008/09/10 19:51:45 1290 @@ -53,7 +53,7 @@ #include #include #include - +#include "config.h" namespace oopse { template ElemType pow(ElemType x, int N) { @@ -129,7 +129,7 @@ namespace oopse { */ void setCoefficient(int exponent, const ElemType& coefficient) { - polyPairMap_.insert(typename PolynomialPairMap::value_type(exponent, coefficient)); + polyPairMap_[exponent] = coefficient; } /** @@ -149,7 +149,6 @@ namespace oopse { } } - /** * Returns the coefficient associated with the given power for this Polynomial. * @return the coefficient associated with the given power for this Polynomial @@ -189,6 +188,22 @@ namespace oopse { return polyPairMap_.size(); } + PolynomialType& operator = (const PolynomialType& p) { + + if (this != &p) // protect against invalid self-assignment + { + typename Polynomial::const_iterator i; + + polyPairMap_.clear(); // clear out the old map + + for (i = p.begin(); i != p.end(); ++i) { + this->setCoefficient(i->first, i->second); + } + } + // by convention, always return *this + return *this; + } + PolynomialType& operator += (const PolynomialType& p) { typename Polynomial::const_iterator i; @@ -204,21 +219,39 @@ namespace oopse { 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) { + Polynomial p2(*this); + + polyPairMap_.clear(); // clear out old map + for (i = p2.begin(); i !=p2.end(); ++i) { for (j = p.begin(); j !=p.end(); ++j) { this->addCoefficient( i->first + j->first, i->second * j->second); } } + return *this; + } + //PolynomialType& operator *= (const ElemType v) + PolynomialType& operator *= (const ElemType v) { + typename Polynomial::const_iterator i; + //Polynomial result; + + for (i = this->begin(); i != this->end(); ++i) { + this->setCoefficient( i->first, i->second*v); + } + return *this; } + PolynomialType& operator += (const ElemType v) { + this->addCoefficient( 0, v); + return *this; + } private: @@ -251,7 +284,7 @@ namespace oopse { Polynomial result; for (i = p.begin(); i !=p.end(); ++i) { - result.addCoefficient( i->first , i->second * v); + result.setCoefficient( i->first , i->second * v); } return result; @@ -263,7 +296,7 @@ namespace oopse { Polynomial result; for (i = p.begin(); i !=p.end(); ++i) { - result.addCoefficient( i->first , i->second * v); + result.setCoefficient( i->first , i->second * v); } return result; @@ -310,7 +343,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 @@ -334,7 +367,7 @@ namespace oopse { return true; } - typedef Polynomial DoublePolynomial; + typedef Polynomial DoublePolynomial; } //end namespace oopse #endif //MATH_POLYNOMIAL_HPP