Example #1
0
  /**
   * What is the min/max/mean/stdev in the collection of aggregates (assuming its over numbers).
   *
   * <p>By default NaNs, Nulls and infinity are fully skipped. However, if the relevant parameters
   * set to false they will be included in the "count" basis and thus influence the mean.
   *
   * <p>*
   */
  public static <N extends Number> Stats<N> stats(
      Aggregates<? extends N> aggregates,
      boolean ignoreNulls,
      boolean ignoreNaNs,
      boolean ignoreInfinity) {
    // For a single-test std. dev is based on:
    // http://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods
    long count = 0;
    long nullCount = 0;
    long nanCount = 0;
    long infCount = 0;
    N min = null;
    N max = null;
    double sum = 0;

    for (N n : aggregates) {
      if (n == null) {
        nullCount++;
        continue;
      }

      double v = n.doubleValue();
      if (Double.isNaN(v)) {
        nanCount++;
        continue;
      }
      if (Double.isInfinite(v)) {
        infCount++;
        continue;
      }
      if (min == null || min.doubleValue() > v) {
        min = n;
      }
      if (max == null || max.doubleValue() < v) {
        max = n;
      }
      sum += v;
      count++;
    }

    final long fullCount =
        count
            + (ignoreNulls ? 0 : nullCount)
            + (ignoreNaNs ? 0 : nanCount)
            + (ignoreInfinity ? 0 : infCount);

    final double mean = sum / fullCount;
    double acc = 0;

    for (Number n : aggregates) {
      if (n == null || Double.isNaN(n.doubleValue())) {
        continue;
      }
      acc = Math.pow((n.doubleValue() - mean), 2);
    }
    double stdev = Math.sqrt(acc / fullCount);

    return new Stats<>(min, max, mean, stdev, nullCount, nanCount);
  }
    /**
     * @param bottom Lowest value to be included
     * @param top Highest value to be included
     * @param spacing How far apart should the contours be placed?
     * @return List of the contour step values
     */
    public static <N extends Number> List<N> steps(N bottom, N top, double spacing) {
      int stepCount = (int) Math.ceil((top.doubleValue() - bottom.doubleValue()) / spacing);

      ArrayList<N> steps = new ArrayList<>(stepCount);

      for (int i = 0; i < stepCount; i++) {
        steps.add(LocalUtils.addTo(bottom, (i * spacing)));
      }
      return steps;
    }
 private static <N extends Number> N scale(Class<N> type, double value, N min, N max) {
   if (type.isAssignableFrom(Integer.class))
     return type.cast(Integer.valueOf((int) (value * max.intValue() + min.intValue())));
   if (type.isAssignableFrom(Double.class))
     return type.cast((value * max.doubleValue() + min.doubleValue()));
   if (type.isAssignableFrom(Long.class))
     return type.cast(Long.valueOf((long) (value * max.longValue() + min.longValue())));
   if (type.isAssignableFrom(Short.class))
     return type.cast(Short.valueOf((short) (value * max.shortValue() + min.shortValue())));
   if (type.isAssignableFrom(Byte.class))
     return type.cast(Byte.valueOf((byte) (value * max.byteValue() + min.byteValue())));
   if (type.isAssignableFrom(Float.class))
     return type.cast(Float.valueOf((float) (value * max.floatValue() + min.floatValue())));
   return null;
 }
Example #4
0
 /**
  * Returns the sum of all elements in this memory.
  *
  * @return the sum of all elements in this memory.
  */
 public double getSum() {
   double sum = 0d;
   for (N value : list) {
     sum += value.doubleValue();
   }
   return sum;
 }
    @Override
    public Float64 apply(final N value) {
      final double x = value.doubleValue();

      Float64 result = Float64.ZERO;
      if (x >= _min && x <= _max) {
        result = Float64.valueOf(_k * x + _d);
      }

      return result;
    }
    @Override
    public Float64 apply(final N value) {
      final double x = value.doubleValue();

      Float64 result = null;
      if (x < _x1) {
        result = Float64.ZERO;
      } else if (x > _x2) {
        result = Float64.ONE;
      } else {
        //				result = Float64.valueOf(
        //						-((x*x - 2*x*_x2)*_y1 - (x*x - 2*x*_x1)*_y2)/
        //						(2*(_x2 - _x1))
        //					);
        result = Float64.valueOf(_k * x * x / 2.0 + _d * x);
      }

      return result;
    }
Example #7
0
 public <N extends Number> void setValue(N x) {
   value = x.doubleValue();
 }