--- branches/new_design/OOPSE-2.0/src/math/Polynomial.hpp 2004/11/17 01:11:36 1745 +++ branches/new_design/OOPSE-2.0/src/math/Polynomial.hpp 2004/11/17 06:37:56 1746 @@ -25,8 +25,8 @@ /** * @file Polynomial.hpp - * @author tlin - * @date 11/01/2004 + * @author teng lin + * @date 11/16/2004 * @version 1.0 */ @@ -38,7 +38,7 @@ template pow(ElemType x) { namespace oopse { -template pow(ElemType x) { +template pow(ElemType x, int N) { ElemType result(1); for (int i = 0; i < N; ++i) { @@ -48,62 +48,82 @@ template pow(ElemType x) { return result; } - +/** + * @class Polynomial Polynomial.hpp "math/Polynomial.hpp" + * A generic Polynomial class + */ template class Polynomial { public: typedef int ExponentType; - typedef ElemType ConstantType; - typedef std::map PolynomialPairMap; + typedef ElemType CoefficientType; + typedef std::map PolynomialPairMap; typedef PolynomialPairMap::iterator PolynomialIterator; - Polynomial(); - - template Polynomial(const Polynomial& p); - template Polynomial& operator=(const Polynomial& p); - /** - * + * Calculates the value of this Polynomial evaluated at the given x value. + * @return The value of this Polynomial evaluates at the given x value + * @param x the value of the independent variable for this Polynomial function */ ElemType evaluate(const ElemType& x) { ElemType result; double exponent; - double constant; + double coefficient; for (PolynomialIterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { exponent = i->first; - constant = i->second; - result += pow(x) * constant; + coefficient = i->second; + result += pow(x, exponent) * coefficient; } return result; } - ElemType evaluateFirstDerivative(const ElemType& x) { + /** + * Returns the first derivative of this polynomial. + * @return the first derivative of this polynomial + * @param x + */ + ElemType evaluateDerivative(const ElemType& x) { ElemType result; double exponent; - double constant; + double coefficient; for (PolynomialIterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { exponent = i->first; - constant = i->second; - result += pow(x) * constant * exponent; + coefficient = i->second; + result += pow(x, exponent - 1) * coefficient * exponent; } return result; } - void addPolynomialTerm(int exponent, const ElemType& constant) { - + /** + * @param exponent exponent of a term in this Polynomial + * @param coefficient multiplier of a term in this Polynomial + */ + + void setCoefficient(int exponent, const ElemType& coefficient) { + polyPairMap_.insert(PolynomialPairMap::value_type(exponent, coefficient)); } - bool getConstant(ExponentType exponent, & constant) { + /** + * Returns the coefficient associated with the given power for this Polynomial. + * @return the coefficient associated with the given power for this Polynomial + * @exponent exponent of any term in this Polynomial + */ + ElemType getCoefficient(ExponentType exponent) { + PolynomialIterator i = find(exponent); + if (i != end()) { + return i->second; + } else { + return ElemType(0); + } } - PolynomialIterator begin() { return polyPairMap_.begin(); } @@ -115,11 +135,123 @@ class Polynomial { PolynomialIterator find(ExponentType exponent) { return polyPairMap_.find(); } - + + size_t size() { + return polyPairMap_.size(); + } + private: PolynomialPairMap polyPairMap_; }; + +/** + * Generates and returns the product of two given Polynomials. + * @return A Polynomial containing the product of the two given Polynomial parameters + */ +template +Polynomial operator *(const Polynomial& p1, const Polynomial& p2) { + typename Polynomial::PolynomialIterator i; + typename Polynomial::PolynomialIterator j; + typename Polynomial::PolynomialIterator k; + Polynomial p; + int exponent; + int coefficient; + + for (i = p1.begin(); i !=p1.end(); ++i) { + for (j = p1.begjn(); j !=p1.end(); ++j) { + exponent = i->first + j->first; + coefficient = i->second * j->second; + k = p->find(exponent); + + if (k != p.end()) { + p[exponent] = coefficient; + } else { + k->second += coefficient; + } + } + } +} + +/** + * Generates and returns the sum of two given Polynomials. + * @param p1 the first polynomial + * @param p2 the second polynomial + */ +template +Polynomial operator +(const Polynomial& p1, const Polynomial& p2) { + Polynomial p(p1); + + typename Polynomial::PolynomialIterator i; + typename Polynomial::PolynomialIterator j; + + for (i = p2.begin(); i != p2.end(); ++i) { + j = p.find(i->first); + if (j == p.end()) { + p[j] = i->second; + } else { + j->second += i->second; + } + } + + return p; + +} + +/** + * Generates and returns the difference of two given Polynomials. + * @return + * @param p1 the first polynomial + * @param p2 the second polynomial + */ +template +Polynomial operator -(const Polynomial& p1, const Polynomial& p2) { + Polynomial p(p1); + + typename Polynomial::PolynomialIterator i; + typename Polynomial::PolynomialIterator j; + + for (i = p2.begin(); i != p2.end(); ++i) { + j = p.find(i->first); + if (j == p.end()) { + p[j] = -i->second; + } else { + j->second -= i->second; + } + } + + return p; + +} + +/** + * Tests if two polynomial have the same exponents + * @return true if these 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 + */ +template +bool equal(const Polynomial& p1, const Polynomial& p2) { + + typename Polynomial::PolynomialIterator i; + typename Polynomial::PolynomialIterator j; + + if (p1.size() !== p2.size() ) { + return false; + } + + for (i = p1.begin(), j = p2.begin(); i != p1.end() && j != p2.end(); ++i, ++j) { + if (i->first != j->first) { + return false; + } + } + + return true; +} + +typedef Polynomial DoublePolynomial; + } //end namespace oopse #endif //MATH_POLYNOMIAL_HPP