Exemplo n.º 1
0
  /**
   * Returns c*log2(c) for a given integer value c.
   *
   * @param c an integer value
   * @returns c*log2(c) (but is careful to return 0 if c is 0)
   */
  public static double xlogx(int c) {

    if (c == 0) {
      return 0.0;
    }
    return c * M5StaticUtils.log2((double) c);
  }
Exemplo n.º 2
0
  /**
   * Main method for testing this class.
   *
   * @param ops some dummy options
   */
  public static void main(String[] ops) {

    double[] doubles = {4.5, 6.7, Double.NaN, 3.4, 4.8, 1.2, 3.4};
    int[] ints = {12, 6, 2, 18, 16, 6, 7, 5};

    try {

      // Option handling
      System.out.println("First option split up:");
      if (ops.length > 0) {
        String[] firstOptionSplitUp = M5StaticUtils.splitOptions(ops[0]);
        for (int i = 0; i < firstOptionSplitUp.length; i++) {
          System.out.println(firstOptionSplitUp[i]);
        }
      }
      System.out.println("Partitioned options: ");
      String[] partitionedOptions = M5StaticUtils.partitionOptions(ops);
      for (int i = 0; i < partitionedOptions.length; i++) {
        System.out.println(partitionedOptions[i]);
      }
      System.out.println("Get flag -f: " + M5StaticUtils.getFlag('f', ops));
      System.out.println("Get option -o: " + M5StaticUtils.getOption('o', ops));
      System.out.println("Checking for remaining options... ");
      M5StaticUtils.checkForRemainingOptions(ops);

      // Statistics
      System.out.println("Original array (doubles): ");
      for (int i = 0; i < doubles.length; i++) {
        System.out.print(doubles[i] + " ");
      }
      System.out.println();
      System.out.println("Original array (ints): ");
      for (int i = 0; i < ints.length; i++) {
        System.out.print(ints[i] + " ");
      }
      System.out.println();
      System.out.println(
          "Correlation: " + M5StaticUtils.correlation(doubles, doubles, doubles.length));
      System.out.println("Mean: " + M5StaticUtils.mean(doubles));
      System.out.println("Variance: " + M5StaticUtils.variance(doubles));
      System.out.println("Sum (doubles): " + M5StaticUtils.sum(doubles));
      System.out.println("Sum (ints): " + M5StaticUtils.sum(ints));
      System.out.println("Max index (doubles): " + M5StaticUtils.maxIndex(doubles));
      System.out.println("Max index (ints): " + M5StaticUtils.maxIndex(ints));
      System.out.println("Min index (doubles): " + M5StaticUtils.minIndex(doubles));
      System.out.println("Min index (ints): " + M5StaticUtils.minIndex(ints));

      // Sorting and normalizing
      System.out.println("Sorted array (doubles): ");
      int[] sorted = M5StaticUtils.sort(doubles);
      for (int i = 0; i < doubles.length; i++) {
        System.out.print(doubles[sorted[i]] + " ");
      }
      System.out.println();
      System.out.println("Normalized array (doubles): ");
      M5StaticUtils.normalize(doubles);
      for (int i = 0; i < doubles.length; i++) {
        System.out.print(doubles[i] + " ");
      }
      System.out.println();
      System.out.println("Normalized again (doubles): ");
      M5StaticUtils.normalize(doubles, M5StaticUtils.sum(doubles));
      for (int i = 0; i < doubles.length; i++) {
        System.out.print(doubles[i] + " ");
      }
      System.out.println();

      // Pretty-printing
      System.out.println("-4.58: " + M5StaticUtils.doubleToString(-4.57826535, 2));
      System.out.println("-6.78: " + M5StaticUtils.doubleToString(-6.78214234, 6, 2));

      // Comparisons
      System.out.println("5.70001 == 5.7 ? " + M5StaticUtils.eq(5.70001, 5.7));
      System.out.println("5.70001 > 5.7 ? " + M5StaticUtils.gr(5.70001, 5.7));
      System.out.println("5.70001 >= 5.7 ? " + M5StaticUtils.grOrEq(5.70001, 5.7));
      System.out.println("5.7 < 5.70001 ? " + M5StaticUtils.sm(5.7, 5.70001));
      System.out.println("5.7 <= 5.70001 ? " + M5StaticUtils.smOrEq(5.7, 5.70001));

      // Math
      System.out.println("Info (ints): " + M5StaticUtils.info(ints));
      System.out.println("log2(4.6): " + M5StaticUtils.log2(4.6));
      System.out.println("5 * log(5): " + M5StaticUtils.xlogx(5));
      System.out.println("5.5 rounded: " + M5StaticUtils.round(5.5));
      System.out.println(
          "5.55555 rounded to 2 decimal places: " + M5StaticUtils.roundDouble(5.55555, 2));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }