xmake-comsim/include/utils.h

43 lines
933 B
C
Raw Normal View History

2022-11-07 00:06:46 +08:00
#ifndef __utils__
#define __utils__
#include <cmath>
#include <vector>
template <typename T>
std::vector<T> cutArrs(std::vector<T> &Arrs, int begin, int end) {
std::vector<T> result;
result.assign(Arrs.begin() + begin, Arrs.begin() + end);
return result;
}
template <typename T>
double average(T x, int len) {
double sum = 0;
for (int i = 0; i < len; i++) sum += x[i];
return sum / len;
}
template <typename T>
double variance(T x, int len) {
double sum = 0;
double avg = average(x, len);
for (int i = 0; i < len; i++) sum += pow(x[i] - avg, 2);
return sum / len;
}
template <typename T>
double standardDev(T x, int len) {
double var = variance(x, len);
return sqrt(var);
}
template <typename T>
double maxBias(T x, int len) {
double res = -1;
double avg = average(x, len);
for (int i = 0; i < len; i++) res = std::max(res, abs(avg - x[i]));
return res;
}
#endif