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