Skip to content

Instantly share code, notes, and snippets.

@agiletalk
Created April 3, 2011 13:58
Show Gist options
  • Save agiletalk/900432 to your computer and use it in GitHub Desktop.
Save agiletalk/900432 to your computer and use it in GitHub Desktop.
정보 엔트로피 구하기 (DIPS Homework #2 2.)
#include <stdio.h>
#include <string.h>
#include <math.h>
#define ASCII 128
// base를 밑으로 하는 x의 로그
double logB(double x, double base)
{
return log(x) / log(base);
}
double entropy(char* string)
{
int i, length = strlen(string), matrix[ASCII] = {0};
double result = 0.0f;
// ASCII 범위 내에서 문자 카운트
for(i = 0; i < length; i++)
{
matrix[string[i]]++;
}
// 문자의 수와 확률, 정보량 출력
printf("symbol\tprob.\tInformation(=surprisal)\n");
for(i = 0; i < ASCII; i++)
{
if(matrix[i] != 0)
{
double t = (double)matrix[i]/length;
printf("%c: %d\t%f\t%f\n", i, matrix[i], t, logB(1/t,2));
result = result + t * logB(1/t,2);
}
}
return result;
}
int main(int argc, char* argv[])
{
double e = 0.0f;
// entropy [string] <return>
if( argc != 2 )
{
printf("usage: %s [string]\n", argv[0]);
return -1;
}
// 엔트로피 계산
e = entropy(argv[1]);
printf("Total entropy = %f (bits)\n", e);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment