-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasics.cpp
44 lines (31 loc) · 901 Bytes
/
basics.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
#include <iostream>
#include <math.h>
#include <array>
#include <vector>
using namespace std;
#define NLOG2N(x) ((x) * log2(x))
double nlog2n(double n) {
return n * log2(n);
}
// entropy of a source, or average self-information per sample
// assuming all events independent
double entropy(vector<double> &probabilities) {
double sum = 0;
for (auto p : probabilities) {
sum += NLOG2N(p);
}
return -sum;
}
int main() {
cout << nlog2n(4) << endl;
cout << nlog2n(2) << endl;
cout << nlog2n(1) << endl;
cout << nlog2n(0.5) << endl;
cout << nlog2n(0.25) << endl;
cout << nlog2n(0.125) << endl;
cout << NLOG2N(0.125) << endl;
cout << log2(.1) << endl;
vector<double> myArray {(2.0/16), (2.0/16), (2.0/16), (2.0/16), (2.0/16), (2.0/16), (1.0/16), (1.0/16),(1.0/16),(1.0/16) };
cout << entropy(myArray) << endl;
return 0;
}