summaryrefslogtreecommitdiff
path: root/tools/process_perc.cpp
blob: f1274b7cf3ee31ea90424ac19c95d685e9b41380 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#include <fstream>
#include <vector>
#include <cinttypes>
#include <sstream>
#include <string>
#include <cmath>

int main(int argc, char* argv[]) {
  std::vector<std::string> all_args;

  if (argc > 1) {
    all_args.assign(argv + 1, argv + argc);
  }

  for (std::string filename : all_args) {
    std::ifstream in_file;
    in_file.open(filename);

    std::vector<std::vector<uint64_t>> dat;

    unsigned line_num = 0;
    std::string line;
    while (std::getline(in_file, line)) {
      dat.resize(line_num + 1);
      std::istringstream iss(line);
      uint64_t a;
      while (iss >> a) {
        dat[line_num].push_back(a);
      }
      line_num++;
    }

    in_file.close();

    unsigned n_moms = 5;

    std::vector<std::vector<double>> output(n_moms);

    for (unsigned i = 0; i < n_moms; i++) {
      output[i].resize(dat.size());
    }

    for (unsigned i = 1; i <= n_moms; i++) {
      for (unsigned j = 0; j < dat.size(); j++) {
        double mom = 0;
        uint64_t total = 0;
        for (unsigned k = 1; k <= dat[j].size(); k++) {
          mom += pow(k, i) * dat[j][k - 1];
          total += dat[j][k - 1];
        }

        output[i - 1][j] = mom / total;
      }
    }

    std::ofstream out_file;
    out_file.open(filename + ".processed");

    for (unsigned i = 0; i < n_moms; i++)  {
      for (unsigned j = 0; j < output[i].size(); j++) {
        out_file << output[i][j] << " ";
      }
      out_file << "\n";
    }

    out_file.close();

  }

  return 0;
}