Jpp  15.0.5
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMath.hh
Go to the documentation of this file.
1 #ifndef __JMATH__
2 #define __JMATH__
3 
4 #include <cmath>
5 #include <iterator>
6 
7 #include "JLang/JNullType.hh"
8 #include "JLang/JClass.hh"
9 #include "JLang/JBool.hh"
10 #include "JLang/JVectorize.hh"
11 #include "JLang/JException.hh"
12 #include "JMath/JZero.hh"
13 #include "JMath/JCalculator.hh"
14 
15 
16 /**
17  * \file
18  *
19  * Base class for data structures with artithmetic capabilities.
20  * \author mdejong
21  */
22 namespace JMATH {}
23 namespace JPP { using namespace JMATH; }
24 
25 namespace JGEOMETRY3D { class JQuaternion3D; }
26 
27 namespace JMATH {
28 
29  using JLANG::JNullType;
30  using JLANG::array_type;
32 
33 
34  /**
35  * Power \f$x^y\f$.
36  *
37  * \param x value
38  * \param y power
39  * \return result
40  */
41  template<class T>
42  inline T pow(const T& x, const double y);
43 
44 
45  /**
46  * Auxiliary class to hide data type specific methods.
47  */
48  struct JMath_t {
49  /**
50  * Friend declaration of global method.
51  */
52  template<class T>
53  friend T JMATH::pow(const T&, const double y);
54 
55  private:
56  /**
57  * Power \f$x^y\f$.
58  * This method corresponds to primitive data types.
59  *
60  * \param x value
61  * \param y power
62  * \param option true
63  * \return result
64  */
65  template<class T>
66  static inline T pow(const T& x, const double y, const JLANG::JBool<true> option)
67  {
68  return std::pow(x, y);
69  }
70 
71 
72  /**
73  * Power \f$x^y\f$.
74  * Power.
75  * This method corresponds to non-primitive data types.
76  *
77  * \param x value
78  * \param y power
79  * \param option false
80  * \return result
81  */
82  template<class T>
83  static inline T pow(const T& x, const double y, const JLANG::JBool<false> option)
84  {
85  return T(x).pow(y);
86  }
87  };
88 
89 
90  /**
91  * Power \f$x^y\f$.
92  *
93  * \param x value
94  * \param y power
95  * \return result
96  */
97  template<class T>
98  inline T pow(const T& x, const double y)
99  {
100  using namespace JPP;
101 
102  return JMath_t::pow(x, y, JBool<JClass<T>::is_primitive>());
103  }
104 
105 
106  /**
107  * Auxiliary base class for aritmetic operations of derived class types.
108  */
109  template<class JFirst_t, class JSecond_t = JNullType>
110  struct JMath;
111 
112 
113  /**
114  * Template base class for data structures with arithmetic capabilities.
115  *
116  * This class provides for the operators <tt> - += -= *= /= + - * / </tt>.\n
117  * To this end, the template parameter should privide for the corresponding member methods:
118  * <pre>
119  * T& negate();
120  * T& add(const T& object);
121  * T& sub(const T& object);
122  * T& mul(const double factor);
123  * T& div(const double factor);
124  * </pre>
125  *
126  * This class also provides for the object multiplication operators <tt>*= *</tt>.\n
127  * To this end, the template parameter should then also provide for the member method:
128  * <pre>
129  * T& mul(const T&, const T&);
130  * </pre>
131  *
132  * This class adds interpolation functionality.\n
133  * This class uses in-class friend operators (see Barton-Nackman trick).
134  */
135  template<class T>
136  struct JMath<T, JNullType> {
137  /**
138  * Affirm operator.
139  *
140  * \param object this object
141  * \return affirmed object
142  */
143  friend T operator+(const T& object)
144  {
145  return T(object);
146  }
147 
148 
149  /**
150  * Negate operator.
151  *
152  * \param object this object
153  * \return negated object
154  */
155  friend T operator-(const T& object)
156  {
157  return T(object).negate();
158  }
159 
160 
161  /**
162  * Add object.
163  *
164  * \param object this object
165  * \param value value
166  * \return this object
167  */
168  friend T& operator+=(T& object, const T& value)
169  {
170  return object.add(value);
171  }
172 
173 
174  /**
175  * Subtract object.
176  *
177  * \param object this object
178  * \param value value
179  * \return this object
180  */
181  friend T& operator-=(T& object, const T& value)
182  {
183  return object.sub(value);
184  }
185 
186 
187  /**
188  * Scale object.
189  *
190  * \param object this object
191  * \param factor factor
192  * \return this object
193  */
194  friend T& operator*=(T& object, const double factor)
195  {
196  return object.mul(factor);
197  }
198 
199 
200  /**
201  * Scale object.
202  *
203  * \param object this object
204  * \param factor factor
205  * \return this object
206  */
207  friend T& operator/=(T& object, const double factor)
208  {
209  return object.div(factor);
210  }
211 
212 
213  /**
214  * Add objects.
215  *
216  * \param first first object
217  * \param second second object
218  * \return result object
219  */
220  friend T operator+(const T& first, const T& second)
221  {
222  return JCalculator<T>::calculator.set(first).add(second);
223  }
224 
225 
226  /**
227  * Subtract objects.
228  *
229  * \param first first object
230  * \param second second object
231  * \return result object
232  */
233  friend T operator-(const T& first, const T& second)
234  {
235  return JCalculator<T>::calculator.set(first).sub(second);
236  }
237 
238 
239  /**
240  * Scale object.
241  *
242  * \param object object
243  * \param factor factor
244  * \return object
245  */
246  friend T operator*(const T& object, const double factor)
247  {
248  return JCalculator<T>::calculator.set(object).mul(factor);
249  }
250 
251 
252  /**
253  * Scale object.
254  *
255  * \param factor factor
256  * \param object object
257  * \return object
258  */
259  friend T operator*(const double factor, const T& object)
260  {
261  return JCalculator<T>::calculator.set(object).mul(factor);
262  }
263 
264 
265  /**
266  * Scale object.
267  *
268  * \param object object
269  * \param factor factor
270  * \return object
271  */
272  friend T operator/(const T& object, const double factor)
273  {
274  return JCalculator<T>::calculator.set(object).div(factor);
275  }
276 
277 
278  /**
279  * Multiply with object.
280  *
281  * \param object object
282  * \return result object
283  */
284  T& mul(const T& object)
285  {
286  return static_cast<T&>(*this) = JCalculator<T>::calculator.mul(static_cast<const T&>(*this), object);
287  }
288 
289 
290  /**
291  * Multiply with object.
292  *
293  * \param first first object
294  * \param second second object
295  * \return result object
296  */
297  friend T& operator*=(T& first, const T& second)
298  {
299  return first.mul(second);
300  }
301 
302 
303  /**
304  * Multiply objects.
305  *
306  * \param first first object
307  * \param second second object
308  * \return calculator
309  */
310  friend const JCalculator<T, 1>& operator*(const T& first, const T& second)
311  {
312  JCalculator<T, 1>::calculator.mul(first, second);
313 
315  }
316 
317 
318  /**
319  * Interpolation between objects.
320  * The result is equal to <tt>*this = (1 - alpha) * (*this) + (alpha) * (object)</tt>.
321  *
322  * \param object object
323  * \param alpha interpolation factor <tt>[0, 1]</tt>
324  * \return this object
325  */
326  T& interpolate(const T& object, const double alpha)
327  {
328  static_cast<T*>(this)->mul(1.0 - alpha);
329  static_cast<T*>(this)->add(T(object).mul(alpha));
330 
331  return static_cast<T&>(*this);
332  }
333  };
334 
335 
336  /**
337  * Specialisation of JMath for two data types.
338  *
339  * This class provides for the object multiplication operators <tt>*= *</tt>.
340  * The template parameter should then have the member method:
341  * <pre>
342  * JFirst_t& mul(const JFirst_t&, const JSecond_t&);
343  * </pre>
344  * where <tt>JFirst_t</tt> and <tt>JSecond_t</tt> refer to the first and second template parameter, respectively.
345  *
346  * This class uses in-class friend operators (see Barton-Nackman trick).
347  */
348  template<class JFirst_t, class JSecond_t>
349  struct JMath
350  {
351  /**
352  * Multiply with object.
353  *
354  * \param object object
355  * \return result object
356  */
357  JFirst_t& mul(const JSecond_t& object)
358  {
359  return static_cast<JFirst_t&>(*this) = JCalculator<JFirst_t>::calculator.mul(static_cast<const JFirst_t&>(*this), object);
360  }
361 
362 
363  /**
364  * Multiply with object.
365  *
366  * \param first first object
367  * \param second second object
368  * \return result object
369  */
370  friend JFirst_t& operator*=(JFirst_t& first, const JSecond_t& second)
371  {
372  return first.mul(second);
373  }
374 
375 
376  /**
377  * Multiply objects.
378  *
379  * \param first first object
380  * \param second second object
381  * \return result object
382  */
383  friend JFirst_t operator*(const JFirst_t& first, const JSecond_t& second)
384  {
385  return JFirst_t(first).mul(second);
386  }
387  };
388 
389 
390  /**
391  * Interpolation between objects.
392  * The result is equal to <tt>result = (1 - alpha) * (first) + (alpha) * (second)</tt>.
393  *
394  * \param first first object
395  * \param second second object
396  * \param alpha interpolation factor <tt>[0, 1]</tt>
397  * \return result
398  */
399  template<class T>
400  inline T interpolate(const T& first,
401  const T& second,
402  const double alpha)
403  {
404  return T(first).interpolate(second, alpha);
405  }
406 
407 
408  /**
409  * Auxiliary class to determine average of set of values.
410  */
411  template<class JValue_t>
412  struct JAverage {
413  /**
414  * Default constructor.
415  */
417  value (getZero<JValue_t>()),
418  weight(0.0)
419  {}
420 
421 
422  /**
423  * Constructor.
424  *
425  * \param __begin begin of data
426  * \param __end end of data
427  */
428  template<class T>
429  JAverage(T __begin, T __end) :
430  value (getZero<JValue_t>()),
431  weight(0.0)
432  {
433  for (T i = __begin; i != __end; ++i) {
434  value += (*i);
435  weight += 1.0;
436  }
437  }
438 
439 
440  /**
441  * Reset.
442  */
443  void reset()
444  {
445  this->value = getZero<JValue_t>();
446  this->weight = 0.0;
447  }
448 
449 
450  /**
451  * Type conversion operator.
452  *
453  * \return value
454  */
455  operator JValue_t () const
456  {
457  if (weight != 0.0)
458  return value * (1.0 / weight);
459  else
460  THROW(JDivisionByZero, "Invalid weight.");
461  }
462 
463 
464  /**
465  * Put value.
466  *
467  * \param value value
468  * \param w weight
469  */
470  void put(const JValue_t& value, const double w = 1.0)
471  {
472  this->value += value;
473  this->weight += w;
474  }
475 
476  private:
477  JValue_t value;
478  double weight;
479  };
480 
481 
482  /**
483  * Template definition so that compiler error is generated if implementation is missing (see JEigen3D.hh).
484  */
485  template<>
486  class JAverage<JGEOMETRY3D::JQuaternion3D>;
487 
488 
489  /**
490  * Get average.
491  *
492  * \param __begin begin of data
493  * \param __end end of data
494  * \return average value
495  */
496  template<class T>
497  typename std::iterator_traits<T>::value_type getAverage(T __begin, T __end)
498  {
499  typedef typename std::iterator_traits<T>::value_type value_type;
500 
501  return JAverage<value_type>(__begin, __end);
502  }
503 
504 
505  /**
506  * Get average.
507  *
508  * \param array c-array of values
509  * \return average value
510  */
511  template<class JValue_t, size_t N>
512  inline JValue_t getAverage(const JValue_t (&array)[N])
513  {
514  typedef JValue_t value_type;
515 
516  return JAverage<value_type>((const value_type*) array, (const value_type*) array + N);
517  }
518 
519 
520  /**
521  * Get average.
522  *
523  * \param buffer input data
524  * \return average value
525  */
526  template<class JElement_t, class JAllocator_t>
528  {
529  return JAverage<JElement_t>(buffer.begin(), buffer.end());
530  }
531 
532 
533  /**
534  * Get average.
535  *
536  * \param __begin begin of data
537  * \param __end end of data
538  * \param value default value
539  * \return average value
540  */
541  template<class T>
542  typename std::iterator_traits<T>::value_type getAverage(T __begin, T __end, typename std::iterator_traits<T>::value_type value)
543  {
544  try {
545  return getAverage(__begin, __end);
546  }
547  catch(const std::exception&) {
548  return value;
549  }
550  }
551 
552 
553  /**
554  * Get average.
555  *
556  * \param array c-array of values
557  * \param value default value
558  * \return average value
559  */
560  template<class JValue_t, size_t N>
561  inline JValue_t getAverage(const JValue_t (&array)[N], typename JLANG::JClass<JValue_t>::argument_type value)
562  {
563  try {
564  return getAverage(array);
565  }
566  catch(const std::exception&) {
567  return value;
568  }
569  }
570 
571 
572  /**
573  * Get average.
574  *
575  * \param buffer input data
576  * \param value default value
577  * \return average value
578  */
579  template<class JElement_t, class JAllocator_t>
581  {
582  try {
583  return getAverage(buffer);
584  }
585  catch(const std::exception&) {
586  return value;
587  }
588  }
589 }
590 
591 #endif
friend T & operator/=(T &object, const double factor)
Scale object.
Definition: JMath.hh:207
static T pow(const T &x, const double y, const JLANG::JBool< false > option)
Power .
Definition: JMath.hh:83
data_type w[N+1][M+1]
Definition: JPolint.hh:741
Exceptions.
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:110
friend T operator-(const T &object)
Negate operator.
Definition: JMath.hh:155
double weight
Definition: JMath.hh:478
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
T & interpolate(const T &object, const double alpha)
Interpolation between objects.
Definition: JMath.hh:326
friend const JCalculator< T, 1 > & operator*(const T &first, const T &second)
Multiply objects.
Definition: JMath.hh:310
friend T operator*(const double factor, const T &object)
Scale object.
Definition: JMath.hh:259
T interpolate(const T &first, const T &second, const double alpha)
Interpolation between objects.
Definition: JMath.hh:400
void reset()
Reset.
Definition: JMath.hh:443
std::iterator_traits< T >::value_type getAverage(T __begin, T __end)
Get average.
Definition: JMath.hh:497
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
friend T & operator+=(T &object, const T &value)
Add object.
Definition: JMath.hh:168
Definition of zero value for any class.
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
void put(const JValue_t &value, const double w=1.0)
Put value.
Definition: JMath.hh:470
friend T & operator*=(T &first, const T &second)
Multiply with object.
Definition: JMath.hh:297
T getZero()
Get zero value for a given data type.
Definition: JZero.hh:26
friend JFirst_t operator*(const JFirst_t &first, const JSecond_t &second)
Multiply objects.
Definition: JMath.hh:383
JArgument< T >::argument_type argument_type
Definition: JClass.hh:82
friend JFirst_t & operator*=(JFirst_t &first, const JSecond_t &second)
Multiply with object.
Definition: JMath.hh:370
friend T operator+(const T &object)
Affirm operator.
Definition: JMath.hh:143
Auxiliary template class for type bool.
Definition: JBool.hh:20
JAverage()
Default constructor.
Definition: JMath.hh:416
static T pow(const T &x, const double y, const JLANG::JBool< true > option)
Power .
Definition: JMath.hh:66
JAverage(T __begin, T __end)
Constructor.
Definition: JMath.hh:429
Auxiliary class to hide data type specific methods.
Definition: JMath.hh:48
do set_variable OUTPUT_DIRECTORY $WORKDIR T
Auxiliary class for no type definition.
Definition: JNullType.hh:19
T pow(const T &x, const double y)
Power .
Definition: JMath.hh:98
JCalculator & set(const T &value)
Set calculator value.
Definition: JCalculator.hh:27
JFirst_t & mul(const JSecond_t &object)
Multiply with object.
Definition: JMath.hh:357
friend T operator+(const T &first, const T &second)
Add objects.
Definition: JMath.hh:220
friend T operator*(const T &object, const double factor)
Scale object.
Definition: JMath.hh:246
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
Auxiliary class for arithmetic operations on objects.
Definition: JCalculator.hh:18
Data structure for unit quaternion in three dimensions.
Template for generic class types.
Definition: JClass.hh:80
JValue_t value
Definition: JMath.hh:477
Exception for division by zero.
Definition: JException.hh:270
friend T & operator-=(T &object, const T &value)
Subtract object.
Definition: JMath.hh:181
friend T operator-(const T &first, const T &second)
Subtract objects.
Definition: JMath.hh:233
friend T & operator*=(T &object, const double factor)
Scale object.
Definition: JMath.hh:194
friend T operator/(const T &object, const double factor)
Scale object.
Definition: JMath.hh:272
T & mul(const T &object)
Multiply with object.
Definition: JMath.hh:284
Auxiliary data structure for return type of make methods.
Definition: JVectorize.hh:26
Auxiliary class to determine average of set of values.
Definition: JMath.hh:412