Commit 04e29167 authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

refactored utilities with some templates

parent 2df5e06a
#include "Utilities.hpp"
#define REAL 0
#define IMAG 1
template<>
std::string vec2str<int>(std::vector<int> vec) {
std::string str = "[";
......@@ -31,37 +34,6 @@ std::string vec2str<std::string>(std::vector<std::string> vec) {
return (str + "]");
}
double calc_mean(const std::vector<int> &vec) {
double mean_ = 0.0;
for (auto const &value: vec)
mean_ += value;
return mean_ / vec.size();
}
double calc_var(const std::vector<int> &vec) {
double sqrs_ = 0.0;
for (auto const &value: vec)
sqrs_ += pow(value, 2);
return sqrs_ / vec.size() - pow(calc_mean(vec), 2);
}
double calc_mean(const std::vector<double> &vec) {
double mean_ = 0.0;
for (auto const &value: vec)
mean_ += value;
return mean_ / vec.size();
}
double calc_var(const std::vector<double> &vec) {
double sqrs = 0.0;
for (auto const &value: vec)
sqrs += pow(value, 2);
return sqrs / vec.size() - pow(calc_mean(vec), 2);
}
#define REAL 0
#define IMAG 1
void fft(int N, fftw_complex *in, fftw_complex *out) {
fftw_plan plan = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(plan);
......
......@@ -11,21 +11,39 @@
template<typename T>
std::string vec2str(std::vector<T> vec);
double calc_mean(const std::vector<double> &vec);
double calc_mean(const std::vector<std::vector<double>> &vec);
double calc_mean(const std::vector<int> &vec);
double calc_mean(const std::vector<std::vector<int>> &vec);
double calc_var(const std::vector<double> &vec);
template<typename T>
T Mean(const std::vector<T> &vec) {
T mean = 0.0;
for (auto const &value: vec)
mean += value;
return mean / T(vec.size());
}
double calc_var(const std::vector<std::vector<double>> &vec);
template<typename T>
T Mean(const std::vector<std::vector<double>> &vec) {
T mean = 0.0;
for (auto const &value_vec: vec)
for (auto const &value: value_vec)
mean += value;
return mean / vec.size();
}
double calc_var(const std::vector<int> &vec);
template<typename T>
T Variance(const std::vector<T> &vec) {
T sqrs = 0.0;
for (auto const &value: vec)
sqrs += std::pow(value, 2);
return sqrs / T(vec.size()) - std::pow(Mean(vec), 2);
}
double calc_var(const std::vector<std::vector<int>> &vec);
template<typename T>
double Variance(const std::vector<std::vector<T>> &vec) {
double sqrs = 0.0;
for (auto const &value_vec: vec)
for (auto const &value: value_vec)
sqrs += std::pow(value, 2);
return sqrs / vec.size() - std::pow(Mean(vec), 2);
}
void fft(int N, fftw_complex *in, fftw_complex *out);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment