SeaBreeze
Polynomial.h
Go to the documentation of this file.
1 /***************************************************/
38 #ifndef POLYNOMIAL_H
39 #define POLYNOMIAL_H
40 
41 #include <vector>
42 
43 namespace seabreeze {
44  template <class T>
45  class Polynomial {
46  public:
47  Polynomial(std::vector<T> *coefficients);
48  Polynomial(T *coefficients, unsigned int length);
49  ~Polynomial();
50  T evaluate(T x);
51  private:
52  std::vector<T> *coefficients;
53  };
54 
55  template <class T>
56  Polynomial<T>::Polynomial(std::vector<T> *coeffs) {
57  unsigned int i;
58 
59  if(NULL == coeffs) {
60  this->coefficients = new std::vector<T>(0);
61  } else {
62  this->coefficients = new std::vector<T>(coeffs->size());
63  }
64  for(i = 0; i < coeffs->size(); i++) {
65  (*(this->coefficients))[i] = (*coeffs)[i];
66  }
67  }
68 
69  template <class T>
70  Polynomial<T>::Polynomial(T *coeffs, unsigned int length) {
71  unsigned int i;
72 
73  if(NULL == coeffs) {
74  this->coefficients = new std::vector<T>(0);
75  } else {
76  this->coefficients = new std::vector<T>(length);
77  }
78  for(i = 0; i < length; i++) {
79  (*(this->coefficients))[i] = coeffs[i];
80  }
81  }
82 
83 
84  template <class T>
86  delete this->coefficients;
87  }
88 
89 
90  template <class T>
92  T acc;
93  T retval = 0.0;
94  unsigned int order;
95 
96  if(NULL == this->coefficients || 0 == this->coefficients->size()) {
97  return 0;
98  }
99 
100  retval = this->coefficients->at(0); /* Initialize to A */
101  acc = 1;
102  for(order = 1; order < this->coefficients->size(); order++) {
103  acc *= x;
104  retval += this->coefficients->at(order) * acc; /* Add x^n term (Bx^2, Cx^3, ...) */
105  }
106  return retval;
107  }
108 
109 }
110 
111 #endif /* POLYNOMIAL_H */
Definition: Polynomial.h:45
Encapsulates all SeaBreeze classes.
Definition: DeviceFactory.h:42