ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-4/src/math/Polynomial.hpp
(Generate patch)

Comparing branches/new_design/OOPSE-4/src/math/Polynomial.hpp (file contents):
Revision 1786 by tim, Fri Nov 26 02:13:56 2004 UTC vs.
Revision 1796 by tim, Mon Nov 29 21:42:08 2004 UTC

# Line 33 | Line 33
33   #ifndef MATH_POLYNOMIAL_HPP
34   #define MATH_POLYNOMIAL_HPP
35  
36 + #include <iostream>
37   #include <list>
37 #include <utility>
38   #include <map>
39 + #include <utility>
40  
41   namespace oopse {
42  
43 < template<typename ElemType> pow(ElemType x, int N) {
43 > template<typename ElemType> ElemType pow(ElemType x, int N) {
44      ElemType result(1);
45  
46      for (int i = 0; i < N; ++i) {
# Line 61 | Line 62 | class Polynomial {
62          typedef int ExponentType;
63          typedef ElemType CoefficientType;
64          typedef std::map<ExponentType, CoefficientType> PolynomialPairMap;
65 <        typedef PolynomialPairMap::iterator PolynomialIterator;
66 <
65 >        typedef PolynomialPairMap::iterator iterator;
66 >        typedef 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;
73 >            ElemType result = ElemType();
74              double exponent;
75              double coefficient;
76              
77 <            for (PolynomialIterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) {
77 >            for (iterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) {
78                  exponent = i->first;
79                  coefficient = i->second;
80                  result  += pow(x, exponent) * coefficient;
# Line 88 | Line 89 | class Polynomial {
89           * @param x
90           */
91          ElemType evaluateDerivative(const ElemType& x) {
92 <            ElemType result;
92 >            ElemType result = ElemType();
93              double exponent;
94              double coefficient;
95              
96 <            for (PolynomialIterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) {
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;
# Line 120 | Line 121 | class Polynomial {
121           */
122          
123          void addCoefficient(int exponent, const ElemType& coefficient) {
124 <            PolynomialIterator i = find(exponent);
124 >            iterator i = polyPairMap_.find(exponent);
125  
126              if (i != end()) {
127                  i->second += coefficient;
# Line 136 | Line 137 | class Polynomial {
137           * @exponent exponent of any term in this Polynomial
138           */
139          ElemType getCoefficient(ExponentType exponent) {
140 <            PolynomialIterator i = find(exponent);
140 >            iterator i = polyPairMap_.find(exponent);
141  
142              if (i != end()) {
143                  return i->second;
# Line 145 | Line 146 | class Polynomial {
146              }
147          }
148  
149 <        PolynomialIterator begin() {
149 >        iterator begin() {
150              return polyPairMap_.begin();
151          }
152 +
153 +        const_iterator begin() const{
154 +            return polyPairMap_.begin();
155 +        }
156          
157 <        PolynomialIterator end() {
157 >        iterator end() {
158              return polyPairMap_.end();
159          }
160  
161 <        PolynomialIterator find(ExponentType exponent) {
162 <            return polyPairMap_.find();
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          }
# Line 173 | Line 182 | Polynomial<ElemType> operator *(const Polynomial<ElemT
182   */
183   template<typename ElemType>
184   Polynomial<ElemType> operator *(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) {
185 <    typename Polynomial<ElemType>::PolynomialIterator i;
186 <    typename Polynomial<ElemType>::PolynomialIterator j;
178 <    typename Polynomial<ElemType>::PolynomialIterator k;
185 >    typename Polynomial<ElemType>::const_iterator i;
186 >    typename Polynomial<ElemType>::const_iterator j;
187      Polynomial<ElemType> p;
180    int exponent;
181    int coefficient;
188      
189      for (i = p1.begin(); i !=p1.end(); ++i) {
190 <        for (j = p1.begin(); j !=p1.end(); ++j) {
191 <            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 <            }
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   /**
# Line 204 | Line 204 | Polynomial<ElemType> operator +(const Polynomial<ElemT
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;
207 >    typename Polynomial<ElemType>::const_iterator i;
208  
209      for (i =  p2.begin(); i  != p2.end(); ++i) {
210 <        j = p.find(i->first);
212 <        if (j == p.end()) {
213 <            p[j] = i->second;
214 <        } else {
215 <            j->second += i->second;
216 <        }
210 >        p.addCoefficient(i->first, i->second);
211      }
212  
213      return p;
# Line 230 | Line 224 | Polynomial<ElemType> operator -(const Polynomial<ElemT
224   Polynomial<ElemType> operator -(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) {
225      Polynomial<ElemType> p(p1);
226  
227 <    typename Polynomial<ElemType>::PolynomialIterator i;
234 <    typename Polynomial<ElemType>::PolynomialIterator j;
227 >    typename Polynomial<ElemType>::const_iterator i;
228  
229      for (i =  p2.begin(); i  != p2.end(); ++i) {
230 <        j = p.find(i->first);
238 <        if (j == p.end()) {
239 <            p[j] = -i->second;
240 <        } else {
241 <            j->second -= i->second;
242 <        }
230 >        p.addCoefficient(i->first, -i->second);
231      }
232  
233      return p;
# Line 256 | Line 244 | bool equal(const Polynomial<ElemType>& p1, const Polyn
244   template<typename ElemType>
245   bool equal(const Polynomial<ElemType>& p1, const Polynomial<ElemType>& p2) {
246  
247 <    typename Polynomial<ElemType>::PolynomialIterator i;
248 <    typename Polynomial<ElemType>::PolynomialIterator j;
247 >    typename Polynomial<ElemType>::const_iterator i;
248 >    typename Polynomial<ElemType>::const_iterator j;
249  
250      if (p1.size() !== p2.size() ) {
251          return false;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines