Files
pearson/Pearson.cpp
2022-01-07 15:42:08 +08:00

40 lines
1.3 KiB
C++

//
// Created by fly on 2021/11/25.
//
#include "Pearson.h"
#include <numeric>
#include <cmath>
Pearson::Pearson(uint64_t index, const std::vector<double> &primary, const std::vector<double> &other, QObject *parent)
:
m_index(index),
m_primary(primary),
m_other(other),
QObject(parent) {
}
void Pearson::run() {
auto n = m_primary.size() * 1.0;
double pearson = n * std::inner_product(m_primary.begin(), m_primary.end(), m_other.begin(), 0.0) -
std::accumulate(m_primary.begin(), m_primary.end(), 0.0) *
std::accumulate(m_other.begin(), m_other.end(), 0.0);
double temp1 = n * std::inner_product(m_primary.begin(), m_primary.end(), m_primary.begin(), 0.0) -
std::pow(std::accumulate(m_primary.begin(), m_primary.end(), 0.0), 2.0);
double temp2 = n * std::inner_product(m_other.begin(), m_other.end(), m_other.begin(), 0.0) -
std::pow(std::accumulate(m_other.begin(), m_other.end(), 0.0), 2.0);
temp1 = std::sqrt(temp1);
temp2 = std::sqrt(temp2);
auto temp = (temp1 * temp2);
if (temp != 0) {
pearson = pearson / temp;
if (!std::isnan(pearson)) {
emit result(m_index, pearson);
}
}
emit finished();
}