74 |
|
class Polynomial { |
75 |
|
|
76 |
|
public: |
77 |
< |
|
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 |
188 |
|
size_t size() { |
189 |
|
return polyPairMap_.size(); |
190 |
|
} |
191 |
< |
|
191 |
> |
|
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 |
> |
return *this; |
208 |
> |
} |
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 |
|
private: |
225 |
|
|
226 |
|
PolynomialPairMap polyPairMap_; |
246 |
|
return p; |
247 |
|
} |
248 |
|
|
249 |
+ |
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 |
|
/** |
274 |
|
* Generates and returns the sum of two given Polynomials. |
275 |
|
* @param p1 the first polynomial |