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//=============================================================================================================
75{
76public:
77 //=========================================================================================================
81 ~Numerics() = default;
82
83 //=========================================================================================================
92 static int gcd(int iA, int iB);
93
94 //=========================================================================================================
102 static bool issparse(Eigen::VectorXd& v);
103
104 //=========================================================================================================
112 static int nchoose2(int n);
113
114 //=========================================================================================================
123 static double chi2Isf(double p, int dof);
124
125 //=========================================================================================================
138 static Eigen::MatrixXd rescale(const Eigen::MatrixXd& data,
139 const Eigen::RowVectorXf& times,
140 const QPair<float, float>& baseline,
141 QString mode);
142
143 //=========================================================================================================
156 static Eigen::MatrixXd rescale(const Eigen::MatrixXd& data,
157 const Eigen::RowVectorXf& times,
158 const std::pair<float, float>& baseline,
159 const std::string& mode);
160
161 //=========================================================================================================
169 template<typename T>
170 static inline double log2(const T d);
171
172 //=========================================================================================================
184 template<typename T>
185 static void histcounts(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& matRawData,
186 bool bMakeSymmetrical,
187 int iClassAmount,
188 Eigen::VectorXd& vecResultClassLimits,
189 Eigen::VectorXi& vecResultFrequency,
190 double dGlobalMin = 0.0,
191 double dGlobalMax = 0.0);
192
193 template<typename T>
194 static void histcounts(const Eigen::Matrix<T, Eigen::Dynamic, 1>& matRawData,
195 bool bMakeSymmetrical,
196 int iClassAmount,
197 Eigen::VectorXd& vecResultClassLimits,
198 Eigen::VectorXi& vecResultFrequency,
199 double dGlobalMin = 0.0,
200 double dGlobalMax = 0.0);
201
202 template<typename T>
203 static void histcounts(const Eigen::Matrix<T, 1, Eigen::Dynamic>& matRawData,
204 bool bMakeSymmetrical,
205 int iClassAmount,
206 Eigen::VectorXd& vecResultClassLimits,
207 Eigen::VectorXi& vecResultFrequency,
208 double dGlobalMin = 0.0,
209 double dGlobalMax = 0.0);
210};
211
212//=============================================================================================================
213// INLINE & TEMPLATE DEFINITIONS
214//=============================================================================================================
215
216template<typename T>
217inline double Numerics::log2(const T d)
218{
219 return log(d) / log(2);
220}
221
222//=============================================================================================================
223
224template<typename T>
225void Numerics::histcounts(const Eigen::Matrix<T, Eigen::Dynamic, 1>& matRawData,
226 bool bMakeSymmetrical,
227 int iClassAmount,
228 Eigen::VectorXd& vecResultClassLimits,
229 Eigen::VectorXi& vecResultFrequency,
230 double dGlobalMin,
231 double dGlobalMax)
232{
233 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(matRawData.rows(), 1);
234 matrixName.col(0) = matRawData;
235 Numerics::histcounts(matrixName, bMakeSymmetrical, iClassAmount, vecResultClassLimits, vecResultFrequency, dGlobalMin, dGlobalMax);
236}
237
238//=============================================================================================================
239
240template<typename T>
241void Numerics::histcounts(const Eigen::Matrix<T, 1, Eigen::Dynamic>& matRawData,
242 bool bMakeSymmetrical,
243 int iClassAmount,
244 Eigen::VectorXd& vecResultClassLimits,
245 Eigen::VectorXi& vecResultFrequency,
246 double dGlobalMin,
247 double dGlobalMax)
248{
249 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrixName(1, matRawData.cols());
250 matrixName.row(0) = matRawData;
251 Numerics::histcounts(matrixName, bMakeSymmetrical, iClassAmount, vecResultClassLimits, vecResultFrequency, dGlobalMin, dGlobalMax);
252}
253
254//=============================================================================================================
255
256template<typename T>
257void Numerics::histcounts(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& matRawData,
258 bool bMakeSymmetrical,
259 int iClassAmount,
260 Eigen::VectorXd& vecResultClassLimits,
261 Eigen::VectorXi& vecResultFrequency,
262 double dGlobalMin,
263 double dGlobalMax)
264{
265 if (matRawData.rows() == 0 || matRawData.cols() == 0) {
266 return;
267 }
268
269 vecResultClassLimits.resize(iClassAmount + 1);
270 vecResultFrequency.resize(iClassAmount);
271
272 for (int count = 0; count < iClassAmount; ++count) {
273 vecResultFrequency(count) = 0;
274 }
275
276 double desiredMin,
277 desiredMax;
278 double rawMin(0.0),
279 rawMax(0.0),
280 localMin(0.0),
281 localMax(0.0);
282
283 rawMin = matRawData.minCoeff();
284 rawMax = matRawData.maxCoeff();
285
286 if (bMakeSymmetrical == true) {
287 if (std::fabs(rawMin) > rawMax) {
288 localMax = std::fabs(rawMin);
289 localMin = rawMin;
290 } else if (rawMax > std::fabs(rawMin)) {
291 localMin = -(rawMax);
292 localMax = rawMax;
293 } else {
294 localMin = rawMin;
295 localMax = rawMax;
296 }
297 } else {
298 localMin = rawMin;
299 localMax = rawMax;
300 }
301
302 if (dGlobalMin == 0.0 && dGlobalMax == 0.0) {
303 desiredMin = localMin;
304 desiredMax = localMax;
305 vecResultClassLimits[0] = desiredMin;
306 vecResultClassLimits[iClassAmount] = desiredMax;
307 } else {
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 dynamicUpperClassLimit = (vecResultClassLimits(0) + (kr * (range / iClassAmount)));
319 vecResultClassLimits(kr) = dynamicUpperClassLimit;
320 }
321
322 for (int ir = 0; ir < matRawData.rows(); ++ir) {
323 for (int jr = 0; jr < matRawData.cols(); ++jr) {
324 if (matRawData(ir, jr) != 0.0) {
325 for (int kr = 0; kr < iClassAmount; ++kr) {
326 if (kr == iClassAmount - 1) {
327 if (matRawData(ir, jr) >= vecResultClassLimits(kr) && matRawData(ir, jr) <= vecResultClassLimits(kr + 1)) {
328 vecResultFrequency(kr) = vecResultFrequency(kr) + 1;
329 }
330 } else {
331 if (matRawData(ir, jr) >= vecResultClassLimits(kr) && matRawData(ir, jr) < vecResultClassLimits(kr + 1)) {
332 vecResultFrequency(kr) = vecResultFrequency(kr) + 1;
333 }
334 }
335 }
336 }
337 }
338 }
339}
340
341//=============================================================================================================
342} // NAMESPACE
343
344#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:75
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:257
static int gcd(int iA, int iB)
Definition numerics.cpp:58
static double chi2Isf(double p, int dof)
Definition numerics.cpp:96
static bool issparse(Eigen::VectorXd &v)
Definition numerics.cpp:69
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:217
static int nchoose2(int n)
Definition numerics.cpp:87