ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-4/src/math/Polynomial.hpp
Revision: 1856
Committed: Mon Dec 6 04:49:53 2004 UTC (19 years, 7 months ago) by tim
File size: 8037 byte(s)
Log Message:
fix a bug in Exclude List

File Contents

# Content
1 /*
2 * Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project
3 *
4 * Contact: oopse@oopse.org
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1
9 * of the License, or (at your option) any later version.
10 * All we ask is that proper credit is given for our work, which includes
11 * - but is not limited to - adding the above copyright notice to the beginning
12 * of your source code files, and to any copyright notice that you may distribute
13 * with programs based on this work.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 */
25
26 /**
27 * @file Polynomial.hpp
28 * @author teng lin
29 * @date 11/16/2004
30 * @version 1.0
31 */
32
33 #ifndef MATH_POLYNOMIAL_HPP
34 #define MATH_POLYNOMIAL_HPP
35
36 #include <iostream>
37 #include <list>
38 #include <map>
39 #include <utility>
40
41 namespace oopse {
42
43 template<typename ElemType> ElemType pow(ElemType x, int N) {
44 ElemType result(1);
45
46 for (int i = 0; i < N; ++i) {
47 result *= x;
48 }
49
50 return result;
51 }
52
53 /**
54 * @class Polynomial Polynomial.hpp "math/Polynomial.hpp"
55 * A generic Polynomial class
56 */
57 template<typename ElemType>
58 class Polynomial {
59
60 public:
61
62 typedef int ExponentType;
63 typedef ElemType CoefficientType;
64 typedef std::map<ExponentType, CoefficientType> PolynomialPairMap;
65 typedef typename PolynomialPairMap::iterator iterator;
66 typedef typename PolynomialPairMap::const_iterator const_iterator;
67 /**
68 * Calculates the value of this Polynomial evaluated at the given x value.
69 * @return The value of this Polynomial evaluates at the given x value
70 * @param x the value of the independent variable for this Polynomial function
71 */
72 ElemType evaluate(const ElemType& x) {
73 ElemType result = ElemType();
74 ExponentType exponent;
75 CoefficientType coefficient;
76
77 for (iterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) {
78 exponent = i->first;
79 coefficient = i->second;
80 result += pow(x, exponent) * coefficient;
81 }
82
83 return result;
84 }
85
86 /**
87 * Returns the first derivative of this polynomial.
88 * @return the first derivative of this polynomial
89 * @param x
90 */
91 ElemType evaluateDerivative(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 - 1) * coefficient * exponent;
100 }
101
102 return result;
103 }
104
105 /**
106 * Set the coefficent of the specified exponent, if the coefficient is already there, it
107 * will be overwritten.
108 * @param exponent exponent of a term in this Polynomial
109 * @param coefficient multiplier of a term in this Polynomial
110 */
111
112 void setCoefficient(int exponent, const ElemType& coefficient) {
113 polyPairMap_.insert(PolynomialPairMap::value_type(exponent, coefficient));
114 }
115
116 /**
117 * Set the coefficent of the specified exponent. If the coefficient is already there, just add the
118 * new coefficient to the old one, otherwise, just call setCoefficent
119 * @param exponent exponent of a term in this Polynomial
120 * @param coefficient multiplier of a term in this Polynomial
121 */
122
123 void addCoefficient(int exponent, const ElemType& coefficient) {
124 iterator i = polyPairMap_.find(exponent);
125
126 if (i != end()) {
127 i->second += coefficient;
128 } else {
129 setCoefficient(exponent, coefficient);
130 }
131 }
132
133
134 /**
135 * Returns the coefficient associated with the given power for this Polynomial.
136 * @return the coefficient associated with the given power for this Polynomial
137 * @exponent exponent of any term in this Polynomial
138 */
139 ElemType getCoefficient(ExponentType exponent) {
140 iterator i = polyPairMap_.find(exponent);
141
142 if (i != end()) {
143 return i->second;
144 } else {
145 return ElemType(0);
146 }
147 }
148
149 iterator begin() {
150 return polyPairMap_.begin();
151 }
152
153 const_iterator begin() const{
154 return polyPairMap_.begin();
155 }
156
157 iterator end() {
158 return polyPairMap_.end();
159 }
160
161 const_iterator end() const{
162 return polyPairMap_.end();
163 }
164
165 iterator find(ExponentType exponent) {
166 return polyPairMap_.find(exponent);
167 }
168
169 size_t size() {
170 return polyPairMap_.size();
171 }
172
173 private:
174
175 PolynomialPairMap polyPairMap_;
176 };
177
178
179 /**
180 * Generates and returns the product of two given Polynomials.
181 * @return A Polynomial containing the product of the two given Polynomial parameters
182 */
183 template<typename ElemType>
184 Polynomial<ElemType> operator *(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) {
185 typename Polynomial<ElemType>::const_iterator i;
186 typename Polynomial<ElemType>::const_iterator j;
187 Polynomial<ElemType> p;
188
189 for (i = p1.begin(); i !=p1.end(); ++i) {
190 for (j = p2.begin(); j !=p2.end(); ++j) {
191 p.addCoefficient( i->first + j->first, i->second * j->second);
192 }
193 }
194
195 return p;
196 }
197
198 /**
199 * Generates and returns the sum of two given Polynomials.
200 * @param p1 the first polynomial
201 * @param p2 the second polynomial
202 */
203 template<typename ElemType>
204 Polynomial<ElemType> operator +(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) {
205 Polynomial<ElemType> p(p1);
206
207 typename Polynomial<ElemType>::const_iterator i;
208
209 for (i = p2.begin(); i != p2.end(); ++i) {
210 p.addCoefficient(i->first, i->second);
211 }
212
213 return p;
214
215 }
216
217 /**
218 * Generates and returns the difference of two given Polynomials.
219 * @return
220 * @param p1 the first polynomial
221 * @param p2 the second polynomial
222 */
223 template<typename ElemType>
224 Polynomial<ElemType> operator -(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) {
225 Polynomial<ElemType> p(p1);
226
227 typename Polynomial<ElemType>::const_iterator i;
228
229 for (i = p2.begin(); i != p2.end(); ++i) {
230 p.addCoefficient(i->first, -i->second);
231 }
232
233 return p;
234
235 }
236
237 /**
238 * Tests if two polynomial have the same exponents
239 * @return true if these all of the exponents in these Polynomial are identical
240 * @param p1 the first polynomial
241 * @param p2 the second polynomial
242 * @note this function does not compare the coefficient
243 */
244 template<typename ElemType>
245 bool equal(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) {
246
247 typename Polynomial<ElemType>::const_iterator i;
248 typename Polynomial<ElemType>::const_iterator j;
249
250 if (p1.size() != p2.size() ) {
251 return false;
252 }
253
254 for (i = p1.begin(), j = p2.begin(); i != p1.end() && j != p2.end(); ++i, ++j) {
255 if (i->first != j->first) {
256 return false;
257 }
258 }
259
260 return true;
261 }
262
263 typedef Polynomial<double> DoublePolynomial;
264
265 } //end namespace oopse
266 #endif //MATH_POLYNOMIAL_HPP

Properties

Name Value
svn:executable *