-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLowerBound.java
33 lines (30 loc) · 1.05 KB
/
LowerBound.java
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
/*********************
* Author: Maxx Boehme
*********************/
import java.text.DecimalFormat;
public class LowerBound{
public static void main(String[] args) throws Exception {
IO.Compressor compressor = new IO.Compressor(args[0]);
//Create charArray to hold all the characters in the file.
char[] charArray = compressor.getCharacters();
compressor.finalize();
double[] freq = new double[256];
/*Calculating the frequency of each character in the charArray*/
for(int i = 0; i<charArray.length; i++){
freq[charArray[i]]++;
}
//Calculating the probability of each character
for(int i = 0; i<freq.length; i++){
freq[i] = freq[i]/(charArray.length-1);
}
double entropy = 0;
//Calculating the entropy. Not including the end of file character
for(int i = 1; i<freq.length; i++){
if(freq[i]!=0)
entropy += freq[i]*(Math.log(freq[i])/Math.log(2));
}
entropy *= -1;
DecimalFormat df = new DecimalFormat("0.000");
System.out.println("The lower bound is "+df.format(entropy*(charArray.length-1))+"; the entropy is "+df.format(entropy)+".");
}
}