Class MathUtil
java.lang.Object
org.cloudsimplus.util.MathUtil
An utility class containing multiple convenient math functions.
- Since:
- CloudSim Toolkit 3.0
- Author:
- Anton Beloglazov
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic double[]abs(double... data) Gets the absolute values from an array of valuesstatic intcountNonZeroBeginning(double... data) Counts the number of values different of zero at the beginning of an array.static intdoubleToInt(double value) Converts a double value to an int, using an appropriate rounding function.static org.apache.commons.math3.stat.descriptive.DescriptiveStatisticsgetStatistics(double... list) Gets an object to compute descriptive statistics for an array of numbers.static org.apache.commons.math3.stat.descriptive.DescriptiveStatisticsgetStatistics(Collection<Double> list) Gets an object to compute descriptive statistics for a list of numbers.static doubleiqr(double... data) Gets the [Inter-quartile Range (IQR)](https://en.wikipedia.org/wiki/Interquartile_range from an array of numbers.static doublemad(double... data) Gets the Median Absolute Deviation (MAD) from an array of numbers.static doubleGets the average from a list of numbers.static doublemedian(double... list) Gets the median from an array of numbers.static doublemedian(Collection<Double> list) Gets the median from a list of numbers.static doublenonNegative(double value, String fieldName) Checks if the given double value is not negative.static intnonNegative(int value, String fieldName) Checks if the given int value is not negative.static longnonNegative(long value, String fieldName) Checks if the given long value is not negative.static intTry to convert a String to an int value.static doublepercentage(double value, String fieldName) Checks if the given number is a percentage between [0 and 1].static doublepercentValue(double partial, double total) Computes the percentage of a current value related to a total value.static doublepositive(double first, double second) Returns the first positive double value.static longpositive(long first, long second) Returns the first positive long value.static booleansame(double first, double second) Checks if two double numbers are equals, considering a precision error or 0.01.static booleansame(double first, double second, double precisionError) Checks if two double numbers are equals, considering a given precision error.static doubleGets the Standard Deviation from a list of numbers.static doubleSums a list of numbers.static doubleGets the Variance from a list of numbers.
-
Method Details
-
sum
-
median
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
-
variance
-
stDev
-
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 appliesMath.floor(double)to round the number down. If it' a positive value, it appliesMath.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; orMath.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 theComparator.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
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 convertingdefaultValue- 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 checksecond- 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 checksecond- the second number to checkprecisionError- 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 percentagetotal- the total value that represents 100%- Returns:
- the percentage of the current value in scale from 0 to 100%
-
percentage
Checks if the given number is a percentage between [0 and 1].- Parameters:
value- the value to checkfieldName- 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 checksecond- 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 checksecond- the first value to check- Returns:
- the first positive double value
-
nonNegative
Checks if the given int value is not negative.- Parameters:
value- the value to checkfieldName- 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
Checks if the given long value is not negative.- Parameters:
value- the value to checkfieldName- 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
Checks if the given double value is not negative.- Parameters:
value- the value to checkfieldName- 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
-