Jpp  15.0.1
the software that should make you happy
 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 <limits>
5 #include <cmath>
6 
7 #include "JLang/JException.hh"
8 #include "JMath/JZero.hh"
9 
10 
11 /**
12  * \file
13  * Auxiliary methods for geometrical methods.
14  * In this, the action of each global method is transferred to
15  * the corresponding member method of the leading (first) argument, e.g:
16  *
17  * <tt>getXXX(first, second, ...)</tt>
18  *
19  * becomes
20  *
21  * <tt>first.getXXX(second, ...)</tt>
22  *\author mdejong
23  */
24 
25 /**
26  * Auxiliary classes and methods for mathematical operations.
27  */
28 namespace JMATH {}
29 namespace JPP { using namespace JMATH; }
30 
31 namespace JMATH {
32 
34 
35 
36  /**
37  * Determine factorial.
38  *
39  * \param n number
40  * \return factorial (= n!)
41  */
42  inline long long int factorial(const long long int n)
43  {
44  if (n < 0) {
45  THROW(JValueOutOfRange, "JMATH::factorial(): invalid argument " << n);
46  }
47 
48  if (n != 1)
49  return n * factorial(n - 1);
50  else
51  return 1;
52  }
53 
54 
55  /**
56  * Determine combinatorics.
57  *
58  * \param n number
59  * \param m number
60  * \return n!/(m!*(n-m)!)
61  */
62  inline long long int factorial(const long long int n, const long long int m)
63  {
64  if (n < 0 || m < 0 || n < m) {
65  THROW(JValueOutOfRange, "JMATH::factorial(): invalid argument " << n << ' ' << m);
66  }
67 
68  if (n == m)
69  return 1;
70  else if (m == 0)
71  return 1;
72  else
73  return factorial(n - 1, m - 1) + factorial(n - 1, m);
74  }
75 
76 
77  /**
78  * Check equality.
79  *
80  * \param first first object
81  * \param second second object
82  * \param precision precision
83  * \return true if two objects are equals; else false
84  */
85  template<class JFirst_t, class JSecond_t>
86  inline bool equals(const JFirst_t& first,
87  const JSecond_t& second,
88  const double precision = std::numeric_limits<double>::min())
89  {
90  return first.equals(second, precision);
91  }
92 
93 
94  /**
95  * Get square of distance between objects.
96  *
97  * \param first first object
98  * \param second second object
99  * \return square of distance
100  */
101  template<class JFirst_t, class JSecond_t>
102  inline double getDistanceSquared(const JFirst_t& first,
103  const JSecond_t& second)
104  {
105  return first.getDistanceSquared(second);
106  }
107 
108 
109  /**
110  * Get distance between objects.
111  *
112  * \param first first object
113  * \param second second object
114  * \return distance
115  */
116  template<class JFirst_t, class JSecond_t>
117  inline double getDistance(const JFirst_t& first,
118  const JSecond_t& second)
119  {
120  return first.getDistance(second);
121  }
122 
123 
124  /**
125  * Get dot product of objects.
126  *
127  * \param first first object
128  * \param second second object
129  * \return dot product
130  */
131  template<class JFirst_t, class JSecond_t>
132  inline double getDot(const JFirst_t& first,
133  const JSecond_t& second)
134  {
135  return first.getDot(second);
136  }
137 
138 
139  /**
140  * Get space angle between objects.
141  *
142  * \param first first object
143  * \param second second object
144  * \return angle [deg]
145  */
146  template<class JFirst_t, class JSecond_t>
147  inline double getAngle(const JFirst_t& first,
148  const JSecond_t& second)
149  {
150  const double dot = getDot(first,second);
151 
152  if (dot >= +1.0)
153  return 0.0;
154  else if (dot <= -1.0)
155  return 180.0;
156  else
157  return acos(dot) * 180.0 / acos(-1.0);
158  }
159 
160 
161  /**
162  * Get perpendicular dot product of objects.
163  *
164  * \param first first object
165  * \param second second object
166  * \return perpendicular dot product
167  */
168  template<class JFirst_t, class JSecond_t>
169  inline double getPerpDot(const JFirst_t& first,
170  const JSecond_t& second)
171  {
172  return first.getPerpDot(second);
173  }
174 
175 
176  /**
177  * Get cross product of objects.
178  *
179  * \param first first object
180  * \param second second object
181  * \return cross product
182  */
183  template<class T>
184  inline T getCross(const T& first,
185  const T& second)
186  {
187  return T().getCross(first, second);
188  }
189 
190 
191  /**
192  * Convolute data with given kernel.
193  *
194  * \param input input data
195  * \param kernel convolution kernel
196  * \return convoluted data
197  */
198  template<class T>
199  inline std::vector<T> convolve(const std::vector<T>& input, const std::vector<T>& kernel)
200  {
201  const size_t k = kernel.size();
202  const size_t n = input .size() - k + 1;
203 
204  std::vector<T> out(n, getZero<T>());
205 
206  for (size_t i = 0; i != n; ++i) {
207  for (size_t j = 0; j != k; ++j) {
208  out[i] += input[i + j] * kernel[k - j - 1];
209  }
210  }
211 
212  return out;
213  }
214 }
215 
216 #endif
double getAngle(const JQuaternion3D &first, const JQuaternion3D &second)
Get space angle between quanternions.
Exceptions.
then fatal No hydrophone data file $HYDROPHONE_TXT fi sort gr k
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:670
double getDistanceSquared(const JFirst_t &first, const JSecond_t &second)
Get square of distance between objects.
Definition of zero value for any class.
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:42
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
double getDistance(const JFirst_t &first, const JSecond_t &second)
Get distance between objects.
const int n
Definition: JPolint.hh:660
do set_variable OUTPUT_DIRECTORY $WORKDIR T
std::vector< T > convolve(const std::vector< T > &input, const std::vector< T > &kernel)
Convolute data with given kernel.
int j
Definition: JPolint.hh:666
bool equals(const JFirst_t &first, const JSecond_t &second, const double precision=std::numeric_limits< double >::min())
Check equality.
Definition: JMathToolkit.hh:86
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
T getCross(const T &first, const T &second)
Get cross product of objects.