--- trunk/OOPSE-4/src/math/Polynomial.hpp 2005/03/01 20:10:14 2069 +++ trunk/OOPSE-4/src/math/Polynomial.hpp 2005/04/15 22:04:00 2204 @@ -1,4 +1,4 @@ - /* +/* * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. * * The University of Notre Dame grants you ("Licensee") a @@ -56,227 +56,227 @@ template ElemType pow(ElemType x, i namespace oopse { -template ElemType pow(ElemType x, int N) { + template ElemType pow(ElemType x, int N) { ElemType result(1); for (int i = 0; i < N; ++i) { - result *= x; + result *= x; } return result; -} + } -/** - * @class Polynomial Polynomial.hpp "math/Polynomial.hpp" - * A generic Polynomial class - */ -template -class Polynomial { + /** + * @class Polynomial Polynomial.hpp "math/Polynomial.hpp" + * A generic Polynomial class + */ + template + class Polynomial { - public: + public: - typedef int ExponentType; - typedef ElemType CoefficientType; - typedef std::map PolynomialPairMap; - typedef typename PolynomialPairMap::iterator iterator; - typedef typename PolynomialPairMap::const_iterator const_iterator; - /** - * 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 = ElemType(); - ExponentType exponent; - CoefficientType coefficient; + typedef int ExponentType; + typedef ElemType CoefficientType; + typedef std::map PolynomialPairMap; + typedef typename PolynomialPairMap::iterator iterator; + typedef typename PolynomialPairMap::const_iterator const_iterator; + /** + * 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 = ElemType(); + ExponentType exponent; + CoefficientType coefficient; - for (iterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { - exponent = i->first; - coefficient = i->second; - result += pow(x, exponent) * coefficient; - } + for (iterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { + exponent = i->first; + coefficient = i->second; + result += pow(x, exponent) * coefficient; + } - return result; - } + return result; + } - /** - * Returns the first derivative of this polynomial. - * @return the first derivative of this polynomial - * @param x - */ - ElemType evaluateDerivative(const ElemType& x) { - ElemType result = ElemType(); - ExponentType exponent; - CoefficientType coefficient; + /** + * Returns the first derivative of this polynomial. + * @return the first derivative of this polynomial + * @param x + */ + ElemType evaluateDerivative(const ElemType& x) { + ElemType result = ElemType(); + ExponentType exponent; + CoefficientType coefficient; - for (iterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { - exponent = i->first; - coefficient = i->second; - result += pow(x, exponent - 1) * coefficient * exponent; - } + for (iterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { + exponent = i->first; + coefficient = i->second; + result += pow(x, exponent - 1) * coefficient * exponent; + } - return result; - } + return result; + } - /** - * Set the coefficent of the specified exponent, if the coefficient is already there, it - * will be overwritten. - * @param exponent exponent of a term in this Polynomial - * @param coefficient multiplier of a term in this Polynomial - */ + /** + * Set the coefficent of the specified exponent, if the coefficient is already there, it + * will be overwritten. + * @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(typename PolynomialPairMap::value_type(exponent, coefficient)); - } + void setCoefficient(int exponent, const ElemType& coefficient) { + polyPairMap_.insert(typename PolynomialPairMap::value_type(exponent, coefficient)); + } - /** - * Set the coefficent of the specified exponent. If the coefficient is already there, just add the - * new coefficient to the old one, otherwise, just call setCoefficent - * @param exponent exponent of a term in this Polynomial - * @param coefficient multiplier of a term in this Polynomial - */ + /** + * Set the coefficent of the specified exponent. If the coefficient is already there, just add the + * new coefficient to the old one, otherwise, just call setCoefficent + * @param exponent exponent of a term in this Polynomial + * @param coefficient multiplier of a term in this Polynomial + */ - void addCoefficient(int exponent, const ElemType& coefficient) { - iterator i = polyPairMap_.find(exponent); + void addCoefficient(int exponent, const ElemType& coefficient) { + iterator i = polyPairMap_.find(exponent); - if (i != end()) { - i->second += coefficient; - } else { - setCoefficient(exponent, coefficient); - } - } + if (i != end()) { + i->second += coefficient; + } else { + setCoefficient(exponent, coefficient); + } + } - /** - * 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) { - iterator i = polyPairMap_.find(exponent); - - if (i != end()) { - return i->second; - } else { - return ElemType(0); - } - } - - iterator begin() { - return polyPairMap_.begin(); - } + /** + * 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) { + iterator i = polyPairMap_.find(exponent); - const_iterator begin() const{ - return polyPairMap_.begin(); - } + if (i != end()) { + return i->second; + } else { + return ElemType(0); + } + } + + iterator begin() { + return polyPairMap_.begin(); + } + + const_iterator begin() const{ + return polyPairMap_.begin(); + } - iterator end() { - return polyPairMap_.end(); - } + iterator end() { + return polyPairMap_.end(); + } - const_iterator end() const{ - return polyPairMap_.end(); - } + const_iterator end() const{ + return polyPairMap_.end(); + } - iterator find(ExponentType exponent) { - return polyPairMap_.find(exponent); - } + iterator find(ExponentType exponent) { + return polyPairMap_.find(exponent); + } - size_t size() { - return polyPairMap_.size(); - } + size_t size() { + return polyPairMap_.size(); + } - private: + private: - PolynomialPairMap polyPairMap_; -}; + 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) { + /** + * 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::const_iterator i; typename Polynomial::const_iterator j; Polynomial p; for (i = p1.begin(); i !=p1.end(); ++i) { - for (j = p2.begin(); j !=p2.end(); ++j) { - p.addCoefficient( i->first + j->first, i->second * j->second); - } + for (j = p2.begin(); j !=p2.end(); ++j) { + p.addCoefficient( i->first + j->first, i->second * j->second); + } } return p; -} + } -/** - * 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) { + /** + * 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::const_iterator i; for (i = p2.begin(); i != p2.end(); ++i) { - p.addCoefficient(i->first, i->second); + p.addCoefficient(i->first, 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) { + /** + * 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::const_iterator i; for (i = p2.begin(); i != p2.end(); ++i) { - p.addCoefficient(i->first, -i->second); + p.addCoefficient(i->first, -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) { + /** + * 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::const_iterator i; typename Polynomial::const_iterator j; if (p1.size() != p2.size() ) { - return false; + return false; } for (i = p1.begin(), j = p2.begin(); i != p1.end() && j != p2.end(); ++i, ++j) { - if (i->first != j->first) { - return false; - } + if (i->first != j->first) { + return false; + } } return true; -} + } -typedef Polynomial DoublePolynomial; + typedef Polynomial DoublePolynomial; } //end namespace oopse #endif //MATH_POLYNOMIAL_HPP