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