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