From cd3d143d841822acfe0c86e02731a40c437c9a9e Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Mon, 8 Jan 2018 23:39:44 +0000 Subject: [PATCH] Add entropy tool --- entropy.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 entropy.c diff --git a/entropy.c b/entropy.c new file mode 100644 index 0000000..7b0244e --- /dev/null +++ b/entropy.c @@ -0,0 +1,26 @@ +#include +#include + +int +main(int argc, char *argv[]) +{ + size_t len = 0; + size_t hist[256] = {0}; + int c; + + while ((c = getchar()) != EOF) { + hist[c] += 1; + len += 1; + } + + float H = 0; + for (int i = 0; i < 256; i += 1) { + if (hist[i]) { + float Hi = (float)hist[i]/len; + H -= Hi * log2(Hi); + } + } + + printf("%f\n", H); +} +