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