Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JPolynome.hh
Go to the documentation of this file.
1 #ifndef __JMATH__JPOLYNOME__
2 #define __JMATH__JPOLYNOME__
3 
4 #include <istream>
5 #include <ostream>
6 #include <vector>
7 #include <cmath>
8 #include <limits>
9 #include <stdarg.h>
10 
11 
12 /**
13  * \author mdejong
14  */
15 
16 namespace JMATH {}
17 namespace JPP { using namespace JMATH; }
18 
19 namespace JMATH {
20 
21  /**
22  * Polynome function object.
23  *
24  * Evaluates function, derivative and integral values.
25  */
26  class JPolynome :
27  public std::vector<double>
28  {
29  public:
30  /**
31  * Default constructor.
32  */
34  {}
35 
36 
37  /**
38  * Constructor.
39  *
40  * \param n degree of polynome (followed by comma separated argument list)
41  */
42  JPolynome(int n, ...)
43  {
44  va_list ap;
45 
46  va_start(ap, n);
47 
48  for (double x; n != 0; --n) {
49 
50  x = va_arg(ap, double); // next argument
51 
52  push_back(x);
53  }
54 
55  va_end(ap);
56  }
57 
58 
59  /**
60  * Constructor.
61  *
62  * \param __begin begin of data
63  * \param __end end of data
64  */
65  template<class T>
66  JPolynome(T __begin,
67  T __end)
68  {
69  for (T i = __begin; i != __end; ++i)
70  push_back(*i);
71  }
72 
73 
74  /**
75  * Function value.
76  *
77  * \param x abscissa value
78  * \return function value
79  */
80  double getValue(const double x) const
81  {
82  double y = 0.0;
83  double z = 1.0;
84 
85  for (const_iterator i = begin(); i != end(); ++i, z *= x) {
86  y += (*i) * z;
87  }
88 
89  return y;
90  }
91 
92 
93  /**
94  * Derivative value.
95  *
96  * \param x abscissa value
97  * \return derivative value
98  */
99  double getDerivative(const double x) const
100  {
101  double y = 0.0;
102 
103  if (!empty()) {
104 
105  double z = 1.0;
106  int n = 1;
107 
108  for (const_iterator i = begin(); ++i != end(); ++n, z *= x) {
109  y += (*i) * z * n;
110  }
111  }
112 
113  return y;
114  }
115 
116 
117  /**
118  * Integral value.
119  *
120  * \param x abscissa value
121  * \return integral value
122  */
123  double getIntegral(const double x) const
124  {
125  double y = 0.0;
126  double z = x;
127  int n = 1;
128 
129  for (const_iterator i = begin(); i != end(); ++i, ++n, z *= x) {
130  y += (*i) * z / n;
131  }
132 
133  return y;
134  }
135 
136 
137  /**
138  * Function value.
139  *
140  * \param x abscissa value
141  * \return function value
142  */
143  double operator()(const double x) const
144  {
145  return getValue(x);
146  }
147 
148 
149  /**
150  * Derivative function.
151  *
152  * \return derivative function
153  */
155  {
156  JPolynome buffer;
157 
158  int n = 1;
159 
160  for (const_iterator i = begin(); ++i != end(); ++n) {
161  buffer.push_back(n * (*i));
162  }
163 
164  return buffer;
165  }
166 
167 
168  /**
169  * Integral function.
170  *
171  * \return integral function
172  */
174  {
175  JPolynome buffer(1, 0.0);
176 
177  int n = 1;
178 
179  for (const_iterator i = begin(); i != end(); ++i, ++n) {
180  buffer.push_back((*i) / (double) n);
181  }
182 
183  return buffer;
184  }
185 
186 
187  /**
188  * Equality.
189  *
190  * \param P polynome
191  * \param eps numerical precision
192  * \return true if polynomes identical; else false
193  */
194  bool equals(const JPolynome& P,
195  const double eps = std::numeric_limits<double>::min()) const
196  {
197  if (this->size() == P.size()) {
198 
199  for (const_iterator p = this->begin(), q = P.begin(); p != this->end(); ++p, ++q) {
200  if (fabs(*p - *q) > eps) {
201  return false;
202  }
203  }
204 
205  return true;
206 
207  } else {
208 
209  return false;
210  }
211  }
212 
213 
214  /**
215  * Read polynome from input.
216  *
217  * \param in input stream
218  * \param object polynome
219  * \return input stream
220  */
221  friend inline std::istream& operator>>(std::istream& in, JPolynome& object)
222  {
223  for (double x; in >> x; ) {
224  object.push_back(x);
225  }
226 
227  return in;
228  }
229 
230 
231  /**
232  * Write polynome to output.
233  *
234  * \param out output stream
235  * \param object polynome
236  * \return output stream
237  */
238  friend inline std::ostream& operator<<(std::ostream& out, const JPolynome& object)
239  {
240  for (JPolynome::const_iterator i = object.begin(); i != object.end(); ++i) {
241  out << ' ' << *i;
242  }
243 
244  return out;
245  }
246  };
247 }
248 
249 #endif
JPolynome getIntegral() const
Integral function.
Definition: JPolynome.hh:173
JPolynome(int n,...)
Constructor.
Definition: JPolynome.hh:42
friend std::istream & operator>>(std::istream &in, JPolynome &object)
Read polynome from input.
Definition: JPolynome.hh:221
JPolynome(T __begin, T __end)
Constructor.
Definition: JPolynome.hh:66
double operator()(const double x) const
Function value.
Definition: JPolynome.hh:143
double getIntegral(const double x) const
Integral value.
Definition: JPolynome.hh:123
Polynome function object.
Definition: JPolynome.hh:26
friend std::ostream & operator<<(std::ostream &out, const JPolynome &object)
Write polynome to output.
Definition: JPolynome.hh:238
JPolynome()
Default constructor.
Definition: JPolynome.hh:33
double getDerivative(const double x) const
Derivative value.
Definition: JPolynome.hh:99
double getValue(const double x) const
Function value.
Definition: JPolynome.hh:80
bool equals(const JPolynome &P, const double eps=std::numeric_limits< double >::min()) const
Equality.
Definition: JPolynome.hh:194
JPolynome getDerivative() const
Derivative function.
Definition: JPolynome.hh:154