forked from alandefreitas/matplotplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram_13.cpp
81 lines (72 loc) · 2.49 KB
/
histogram_13.cpp
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
74
75
76
77
78
79
80
81
#include <cmath>
#include <matplot/matplot.h>
#include <random>
#include <thread>
template <class T> void cout_vector_summary(const std::vector<T> &x);
int main() {
using namespace matplot;
{
std::vector<double> x = randn(100, 0, 1);
cout_vector_summary(x);
auto [N, edges] = histcounts(x);
for (size_t i = 0; i < N.size(); ++i) {
std::cout << "[" << edges[i] << ";" << edges[i + 1] << "]: " << N[i]
<< std::endl;
}
}
{
std::vector<double> x = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
cout_vector_summary(x);
auto [N, edges] = histcounts(x, 6);
for (size_t i = 0; i < N.size(); ++i) {
std::cout << "[" << edges[i] << ";" << edges[i + 1] << "]: " << N[i]
<< std::endl;
}
}
{
std::vector<double> x = randn(100, 0, 1);
cout_vector_summary(x);
std::vector<double> edges = {-5, -4, -2, -1, -0.5, 0, 0.5, 1, 2, 4, 5};
auto N = histcounts(x, edges);
for (size_t i = 0; i < N.size(); ++i) {
std::cout << "[" << edges[i] << ";" << edges[i + 1] << "]: " << N[i]
<< std::endl;
}
}
{
std::vector<double> x = {2, 3, 5, 7, 11, 13, 17, 19, 23,
29, 31, 37, 41, 43, 47, 53, 59, 61,
67, 71, 73, 79, 83, 89, 97};
cout_vector_summary(x);
auto [N, edges] = histcounts(x, histogram::normalization::probability);
for (size_t i = 0; i < N.size(); ++i) {
std::cout << "[" << edges[i] << ";" << edges[i + 1] << "]: " << N[i]
<< std::endl;
}
}
{
std::vector<int> xi = randi(100, -5, 5);
cout_vector_summary(xi);
std::vector<double> x(xi.size());
std::copy(xi.begin(), xi.end(), x.begin());
auto [N, edges] = histcounts(x, histogram::binning_algorithm::integers);
for (size_t i = 0; i < N.size(); ++i) {
std::cout << "[" << edges[i] << ";" << edges[i + 1] << "]: " << N[i]
<< std::endl;
}
}
return 0;
}
template <class T> void cout_vector_summary(const std::vector<T> &x) {
std::cout << "x: [";
for (size_t i = 0; i < std::min(x.size(), size_t(10)); ++i) {
if (i != 0) {
std::cout << " ";
}
std::cout << x[i];
}
if (x.size() > 10) {
std::cout << "...";
}
std::cout << "]" << std::endl;
}