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 
10 #include "JLang/JEquals.hh"
11 #include "JMath/JMath.hh"
12 #include "Jeep/JPrint.hh"
13 
14 /**
15  * \author mdejong
16  */
17 
18 namespace JMATH {}
19 namespace JPP { using namespace JMATH; }
20 
21 namespace JMATH {
22 
23  /**
24  * Polynome model.
25  */
26  struct JPolynome_t :
27  public std::vector<double>,
28  public JMath<JPolynome_t>,
29  public JLANG::JEquals<JPolynome_t>
30  {
31  /**
32  * Default constructor.
33  */
35  {}
36 
37 
38  /**
39  * Equality.
40  *
41  * \param P polynome
42  * \param eps numerical precision
43  * \return true if polynomes identical; else false
44  */
45  bool equals(const JPolynome_t& P,
46  const double eps = std::numeric_limits<double>::min()) const
47  {
48  if (this->size() == P.size()) {
49 
50  for (const_iterator p = this->begin(), q = P.begin(); p != this->end(); ++p, ++q) {
51  if (fabs(*p - *q) > eps) {
52  return false;
53  }
54  }
55 
56  return true;
57 
58  } else {
59 
60  return false;
61  }
62  }
63 
64 
65  /**
66  * Add polynome.
67  *
68  * \param polynome polynome
69  * \return this polynome
70  */
71  JPolynome_t& add(const JPolynome_t& polynome)
72  {
73  while (this->size() < polynome.size()) {
74  this->push_back(0.0);
75  }
76 
77  for (size_t i = 0; i != this->size(); ++i) {
78  (*this)[i] += polynome[i];
79  }
80 
81  return *this;
82  }
83 
84 
85  /**
86  * Subtract polynome.
87  *
88  * \param polynome polynome
89  * \return this polynome
90  */
91  JPolynome_t& sub(const JPolynome_t& polynome)
92  {
93  while (this->size() < polynome.size()) {
94  this->push_back(0.0);
95  }
96 
97  for (size_t i = 0; i != this->size(); ++i) {
98  (*this)[i] -= polynome[i];
99  }
100 
101  return *this;
102  }
103 
104 
105  /**
106  * Scale polynome.
107  *
108  * \param factor multiplication factor
109  * \return this polynome
110  */
111  JPolynome_t& mul(const double factor)
112  {
113  for (iterator i = begin(); i != end(); ++i) {
114  (*i) *= factor;
115  }
116 
117  return *this;
118  }
119 
120 
121  /**
122  * Read polynome from input.
123  *
124  * \param in input stream
125  * \param object polynome
126  * \return input stream
127  */
128  friend inline std::istream& operator>>(std::istream& in, JPolynome_t& object)
129  {
130  for (double x; in >> x; ) {
131  object.push_back(x);
132  }
133 
134  return in;
135  }
136 
137 
138  /**
139  * Write polynome to output.
140  *
141  * \param out output stream
142  * \param object polynome
143  * \return output stream
144  */
145  friend inline std::ostream& operator<<(std::ostream& out, const JPolynome_t& object)
146  {
147  for (JPolynome_t::const_iterator i = object.begin(); i != object.end(); ++i) {
148  out << ' ' << FIXED(9,3) << *i;
149  }
150 
151  return out;
152  }
153  };
154 
155 
156  /**
157  * Polynome function object.
158  *
159  * Evaluates function, derivative, integral and gradient values.
160  */
161  struct JPolynome :
162  public JPolynome_t
163  {
164  /**
165  * Default constructor.
166  */
168  {}
169 
170 
171  /**
172  * Copy constructor.
173  *
174  * \param polynome polynome
175  */
176  JPolynome(const JPolynome_t& polynome) :
177  JPolynome_t(polynome)
178  {}
179 
180 
181  /**
182  * Constructor.
183  *
184  * \param __begin begin of data
185  * \param __end end of data
186  */
187  template<class T>
188  JPolynome(T __begin,
189  T __end)
190  {
191  for (T i = __begin; i != __end; ++i)
192  push_back(*i);
193  }
194 
195 
196  /**
197  * Initialise constructor.
198  *
199  * \param args values
200  */
201  template<class ...Args>
202  JPolynome(const Args& ...args)
203  {
204  set(args...);
205  }
206 
207 
208  /**
209  * Set array.
210  *
211  * \param args values
212  */
213  template<class ...Args>
214  JPolynome& set(const Args& ...args)
215  {
216  clear();
217 
218  __set__(args...);
219 
220  return *this;
221  }
222 
223 
224  /**
225  * Function value.
226  *
227  * \param x abscissa value
228  * \return function value
229  */
230  double getValue(const double x) const
231  {
232  double y = 0.0;
233  double z = 1.0;
234 
235  for (const_iterator i = begin(); i != end(); ++i, z *= x) {
236  y += (*i) * z;
237  }
238 
239  return y;
240  }
241 
242 
243  /**
244  * Derivative value.
245  *
246  * \param x abscissa value
247  * \return derivative value
248  */
249  double getDerivative(const double x) const
250  {
251  double y = 0.0;
252 
253  if (!empty()) {
254 
255  double z = 1.0;
256  int n = 1;
257 
258  for (const_iterator i = begin(); ++i != end(); ++n, z *= x) {
259  y += (*i) * z * n;
260  }
261  }
262 
263  return y;
264  }
265 
266 
267  /**
268  * Integral value.
269  *
270  * \param x abscissa value
271  * \return integral value
272  */
273  double getIntegral(const double x) const
274  {
275  double y = 0.0;
276  double z = x;
277  int n = 1;
278 
279  for (const_iterator i = begin(); i != end(); ++i, ++n, z *= x) {
280  y += (*i) * z / n;
281  }
282 
283  return y;
284  }
285 
286 
287  /**
288  * Function value.
289  *
290  * \param x abscissa value
291  * \return function value
292  */
293  double operator()(const double x) const
294  {
295  return getValue(x);
296  }
297 
298 
299  /**
300  * Get derivative function.
301  *
302  * \return derivative function
303  */
305  {
306  JPolynome buffer;
307 
308  if (size() == 0u) {
309  } else if (size() == 1u) {
310 
311  buffer.push_back(0.0);
312 
313  } else {
314 
315  int n = 1;
316 
317  for (const_iterator i = begin(); ++i != end(); ++n) {
318  buffer.push_back(n * (*i));
319  }
320  }
321 
322  return buffer;
323  }
324 
325 
326  /**
327  * get integral function.
328  *
329  * \return integral function
330  */
332  {
333  JPolynome buffer(0.0);
334 
335  int n = 1;
336 
337  for (const_iterator i = begin(); i != end(); ++i, ++n) {
338  buffer.push_back((*i) / (double) n);
339  }
340 
341  return buffer;
342  }
343 
344 
345  /**
346  * Get gradient.
347  *
348  * \param x abscissa value
349  * \return gradient
350  */
351  JPolynome_t getGradient(const double x) const
352  {
353  JPolynome_t buffer;
354 
355  double z = 1.0;
356 
357  for (const_iterator i = begin(); i != end(); ++i, z *= x) {
358  buffer.push_back(z); // d(f)/d(x_i)
359  }
360 
361  return buffer;
362  }
363 
364  private:
365  /**
366  * Recursive method for setting polynome.
367  *
368  * \param x next value
369  * \param args remaining values
370  */
371  template<class ...Args>
372  void __set__(const double x, const Args& ...args)
373  {
374  this->push_back(x);
375 
376  __set__(args...);
377  }
378 
379 
380  /**
381  * Termination method for setting polynome.
382  */
383  void __set__() const
384  {}
385  };
386 }
387 
388 #endif
Polynome model.
Definition: JPolynome.hh:26
JPolynome_t & mul(const double factor)
Scale polynome.
Definition: JPolynome.hh:111
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
friend std::istream & operator>>(std::istream &in, JPolynome_t &object)
Read polynome from input.
Definition: JPolynome.hh:128
JPolynome_t & sub(const JPolynome_t &polynome)
Subtract polynome.
Definition: JPolynome.hh:91
JPolynome & set(const Args &...args)
Set array.
Definition: JPolynome.hh:214
JPolynome()
Default constructor.
Definition: JPolynome.hh:167
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
void __set__(const double x, const Args &...args)
Recursive method for setting polynome.
Definition: JPolynome.hh:372
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:481
JPolynome_t()
Default constructor.
Definition: JPolynome.hh:34
JPolynome_t & add(const JPolynome_t &polynome)
Add polynome.
Definition: JPolynome.hh:71
I/O formatting auxiliaries.
double getDerivative(const double x) const
Derivative value.
Definition: JPolynome.hh:249
do set_variable OUTPUT_DIRECTORY $WORKDIR T
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:24
JPolynome(const Args &...args)
Initialise constructor.
Definition: JPolynome.hh:202
friend std::ostream & operator<<(std::ostream &out, const JPolynome_t &object)
Write polynome to output.
Definition: JPolynome.hh:145
double getIntegral(const double x) const
Integral value.
Definition: JPolynome.hh:273
JPolynome_t getGradient(const double x) const
Get gradient.
Definition: JPolynome.hh:351
void __set__() const
Termination method for setting polynome.
Definition: JPolynome.hh:383
alias put_queue eval echo n
Definition: qlib.csh:19
JPolynome getIntegral() const
get integral function.
Definition: JPolynome.hh:331
Base class for data structures with artithmetic capabilities.
JPolynome(T __begin, T __end)
Constructor.
Definition: JPolynome.hh:188
Polynome function object.
Definition: JPolynome.hh:161
JPolynome getDerivative() const
Get derivative function.
Definition: JPolynome.hh:304
double u[N+1]
Definition: JPolint.hh:706
JPolynome(const JPolynome_t &polynome)
Copy constructor.
Definition: JPolynome.hh:176
double operator()(const double x) const
Function value.
Definition: JPolynome.hh:293
then $DIR JPlotNPE PDG P
Definition: JPlotNPE-PDG.sh:60
double getValue(const double x) const
Function value.
Definition: JPolynome.hh:230
bool equals(const JPolynome_t &P, const double eps=std::numeric_limits< double >::min()) const
Equality.
Definition: JPolynome.hh:45