Jpp 21.0.0
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 "JMath/JConstants.hh"
9#include "JMath/JZero.hh"
10
11
12/**
13 * \file
14 * Binary methods for member 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 *
24 * Auxiliary methods for mathematical operations.
25 *
26 *\author mdejong
27 */
28
29namespace JMATH {}
30namespace JPP { using namespace JMATH; }
31
32namespace JMATH {
33
34 /**
35 * Check equality.
36 *
37 * \param first first object
38 * \param second second object
39 * \param precision precision
40 * \return true if two objects are equals; else false
41 */
42 template<class JFirst_t, class JSecond_t>
43 inline bool equals(const JFirst_t& first,
44 const JSecond_t& second,
45 const double precision = std::numeric_limits<double>::min())
46 {
47 return first.equals(second, precision);
48 }
49
50
51 /**
52 * Get square of distance between objects.
53 *
54 * \param first first object
55 * \param second second object
56 * \return square of distance
57 */
58 template<class JFirst_t, class JSecond_t>
59 inline double getDistanceSquared(const JFirst_t& first,
60 const JSecond_t& second)
61 {
62 return first.getDistanceSquared(second);
63 }
64
65
66 /**
67 * Get distance between objects.
68 *
69 * \param first first object
70 * \param second second object
71 * \return distance
72 */
73 template<class JFirst_t, class JSecond_t>
74 inline double getDistance(const JFirst_t& first,
75 const JSecond_t& second)
76 {
77 return first.getDistance(second);
78 }
79
80
81 /**
82 * Get dot product of objects.
83 *
84 * \param first first object
85 * \param second second object
86 * \return dot product
87 */
88 template<class JFirst_t, class JSecond_t>
89 inline double getDot(const JFirst_t& first,
90 const JSecond_t& second)
91 {
92 return first.getDot(second);
93 }
94
95
96 /**
97 * Get space angle between objects.
98 *
99 * \param first first object
100 * \param second second object
101 * \return angle [deg]
102 */
103 template<class JFirst_t, class JSecond_t>
104 inline double getAngle(const JFirst_t& first,
105 const JSecond_t& second)
106 {
107 const double dot = getDot(first,second);
108
109 if (dot >= +1.0)
110 return 0.0;
111 else if (dot <= -1.0)
112 return 180.0;
113 else
114 return acos(dot) * 180.0 / PI;
115 }
116
117
118 /**
119 * Get perpendicular dot product of objects.
120 *
121 * \param first first object
122 * \param second second object
123 * \return perpendicular dot product
124 */
125 template<class JFirst_t, class JSecond_t>
126 inline double getPerpDot(const JFirst_t& first,
127 const JSecond_t& second)
128 {
129 return first.getPerpDot(second);
130 }
131
132
133 /**
134 * Get cross product of objects.
135 *
136 * \param first first object
137 * \param second second object
138 * \return cross product
139 */
140 template<class T>
141 inline T getCross(const T& first,
142 const T& second)
143 {
144 return T().getCross(first, second);
145 }
146
147
148 /**
149 * Convolute data with given kernel.
150 *
151 * \param input input data
152 * \param kernel convolution kernel
153 * \return convoluted data
154 */
155 template<class T>
156 inline std::vector<T> convolve(const std::vector<T>& input, const std::vector<T>& kernel)
157 {
158 const size_t k = kernel.size();
159 const size_t n = input .size() - k + 1;
160
161 std::vector<T> out(n, getZero<T>());
162
163 for (size_t i = 0; i != n; ++i) {
164 for (size_t j = 0; j != k; ++j) {
165 out[i] += input[i + j] * kernel[k - j - 1];
166 }
167 }
168
169 return out;
170 }
171}
172
173#endif
Mathematical constants.
Definition of zero value for any class.
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.
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.
static const double PI
Mathematical constants.
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).