-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentropy.c
63 lines (42 loc) · 816 Bytes
/
entropy.c
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
#include "entropy.h"
#include "stdlib.h"
struct _entropy_transcation{
int n;
int m;
int *c;
};
int entropy_transcation_init(entropy_t *e, int _n){
*e = malloc(sizeof(entropy_t));
(*e)->n = _n;
(*e)->m = 0;
(*e)->c = malloc(sizeof(int) * (*e)->n);
int i;
for (i = 0; i < (*e)->n; i++){
(*e)->c[i] = 0;
}
return 1;
}
int entropy_transcation_sample(entropy_t e, int state){
if (state >= 0 && state < e->n){
e->c[state] += 1;
e->m += 1;
return 1;
}
else
return 0;
}
double entropy_transcation_get(entropy_t e){
int i;
double s = 0;
#include "math.h"
for (i = 0; i < e->n; i++){
if (e->c[i] != 0)
s += e->c[i] * log(e->c[i]) / log(2);
}
return log(e->m) / log(2) - s / e->m;
}
int entropy_transcation_abort(entropy_t *e){
free((*e)->c);
free(*e);
return 1;
}