Utils { public static Double entropy(double[] p) { double h = 0; for (int i = 0; i < p.length; i++) { h += (p[i] > 0) ? p[i] * Math.log(p[i]) : 0.0; } return -h; }  static String cleanLine(String line); static String getFile(String FileName); static void writeFile(String in, String filename); static Double round(double d, int decimalPlace); static Double entropy(double[] p);  }
@Test public final void testEntropy() { double p1[] = {1,0.1,0.99,0.2,0.4,0.5}; double h1 = 1.275185807; double p2[] = {0.4, 0.5, 0.1}; double h2 = 0.943348392; double p3[] = {-1, -1}; double h3 = 0; double p4[] = {10,10}; double h4 = -46.05170186; double p5[] = {0.4, 0.5}; double h5 = 0.713089883; double p6[] = {0.1,0.2,0.4,0.5,0.1,0.2,0.4,0.5}; double h6 = 2.53047195; assertTrue(isEntropyTheSame(p1,h1)); assertTrue(isEntropyTheSame(p2,h2)); assertTrue(isEntropyTheSame(p3,h3)); assertTrue(isEntropyTheSame(p4,h4)); assertTrue(isEntropyTheSame(p5,h5)); assertTrue(isEntropyTheSame(p6,h6)); }