Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JLegendre.hh
Go to the documentation of this file.
1#ifndef __JMATH__JLEGENDRE__
2#define __JMATH__JLEGENDRE__
3
4#include <istream>
5#include <ostream>
6#include <vector>
7#include <limits>
8
9#include"JMath/JZero.hh"
11
12
13/**
14 * \author mdejong
15 */
16
17namespace JMATH {}
18namespace JPP { using namespace JMATH; }
19
20namespace JMATH {
21
22 /**
23 * Base class for Legendre polynome.
24 */
25 template<class JOrdinate_t>
26 struct JLegendre_t :
27 public std::vector<JOrdinate_t>
28 {
29 typedef typename std::vector<JOrdinate_t>::const_iterator const_iterator;
31 typedef typename std::vector<JOrdinate_t>::const_reverse_iterator const_reverse_iterator;
33
34
35 /**
36 * Default constructor.
37 */
39 xmin(-1.0),
40 xmax(+1.0)
41 {}
42
43
44 /**
45 * Constructor.
46 *
47 * \param xmin minimal abscissa value
48 * \param xmax maximal abscissa value
49 */
50 JLegendre_t(const double xmin,
51 const double xmax) :
52 xmin(xmin),
53 xmax(xmax)
54 {}
55
56
57 /**
58 * Virtual destructor.
59 */
60 virtual ~JLegendre_t()
61 {}
62
63
64 /**
65 * Function value.
66 *
67 * \param x abscissa value
68 * \return function value
69 */
70 virtual JOrdinate_t getValue(const double x) const = 0;
71
72
73 /**
74 * Function value.
75 *
76 * \param x abscissa value
77 * \return function value
78 */
79 JOrdinate_t operator()(const double x) const
80 {
81 return this->getValue(x);
82 }
83
84
85 /**
86 * Get minimal abscissa value.
87 *
88 * \return minimal abscissa value
89 */
90 double getXmin() const
91 {
92 return xmin;
93 }
94
95
96 /**
97 * Get maximal abscissa value.
98 *
99 * \return maximal abscissa value
100 */
101 double getXmax() const
102 {
103 return xmax;
104 }
105
106
107 /**
108 * Get normalised abscissa value.
109 *
110 * \param x abscissa value
111 * \return normalised abscissa value
112 */
113 double getX(const double x) const
114 {
115 return 2.0 * (x - xmin) / (xmax - xmin) - 1.0;
116 }
117
118
119 /**
120 * Reset.
121 */
122 void reset()
123 {
124 for (iterator i = this->begin(); i != this->end(); ++i) {
125 *i = zero;
126 }
127 }
128
129
130 /**
131 * Set abscissa values.
132 *
133 * \param xmin minimal abscissa value
134 * \param xmax maximal abscissa value
135 */
136 void set(const double xmin,
137 const double xmax)
138 {
139 this->xmin = xmin;
140 this->xmax = xmax;
141 }
142
143
144 /**
145 * Set abscissa values.
146 *
147 * The template argument <tt>T</tt> refers to an iterator of a data structure which should have the following data member:
148 * - double %first; //
149 * which conforms with std::pair.
150 *
151 * \param __begin begin of data
152 * \param __end end of data
153 */
154 template<class T>
155 void set(T __begin, T __end)
156 {
157 this->xmin = std::numeric_limits<double>::max();
158 this->xmax = std::numeric_limits<double>::lowest();
159
160 for (T i = __begin; i != __end; ++i) {
161 if (i->first < this->xmin) { this->xmin = i->first; }
162 if (i->first > this->xmax) { this->xmax = i->first; }
163 }
164 }
165
166
167 /**
168 * Read Legendre polynome from input.
169 *
170 * \param in input stream
171 * \param object Legendre polynome
172 * \return input stream
173 */
174 friend inline std::istream& operator>>(std::istream& in, JLegendre_t& object)
175 {
176 object.clear();
177
178 for (JOrdinate_t x; in >> x; ) {
179 object.push_back(x);
180 }
181
182 return in;
183 }
184
185
186 /**
187 * Write Legendre polynome to output.
188 *
189 * \param out output stream
190 * \param object Legendre polynome
191 * \return output stream
192 */
193 friend inline std::ostream& operator<<(std::ostream& out, const JLegendre_t& object)
194 {
195 for (typename JLegendre_t::const_iterator i = object.begin(); i != object.end(); ++i) {
196 out << ' ' << *i;
197 }
198
199 return out;
200 }
201
202
203 protected:
204 double xmin; //!< minimal abscissa
205 double xmax; //!< maximal abscissa
206 };
207
208
209 /**
210 * Template definition for function evaluation of Legendre polynome.
211 */
212 template<class JOrdinate_t, size_t N = (size_t) -1>
213 struct JLegendre;
214
215
216 /**
217 * Template specialisation for function evaluation of of Legendre polynome for undefined number of degrees.
218 */
219 template<class JOrdinate_t>
220 struct JLegendre<JOrdinate_t, (size_t) -1> :
221 public JLegendre_t<JOrdinate_t>
222 {
223 /**
224 * Default constructor.
225 */
227 {}
228
229
230 /**
231 * Constructor.
232 *
233 * \param xmin minimal abscissa value
234 * \param xmax maximal abscissa value
235 */
236 JLegendre(const double xmin,
237 const double xmax)
238 {
239 this->set(xmin, xmax);
240 }
241
242
243 /**
244 * Function value.
245 *
246 * \param x abscissa value
247 * \return function value
248 */
249 virtual JOrdinate_t getValue(const double x) const override
250 {
251 const double z = this->getX(x);
252 JOrdinate_t y = zero;
253
254 for (size_t n = 0; n != this->size(); ++n) {
255 y += (*this)[n] * legendre(n,z);
256 }
257
258 return y;
259 }
260 };
261
262
263 /**
264 * Template specialisation for function evaluation of of Legendre polynome for defined number of degrees.
265 */
266 template<class JOrdinate_t, size_t N>
267 struct JLegendre :
269 {
270 using JLegendre_t<JOrdinate_t>::set;
271
272
273 /**
274 * Default constructor.
275 */
277 {
278 this->JLegendre<JOrdinate_t>::resize(N+1, getZero<JOrdinate_t>());
279 }
280
281
282 /**
283 * Constructor.
284 *
285 * \param xmin minimal abscissa value
286 * \param xmax maximal abscissa value
287 */
288 JLegendre(const double xmin,
289 const double xmax)
290 {
291 this->JLegendre<JOrdinate_t>::resize(N+1, getZero<JOrdinate_t>());
292 this->set(xmin, xmax);
293 }
294
295
296 /**
297 * Constructor.
298 *
299 * The template argument <tt>T</tt> refers to an iterator of a data structure which should have the following data members:
300 * - double %first; //
301 * - JOrdinate_t %second; //
302 * which conforms with std::pair.
303 *
304 * \param __begin begin of data
305 * \param __end end of data
306 */
307 template<class T>
308 JLegendre(T __begin, T __end)
309 {
310 this->JLegendre<JOrdinate_t>::resize(N+1, getZero<JOrdinate_t>());
311 this->set(__begin, __end);
312 }
313
314
315 /**
316 * Set Legendre polynome.
317 *
318 * The template argument <tt>T</tt> refers to an iterator of a data structure which should have the following data members:
319 * - double %first; //
320 * - JOrdinate_t %second; //
321 * which conforms with std::pair.
322 *
323 * \param __begin begin of data
324 * \param __end end of data
325 */
326 template<class T>
327 void set(T __begin, T __end)
328 {
329 this->reset();
330 this->JLegendre<JOrdinate_t>::set(__begin, __end);
331
332 for (size_t n = 0; n != N + 1; ++n) {
333
334 JOrdinate_t V = zero;
335 double W = 0.0;
336
337 for (T i = __begin; i != __end; ++i) {
338
339 const double x = i->first;
340 const JOrdinate_t& y = i->second;
341 const double z = this->getX(x);
342 const double w = legendre(n, z);
343
344 V += w * (y - (*this)(x));
345 W += w * (w);
346 }
347
348 (*this)[n] = V / W;
349 }
350 }
351
352
353 /**
354 * Read Legendre polynome from input.
355 *
356 * \param in input stream
357 * \param object Legendre polynome
358 * \return input stream
359 */
360 friend inline std::istream& operator>>(std::istream& in, JLegendre& object)
361 {
362 for (typename JLegendre::iterator i = object.begin(); i != object.end(); ++i) {
363 in >> *i;
364 }
365
366 return in;
367 }
368
369 private:
370 void clear();
371 void resize();
372 void erase();
373 void push_back();
374 void pop_back();
375 };
376}
377
378#endif
379
Auxiliary methods for mathematics.
Definition of zero value for any class.
Auxiliary classes and methods for mathematical operations.
Definition JEigen3D.hh:88
static const JZero zero
Function object to assign zero value.
Definition JZero.hh:105
double legendre(const size_t n, const double x)
Legendre polynome.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
JLegendre(const double xmin, const double xmax)
Constructor.
Definition JLegendre.hh:236
virtual JOrdinate_t getValue(const double x) const override
Function value.
Definition JLegendre.hh:249
Base class for Legendre polynome.
Definition JLegendre.hh:28
double getXmin() const
Get minimal abscissa value.
Definition JLegendre.hh:90
void reset()
Reset.
Definition JLegendre.hh:122
JLegendre_t()
Default constructor.
Definition JLegendre.hh:38
std::vector< JOrdinate_t >::const_reverse_iterator const_reverse_iterator
Definition JLegendre.hh:31
std::vector< JOrdinate_t >::const_iterator const_iterator
Definition JLegendre.hh:29
std::vector< JOrdinate_t >::iterator iterator
Definition JLegendre.hh:30
friend std::ostream & operator<<(std::ostream &out, const JLegendre_t &object)
Write Legendre polynome to output.
Definition JLegendre.hh:193
virtual JOrdinate_t getValue(const double x) const =0
Function value.
friend std::istream & operator>>(std::istream &in, JLegendre_t &object)
Read Legendre polynome from input.
Definition JLegendre.hh:174
double xmin
minimal abscissa
Definition JLegendre.hh:204
virtual ~JLegendre_t()
Virtual destructor.
Definition JLegendre.hh:60
double xmax
maximal abscissa
Definition JLegendre.hh:205
void set(const double xmin, const double xmax)
Set abscissa values.
Definition JLegendre.hh:136
JOrdinate_t operator()(const double x) const
Function value.
Definition JLegendre.hh:79
std::vector< JOrdinate_t >::reverse_iterator reverse_iterator
Definition JLegendre.hh:32
JLegendre_t(const double xmin, const double xmax)
Constructor.
Definition JLegendre.hh:50
double getX(const double x) const
Get normalised abscissa value.
Definition JLegendre.hh:113
double getXmax() const
Get maximal abscissa value.
Definition JLegendre.hh:101
void set(T __begin, T __end)
Set abscissa values.
Definition JLegendre.hh:155
Template definition for function evaluation of Legendre polynome.
Definition JLegendre.hh:269
void set(T __begin, T __end)
Set Legendre polynome.
Definition JLegendre.hh:327
JLegendre()
Default constructor.
Definition JLegendre.hh:276
friend std::istream & operator>>(std::istream &in, JLegendre &object)
Read Legendre polynome from input.
Definition JLegendre.hh:360
JLegendre(T __begin, T __end)
Constructor.
Definition JLegendre.hh:308
JLegendre(const double xmin, const double xmax)
Constructor.
Definition JLegendre.hh:288