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