ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/math/Polynomial.hpp
(Generate patch)

Comparing trunk/src/math/Polynomial.hpp (file contents):
Revision 507 by gezelter, Fri Apr 15 22:04:00 2005 UTC vs.
Revision 1290 by cli2, Wed Sep 10 19:51:45 2008 UTC

# Line 53 | Line 53
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) {
# Line 74 | Line 74 | namespace oopse {
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
# Line 126 | Line 129 | namespace oopse {
129       */
130          
131      void setCoefficient(int exponent, const ElemType& coefficient) {
132 <      polyPairMap_.insert(typename PolynomialPairMap::value_type(exponent, coefficient));
132 >      polyPairMap_[exponent] = coefficient;
133      }
134  
135      /**
# Line 146 | Line 149 | namespace oopse {
149        }
150      }
151  
149
152      /**
153       * Returns the coefficient associated with the given power for this Polynomial.
154       * @return the coefficient associated with the given power for this Polynomial
# Line 185 | Line 187 | namespace oopse {
187      size_t size() {
188        return polyPairMap_.size();
189      }
190 <        
190 >
191 >    PolynomialType& operator = (const PolynomialType& p) {
192 >
193 >      if (this != &p)  // protect against invalid self-assignment
194 >      {
195 >        typename Polynomial<ElemType>::const_iterator i;
196 >
197 >        polyPairMap_.clear();  // clear out the old map
198 >      
199 >        for (i =  p.begin(); i != p.end(); ++i) {
200 >          this->setCoefficient(i->first, i->second);
201 >        }
202 >      }
203 >      // by convention, always return *this
204 >      return *this;
205 >    }
206 >
207 >    PolynomialType& operator += (const PolynomialType& p) {
208 >        typename Polynomial<ElemType>::const_iterator i;
209 >
210 >        for (i =  p.begin(); i  != p.end(); ++i) {
211 >          this->addCoefficient(i->first, i->second);
212 >        }
213 >
214 >        return *this;        
215 >    }
216 >
217 >    PolynomialType& operator -= (const PolynomialType& p) {
218 >        typename Polynomial<ElemType>::const_iterator i;
219 >        for (i =  p.begin(); i  != p.end(); ++i) {
220 >          this->addCoefficient(i->first, -i->second);
221 >        }        
222 >        return *this;
223 >    }
224 >
225 >    PolynomialType& operator *= (const PolynomialType& p) {
226 >    typename Polynomial<ElemType>::const_iterator i;
227 >    typename Polynomial<ElemType>::const_iterator j;
228 >    Polynomial<ElemType> p2(*this);
229 >  
230 >    polyPairMap_.clear();  // clear out old map
231 >    for (i = p2.begin(); i !=p2.end(); ++i) {
232 >      for (j = p.begin(); j !=p.end(); ++j) {
233 >        this->addCoefficient( i->first + j->first, i->second * j->second);
234 >      }
235 >    }
236 >    return *this;
237 >    }
238 >
239 >    //PolynomialType& operator *= (const ElemType v)
240 >    PolynomialType& operator *= (const ElemType v) {
241 >    typename Polynomial<ElemType>::const_iterator i;
242 >    //Polynomial<ElemType> result;
243 >
244 >    for (i = this->begin(); i != this->end(); ++i) {
245 >        this->setCoefficient( i->first, i->second*v);
246 >    }
247 >
248 >    return *this;
249 >    }
250 >
251 >    PolynomialType& operator += (const ElemType v) {    
252 >    this->addCoefficient( 0, v);
253 >    return *this;
254 >    }
255 >  
256    private:
257          
258      PolynomialPairMap polyPairMap_;
# Line 211 | Line 278 | namespace oopse {
278      return p;
279    }
280  
281 +  template<typename ElemType>
282 +  Polynomial<ElemType> operator *(const Polynomial<ElemType>& p, const ElemType v) {
283 +    typename Polynomial<ElemType>::const_iterator i;
284 +    Polynomial<ElemType> result;
285 +    
286 +    for (i = p.begin(); i !=p.end(); ++i) {
287 +        result.setCoefficient( i->first , i->second * v);
288 +    }
289 +
290 +    return result;
291 +  }
292 +
293 +  template<typename ElemType>
294 +  Polynomial<ElemType> operator *( const ElemType v, const Polynomial<ElemType>& p) {
295 +    typename Polynomial<ElemType>::const_iterator i;
296 +    Polynomial<ElemType> result;
297 +    
298 +    for (i = p.begin(); i !=p.end(); ++i) {
299 +        result.setCoefficient( i->first , i->second * v);
300 +    }
301 +
302 +    return result;
303 +  }
304 +  
305    /**
306     * Generates and returns the sum of two given Polynomials.
307     * @param p1 the first polynomial
# Line 252 | Line 343 | namespace oopse {
343  
344    /**
345     * Tests if two polynomial have the same exponents
346 <   * @return true if these all of the exponents in these Polynomial are identical
346 >   * @return true if all of the exponents in these Polynomial are identical
347     * @param p1 the first polynomial
348     * @param p2 the second polynomial
349     * @note this function does not compare the coefficient
# Line 276 | Line 367 | namespace oopse {
367      return true;
368    }
369  
370 <  typedef Polynomial<double> DoublePolynomial;
370 >  typedef Polynomial<RealType> DoublePolynomial;
371  
372   } //end namespace oopse
373   #endif //MATH_POLYNOMIAL_HPP

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines