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 |
> |
} |
208 |
> |
|
209 |
> |
PolynomialType& operator *= (const PolynomialType& p) { |
210 |
> |
typename Polynomial<ElemType>::const_iterator i; |
211 |
> |
typename Polynomial<ElemType>::const_iterator j; |
212 |
> |
|
213 |
> |
for (i = this->begin(); i !=this->end(); ++i) { |
214 |
> |
for (j = p.begin(); j !=p.end(); ++j) { |
215 |
> |
this->addCoefficient( i->first + j->first, i->second * j->second); |
216 |
> |
} |
217 |
> |
} |
218 |
> |
|
219 |
> |
return *this; |
220 |
> |
} |
221 |
> |
|
222 |
> |
|
223 |
|
private: |
224 |
|
|
225 |
|
PolynomialPairMap polyPairMap_; |
245 |
|
return p; |
246 |
|
} |
247 |
|
|
248 |
+ |
template<typename ElemType> |
249 |
+ |
Polynomial<ElemType> operator *(const Polynomial<ElemType>& p, const ElemType v) { |
250 |
+ |
typename Polynomial<ElemType>::const_iterator i; |
251 |
+ |
Polynomial<ElemType> result; |
252 |
+ |
|
253 |
+ |
for (i = p.begin(); i !=p.end(); ++i) { |
254 |
+ |
result.addCoefficient( i->first , i->second * v); |
255 |
+ |
} |
256 |
+ |
|
257 |
+ |
return result; |
258 |
+ |
} |
259 |
+ |
|
260 |
+ |
template<typename ElemType> |
261 |
+ |
Polynomial<ElemType> operator *( const ElemType v, const Polynomial<ElemType>& p) { |
262 |
+ |
typename Polynomial<ElemType>::const_iterator i; |
263 |
+ |
Polynomial<ElemType> result; |
264 |
+ |
|
265 |
+ |
for (i = p.begin(); i !=p.end(); ++i) { |
266 |
+ |
result.addCoefficient( i->first , i->second * v); |
267 |
+ |
} |
268 |
+ |
|
269 |
+ |
return result; |
270 |
+ |
} |
271 |
+ |
|
272 |
|
/** |
273 |
|
* Generates and returns the sum of two given Polynomials. |
274 |
|
* @param p1 the first polynomial |