예제 #1
0
 public static double getBitwiseEntropyOfCharacters(String text) {
   if (text == null || text.equals("")) {
     return Double.NaN;
   }
   int[] current = null;
   int sum = 0;
   for (int i = 0; i < text.length(); i++) {
     current = ListArrayUtil.toBinaryArray(text.charAt(i), 8);
     sum += MathUtil.sum(current);
     // System.out.println(ListArrayUtil.arrayToString(current) + " " + sum);
   }
   double[] probs = new double[2];
   probs[1] = (double) sum / (double) (text.length() * 8.0);
   probs[0] = 1.0 - probs[1];
   // System.out.println(ListArrayUtil.arrayToString(probs));
   double H = 0.0;
   for (int i = 0; i < probs.length; i++) {
     H -= probs[i] * MathUtil.logBase2(probs[i]);
   }
   return H;
 }
예제 #2
0
 @Test
 public void testSum_List_Double() {
   logger.info("\ntesting sum(List<Double> list)");
   List<Double> l = new ArrayList<Double>();
   assertEquals(0.0, MathUtil.sum(null), 0.0);
   assertEquals(0.0, MathUtil.sum(l), 0.0);
   l.add(27.0);
   assertEquals(27.0, MathUtil.sum(l), 0.0);
   l.add(-5.0);
   assertEquals(22.0, MathUtil.sum(l), 0.0);
   l.remove(27.0);
   assertEquals(-5.0, MathUtil.sum(l), 0.0);
   l.add(1.0);
   l.add(2.0);
   l.add(48.0);
   assertEquals(46.0, MathUtil.sum(l), 0.0);
 }
예제 #3
0
  @Test
  public void testSum_int_array() {
    logger.info("\ntesting sum(int[] input, int...endPoints)");
    int[] input = null;
    assertEquals(0, MathUtil.sum(input));
    assertEquals(0, MathUtil.sum(input, 0));
    assertEquals(0, MathUtil.sum(input, 0, 57, 3));

    input = new int[0];
    assertEquals(0, MathUtil.sum(input));
    assertEquals(0, MathUtil.sum(input, 0));
    assertEquals(0, MathUtil.sum(input, 0, 57, 3));

    input = new int[] {1, 2, 3, 4};
    assertEquals(10, MathUtil.sum(input), 0.0);
    assertEquals(3, MathUtil.sum(input, 0, 1));
    assertEquals(3, MathUtil.sum(input, 1, 0));
    assertEquals(6, MathUtil.sum(input, 0, 2));
    assertEquals(10, MathUtil.sum(input, 0, 3));
    assertEquals(10, MathUtil.sum(input, 0, 4));

    assertEquals(2, MathUtil.sum(input, 1, 1));
    assertEquals(5, MathUtil.sum(input, 1, 2));
    assertEquals(9, MathUtil.sum(input, 1, 3));
    assertEquals(9, MathUtil.sum(input, 3, 1));

    // with indeces out of bounds
    assertEquals(9, MathUtil.sum(input, 4, 1));
    assertEquals(9, MathUtil.sum(input, 1, 4));
    assertEquals(10, MathUtil.sum(input, -8, 45));
    assertEquals(10, MathUtil.sum(input, 45, -8));

    // binary
    input = new int[] {0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0};
    assertEquals(5, MathUtil.sum(input));
  }
예제 #4
0
  @Test
  public void testSum_double_array() {
    logger.info("\ntesting sum(double[] input, int...endPoints)");
    double[] input = null;
    assertEquals(0.0, MathUtil.sum(input), 0.0);
    assertEquals(0.0, MathUtil.sum(input, 0), 0.0);
    assertEquals(0.0, MathUtil.sum(input, 0, 57, 3), 0.0);

    input = new double[] {1.0, 2.0, 3.0, 4.0};
    assertEquals(10.0, MathUtil.sum(input), 0.0);
    assertEquals(3.0, MathUtil.sum(input, 0, 1), 0.0);
    assertEquals(6.0, MathUtil.sum(input, 0, 2), 0.0);
    assertEquals(10.0, MathUtil.sum(input, 0, 3), 0.0);
    assertEquals(10.0, MathUtil.sum(input, 0, 4), 0.0);

    assertEquals(2.0, MathUtil.sum(input, 1, 1), 0.0);
    assertEquals(5.0, MathUtil.sum(input, 1, 2), 0.0);
    assertEquals(9.0, MathUtil.sum(input, 1, 3), 0.0);

    // with indeces out of bounds
    assertEquals(9.0, MathUtil.sum(input, 1, 4), 0.0);
    assertEquals(9.0, MathUtil.sum(input, 4, 1), 0.0);

    input =
        new double[] {
          1.3310000000000004, 1.3310000000000004, 1.3310000000000004, 1.3310000000000004
        };
    double[] cumBins = MathUtil.cumProd(input, true);
    logger.debug(ListArrayUtil.arrayToString(cumBins));
    int regularContribution = 1;
    logger.debug(cumBins[0] + regularContribution * MathUtil.sum(cumBins, 1, cumBins.length - 1));
  }