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 |
|
|
35 |
|
|
36 |
|
#include <list> |
37 |
|
#include <utility> |
38 |
+ |
#include <map> |
39 |
|
|
40 |
|
namespace oopse { |
41 |
|
|
42 |
< |
template<typename ElemType, int N> pow(ElemType x) { |
42 |
> |
template<typename ElemType> pow(ElemType x, int N) { |
43 |
|
ElemType result(1); |
44 |
|
|
45 |
|
for (int i = 0; i < N; ++i) { |
49 |
|
return result; |
50 |
|
} |
51 |
|
|
52 |
< |
|
52 |
> |
/** |
53 |
> |
* @class Polynomial Polynomial.hpp "math/Polynomial.hpp" |
54 |
> |
* A generic Polynomial class |
55 |
> |
*/ |
56 |
|
template<typename ElemType> |
57 |
|
class Polynomial { |
58 |
|
|
59 |
|
public: |
60 |
|
|
61 |
|
typedef int ExponentType; |
62 |
< |
typedef ElemType ConstantType; |
63 |
< |
typedef std::map<ExponentType, ConstantType> PolynomialPairMap; |
62 |
> |
typedef ElemType CoefficientType; |
63 |
> |
typedef std::map<ExponentType, CoefficientType> PolynomialPairMap; |
64 |
|
typedef PolynomialPairMap::iterator PolynomialIterator; |
65 |
|
|
62 |
– |
Polynomial(); |
63 |
– |
|
64 |
– |
template<U> Polynomial(const Polynomial<U>& p); |
65 |
– |
template<U> Polynomial& operator=(const Polynomial<U>& p); |
66 |
– |
|
66 |
|
/** |
67 |
< |
* |
67 |
> |
* Calculates the value of this Polynomial evaluated at the given x value. |
68 |
> |
* @return The value of this Polynomial evaluates at the given x value |
69 |
> |
* @param x the value of the independent variable for this Polynomial function |
70 |
|
*/ |
71 |
|
ElemType evaluate(const ElemType& x) { |
72 |
|
ElemType result; |
73 |
|
double exponent; |
74 |
< |
double constant; |
74 |
> |
double coefficient; |
75 |
|
|
76 |
|
for (PolynomialIterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { |
77 |
|
exponent = i->first; |
78 |
< |
constant = i->second; |
79 |
< |
result += pow<exponent>(x) * constant; |
78 |
> |
coefficient = i->second; |
79 |
> |
result += pow(x, exponent) * coefficient; |
80 |
|
} |
81 |
|
|
82 |
|
return result; |
83 |
|
} |
84 |
|
|
85 |
< |
ElemType evaluateFirstDerivative(const ElemType& x) { |
85 |
> |
/** |
86 |
> |
* Returns the first derivative of this polynomial. |
87 |
> |
* @return the first derivative of this polynomial |
88 |
> |
* @param x |
89 |
> |
*/ |
90 |
> |
ElemType evaluateDerivative(const ElemType& x) { |
91 |
|
ElemType result; |
92 |
|
double exponent; |
93 |
< |
double constant; |
93 |
> |
double coefficient; |
94 |
|
|
95 |
|
for (PolynomialIterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { |
96 |
|
exponent = i->first; |
97 |
< |
constant = i->second; |
98 |
< |
result += pow<exponent - 1>(x) * constant * exponent; |
97 |
> |
coefficient = i->second; |
98 |
> |
result += pow(x, exponent - 1) * coefficient * exponent; |
99 |
|
} |
100 |
|
|
101 |
|
return result; |
102 |
|
} |
103 |
|
|
104 |
< |
void addPolynomialTerm(int exponent, const ElemType& constant) { |
105 |
< |
|
104 |
> |
/** |
105 |
> |
* Set the coefficent of the specified exponent, if the coefficient is already there, it |
106 |
> |
* will be overwritten. |
107 |
> |
* @param exponent exponent of a term in this Polynomial |
108 |
> |
* @param coefficient multiplier of a term in this Polynomial |
109 |
> |
*/ |
110 |
> |
|
111 |
> |
void setCoefficient(int exponent, const ElemType& coefficient) { |
112 |
> |
polyPairMap_.insert(PolynomialPairMap::value_type(exponent, coefficient)); |
113 |
|
} |
114 |
|
|
115 |
< |
bool getConstant(ExponentType exponent, & constant) { |
115 |
> |
/** |
116 |
> |
* Set the coefficent of the specified exponent. If the coefficient is already there, just add the |
117 |
> |
* new coefficient to the old one, otherwise, just call setCoefficent |
118 |
> |
* @param exponent exponent of a term in this Polynomial |
119 |
> |
* @param coefficient multiplier of a term in this Polynomial |
120 |
> |
*/ |
121 |
> |
|
122 |
> |
void addCoefficient(int exponent, const ElemType& coefficient) { |
123 |
> |
PolynomialIterator i = find(exponent); |
124 |
|
|
125 |
+ |
if (i != end()) { |
126 |
+ |
i->second += coefficient; |
127 |
+ |
} else { |
128 |
+ |
setCoefficient(exponent, coefficient); |
129 |
+ |
} |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
+ |
/** |
134 |
+ |
* Returns the coefficient associated with the given power for this Polynomial. |
135 |
+ |
* @return the coefficient associated with the given power for this Polynomial |
136 |
+ |
* @exponent exponent of any term in this Polynomial |
137 |
+ |
*/ |
138 |
+ |
ElemType getCoefficient(ExponentType exponent) { |
139 |
+ |
PolynomialIterator i = find(exponent); |
140 |
+ |
|
141 |
+ |
if (i != end()) { |
142 |
+ |
return i->second; |
143 |
+ |
} else { |
144 |
+ |
return ElemType(0); |
145 |
+ |
} |
146 |
+ |
} |
147 |
+ |
|
148 |
|
PolynomialIterator begin() { |
149 |
|
return polyPairMap_.begin(); |
150 |
|
} |
156 |
|
PolynomialIterator find(ExponentType exponent) { |
157 |
|
return polyPairMap_.find(); |
158 |
|
} |
159 |
< |
|
159 |
> |
|
160 |
> |
size_t size() { |
161 |
> |
return polyPairMap_.size(); |
162 |
> |
} |
163 |
> |
|
164 |
|
private: |
165 |
|
|
166 |
|
PolynomialPairMap polyPairMap_; |
167 |
|
}; |
168 |
|
|
169 |
+ |
|
170 |
+ |
/** |
171 |
+ |
* Generates and returns the product of two given Polynomials. |
172 |
+ |
* @return A Polynomial containing the product of the two given Polynomial parameters |
173 |
+ |
*/ |
174 |
+ |
template<typename ElemType> |
175 |
+ |
Polynomial<ElemType> operator *(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
176 |
+ |
typename Polynomial<ElemType>::PolynomialIterator i; |
177 |
+ |
typename Polynomial<ElemType>::PolynomialIterator j; |
178 |
+ |
typename Polynomial<ElemType>::PolynomialIterator k; |
179 |
+ |
Polynomial<ElemType> p; |
180 |
+ |
int exponent; |
181 |
+ |
int coefficient; |
182 |
+ |
|
183 |
+ |
for (i = p1.begin(); i !=p1.end(); ++i) { |
184 |
+ |
for (j = p1.begin(); j !=p1.end(); ++j) { |
185 |
+ |
exponent = i->first + j->first; |
186 |
+ |
coefficient = i->second * j->second; |
187 |
+ |
k = p->find(exponent); |
188 |
+ |
|
189 |
+ |
if (k != p.end()) { |
190 |
+ |
p[exponent] = coefficient; |
191 |
+ |
} else { |
192 |
+ |
k->second += coefficient; |
193 |
+ |
} |
194 |
+ |
} |
195 |
+ |
} |
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>::PolynomialIterator i; |
208 |
+ |
typename Polynomial<ElemType>::PolynomialIterator j; |
209 |
+ |
|
210 |
+ |
for (i = p2.begin(); i != p2.end(); ++i) { |
211 |
+ |
j = p.find(i->first); |
212 |
+ |
if (j == p.end()) { |
213 |
+ |
p[j] = i->second; |
214 |
+ |
} else { |
215 |
+ |
j->second += i->second; |
216 |
+ |
} |
217 |
+ |
} |
218 |
+ |
|
219 |
+ |
return p; |
220 |
+ |
|
221 |
+ |
} |
222 |
+ |
|
223 |
+ |
/** |
224 |
+ |
* Generates and returns the difference of two given Polynomials. |
225 |
+ |
* @return |
226 |
+ |
* @param p1 the first polynomial |
227 |
+ |
* @param p2 the second polynomial |
228 |
+ |
*/ |
229 |
+ |
template<typename ElemType> |
230 |
+ |
Polynomial<ElemType> operator -(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
231 |
+ |
Polynomial<ElemType> p(p1); |
232 |
+ |
|
233 |
+ |
typename Polynomial<ElemType>::PolynomialIterator i; |
234 |
+ |
typename Polynomial<ElemType>::PolynomialIterator j; |
235 |
+ |
|
236 |
+ |
for (i = p2.begin(); i != p2.end(); ++i) { |
237 |
+ |
j = p.find(i->first); |
238 |
+ |
if (j == p.end()) { |
239 |
+ |
p[j] = -i->second; |
240 |
+ |
} else { |
241 |
+ |
j->second -= i->second; |
242 |
+ |
} |
243 |
+ |
} |
244 |
+ |
|
245 |
+ |
return p; |
246 |
+ |
|
247 |
+ |
} |
248 |
+ |
|
249 |
+ |
/** |
250 |
+ |
* Tests if two polynomial have the same exponents |
251 |
+ |
* @return true if these all of the exponents in these Polynomial are identical |
252 |
+ |
* @param p1 the first polynomial |
253 |
+ |
* @param p2 the second polynomial |
254 |
+ |
* @note this function does not compare the coefficient |
255 |
+ |
*/ |
256 |
+ |
template<typename ElemType> |
257 |
+ |
bool equal(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) { |
258 |
+ |
|
259 |
+ |
typename Polynomial<ElemType>::PolynomialIterator i; |
260 |
+ |
typename Polynomial<ElemType>::PolynomialIterator j; |
261 |
+ |
|
262 |
+ |
if (p1.size() !== p2.size() ) { |
263 |
+ |
return false; |
264 |
+ |
} |
265 |
+ |
|
266 |
+ |
for (i = p1.begin(), j = p2.begin(); i != p1.end() && j != p2.end(); ++i, ++j) { |
267 |
+ |
if (i->first != j->first) { |
268 |
+ |
return false; |
269 |
+ |
} |
270 |
+ |
} |
271 |
+ |
|
272 |
+ |
return true; |
273 |
+ |
} |
274 |
+ |
|
275 |
+ |
typedef Polynomial<double> DoublePolynomial; |
276 |
+ |
|
277 |
|
} //end namespace oopse |
278 |
|
#endif //MATH_POLYNOMIAL_HPP |