Jpp
JMathToolkit.hh
Go to the documentation of this file.
1 #ifndef __JMATHTOOLKIT__
2 #define __JMATHTOOLKIT__
3 
4 #include <limits>
5 #include <cmath>
6 
7 #include "JLang/JException.hh"
8 
9 
10 /**
11  * \file
12  * Auxiliary methods for geometrical methods.
13  * In this, the action of each global method is transferred to
14  * the corresponding member method of the leading (first) argument, e.g:
15  *
16  * <tt>getXXX(first, second, ...)</tt>
17  *
18  * becomes
19  *
20  * <tt>first.getXXX(second, ...)</tt>
21  *\author mdejong
22  */
23 
24 /**
25  * Auxiliary classes and methods for mathematical operations.
26  */
27 namespace JMATH {}
28 namespace JPP { using namespace JMATH; }
29 
30 namespace JMATH {
31 
33 
34 
35  /**
36  * Determine factorial.
37  *
38  * \param n number
39  * \return factorial (= n!)
40  */
41  inline long long int factorial(const long long int n)
42  {
43  if (n < 0) {
44  THROW(JValueOutOfRange, "JMATH::factorial(): invalid argument " << n);
45  }
46 
47  if (n != 1)
48  return n * factorial(n - 1);
49  else
50  return 1;
51  }
52 
53 
54  /**
55  * Determine combinatorics.
56  *
57  * \param n number
58  * \param m number
59  * \return n!/(m!*(n-m)!)
60  */
61  inline long long int factorial(const long long int n, const long long int m)
62  {
63  if (n < 0 || m < 0 || n < m) {
64  THROW(JValueOutOfRange, "JMATH::factorial(): invalid argument " << n << ' ' << m);
65  }
66 
67  if (n == m)
68  return 1;
69  else if (m == 0)
70  return 1;
71  else
72  return factorial(n - 1, m - 1) + factorial(n - 1, m);
73  }
74 
75 
76  /**
77  * Check equality.
78  *
79  * \param first first object
80  * \param second second object
81  * \param precision precision
82  * \return true if two objects are equals; else false
83  */
84  template<class JFirst_t, class JSecond_t>
85  inline bool equals(const JFirst_t& first,
86  const JSecond_t& second,
87  const double precision = std::numeric_limits<double>::min())
88  {
89  return first.equals(second, precision);
90  }
91 
92 
93  /**
94  * Get square of distance between objects.
95  *
96  * \param first first object
97  * \param second second object
98  * \return square of distance
99  */
100  template<class JFirst_t, class JSecond_t>
101  inline double getDistanceSquared(const JFirst_t& first,
102  const JSecond_t& second)
103  {
104  return first.getDistanceSquared(second);
105  }
106 
107 
108  /**
109  * Get distance between objects.
110  *
111  * \param first first object
112  * \param second second object
113  * \return distance
114  */
115  template<class JFirst_t, class JSecond_t>
116  inline double getDistance(const JFirst_t& first,
117  const JSecond_t& second)
118  {
119  return first.getDistance(second);
120  }
121 
122 
123  /**
124  * Get dot product of objects.
125  *
126  * \param first first object
127  * \param second second object
128  * \return dot product
129  */
130  template<class JFirst_t, class JSecond_t>
131  inline double getDot(const JFirst_t& first,
132  const JSecond_t& second)
133  {
134  return first.getDot(second);
135  }
136 
137 
138  /**
139  * Get space angle between objects.
140  *
141  * \param first first object
142  * \param second second object
143  * \return angle [deg]
144  */
145  template<class JFirst_t, class JSecond_t>
146  inline double getAngle(const JFirst_t& first,
147  const JSecond_t& second)
148  {
149  const double dot = getDot(first,second);
150 
151  if (dot >= +1.0)
152  return 0.0;
153  else if (dot <= -1.0)
154  return 180.0;
155  else
156  return acos(dot) * 180.0 / acos(-1.0);
157  }
158 
159 
160  /**
161  * Get perpendicular dot product of objects.
162  *
163  * \param first first object
164  * \param second second object
165  * \return perpendicular dot product
166  */
167  template<class JFirst_t, class JSecond_t>
168  inline double getPerpDot(const JFirst_t& first,
169  const JSecond_t& second)
170  {
171  return first.getPerpDot(second);
172  }
173 
174 
175  /**
176  * Interpolation between objects.
177  * The result is equal to <tt>result = (1 - alpha) x (first) + (alpha) x (second)</tt>.
178  *
179  * \param first first object
180  * \param second second object
181  * \param alpha interpolation factor (normally between 0 and 1)
182  * \return result
183  */
184  template<class T>
185  inline T interpolate(const T& first,
186  const T& second,
187  const double alpha)
188  {
189  return T(first).interpolate(second, alpha);
190  }
191 }
192 
193 #endif
JException.hh
JTOOLS::n
const int n
Definition: JPolint.hh:628
JMATH::interpolate
T interpolate(const T &first, const T &second, const double alpha)
Interpolation between objects.
Definition: JMathToolkit.hh:185
JMATH::equals
bool equals(const JFirst_t &first, const JSecond_t &second, const double precision=std::numeric_limits< double >::min())
Check equality.
Definition: JMathToolkit.hh:85
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JMATH::getPerpDot
double getPerpDot(const JFirst_t &first, const JSecond_t &second)
Get perpendicular dot product of objects.
Definition: JMathToolkit.hh:168
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
JMATH::getDistance
double getDistance(const JFirst_t &first, const JSecond_t &second)
Get distance between objects.
Definition: JMathToolkit.hh:116
JLANG::JValueOutOfRange
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
JMATH
Auxiliary classes and methods for mathematical operations.
Definition: JCalculator.hh:9
JMATH::factorial
long long int factorial(const long long int n, const long long int m)
Determine combinatorics.
Definition: JMathToolkit.hh:61
JMATH::getAngle
double getAngle(const JFirst_t &first, const JSecond_t &second)
Get space angle between objects.
Definition: JMathToolkit.hh:146
JMATH::getDot
double getDot(const JFirst_t &first, const JSecond_t &second)
Get dot product of objects.
Definition: JMathToolkit.hh:131
JMATH::getDistanceSquared
double getDistanceSquared(const JFirst_t &first, const JSecond_t &second)
Get square of distance between objects.
Definition: JMathToolkit.hh:101