| 53 | 
  | 
#include <list> | 
| 54 | 
  | 
#include <map> | 
| 55 | 
  | 
#include <utility> | 
| 56 | 
< | 
 | 
| 56 | 
> | 
#include "config.h" | 
| 57 | 
  | 
namespace oopse { | 
| 58 | 
  | 
 | 
| 59 | 
  | 
  template<typename ElemType> ElemType pow(ElemType x, int N) { | 
| 74 | 
  | 
  class Polynomial { | 
| 75 | 
  | 
 | 
| 76 | 
  | 
  public: | 
| 77 | 
< | 
         | 
| 77 | 
> | 
    typedef Polynomial<ElemType> PolynomialType;     | 
| 78 | 
  | 
    typedef int ExponentType; | 
| 79 | 
  | 
    typedef ElemType CoefficientType; | 
| 80 | 
  | 
    typedef std::map<ExponentType, CoefficientType> PolynomialPairMap; | 
| 81 | 
  | 
    typedef typename PolynomialPairMap::iterator iterator; | 
| 82 | 
  | 
    typedef typename PolynomialPairMap::const_iterator const_iterator; | 
| 83 | 
+ | 
 | 
| 84 | 
+ | 
    Polynomial() {} | 
| 85 | 
+ | 
    Polynomial(ElemType v) {setCoefficient(0, v);} | 
| 86 | 
  | 
    /**  | 
| 87 | 
  | 
     * Calculates the value of this Polynomial evaluated at the given x value. | 
| 88 | 
  | 
     * @return The value of this Polynomial evaluates at the given x value | 
| 188 | 
  | 
    size_t size() { | 
| 189 | 
  | 
      return polyPairMap_.size(); | 
| 190 | 
  | 
    } | 
| 191 | 
< | 
         | 
| 191 | 
> | 
 | 
| 192 | 
> | 
    PolynomialType& operator = (const PolynomialType& p) { | 
| 193 | 
> | 
 | 
| 194 | 
> | 
      if (this != &p)  // protect against invalid self-assignment | 
| 195 | 
> | 
      { | 
| 196 | 
> | 
        typename Polynomial<ElemType>::const_iterator i; | 
| 197 | 
> | 
 | 
| 198 | 
> | 
        polyPairMap_.clear();  // clear out the old map | 
| 199 | 
> | 
       | 
| 200 | 
> | 
        for (i =  p.begin(); i != p.end(); ++i) { | 
| 201 | 
> | 
          this->setCoefficient(i->first, i->second); | 
| 202 | 
> | 
        } | 
| 203 | 
> | 
      } | 
| 204 | 
> | 
      // by convention, always return *this | 
| 205 | 
> | 
      return *this;  | 
| 206 | 
> | 
    } | 
| 207 | 
> | 
 | 
| 208 | 
> | 
    PolynomialType& operator += (const PolynomialType& p) { | 
| 209 | 
> | 
        typename Polynomial<ElemType>::const_iterator i; | 
| 210 | 
> | 
 | 
| 211 | 
> | 
        for (i =  p.begin(); i  != p.end(); ++i) { | 
| 212 | 
> | 
          this->addCoefficient(i->first, i->second); | 
| 213 | 
> | 
        } | 
| 214 | 
> | 
 | 
| 215 | 
> | 
        return *this;         | 
| 216 | 
> | 
    } | 
| 217 | 
> | 
 | 
| 218 | 
> | 
    PolynomialType& operator -= (const PolynomialType& p) { | 
| 219 | 
> | 
        typename Polynomial<ElemType>::const_iterator i; | 
| 220 | 
> | 
        for (i =  p.begin(); i  != p.end(); ++i) { | 
| 221 | 
> | 
          this->addCoefficient(i->first, -i->second); | 
| 222 | 
> | 
        }         | 
| 223 | 
> | 
        return *this; | 
| 224 | 
> | 
    } | 
| 225 | 
> | 
 | 
| 226 | 
> | 
    PolynomialType& operator *= (const PolynomialType& p) { | 
| 227 | 
> | 
    typename Polynomial<ElemType>::const_iterator i; | 
| 228 | 
> | 
    typename Polynomial<ElemType>::const_iterator j; | 
| 229 | 
> | 
     | 
| 230 | 
> | 
    for (i = this->begin(); i !=this->end(); ++i) { | 
| 231 | 
> | 
      for (j = p.begin(); j !=p.end(); ++j) { | 
| 232 | 
> | 
        this->addCoefficient( i->first + j->first, i->second * j->second); | 
| 233 | 
> | 
      } | 
| 234 | 
> | 
    } | 
| 235 | 
> | 
 | 
| 236 | 
> | 
    return *this; | 
| 237 | 
> | 
    } | 
| 238 | 
> | 
 | 
| 239 | 
> | 
    | 
| 240 | 
  | 
  private: | 
| 241 | 
  | 
         | 
| 242 | 
  | 
    PolynomialPairMap polyPairMap_; | 
| 262 | 
  | 
    return p; | 
| 263 | 
  | 
  } | 
| 264 | 
  | 
 | 
| 265 | 
+ | 
  template<typename ElemType> | 
| 266 | 
+ | 
  Polynomial<ElemType> operator *(const Polynomial<ElemType>& p, const ElemType v) { | 
| 267 | 
+ | 
    typename Polynomial<ElemType>::const_iterator i; | 
| 268 | 
+ | 
    Polynomial<ElemType> result; | 
| 269 | 
+ | 
     | 
| 270 | 
+ | 
    for (i = p.begin(); i !=p.end(); ++i) { | 
| 271 | 
+ | 
        result.addCoefficient( i->first , i->second * v); | 
| 272 | 
+ | 
    } | 
| 273 | 
+ | 
 | 
| 274 | 
+ | 
    return result; | 
| 275 | 
+ | 
  } | 
| 276 | 
+ | 
 | 
| 277 | 
+ | 
  template<typename ElemType> | 
| 278 | 
+ | 
  Polynomial<ElemType> operator *( const ElemType v, const Polynomial<ElemType>& p) { | 
| 279 | 
+ | 
    typename Polynomial<ElemType>::const_iterator i; | 
| 280 | 
+ | 
    Polynomial<ElemType> result; | 
| 281 | 
+ | 
     | 
| 282 | 
+ | 
    for (i = p.begin(); i !=p.end(); ++i) { | 
| 283 | 
+ | 
        result.addCoefficient( i->first , i->second * v); | 
| 284 | 
+ | 
    } | 
| 285 | 
+ | 
 | 
| 286 | 
+ | 
    return result; | 
| 287 | 
+ | 
  } | 
| 288 | 
+ | 
   | 
| 289 | 
  | 
  /** | 
| 290 | 
  | 
   * Generates and returns the sum of two given Polynomials. | 
| 291 | 
  | 
   * @param p1 the first polynomial | 
| 327 | 
  | 
 | 
| 328 | 
  | 
  /** | 
| 329 | 
  | 
   * Tests if two polynomial have the same exponents | 
| 330 | 
< | 
   * @return true if these all of the exponents in these Polynomial are identical | 
| 330 | 
> | 
   * @return true if all of the exponents in these Polynomial are identical | 
| 331 | 
  | 
   * @param p1 the first polynomial | 
| 332 | 
  | 
   * @param p2 the second polynomial | 
| 333 | 
  | 
   * @note this function does not compare the coefficient | 
| 351 | 
  | 
    return true; | 
| 352 | 
  | 
  } | 
| 353 | 
  | 
 | 
| 354 | 
< | 
  typedef Polynomial<double> DoublePolynomial; | 
| 354 | 
> | 
  typedef Polynomial<RealType> DoublePolynomial; | 
| 355 | 
  | 
 | 
| 356 | 
  | 
} //end namespace oopse | 
| 357 | 
  | 
#endif //MATH_POLYNOMIAL_HPP |