AverageMethod.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "AverageMethod.h"
  2. #include <QDebug>
  3. #include <QtMath>
  4. AverageMethod::AverageMethod(const QVector<AverageMethod::EvaluateIndex> &rMat, const QVector<qreal> &wMat,
  5. const QVector<qreal> &maxMat, const QVector<qreal> &minMat, const QVector<qreal> &sumMat,
  6. int type)
  7. : m_rMat(rMat), m_wMat(wMat), m_maxMat(maxMat), m_minMat(minMat), m_sumMat(sumMat)
  8. {
  9. Q_ASSERT(rMat.count() != 0);
  10. Q_ASSERT(rMat.at(0).count() != 0);
  11. if (type == MethodType::Max) {
  12. for (auto &r : m_rMat) {
  13. for (int i = 0; i < r.count(); ++i) {
  14. r[i] = r[i] / m_maxMat.at(i);
  15. }
  16. }
  17. } else if (type == MethodType::Min) {
  18. for (auto &r : m_rMat) {
  19. for (int i = 0; i < r.count(); ++i) {
  20. r[i] = r[i] / m_minMat.at(i);
  21. }
  22. }
  23. } else {
  24. // Z-Score 需要求方差和均值
  25. QVector<qreal> aMat;
  26. QVector<qreal> sMat;
  27. int count = m_rMat.count();
  28. for (auto s : m_sumMat) {
  29. aMat.append(s / count);
  30. }
  31. //方差
  32. int rCount = m_sumMat.count();
  33. for (int i = 0; i < rCount; ++i) {
  34. qreal s2 = 0;
  35. for (auto r : m_rMat) {
  36. s2 += pow(r.at(i) - aMat.at(i), 2);
  37. }
  38. sMat.append(sqrt(s2 / count));
  39. }
  40. for (auto &r : m_rMat) {
  41. for (int i = 0; i < r.count(); ++i) {
  42. r[i] = (r[i] - aMat.at(i)) / sMat.at(i);
  43. }
  44. }
  45. }
  46. }
  47. AverageMethod::Relations AverageMethod::getRelations() const
  48. {
  49. Relations uMat;
  50. for (auto r : m_rMat) {
  51. qreal ui = 0;
  52. for (int i = 0; i < r.count(); ++i) {
  53. ui += r.at(i) * m_wMat.at(i);
  54. }
  55. uMat << ui;
  56. }
  57. return uMat;
  58. }