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