ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-2.0/src/math/Polynomial.hpp
(Generate patch)

Comparing branches/new_design/OOPSE-2.0/src/math/Polynomial.hpp (file contents):
Revision 1745 by tim, Tue Nov 16 23:20:03 2004 UTC vs.
Revision 1746 by tim, Wed Nov 17 06:37:56 2004 UTC

# Line 25 | Line 25
25  
26   /**
27   * @file Polynomial.hpp
28 < * @author    tlin
29 < * @date  11/01/2004
28 > * @author    teng lin
29 > * @date  11/16/2004
30   * @version 1.0
31   */
32  
# Line 38 | Line 38 | template<typename ElemType, int N> pow(ElemType x) {
38  
39   namespace oopse {
40  
41 < template<typename ElemType, int N> pow(ElemType x) {
41 > template<typename ElemType> pow(ElemType x, int N) {
42      ElemType result(1);
43  
44      for (int i = 0; i < N; ++i) {
# Line 48 | Line 48 | template<typename ElemType, int N> pow(ElemType x) {
48      return result;
49   }
50  
51 <
51 > /**
52 > * @class Polynomial Polynomial.hpp "math/Polynomial.hpp"
53 > * A generic Polynomial class
54 > */
55   template<typename ElemType>
56   class Polynomial {
57  
58      public:
59          
60          typedef int ExponentType;
61 <        typedef ElemType ConstantType;
62 <        typedef std::map<ExponentType, ConstantType> PolynomialPairMap;
61 >        typedef ElemType CoefficientType;
62 >        typedef std::map<ExponentType, CoefficientType> PolynomialPairMap;
63          typedef PolynomialPairMap::iterator PolynomialIterator;
64  
62        Polynomial();
63
64        template<U> Polynomial(const Polynomial<U>& p);
65        template<U> Polynomial& operator=(const Polynomial<U>& p);
66
65          /**
66 <         *
66 >         * Calculates the value of this Polynomial evaluated at the given x value.
67 >         * @return The value of this Polynomial evaluates at the given x value
68 >         * @param x the value of the independent variable for this Polynomial function
69           */
70          ElemType evaluate(const ElemType& x) {
71              ElemType result;
72              double exponent;
73 <            double constant;
73 >            double coefficient;
74              
75              for (PolynomialIterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) {
76                  exponent = i->first;
77 <                constant = i->second;
78 <                result  += pow<exponent>(x) * constant;
77 >                coefficient = i->second;
78 >                result  += pow(x, exponent) * coefficient;
79              }
80  
81              return result;
82          }
83  
84 <        ElemType evaluateFirstDerivative(const ElemType& x) {
84 >        /**
85 >         * Returns the first derivative of this polynomial.
86 >         * @return the first derivative of this polynomial
87 >         * @param x
88 >         */
89 >        ElemType evaluateDerivative(const ElemType& x) {
90              ElemType result;
91              double exponent;
92 <            double constant;
92 >            double coefficient;
93              
94              for (PolynomialIterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) {
95                  exponent = i->first;
96 <                constant = i->second;
97 <                result  += pow<exponent - 1>(x) * constant * exponent;
96 >                coefficient = i->second;
97 >                result  += pow(x, exponent - 1) * coefficient * exponent;
98              }
99  
100              return result;
101          }
102  
103 <        void addPolynomialTerm(int exponent, const ElemType& constant) {
104 <
103 >        /**
104 >         * @param exponent exponent of a term in this Polynomial
105 >         * @param coefficient multiplier of a term in this Polynomial
106 >         */
107 >        
108 >        void setCoefficient(int exponent, const ElemType& coefficient) {
109 >            polyPairMap_.insert(PolynomialPairMap::value_type(exponent, coefficient));
110          }
111  
112 <        bool getConstant(ExponentType exponent, & constant) {
112 >        /**
113 >         * Returns the coefficient associated with the given power for this Polynomial.
114 >         * @return the coefficient associated with the given power for this Polynomial
115 >         * @exponent exponent of any term in this Polynomial
116 >         */
117 >        ElemType getCoefficient(ExponentType exponent) {
118 >            PolynomialIterator i = find(exponent);
119  
120 +            if (i != end()) {
121 +                return i->second;
122 +            } else {
123 +                return ElemType(0);
124 +            }
125          }
126  
106
127          PolynomialIterator begin() {
128              return polyPairMap_.begin();
129          }
# Line 115 | Line 135 | class Polynomial {
135          PolynomialIterator find(ExponentType exponent) {
136              return polyPairMap_.find();
137          }
138 <                      
138 >
139 >        size_t size() {
140 >            return polyPairMap_.size();
141 >        }
142 >        
143      private:
144          
145          PolynomialPairMap polyPairMap_;
146   };
147  
148 +
149 + /**
150 + * Generates and returns the product of two given Polynomials.
151 + * @return A Polynomial containing the product of the two given Polynomial parameters
152 + */
153 + template<typename ElemType>
154 + Polynomial<ElemType> operator *(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) {
155 +    typename Polynomial<ElemType>::PolynomialIterator i;
156 +    typename Polynomial<ElemType>::PolynomialIterator j;
157 +    typename Polynomial<ElemType>::PolynomialIterator k;
158 +    Polynomial<ElemType> p;
159 +    int exponent;
160 +    int coefficient;
161 +    
162 +    for (i = p1.begin(); i !=p1.end(); ++i) {
163 +        for (j = p1.begjn(); j !=p1.end(); ++j) {
164 +            exponent = i->first + j->first;            
165 +            coefficient = i->second * j->second;
166 +            k = p->find(exponent);
167 +
168 +            if (k != p.end()) {
169 +                p[exponent] = coefficient;
170 +            } else {
171 +                k->second += coefficient;
172 +            }
173 +        }
174 +    }
175 + }
176 +
177 + /**
178 + * Generates and returns the sum of two given Polynomials.
179 + * @param p1 the first polynomial
180 + * @param p2 the second polynomial
181 + */
182 + template<typename ElemType>
183 + Polynomial<ElemType> operator +(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) {
184 +    Polynomial<ElemType> p(p1);
185 +
186 +    typename Polynomial<ElemType>::PolynomialIterator i;
187 +    typename Polynomial<ElemType>::PolynomialIterator j;
188 +
189 +    for (i =  p2.begin(); i  != p2.end(); ++i) {
190 +        j = p.find(i->first);
191 +        if (j == p.end()) {
192 +            p[j] = i->second;
193 +        } else {
194 +            j->second += i->second;
195 +        }
196 +    }
197 +
198 +    return p;
199 +
200 + }
201 +
202 + /**
203 + * Generates and returns the difference of two given Polynomials.
204 + * @return
205 + * @param p1 the first polynomial
206 + * @param p2 the second polynomial
207 + */
208 + template<typename ElemType>
209 + Polynomial<ElemType> operator -(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) {
210 +    Polynomial<ElemType> p(p1);
211 +
212 +    typename Polynomial<ElemType>::PolynomialIterator i;
213 +    typename Polynomial<ElemType>::PolynomialIterator j;
214 +
215 +    for (i =  p2.begin(); i  != p2.end(); ++i) {
216 +        j = p.find(i->first);
217 +        if (j == p.end()) {
218 +            p[j] = -i->second;
219 +        } else {
220 +            j->second -= i->second;
221 +        }
222 +    }
223 +
224 +    return p;
225 +
226 + }
227 +
228 + /**
229 + * Tests if two polynomial have the same exponents
230 + * @return true if these all of the exponents in these Polynomial are identical
231 + * @param p1 the first polynomial
232 + * @param p2 the second polynomial
233 + * @note this function does not compare the coefficient
234 + */
235 + template<typename ElemType>
236 + bool equal(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) {
237 +
238 +    typename Polynomial<ElemType>::PolynomialIterator i;
239 +    typename Polynomial<ElemType>::PolynomialIterator j;
240 +
241 +    if (p1.size() !== p2.size() ) {
242 +        return false;
243 +    }
244 +    
245 +    for (i =  p1.begin(), j = p2.begin(); i  != p1.end() && j != p2.end(); ++i, ++j) {
246 +        if (i->first != j->first) {
247 +            return false;
248 +        }
249 +    }
250 +
251 +    return true;
252 + }
253 +
254 + typedef Polynomial<double> DoublePolynomial;
255 +
256   } //end namespace oopse
257   #endif //MATH_POLYNOMIAL_HPP

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines