v2.0.0
Loading...
Searching...
No Matches
numerics.h
Go to the documentation of this file.
1//=============================================================================================================
34
35#ifndef NUMERICS_H
36#define NUMERICS_H
37
38//=============================================================================================================
39// INCLUDES
40//=============================================================================================================
41
42#include "math_global.h"
43
44#include <string>
45#include <utility>
46#include <vector>
47
48//=============================================================================================================
49// EIGEN INCLUDES
50//=============================================================================================================
51
52#include <Eigen/Core>
53
54//=============================================================================================================
55// QT INCLUDES
56//=============================================================================================================
57
58#include <QVariant>
59
60//=============================================================================================================
61// DEFINE NAMESPACE UTILSLIB
62//=============================================================================================================
63
64namespace UTILSLIB
65{
66
67//=============================================================================================================
74{
75public:
76 //=========================================================================================================
80 ~Numerics() = default;
81
82 //=========================================================================================================
91 static int gcd(int iA, int iB);
92
93 //=========================================================================================================
101 static bool issparse(Eigen::VectorXd &v);
102
103 //=========================================================================================================
111 static int nchoose2(int n);
112
113 //=========================================================================================================
126 static Eigen::MatrixXd rescale(const Eigen::MatrixXd &data,
127 const Eigen::RowVectorXf &times,
128 const QPair<float, float> &baseline,
129 QString mode);
130
131 //=========================================================================================================
144 static Eigen::MatrixXd rescale(const Eigen::MatrixXd& data,
145 const Eigen::RowVectorXf& times,
146 const std::pair<float, float>& baseline,
147 const std::string& mode);
148
149 //=========================================================================================================
157 template<typename T>
158 static inline double log2(const T d);
159
160 //=========================================================================================================
172 template<typename T>
173 static void histcounts(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& matRawData,
174 bool bMakeSymmetrical,
175 int iClassAmount,
176 Eigen::VectorXd& vecResultClassLimits,
177 Eigen::VectorXi& vecResultFrequency,
178 double dGlobalMin = 0.0,
179 double dGlobalMax = 0.0);
180
181 template<typename T>
182 static void histcounts(const Eigen::Matrix<T, Eigen::Dynamic, 1>& matRawData,
183 bool bMakeSymmetrical,
184 int iClassAmount,
185 Eigen::VectorXd& vecResultClassLimits,
186 Eigen::VectorXi& vecResultFrequency,
187 double dGlobalMin = 0.0,
188 double dGlobalMax = 0.0);
189
190 template<typename T>
191 static void histcounts(const Eigen::Matrix<T, 1, Eigen::Dynamic>& matRawData,
192 bool bMakeSymmetrical,
193 int iClassAmount,
194 Eigen::VectorXd& vecResultClassLimits,
195 Eigen::VectorXi& vecResultFrequency,
196 double dGlobalMin = 0.0,
197 double dGlobalMax = 0.0);
198};
199
200//=============================================================================================================
201// INLINE & TEMPLATE DEFINITIONS
202//=============================================================================================================
203
204template<typename T>
205inline double Numerics::log2(const T d)
206{
207 return log(d)/log(2);
208}
209
210//=============================================================================================================
211
212template<typename T>
213void Numerics::histcounts(const Eigen::Matrix<T, Eigen::Dynamic, 1>& matRawData,
214 bool bMakeSymmetrical,
215 int iClassAmount,
216 Eigen::VectorXd& vecResultClassLimits,
217 Eigen::VectorXi& vecResultFrequency,
218 double dGlobalMin,
219 double dGlobalMax)
220{
221 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(matRawData.rows(), 1);
222 matrixName.col(0) = matRawData;
223 Numerics::histcounts(matrixName, bMakeSymmetrical, iClassAmount, vecResultClassLimits, vecResultFrequency, dGlobalMin, dGlobalMax);
224}
225
226//=============================================================================================================
227
228template<typename T>
229void Numerics::histcounts(const Eigen::Matrix<T, 1, Eigen::Dynamic>& matRawData,
230 bool bMakeSymmetrical,
231 int iClassAmount,
232 Eigen::VectorXd& vecResultClassLimits,
233 Eigen::VectorXi& vecResultFrequency,
234 double dGlobalMin,
235 double dGlobalMax)
236{
237 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(1, matRawData.cols());
238 matrixName.row(0) = matRawData;
239 Numerics::histcounts(matrixName, bMakeSymmetrical, iClassAmount, vecResultClassLimits, vecResultFrequency, dGlobalMin, dGlobalMax);
240}
241
242//=============================================================================================================
243
244template<typename T>
245void Numerics::histcounts(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& matRawData,
246 bool bMakeSymmetrical,
247 int iClassAmount,
248 Eigen::VectorXd& vecResultClassLimits,
249 Eigen::VectorXi& vecResultFrequency,
250 double dGlobalMin,
251 double dGlobalMax)
252{
253 if(matRawData.rows() == 0 || matRawData.cols() == 0) {
254 return;
255 }
256
257 vecResultClassLimits.resize(iClassAmount + 1);
258 vecResultFrequency.resize(iClassAmount);
259
260 for (int count = 0; count < iClassAmount; ++count)
261 {
262 vecResultFrequency(count) = 0;
263 }
264
265 double desiredMin,
266 desiredMax;
267 double rawMin(0.0),
268 rawMax(0.0),
269 localMin(0.0),
270 localMax(0.0);
271
272 rawMin = matRawData.minCoeff();
273 rawMax = matRawData.maxCoeff();
274
275 if (bMakeSymmetrical == true)
276 {
277 if (std::fabs(rawMin) > rawMax)
278 {
279 localMax = std::fabs(rawMin);
280 localMin = rawMin;
281 }
282 else if (rawMax > std::fabs(rawMin))
283 {
284 localMin = -(rawMax);
285 localMax = rawMax;
286 }
287 else
288 {
289 localMin = rawMin;
290 localMax = rawMax;
291 }
292 }
293 else
294 {
295 localMin = rawMin;
296 localMax = rawMax;
297 }
298
299 if (dGlobalMin == 0.0 && dGlobalMax == 0.0)
300 {
301 desiredMin = localMin;
302 desiredMax = localMax;
303 vecResultClassLimits[0] = desiredMin;
304 vecResultClassLimits[iClassAmount] = desiredMax;
305 }
306 else
307 {
308 desiredMin = dGlobalMin;
309 desiredMax = dGlobalMax;
310 vecResultClassLimits(0) = desiredMin;
311 vecResultClassLimits(iClassAmount) = desiredMax;
312 }
313
314 double range = (vecResultClassLimits(iClassAmount) - vecResultClassLimits(0)),
315 dynamicUpperClassLimit;
316
317 for (int kr = 0; kr < iClassAmount; ++kr)
318 {
319 dynamicUpperClassLimit = (vecResultClassLimits(0) + (kr * (range / iClassAmount)));
320 vecResultClassLimits(kr) = dynamicUpperClassLimit;
321 }
322
323 for (int ir = 0; ir < matRawData.rows(); ++ir)
324 {
325 for (int jr = 0; jr < matRawData.cols(); ++jr)
326 {
327 if(matRawData(ir,jr) != 0.0) {
328 for (int kr = 0; kr < iClassAmount; ++kr)
329 {
330 if (kr == iClassAmount - 1)
331 {
332 if (matRawData(ir,jr) >= vecResultClassLimits(kr) && matRawData(ir,jr) <= vecResultClassLimits(kr + 1))
333 {
334 vecResultFrequency(kr) = vecResultFrequency(kr) + 1;
335 }
336 }
337 else
338 {
339 if (matRawData(ir,jr) >= vecResultClassLimits(kr) && matRawData(ir,jr) < vecResultClassLimits(kr + 1))
340 {
341 vecResultFrequency(kr) = vecResultFrequency(kr) + 1;
342 }
343 }
344 }
345 }
346 }
347 }
348}
349
350//=============================================================================================================
351} // NAMESPACE
352
353#endif // NUMERICS_H
math library export/import macros.
#define MATHSHARED_EXPORT
Definition math_global.h:55
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
Static numerical utility functions.
Definition numerics.h:74
static Eigen::MatrixXd rescale(const Eigen::MatrixXd &data, const Eigen::RowVectorXf &times, const std::pair< float, float > &baseline, const std::string &mode)
static void histcounts(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &matRawData, bool bMakeSymmetrical, int iClassAmount, Eigen::VectorXd &vecResultClassLimits, Eigen::VectorXi &vecResultFrequency, double dGlobalMin=0.0, double dGlobalMax=0.0)
Definition numerics.h:245
static int gcd(int iA, int iB)
Definition numerics.cpp:62
static bool issparse(Eigen::VectorXd &v)
Definition numerics.cpp:73
static Eigen::MatrixXd rescale(const Eigen::MatrixXd &data, const Eigen::RowVectorXf &times, const QPair< float, float > &baseline, QString mode)
static double log2(const T d)
Definition numerics.h:205
static int nchoose2(int n)
Definition numerics.cpp:92