forked from armbues/python-entropy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entropymodule.c
97 lines (78 loc) · 1.71 KB
/
entropymodule.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
static PyObject *shannon_entropy(PyObject *, PyObject *);
PyDoc_STRVAR(module_doc,
"Fast entropy calculation.\n"
"\n"
"This module provides a method implemented in C for calculating the\n"
"shannon entropy of a byte string.");
PyDoc_STRVAR(shannon_entropy_doc,
"shannon_entropy(bytes) -> float\n"
"\n"
"H(S) = - Sum(p_i * log(p_i))\n");
static PyMethodDef entropy_methods[] = {
{"shannon_entropy", shannon_entropy, METH_VARARGS, shannon_entropy_doc},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"entropy",
module_doc,
0,
entropy_methods,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC
PyInit_entropy(void)
{
return (PyModule_Create(&moduledef));
}
#else
PyMODINIT_FUNC
initentropy(void)
{
Py_InitModule3("entropy", entropy_methods, module_doc);
}
#endif
static PyObject *
shannon_entropy(PyObject *self, PyObject *args)
{
Py_buffer *buf = (Py_buffer *) malloc(sizeof(*buf));
const char *data;
double ent = 0, p;
size_t *counts;
#ifdef PYPY_VERSION
int n;
#else
Py_ssize_t n;
#endif
size_t i;
if (!PyArg_ParseTuple(args, "s*", buf))
return (NULL);
data = buf->buf;
n = buf->len;
if (!(counts = calloc(256, sizeof(*counts))))
return (PyErr_NoMemory());
memset(counts, '\0', sizeof(*counts) * 256);
for (i = 0; i < n; i++)
counts[(unsigned char)data[i]] += 1;
PyBuffer_Release(buf);
free(buf);
for (i = 0; i < 256; i++) {
if (!counts[i])
continue;
p = (double)counts[i] / n;
ent -= p * logf(p);
}
free(counts);
ent /= logf(256);
return (Py_BuildValue("d", ent));
}