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