Jpp
JMath.hh
Go to the documentation of this file.
1 #ifndef __JMATH__
2 #define __JMATH__
3 
4 #include "JLang/JNullType.hh"
5 #include "JMath/JCalculator.hh"
6 
7 
8 /**
9  * \file
10  *
11  * Base class for data structures with artithmetic capabilities.
12  * \author mdejong
13  */
14 namespace JMATH {}
15 namespace JPP { using namespace JMATH; }
16 
17 namespace JMATH {
18 
19  using JLANG::JNullType;
20 
21 
22  /**
23  * Auxiliary base class for aritmetic operations of derived class types.
24  */
25  template<class JFirst_t, class JSecond_t = JNullType>
26  struct JMath;
27 
28 
29  /**
30  * Template class for data structures with arithmetic capabilities.
31  *
32  * This class provides for the operators <tt> - += -= *= /= + - * / </tt>.
33  * The template parameter should have the corresponding member methods:
34  * <pre>
35  * T& negate();
36  * T& add(const T& object);
37  * T& sub(const T& object);
38  * T& mul(const double factor);
39  * T& div(const double factor);
40  * </pre>
41  *
42  * This class also provides for the object multiplication operators <tt>*= *</tt>.
43  * The template parameter should then also have the member method:
44  * <pre>
45  * T& mul(const T&, const T&);
46  * </pre>
47  *
48  * This class uses in-class friend operators (see Barton-Nackman trick).
49  * This class adds interpolation functionality.
50  */
51  template<class T>
52  struct JMath<T, JNullType> {
53  /**
54  * Affirm operator.
55  *
56  * \param object this object
57  * \return affirmed object
58  */
59  friend T operator+(const T& object)
60  {
61  return T(object);
62  }
63 
64 
65  /**
66  * Negate operator.
67  *
68  * \param object this object
69  * \return negated object
70  */
71  friend T operator-(const T& object)
72  {
73  return T(object).negate();
74  }
75 
76 
77  /**
78  * Add object.
79  *
80  * \param object this object
81  * \param value value
82  * \return this object
83  */
84  friend T& operator+=(T& object, const T& value)
85  {
86  return object.add(value);
87  }
88 
89 
90  /**
91  * Subtract object.
92  *
93  * \param object this object
94  * \param value value
95  * \return this object
96  */
97  friend T& operator-=(T& object, const T& value)
98  {
99  return object.sub(value);
100  }
101 
102 
103  /**
104  * Scale object.
105  *
106  * \param object this object
107  * \param factor factor
108  * \return this object
109  */
110  friend T& operator*=(T& object, const double factor)
111  {
112  return object.mul(factor);
113  }
114 
115 
116  /**
117  * Scale object.
118  *
119  * \param object this object
120  * \param factor factor
121  * \return this object
122  */
123  friend T& operator/=(T& object, const double factor)
124  {
125  return object.div(factor);
126  }
127 
128 
129  /**
130  * Add objects.
131  *
132  * \param first first object
133  * \param second second object
134  * \return result object
135  */
136  friend T operator+(const T& first, const T& second)
137  {
138  return JCalculator<T>::calculator.set(first).add(second);
139  }
140 
141 
142  /**
143  * Subtract objects.
144  *
145  * \param first first object
146  * \param second second object
147  * \return result object
148  */
149  friend T operator-(const T& first, const T& second)
150  {
151  return JCalculator<T>::calculator.set(first).sub(second);
152  }
153 
154 
155  /**
156  * Scale object.
157  *
158  * \param object object
159  * \param factor factor
160  * \return object
161  */
162  friend T operator*(const T& object, const double factor)
163  {
164  return JCalculator<T>::calculator.set(object).mul(factor);
165  }
166 
167 
168  /**
169  * Scale object.
170  *
171  * \param factor factor
172  * \param object object
173  * \return object
174  */
175  friend T operator*(const double factor, const T& object)
176  {
177  return JCalculator<T>::calculator.set(object).mul(factor);
178  }
179 
180 
181  /**
182  * Scale object.
183  *
184  * \param object object
185  * \param factor factor
186  * \return object
187  */
188  friend T operator/(const T& object, const double factor)
189  {
190  return JCalculator<T>::calculator.set(object).div(factor);
191  }
192 
193 
194  /**
195  * Multiply with object.
196  *
197  * \param object object
198  * \return result object
199  */
200  T& mul(const T& object)
201  {
202  return static_cast<T&>(*this) = JCalculator<T>::calculator.mul(static_cast<const T&>(*this), object);
203  }
204 
205 
206  /**
207  * Multiply with object.
208  *
209  * \param first first object
210  * \param second second object
211  * \return result object
212  */
213  friend T& operator*=(T& first, const T& second)
214  {
215  return first.mul(second);
216  }
217 
218 
219  /**
220  * Multiply objects.
221  *
222  * \param first first object
223  * \param second second object
224  * \return calculator
225  */
226  friend const JCalculator<T, 1>& operator*(const T& first, const T& second)
227  {
228  JCalculator<T, 1>::calculator.mul(first, second);
229 
231  }
232 
233 
234  /**
235  * Interpolation between objects.
236  * The result is equal to <tt>*this = (1 - alpha) x (*this) + (alpha) x (object)</tt>.
237  *
238  * \param object object
239  * \param alpha interpolation factor (normally between 0 and 1)
240  * \return this object
241  */
242  T& interpolate(const T& object, const double alpha)
243  {
244  static_cast<T*>(this)->mul(1.0 - alpha);
245  static_cast<T*>(this)->add(T(object).mul(alpha));
246 
247  return static_cast<T&>(*this);
248  }
249  };
250 
251 
252  /**
253  * Specialisation of JMath for two data types.
254  *
255  * This class provides for the object multiplication operators <tt>*= *</tt>.
256  * The template parameter should then have the member method:
257  * <pre>
258  * JFirst_t& mul(const JFirst_t&, const JSecond_t&);
259  * </pre>
260  * where <tt>JFirst_t</tt> and <tt>JSecond_t</tt> refer to the first and second template parameter, respectively.
261  *
262  * This class uses in-class friend operators (see Barton-Nackman trick).
263  */
264  template<class JFirst_t, class JSecond_t>
265  struct JMath
266  {
267  /**
268  * Multiply with object.
269  *
270  * \param object object
271  * \return result object
272  */
273  JFirst_t& mul(const JSecond_t& object)
274  {
275  return static_cast<JFirst_t&>(*this) = JCalculator<JFirst_t>::calculator.mul(static_cast<const JFirst_t&>(*this), object);
276  }
277 
278 
279  /**
280  * Multiply with object.
281  *
282  * \param first first object
283  * \param second second object
284  * \return result object
285  */
286  friend JFirst_t& operator*=(JFirst_t& first, const JSecond_t& second)
287  {
288  return first.mul(second);
289  }
290 
291 
292  /**
293  * Multiply objects.
294  *
295  * \param first first object
296  * \param second second object
297  * \return result object
298  */
299  friend JFirst_t operator*(const JFirst_t& first, const JSecond_t& second)
300  {
301  return JFirst_t(first).mul(second);
302  }
303  };
304 }
305 
306 #endif
JMATH::JMath::mul
JFirst_t & mul(const JSecond_t &object)
Multiply with object.
Definition: JMath.hh:273
JLANG::JNullType
Auxiliary class for no type definition.
Definition: JNullType.hh:19
JMATH::JMath< T, JNullType >::operator-
friend T operator-(const T &object)
Negate operator.
Definition: JMath.hh:71
JMATH::JMath< T, JNullType >::operator*
friend T operator*(const double factor, const T &object)
Scale object.
Definition: JMath.hh:175
JMATH::JMath
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
JMATH::JMath< T, JNullType >::interpolate
T & interpolate(const T &object, const double alpha)
Interpolation between objects.
Definition: JMath.hh:242
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JMATH::JMath< T, JNullType >::operator+=
friend T & operator+=(T &object, const T &value)
Add object.
Definition: JMath.hh:84
JMATH::JMath< T, JNullType >::operator+
friend T operator+(const T &first, const T &second)
Add objects.
Definition: JMath.hh:136
JMATH::JMath< T, JNullType >::operator*
friend T operator*(const T &object, const double factor)
Scale object.
Definition: JMath.hh:162
JMATH::JCalculator::set
JCalculator & set(const T &value)
Set calculator value.
Definition: JCalculator.hh:27
JMATH::JMath< T, JNullType >::operator+
friend T operator+(const T &object)
Affirm operator.
Definition: JMath.hh:59
JMATH::JMath< T, JNullType >::operator*=
friend T & operator*=(T &first, const T &second)
Multiply with object.
Definition: JMath.hh:213
JMATH
Auxiliary classes and methods for mathematical operations.
Definition: JCalculator.hh:9
JMATH::JMath< T, JNullType >::operator*=
friend T & operator*=(T &object, const double factor)
Scale object.
Definition: JMath.hh:110
JMATH::JMath< T, JNullType >::operator/
friend T operator/(const T &object, const double factor)
Scale object.
Definition: JMath.hh:188
JMATH::JMath< T, JNullType >::operator-=
friend T & operator-=(T &object, const T &value)
Subtract object.
Definition: JMath.hh:97
JMATH::JMath< T, JNullType >::mul
T & mul(const T &object)
Multiply with object.
Definition: JMath.hh:200
JNullType.hh
JMATH::JCalculator
Auxiliary class for arithmetic operations on objects.
Definition: JCalculator.hh:18
JMATH::JMath< T, JNullType >::operator*
const friend JCalculator< T, 1 > & operator*(const T &first, const T &second)
Multiply objects.
Definition: JMath.hh:226
JMATH::JMath::operator*
friend JFirst_t operator*(const JFirst_t &first, const JSecond_t &second)
Multiply objects.
Definition: JMath.hh:299
JMATH::JMath::operator*=
friend JFirst_t & operator*=(JFirst_t &first, const JSecond_t &second)
Multiply with object.
Definition: JMath.hh:286
JMATH::JMath< T, JNullType >::operator-
friend T operator-(const T &first, const T &second)
Subtract objects.
Definition: JMath.hh:149
JMATH::JMath< T, JNullType >::operator/=
friend T & operator/=(T &object, const double factor)
Scale object.
Definition: JMath.hh:123
JCalculator.hh