1 |
gezelter |
507 |
/* |
2 |
gezelter |
246 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
|
* |
4 |
|
|
* The University of Notre Dame grants you ("Licensee") a |
5 |
|
|
* non-exclusive, royalty free, license to use, modify and |
6 |
|
|
* redistribute this software in source and binary code form, provided |
7 |
|
|
* that the following conditions are met: |
8 |
|
|
* |
9 |
|
|
* 1. Acknowledgement of the program authors must be made in any |
10 |
|
|
* publication of scientific results based in part on use of the |
11 |
|
|
* program. An acceptable form of acknowledgement is citation of |
12 |
|
|
* the article in which the program was described (Matthew |
13 |
|
|
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
|
|
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
|
|
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
|
|
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
|
|
* |
18 |
|
|
* 2. Redistributions of source code must retain the above copyright |
19 |
|
|
* notice, this list of conditions and the following disclaimer. |
20 |
|
|
* |
21 |
|
|
* 3. Redistributions in binary form must reproduce the above copyright |
22 |
|
|
* notice, this list of conditions and the following disclaimer in the |
23 |
|
|
* documentation and/or other materials provided with the |
24 |
|
|
* distribution. |
25 |
|
|
* |
26 |
|
|
* This software is provided "AS IS," without a warranty of any |
27 |
|
|
* kind. All express or implied conditions, representations and |
28 |
|
|
* warranties, including any implied warranty of merchantability, |
29 |
|
|
* fitness for a particular purpose or non-infringement, are hereby |
30 |
|
|
* excluded. The University of Notre Dame and its licensors shall not |
31 |
|
|
* be liable for any damages suffered by licensee as a result of |
32 |
|
|
* using, modifying or distributing the software or its |
33 |
|
|
* derivatives. In no event will the University of Notre Dame or its |
34 |
|
|
* licensors be liable for any lost revenue, profit or data, or for |
35 |
|
|
* direct, indirect, special, consequential, incidental or punitive |
36 |
|
|
* damages, however caused and regardless of the theory of liability, |
37 |
|
|
* arising out of the use of or inability to use software, even if the |
38 |
|
|
* University of Notre Dame has been advised of the possibility of |
39 |
|
|
* such damages. |
40 |
|
|
*/ |
41 |
|
|
|
42 |
|
|
/** |
43 |
|
|
* @file Polynomial.hpp |
44 |
|
|
* @author teng lin |
45 |
|
|
* @date 11/16/2004 |
46 |
|
|
* @version 1.0 |
47 |
|
|
*/ |
48 |
|
|
|
49 |
|
|
#ifndef MATH_POLYNOMIAL_HPP |
50 |
|
|
#define MATH_POLYNOMIAL_HPP |
51 |
|
|
|
52 |
|
|
#include <iostream> |
53 |
|
|
#include <list> |
54 |
|
|
#include <map> |
55 |
|
|
#include <utility> |
56 |
tim |
963 |
#include "config.h" |
57 |
gezelter |
246 |
namespace oopse { |
58 |
|
|
|
59 |
gezelter |
507 |
template<typename ElemType> ElemType pow(ElemType x, int N) { |
60 |
gezelter |
246 |
ElemType result(1); |
61 |
|
|
|
62 |
|
|
for (int i = 0; i < N; ++i) { |
63 |
gezelter |
507 |
result *= x; |
64 |
gezelter |
246 |
} |
65 |
|
|
|
66 |
|
|
return result; |
67 |
gezelter |
507 |
} |
68 |
gezelter |
246 |
|
69 |
gezelter |
507 |
/** |
70 |
|
|
* @class Polynomial Polynomial.hpp "math/Polynomial.hpp" |
71 |
|
|
* A generic Polynomial class |
72 |
|
|
*/ |
73 |
|
|
template<typename ElemType> |
74 |
|
|
class Polynomial { |
75 |
gezelter |
246 |
|
76 |
gezelter |
507 |
public: |
77 |
tim |
749 |
typedef Polynomial<ElemType> PolynomialType; |
78 |
gezelter |
507 |
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 |
tim |
749 |
|
84 |
|
|
Polynomial() {} |
85 |
|
|
Polynomial(ElemType v) {setCoefficient(0, v);} |
86 |
gezelter |
507 |
/** |
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 |
gezelter |
246 |
|
96 |
gezelter |
507 |
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 |
gezelter |
246 |
|
102 |
gezelter |
507 |
return result; |
103 |
|
|
} |
104 |
gezelter |
246 |
|
105 |
gezelter |
507 |
/** |
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 |
gezelter |
246 |
|
115 |
gezelter |
507 |
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 |
gezelter |
246 |
|
121 |
gezelter |
507 |
return result; |
122 |
|
|
} |
123 |
gezelter |
246 |
|
124 |
gezelter |
507 |
/** |
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 |
gezelter |
246 |
|
131 |
gezelter |
507 |
void setCoefficient(int exponent, const ElemType& coefficient) { |
132 |
|
|
polyPairMap_.insert(typename PolynomialPairMap::value_type(exponent, coefficient)); |
133 |
|
|
} |
134 |
gezelter |
246 |
|
135 |
gezelter |
507 |
/** |
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 |
gezelter |
246 |
|
142 |
gezelter |
507 |
void addCoefficient(int exponent, const ElemType& coefficient) { |
143 |
|
|
iterator i = polyPairMap_.find(exponent); |
144 |
gezelter |
246 |
|
145 |
gezelter |
507 |
if (i != end()) { |
146 |
|
|
i->second += coefficient; |
147 |
|
|
} else { |
148 |
|
|
setCoefficient(exponent, coefficient); |
149 |
|
|
} |
150 |
|
|
} |
151 |
gezelter |
246 |
|
152 |
|
|
|
153 |
gezelter |
507 |
/** |
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 |
gezelter |
246 |
|
161 |
gezelter |
507 |
if (i != end()) { |
162 |
|
|
return i->second; |
163 |
|
|
} else { |
164 |
|
|
return ElemType(0); |
165 |
|
|
} |
166 |
|
|
} |
167 |
gezelter |
246 |
|
168 |
gezelter |
507 |
iterator begin() { |
169 |
|
|
return polyPairMap_.begin(); |
170 |
|
|
} |
171 |
gezelter |
246 |
|
172 |
gezelter |
507 |
const_iterator begin() const{ |
173 |
|
|
return polyPairMap_.begin(); |
174 |
|
|
} |
175 |
gezelter |
246 |
|
176 |
gezelter |
507 |
iterator end() { |
177 |
|
|
return polyPairMap_.end(); |
178 |
|
|
} |
179 |
gezelter |
246 |
|
180 |
gezelter |
507 |
const_iterator end() const{ |
181 |
|
|
return polyPairMap_.end(); |
182 |
|
|
} |
183 |
gezelter |
246 |
|
184 |
gezelter |
507 |
iterator find(ExponentType exponent) { |
185 |
|
|
return polyPairMap_.find(exponent); |
186 |
|
|
} |
187 |
gezelter |
246 |
|
188 |
gezelter |
507 |
size_t size() { |
189 |
|
|
return polyPairMap_.size(); |
190 |
|
|
} |
191 |
tim |
749 |
|
192 |
|
|
PolynomialType& operator += (const PolynomialType& p) { |
193 |
|
|
typename Polynomial<ElemType>::const_iterator i; |
194 |
|
|
|
195 |
|
|
for (i = p.begin(); i != p.end(); ++i) { |
196 |
|
|
this->addCoefficient(i->first, i->second); |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
return *this; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
PolynomialType& operator -= (const PolynomialType& p) { |
203 |
|
|
typename Polynomial<ElemType>::const_iterator i; |
204 |
|
|
for (i = p.begin(); i != p.end(); ++i) { |
205 |
|
|
this->addCoefficient(i->first, -i->second); |
206 |
|
|
} |
207 |
gezelter |
877 |
return *this; |
208 |
tim |
749 |
} |
209 |
|
|
|
210 |
|
|
PolynomialType& operator *= (const PolynomialType& p) { |
211 |
|
|
typename Polynomial<ElemType>::const_iterator i; |
212 |
|
|
typename Polynomial<ElemType>::const_iterator j; |
213 |
|
|
|
214 |
|
|
for (i = this->begin(); i !=this->end(); ++i) { |
215 |
|
|
for (j = p.begin(); j !=p.end(); ++j) { |
216 |
|
|
this->addCoefficient( i->first + j->first, i->second * j->second); |
217 |
|
|
} |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
return *this; |
221 |
|
|
} |
222 |
|
|
|
223 |
|
|
|
224 |
gezelter |
507 |
private: |
225 |
gezelter |
246 |
|
226 |
gezelter |
507 |
PolynomialPairMap polyPairMap_; |
227 |
|
|
}; |
228 |
gezelter |
246 |
|
229 |
|
|
|
230 |
gezelter |
507 |
/** |
231 |
|
|
* Generates and returns the product of two given Polynomials. |
232 |
|
|
* @return A Polynomial containing the product of the two given Polynomial parameters |
233 |
|
|
*/ |
234 |
|
|
template<typename ElemType> |
235 |
|
|
Polynomial<ElemType> operator *(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
236 |
gezelter |
246 |
typename Polynomial<ElemType>::const_iterator i; |
237 |
|
|
typename Polynomial<ElemType>::const_iterator j; |
238 |
|
|
Polynomial<ElemType> p; |
239 |
|
|
|
240 |
|
|
for (i = p1.begin(); i !=p1.end(); ++i) { |
241 |
gezelter |
507 |
for (j = p2.begin(); j !=p2.end(); ++j) { |
242 |
|
|
p.addCoefficient( i->first + j->first, i->second * j->second); |
243 |
|
|
} |
244 |
gezelter |
246 |
} |
245 |
|
|
|
246 |
|
|
return p; |
247 |
gezelter |
507 |
} |
248 |
gezelter |
246 |
|
249 |
tim |
876 |
template<typename ElemType> |
250 |
|
|
Polynomial<ElemType> operator *(const Polynomial<ElemType>& p, const ElemType v) { |
251 |
|
|
typename Polynomial<ElemType>::const_iterator i; |
252 |
|
|
Polynomial<ElemType> result; |
253 |
|
|
|
254 |
|
|
for (i = p.begin(); i !=p.end(); ++i) { |
255 |
|
|
result.addCoefficient( i->first , i->second * v); |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
return result; |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
template<typename ElemType> |
262 |
|
|
Polynomial<ElemType> operator *( const ElemType v, const Polynomial<ElemType>& p) { |
263 |
|
|
typename Polynomial<ElemType>::const_iterator i; |
264 |
|
|
Polynomial<ElemType> result; |
265 |
|
|
|
266 |
|
|
for (i = p.begin(); i !=p.end(); ++i) { |
267 |
|
|
result.addCoefficient( i->first , i->second * v); |
268 |
|
|
} |
269 |
|
|
|
270 |
|
|
return result; |
271 |
|
|
} |
272 |
|
|
|
273 |
gezelter |
507 |
/** |
274 |
|
|
* Generates and returns the sum of two given Polynomials. |
275 |
|
|
* @param p1 the first polynomial |
276 |
|
|
* @param p2 the second polynomial |
277 |
|
|
*/ |
278 |
|
|
template<typename ElemType> |
279 |
|
|
Polynomial<ElemType> operator +(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
280 |
gezelter |
246 |
Polynomial<ElemType> p(p1); |
281 |
|
|
|
282 |
|
|
typename Polynomial<ElemType>::const_iterator i; |
283 |
|
|
|
284 |
|
|
for (i = p2.begin(); i != p2.end(); ++i) { |
285 |
gezelter |
507 |
p.addCoefficient(i->first, i->second); |
286 |
gezelter |
246 |
} |
287 |
|
|
|
288 |
|
|
return p; |
289 |
|
|
|
290 |
gezelter |
507 |
} |
291 |
gezelter |
246 |
|
292 |
gezelter |
507 |
/** |
293 |
|
|
* Generates and returns the difference of two given Polynomials. |
294 |
|
|
* @return |
295 |
|
|
* @param p1 the first polynomial |
296 |
|
|
* @param p2 the second polynomial |
297 |
|
|
*/ |
298 |
|
|
template<typename ElemType> |
299 |
|
|
Polynomial<ElemType> operator -(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
300 |
gezelter |
246 |
Polynomial<ElemType> p(p1); |
301 |
|
|
|
302 |
|
|
typename Polynomial<ElemType>::const_iterator i; |
303 |
|
|
|
304 |
|
|
for (i = p2.begin(); i != p2.end(); ++i) { |
305 |
gezelter |
507 |
p.addCoefficient(i->first, -i->second); |
306 |
gezelter |
246 |
} |
307 |
|
|
|
308 |
|
|
return p; |
309 |
|
|
|
310 |
gezelter |
507 |
} |
311 |
gezelter |
246 |
|
312 |
gezelter |
507 |
/** |
313 |
|
|
* Tests if two polynomial have the same exponents |
314 |
tim |
883 |
* @return true if all of the exponents in these Polynomial are identical |
315 |
gezelter |
507 |
* @param p1 the first polynomial |
316 |
|
|
* @param p2 the second polynomial |
317 |
|
|
* @note this function does not compare the coefficient |
318 |
|
|
*/ |
319 |
|
|
template<typename ElemType> |
320 |
|
|
bool equal(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
321 |
gezelter |
246 |
|
322 |
|
|
typename Polynomial<ElemType>::const_iterator i; |
323 |
|
|
typename Polynomial<ElemType>::const_iterator j; |
324 |
|
|
|
325 |
|
|
if (p1.size() != p2.size() ) { |
326 |
gezelter |
507 |
return false; |
327 |
gezelter |
246 |
} |
328 |
|
|
|
329 |
|
|
for (i = p1.begin(), j = p2.begin(); i != p1.end() && j != p2.end(); ++i, ++j) { |
330 |
gezelter |
507 |
if (i->first != j->first) { |
331 |
|
|
return false; |
332 |
|
|
} |
333 |
gezelter |
246 |
} |
334 |
|
|
|
335 |
|
|
return true; |
336 |
gezelter |
507 |
} |
337 |
gezelter |
246 |
|
338 |
tim |
963 |
typedef Polynomial<RealType> DoublePolynomial; |
339 |
gezelter |
246 |
|
340 |
|
|
} //end namespace oopse |
341 |
|
|
#endif //MATH_POLYNOMIAL_HPP |