1 |
< |
/* |
1 |
> |
/* |
2 |
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
* |
4 |
|
* The University of Notre Dame grants you ("Licensee") a |
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) { |
59 |
> |
template<typename ElemType> ElemType pow(ElemType x, int N) { |
60 |
|
ElemType result(1); |
61 |
|
|
62 |
|
for (int i = 0; i < N; ++i) { |
63 |
< |
result *= x; |
63 |
> |
result *= x; |
64 |
|
} |
65 |
|
|
66 |
|
return result; |
67 |
< |
} |
67 |
> |
} |
68 |
|
|
69 |
< |
/** |
70 |
< |
* @class Polynomial Polynomial.hpp "math/Polynomial.hpp" |
71 |
< |
* A generic Polynomial class |
72 |
< |
*/ |
73 |
< |
template<typename ElemType> |
74 |
< |
class Polynomial { |
69 |
> |
/** |
70 |
> |
* @class Polynomial Polynomial.hpp "math/Polynomial.hpp" |
71 |
> |
* A generic Polynomial class |
72 |
> |
*/ |
73 |
> |
template<typename ElemType> |
74 |
> |
class Polynomial { |
75 |
|
|
76 |
< |
public: |
77 |
< |
|
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 |
< |
* Calculates the value of this Polynomial evaluated at the given x value. |
85 |
< |
* @return The value of this Polynomial evaluates at the given x value |
86 |
< |
* @param x the value of the independent variable for this Polynomial function |
87 |
< |
*/ |
88 |
< |
ElemType evaluate(const ElemType& x) { |
89 |
< |
ElemType result = ElemType(); |
90 |
< |
ExponentType exponent; |
91 |
< |
CoefficientType coefficient; |
76 |
> |
public: |
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 |
89 |
> |
* @param x the value of the independent variable for this Polynomial function |
90 |
> |
*/ |
91 |
> |
ElemType evaluate(const ElemType& x) { |
92 |
> |
ElemType result = ElemType(); |
93 |
> |
ExponentType exponent; |
94 |
> |
CoefficientType coefficient; |
95 |
|
|
96 |
< |
for (iterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { |
97 |
< |
exponent = i->first; |
98 |
< |
coefficient = i->second; |
99 |
< |
result += pow(x, exponent) * coefficient; |
100 |
< |
} |
96 |
> |
for (iterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { |
97 |
> |
exponent = i->first; |
98 |
> |
coefficient = i->second; |
99 |
> |
result += pow(x, exponent) * coefficient; |
100 |
> |
} |
101 |
|
|
102 |
< |
return result; |
103 |
< |
} |
102 |
> |
return result; |
103 |
> |
} |
104 |
|
|
105 |
< |
/** |
106 |
< |
* Returns the first derivative of this polynomial. |
107 |
< |
* @return the first derivative of this polynomial |
108 |
< |
* @param x |
109 |
< |
*/ |
110 |
< |
ElemType evaluateDerivative(const ElemType& x) { |
111 |
< |
ElemType result = ElemType(); |
112 |
< |
ExponentType exponent; |
113 |
< |
CoefficientType coefficient; |
105 |
> |
/** |
106 |
> |
* Returns the first derivative of this polynomial. |
107 |
> |
* @return the first derivative of this polynomial |
108 |
> |
* @param x |
109 |
> |
*/ |
110 |
> |
ElemType evaluateDerivative(const ElemType& x) { |
111 |
> |
ElemType result = ElemType(); |
112 |
> |
ExponentType exponent; |
113 |
> |
CoefficientType coefficient; |
114 |
|
|
115 |
< |
for (iterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { |
116 |
< |
exponent = i->first; |
117 |
< |
coefficient = i->second; |
118 |
< |
result += pow(x, exponent - 1) * coefficient * exponent; |
119 |
< |
} |
115 |
> |
for (iterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { |
116 |
> |
exponent = i->first; |
117 |
> |
coefficient = i->second; |
118 |
> |
result += pow(x, exponent - 1) * coefficient * exponent; |
119 |
> |
} |
120 |
|
|
121 |
< |
return result; |
122 |
< |
} |
121 |
> |
return result; |
122 |
> |
} |
123 |
|
|
124 |
< |
/** |
125 |
< |
* Set the coefficent of the specified exponent, if the coefficient is already there, it |
126 |
< |
* will be overwritten. |
127 |
< |
* @param exponent exponent of a term in this Polynomial |
128 |
< |
* @param coefficient multiplier of a term in this Polynomial |
129 |
< |
*/ |
124 |
> |
/** |
125 |
> |
* Set the coefficent of the specified exponent, if the coefficient is already there, it |
126 |
> |
* will be overwritten. |
127 |
> |
* @param exponent exponent of a term in this Polynomial |
128 |
> |
* @param coefficient multiplier of a term in this Polynomial |
129 |
> |
*/ |
130 |
|
|
131 |
< |
void setCoefficient(int exponent, const ElemType& coefficient) { |
132 |
< |
polyPairMap_.insert(typename PolynomialPairMap::value_type(exponent, coefficient)); |
133 |
< |
} |
131 |
> |
void setCoefficient(int exponent, const ElemType& coefficient) { |
132 |
> |
polyPairMap_.insert(typename PolynomialPairMap::value_type(exponent, coefficient)); |
133 |
> |
} |
134 |
|
|
135 |
< |
/** |
136 |
< |
* Set the coefficent of the specified exponent. If the coefficient is already there, just add the |
137 |
< |
* new coefficient to the old one, otherwise, just call setCoefficent |
138 |
< |
* @param exponent exponent of a term in this Polynomial |
139 |
< |
* @param coefficient multiplier of a term in this Polynomial |
140 |
< |
*/ |
135 |
> |
/** |
136 |
> |
* Set the coefficent of the specified exponent. If the coefficient is already there, just add the |
137 |
> |
* new coefficient to the old one, otherwise, just call setCoefficent |
138 |
> |
* @param exponent exponent of a term in this Polynomial |
139 |
> |
* @param coefficient multiplier of a term in this Polynomial |
140 |
> |
*/ |
141 |
|
|
142 |
< |
void addCoefficient(int exponent, const ElemType& coefficient) { |
143 |
< |
iterator i = polyPairMap_.find(exponent); |
142 |
> |
void addCoefficient(int exponent, const ElemType& coefficient) { |
143 |
> |
iterator i = polyPairMap_.find(exponent); |
144 |
|
|
145 |
< |
if (i != end()) { |
146 |
< |
i->second += coefficient; |
147 |
< |
} else { |
148 |
< |
setCoefficient(exponent, coefficient); |
149 |
< |
} |
150 |
< |
} |
145 |
> |
if (i != end()) { |
146 |
> |
i->second += coefficient; |
147 |
> |
} else { |
148 |
> |
setCoefficient(exponent, coefficient); |
149 |
> |
} |
150 |
> |
} |
151 |
|
|
152 |
|
|
153 |
< |
/** |
154 |
< |
* Returns the coefficient associated with the given power for this Polynomial. |
155 |
< |
* @return the coefficient associated with the given power for this Polynomial |
156 |
< |
* @exponent exponent of any term in this Polynomial |
157 |
< |
*/ |
158 |
< |
ElemType getCoefficient(ExponentType exponent) { |
159 |
< |
iterator i = polyPairMap_.find(exponent); |
153 |
> |
/** |
154 |
> |
* Returns the coefficient associated with the given power for this Polynomial. |
155 |
> |
* @return the coefficient associated with the given power for this Polynomial |
156 |
> |
* @exponent exponent of any term in this Polynomial |
157 |
> |
*/ |
158 |
> |
ElemType getCoefficient(ExponentType exponent) { |
159 |
> |
iterator i = polyPairMap_.find(exponent); |
160 |
|
|
161 |
< |
if (i != end()) { |
162 |
< |
return i->second; |
163 |
< |
} else { |
164 |
< |
return ElemType(0); |
165 |
< |
} |
166 |
< |
} |
161 |
> |
if (i != end()) { |
162 |
> |
return i->second; |
163 |
> |
} else { |
164 |
> |
return ElemType(0); |
165 |
> |
} |
166 |
> |
} |
167 |
|
|
168 |
< |
iterator begin() { |
169 |
< |
return polyPairMap_.begin(); |
170 |
< |
} |
168 |
> |
iterator begin() { |
169 |
> |
return polyPairMap_.begin(); |
170 |
> |
} |
171 |
|
|
172 |
< |
const_iterator begin() const{ |
173 |
< |
return polyPairMap_.begin(); |
174 |
< |
} |
172 |
> |
const_iterator begin() const{ |
173 |
> |
return polyPairMap_.begin(); |
174 |
> |
} |
175 |
|
|
176 |
< |
iterator end() { |
177 |
< |
return polyPairMap_.end(); |
178 |
< |
} |
176 |
< |
|
177 |
< |
const_iterator end() const{ |
178 |
< |
return polyPairMap_.end(); |
179 |
< |
} |
176 |
> |
iterator end() { |
177 |
> |
return polyPairMap_.end(); |
178 |
> |
} |
179 |
|
|
180 |
< |
iterator find(ExponentType exponent) { |
181 |
< |
return polyPairMap_.find(exponent); |
180 |
> |
const_iterator end() const{ |
181 |
> |
return polyPairMap_.end(); |
182 |
> |
} |
183 |
> |
|
184 |
> |
iterator find(ExponentType exponent) { |
185 |
> |
return polyPairMap_.find(exponent); |
186 |
> |
} |
187 |
> |
|
188 |
> |
size_t size() { |
189 |
> |
return polyPairMap_.size(); |
190 |
> |
} |
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 |
< |
size_t size() { |
209 |
< |
return polyPairMap_.size(); |
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 |
< |
private: |
243 |
< |
|
191 |
< |
PolynomialPairMap polyPairMap_; |
192 |
< |
}; |
242 |
> |
PolynomialPairMap polyPairMap_; |
243 |
> |
}; |
244 |
|
|
245 |
|
|
246 |
< |
/** |
247 |
< |
* Generates and returns the product of two given Polynomials. |
248 |
< |
* @return A Polynomial containing the product of the two given Polynomial parameters |
249 |
< |
*/ |
250 |
< |
template<typename ElemType> |
251 |
< |
Polynomial<ElemType> operator *(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
246 |
> |
/** |
247 |
> |
* Generates and returns the product of two given Polynomials. |
248 |
> |
* @return A Polynomial containing the product of the two given Polynomial parameters |
249 |
> |
*/ |
250 |
> |
template<typename ElemType> |
251 |
> |
Polynomial<ElemType> operator *(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
252 |
|
typename Polynomial<ElemType>::const_iterator i; |
253 |
|
typename Polynomial<ElemType>::const_iterator j; |
254 |
|
Polynomial<ElemType> p; |
255 |
|
|
256 |
|
for (i = p1.begin(); i !=p1.end(); ++i) { |
257 |
< |
for (j = p2.begin(); j !=p2.end(); ++j) { |
258 |
< |
p.addCoefficient( i->first + j->first, i->second * j->second); |
259 |
< |
} |
257 |
> |
for (j = p2.begin(); j !=p2.end(); ++j) { |
258 |
> |
p.addCoefficient( i->first + j->first, i->second * j->second); |
259 |
> |
} |
260 |
|
} |
261 |
|
|
262 |
|
return p; |
263 |
< |
} |
263 |
> |
} |
264 |
|
|
265 |
< |
/** |
266 |
< |
* Generates and returns the sum of two given Polynomials. |
267 |
< |
* @param p1 the first polynomial |
268 |
< |
* @param p2 the second polynomial |
269 |
< |
*/ |
270 |
< |
template<typename ElemType> |
271 |
< |
Polynomial<ElemType> operator +(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
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 |
292 |
> |
* @param p2 the second polynomial |
293 |
> |
*/ |
294 |
> |
template<typename ElemType> |
295 |
> |
Polynomial<ElemType> operator +(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
296 |
|
Polynomial<ElemType> p(p1); |
297 |
|
|
298 |
|
typename Polynomial<ElemType>::const_iterator i; |
299 |
|
|
300 |
|
for (i = p2.begin(); i != p2.end(); ++i) { |
301 |
< |
p.addCoefficient(i->first, i->second); |
301 |
> |
p.addCoefficient(i->first, i->second); |
302 |
|
} |
303 |
|
|
304 |
|
return p; |
305 |
|
|
306 |
< |
} |
306 |
> |
} |
307 |
|
|
308 |
< |
/** |
309 |
< |
* Generates and returns the difference of two given Polynomials. |
310 |
< |
* @return |
311 |
< |
* @param p1 the first polynomial |
312 |
< |
* @param p2 the second polynomial |
313 |
< |
*/ |
314 |
< |
template<typename ElemType> |
315 |
< |
Polynomial<ElemType> operator -(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
308 |
> |
/** |
309 |
> |
* Generates and returns the difference of two given Polynomials. |
310 |
> |
* @return |
311 |
> |
* @param p1 the first polynomial |
312 |
> |
* @param p2 the second polynomial |
313 |
> |
*/ |
314 |
> |
template<typename ElemType> |
315 |
> |
Polynomial<ElemType> operator -(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
316 |
|
Polynomial<ElemType> p(p1); |
317 |
|
|
318 |
|
typename Polynomial<ElemType>::const_iterator i; |
319 |
|
|
320 |
|
for (i = p2.begin(); i != p2.end(); ++i) { |
321 |
< |
p.addCoefficient(i->first, -i->second); |
321 |
> |
p.addCoefficient(i->first, -i->second); |
322 |
|
} |
323 |
|
|
324 |
|
return p; |
325 |
|
|
326 |
< |
} |
326 |
> |
} |
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 |
331 |
< |
* @param p1 the first polynomial |
332 |
< |
* @param p2 the second polynomial |
333 |
< |
* @note this function does not compare the coefficient |
334 |
< |
*/ |
335 |
< |
template<typename ElemType> |
336 |
< |
bool equal(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
328 |
> |
/** |
329 |
> |
* Tests if two polynomial have the same exponents |
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 |
334 |
> |
*/ |
335 |
> |
template<typename ElemType> |
336 |
> |
bool equal(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
337 |
|
|
338 |
|
typename Polynomial<ElemType>::const_iterator i; |
339 |
|
typename Polynomial<ElemType>::const_iterator j; |
340 |
|
|
341 |
|
if (p1.size() != p2.size() ) { |
342 |
< |
return false; |
342 |
> |
return false; |
343 |
|
} |
344 |
|
|
345 |
|
for (i = p1.begin(), j = p2.begin(); i != p1.end() && j != p2.end(); ++i, ++j) { |
346 |
< |
if (i->first != j->first) { |
347 |
< |
return false; |
348 |
< |
} |
346 |
> |
if (i->first != j->first) { |
347 |
> |
return false; |
348 |
> |
} |
349 |
|
} |
350 |
|
|
351 |
|
return true; |
352 |
< |
} |
352 |
> |
} |
353 |
|
|
354 |
< |
typedef Polynomial<double> DoublePolynomial; |
354 |
> |
typedef Polynomial<RealType> DoublePolynomial; |
355 |
|
|
356 |
|
} //end namespace oopse |
357 |
|
#endif //MATH_POLYNOMIAL_HPP |