MathUtil.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef MATHUTIL_H
  2. #define MATHUTIL_H
  3. #include <QVector>
  4. #include <QtMath>
  5. #include <limits>
  6. namespace Helper {
  7. static double minvec(const QVector<double> &vec)
  8. {
  9. double val = std::numeric_limits<double>::min();
  10. for (int i = 0; i < vec.count(); ++i) {
  11. if (i == 0) {
  12. val = vec.at(i);
  13. } else if (val > vec.at(i)) {
  14. val = vec.at(i);
  15. }
  16. }
  17. return val;
  18. }
  19. static double maxvec(const QVector<double> &vec)
  20. {
  21. double val = std::numeric_limits<double>::max();
  22. for (int i = 0; i < vec.count(); ++i) {
  23. if (i == 0) {
  24. val = vec.at(i);
  25. } else if (val < vec.at(i)) {
  26. val = vec.at(i);
  27. }
  28. }
  29. return val;
  30. }
  31. /// 均值
  32. static double mean(const QVector<double> &vec)
  33. {
  34. if (vec.count() == 0) {
  35. return 0;
  36. }
  37. double sum = 0;
  38. for (auto v : vec) {
  39. sum += v;
  40. }
  41. return sum / vec.count();
  42. }
  43. /// 标准差
  44. static double standardDeviation(const QVector<double> &vec)
  45. {
  46. return qSqrt(standardDeviation(vec));
  47. }
  48. static std::pair<double, double> meanAndStandardDeviation(const QVector<double> &vec)
  49. {
  50. if (vec.count() == 0) {
  51. return std::make_pair(0, 0);
  52. }
  53. double meanVal = mean(vec);
  54. double sum = 0;
  55. for (auto v : vec) {
  56. sum += qPow(v - meanVal, 2.0);
  57. }
  58. return std::make_pair(meanVal, sum / vec.count());
  59. }
  60. /// 方差
  61. static double variance(const QVector<double> &vec)
  62. {
  63. if (vec.count() == 0) {
  64. return 0;
  65. }
  66. double meanVal = mean(vec);
  67. double sum = 0;
  68. for (auto v : vec) {
  69. sum += qPow(v - meanVal, 2.0);
  70. }
  71. return sum / vec.count();
  72. }
  73. static void Standardization(QVector<QVector<double>> &v)
  74. {
  75. double Avg, STD;
  76. for (int i = 0; i < v.size(); i++) {
  77. Avg = mean(v[i]);
  78. STD = standardDeviation(v[i]);
  79. for (int j = 0; j < v[i].size(); j++) {
  80. v[i][j] = (v[i][j] - Avg) / STD;
  81. }
  82. }
  83. }
  84. static double cov(QVector<double> vA, QVector<double> vB)
  85. {
  86. double total;
  87. for (int i = 0; i < vA.size(); i++) {
  88. total += vA[i] * vB[i];
  89. }
  90. return (total / vA.size());
  91. }
  92. static QVector<QVector<double>> Covarience(QVector<QVector<double>> features)
  93. {
  94. QVector<QVector<double>> covMatrix;
  95. QVector<double> temp;
  96. for (int i = 0; i < features.size(); i++) {
  97. for (int j = 0; j < features.size(); j++) {
  98. temp.push_back(cov(features[i], features[j]));
  99. }
  100. covMatrix.push_back(temp);
  101. temp.clear();
  102. }
  103. return covMatrix;
  104. }
  105. } // namespace Helper
  106. #endif // MATHUTIL_H