Class MathUtil

java.lang.Object
org.cloudsimplus.util.MathUtil

public final class MathUtil extends Object
An utility class containing multiple convenient math functions.
Since:
CloudSim Toolkit 3.0
Author:
Anton Beloglazov
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static double[]
    abs(double... data)
    Gets the absolute values from an array of values
    static int
    countNonZeroBeginning(double... data)
    Counts the number of values different of zero at the beginning of an array.
    static int
    doubleToInt(double value)
    Converts a double value to an int, using an appropriate rounding function.
    static org.apache.commons.math3.stat.descriptive.DescriptiveStatistics
    getStatistics(double... list)
    Gets an object to compute descriptive statistics for an array of numbers.
    static org.apache.commons.math3.stat.descriptive.DescriptiveStatistics
    Gets an object to compute descriptive statistics for a list of numbers.
    static double
    iqr(double... data)
    Gets the [Inter-quartile Range (IQR)](https://en.wikipedia.org/wiki/Interquartile_range from an array of numbers.
    static double
    mad(double... data)
    Gets the Median Absolute Deviation (MAD) from an array of numbers.
    static double
    mean(List<Double> list)
    Gets the average from a list of numbers.
    static double
    median(double... list)
    Gets the median from an array of numbers.
    static double
    Gets the median from a list of numbers.
    static double
    nonNegative(double value, String fieldName)
    Checks if the given double value is not negative.
    static int
    nonNegative(int value, String fieldName)
    Checks if the given int value is not negative.
    static long
    nonNegative(long value, String fieldName)
    Checks if the given long value is not negative.
    static int
    parseInt(String value, int defaultValue)
    Try to convert a String to an int value.
    static double
    percentage(double value, String fieldName)
    Checks if the given number is a percentage between [0 and 1].
    static double
    percentValue(double partial, double total)
    Computes the percentage of a current value related to a total value.
    static double
    positive(double first, double second)
    Returns the first positive double value.
    static long
    positive(long first, long second)
    Returns the first positive long value.
    static boolean
    same(double first, double second)
    Checks if two double numbers are equals, considering a precision error or 0.01.
    static boolean
    same(double first, double second, double precisionError)
    Checks if two double numbers are equals, considering a given precision error.
    static double
    stDev(List<Double> list)
    Gets the Standard Deviation from a list of numbers.
    static double
    sum(List<? extends Number> list)
    Sums a list of numbers.
    static double
    Gets the Variance from a list of numbers.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • sum

      public static double sum(List<? extends Number> list)
      Sums a list of numbers.
      Parameters:
      list - the list of numbers
      Returns:
      the double
    • median

      public static double median(Collection<Double> list)
      Gets the median from a list of numbers.
      Parameters:
      list - the list of numbers
      Returns:
      the median
    • median

      public static double median(double... list)
      Gets the median from an array of numbers.
      Parameters:
      list - the array of numbers
      Returns:
      the median
    • getStatistics

      public static org.apache.commons.math3.stat.descriptive.DescriptiveStatistics getStatistics(Collection<Double> list)
      Gets an object to compute descriptive statistics for a list of numbers.
      Parameters:
      list - the list of numbers. Must not be null.
      Returns:
      descriptive statistics for the list of numbers.
    • getStatistics

      public static org.apache.commons.math3.stat.descriptive.DescriptiveStatistics getStatistics(double... list)
      Gets an object to compute descriptive statistics for an array of numbers.
      Parameters:
      list - the array of numbers. Must not be null.
      Returns:
      descriptive statistics for the array of numbers.
    • mean

      public static double mean(List<Double> list)
      Gets the average from a list of numbers. If the list is empty or contains just zeros, returns 0.
      Parameters:
      list - the list of numbers
      Returns:
      the average
    • variance

      public static double variance(List<Double> list)
      Gets the Variance from a list of numbers.
      Parameters:
      list - the list of numbers
      Returns:
      the variance
    • stDev

      public static double stDev(List<Double> list)
      Gets the Standard Deviation from a list of numbers.
      Parameters:
      list - the list of numbers
      Returns:
      the standard deviation
    • mad

      public static double mad(double... data)
      Gets the Median Absolute Deviation (MAD) from an array of numbers.
      Parameters:
      data - the array of numbers
      Returns:
      the mad
    • iqr

      public static double iqr(double... data)
      Gets the [Inter-quartile Range (IQR)](https://en.wikipedia.org/wiki/Interquartile_range from an array of numbers.
      Parameters:
      data - the array of numbers
      Returns:
      the IQR
    • countNonZeroBeginning

      public static int countNonZeroBeginning(double... data)
      Counts the number of values different of zero at the beginning of an array.
      Parameters:
      data - the array of numbers
      Returns:
      the number of values different of zero at the beginning of the array
    • abs

      public static double[] abs(double... data)
      Gets the absolute values from an array of values
      Parameters:
      data - the array of values
      Returns:
      a new array with the absolute value of each element in the given array.
    • doubleToInt

      public static int doubleToInt(double value)
      Converts a double value to an int, using an appropriate rounding function. If the double is negative, it applies Math.floor(double) to round the number down. If it' a positive value, it applies Math.ceil(double) to round the number up. This way, a negative double will be converted to a negative int, and a positive double will be converted to a positive int.

      It's different from using: Math.round(double) which always rounds to the next positive integer; Math.floor(double) which always rounds down; or Math.ceil(double) which always rounds up. It applies the floor for negative values and ceil for positive ones.

      This method is useful to be used by Comparators which rely on a double attribute to compare a list of objects. Since the Comparator.compare(Object, Object) method must return an int, the method being implemented here converts a double to an int value which can be used by a Comparator.

      Parameters:
      value - the double value to convert
      Returns:
      zero if the double value is zero, a negative int if the double is negative, or a positive int if the double is positive.
    • parseInt

      public static int parseInt(String value, int defaultValue)
      Try to convert a String to an int value. If the conversion is not possible, returns a default value.
      Parameters:
      value - the value to try converting
      defaultValue - the default value to return in case of error
      Returns:
      the converted value or the default one in case of error
    • same

      public static boolean same(double first, double second)
      Checks if two double numbers are equals, considering a precision error or 0.01. That is, if the different between the two numbers are lower or equal to 0.01, they are considered equal.
      Parameters:
      first - the first number to check
      second - the second number to check
      Returns:
      true if the numbers are equal considering the precision error, false otherwise
    • same

      public static boolean same(double first, double second, double precisionError)
      Checks if two double numbers are equals, considering a given precision error. That is, if the different between the two numbers are lower or equal to the precision error, they are considered equal.
      Parameters:
      first - the first number to check
      second - the second number to check
      precisionError - the precision error used to compare the numbers
      Returns:
      true if the numbers are equal considering the precision error, false otherwise
    • percentValue

      public static double percentValue(double partial, double total)
      Computes the percentage of a current value related to a total value.
      Parameters:
      partial - the partial value to compute the percentage
      total - the total value that represents 100%
      Returns:
      the percentage of the current value in scale from 0 to 100%
    • percentage

      public static double percentage(double value, String fieldName)
      Checks if the given number is a percentage between [0 and 1].
      Parameters:
      value - the value to check
      fieldName - the name of the field to validate (used in a possible validation error message)
      Returns:
      the given value
      Throws:
      IllegalArgumentException - if the value is not between [0 and 1]
    • positive

      public static long positive(long first, long second)
      Returns the first positive long value. given.
      Parameters:
      first - the first value to check
      second - the first value to check
      Returns:
      the first positive long value
    • positive

      public static double positive(double first, double second)
      Returns the first positive double value. given.
      Parameters:
      first - the first value to check
      second - the first value to check
      Returns:
      the first positive double value
    • nonNegative

      public static int nonNegative(int value, String fieldName)
      Checks if the given int value is not negative.
      Parameters:
      value - the value to check
      fieldName - the name of the field to validate (used in a possible validation error message)
      Returns:
      the given value
      Throws:
      IllegalArgumentException - if the value is negative
    • nonNegative

      public static long nonNegative(long value, String fieldName)
      Checks if the given long value is not negative.
      Parameters:
      value - the value to check
      fieldName - the name of the field to validate (used in a possible validation error message)
      Returns:
      the given value
      Throws:
      IllegalArgumentException - if the value is negative
    • nonNegative

      public static double nonNegative(double value, String fieldName)
      Checks if the given double value is not negative.
      Parameters:
      value - the value to check
      fieldName - the name of the field to validate (used in a possible validation error message)
      Returns:
      the given value
      Throws:
      IllegalArgumentException - if the value is negative