Package com.gengoai.math
Class Math2
- java.lang.Object
-
- com.gengoai.math.Math2
-
public final class Math2 extends Object
Commonly needed math routines and methods that work over arrays and iterable.
- Author:
- David B. Bracewell
-
-
Field Summary
Fields Modifier and Type Field Description static double
LOG_2
The constant LOG_2.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static double
auc(double[] x, double[] y)
Calculates the AUC (Area Under the Curve)static double
clip(double value, double min, double max)
Clips a value to ensure it falls between the lower or upper bound of range.static double
log2(double number)
Calculates the base 2 log of a given numberstatic <E> E
maxBy(E e1, E e2, Comparator<E> comparator)
static <E> E
minBy(E e1, E e2, Comparator<E> comparator)
static double
rescale(double value, double originalMin, double originalMax, double newMin, double newMax)
Rescales a value from an old range to a new range, e.g.static double
safeLog(double d)
Safe log double.static double
sum(double... values)
Sums the numbers in the given array.static int
sum(int... values)
Sums the numbers in the given array.static long
sum(long... values)
Sums the numbers in the given array.static double
sum(Iterable<? extends Number> values)
Sums the numbers in a given iterable treating them as doubles.static EnhancedDoubleStatistics
summaryStatistics(double... values)
Calculates the summary statistics for the values in the given array.static EnhancedDoubleStatistics
summaryStatistics(int... values)
Calculates the summary statistics for the values in the given array.static EnhancedDoubleStatistics
summaryStatistics(long... values)
Calculates the summary statistics for the values in the given array.static EnhancedDoubleStatistics
summaryStatistics(Iterable<? extends Number> values)
Calculates the summary statistics for the values in the given iterable.static Double
tryParseDouble(String string)
Try to parse a string into a double returning null if a parse error occursstatic Float
tryParseFloat(String string)
Try to parse a string into a float returning null if a parse error occursstatic Integer
tryParseInteger(String string)
Try to parse a string into a integer returning null if a parse error occursstatic Long
tryParseLong(String string)
Try to parse a string into a long returning null if a parse error occurs
-
-
-
Method Detail
-
auc
public static double auc(double[] x, double[] y)
Calculates the AUC (Area Under the Curve)- Parameters:
x
- the xy
- the y- Returns:
- the AUC
-
clip
public static double clip(double value, double min, double max)
Clips a value to ensure it falls between the lower or upper bound of range.
- Parameters:
value
- the value to clipmin
- the lower bound of the rangemax
- the upper bound of the range- Returns:
- the clipped value
-
log2
public static double log2(double number)
Calculates the base 2 log of a given number- Parameters:
number
- the number to calculate the base 2 log of- Returns:
- the base 2 log of the given number
-
maxBy
public static <E> E maxBy(E e1, E e2, Comparator<E> comparator)
-
minBy
public static <E> E minBy(E e1, E e2, Comparator<E> comparator)
-
rescale
public static double rescale(double value, double originalMin, double originalMax, double newMin, double newMax)
Rescales a value from an old range to a new range, e.g. change the value 2 in a 1 to 5 scale to the value 3.25 in a 1 to 10 scale
- Parameters:
value
- the value to rescaleoriginalMin
- the lower bound of the original rangeoriginalMax
- the upper bound of the original rangenewMin
- the lower bound of the new rangenewMax
- the upper bound of the new range- Returns:
- the given value rescaled to fall between newMin and new Max
- Throws:
IllegalArgumentException
- if originalMax<=
originalMin or newMax<=
newMin
-
safeLog
public static double safeLog(double d)
Safe log double.- Parameters:
d
- the d- Returns:
- the double
-
sum
public static double sum(Iterable<? extends Number> values)
Sums the numbers in a given iterable treating them as doubles.
- Parameters:
values
- the iterable of numbers to sum- Returns:
- the sum of the iterable
- Throws:
NullPointerException
- if the values are null
-
sum
public static double sum(double... values)
Sums the numbers in the given array.
- Parameters:
values
- the values to sum- Returns:
- the sum of the values
- Throws:
NullPointerException
- if the values are null
-
sum
public static int sum(int... values)
Sums the numbers in the given array.
- Parameters:
values
- the values to sum- Returns:
- the sum of the values
- Throws:
NullPointerException
- if the values are null
-
sum
public static long sum(long... values)
Sums the numbers in the given array.
- Parameters:
values
- the values to sum- Returns:
- the sum of the values
- Throws:
NullPointerException
- if the values are null
-
summaryStatistics
public static EnhancedDoubleStatistics summaryStatistics(double... values)
Calculates the summary statistics for the values in the given array.
- Parameters:
values
- the values to calculate summary statistics over- Returns:
- the summary statistics of the given array
- Throws:
NullPointerException
- if the values are null
-
summaryStatistics
public static EnhancedDoubleStatistics summaryStatistics(int... values)
Calculates the summary statistics for the values in the given array.
- Parameters:
values
- the values to calculate summary statistics over- Returns:
- the summary statistics of the given array
- Throws:
NullPointerException
- if the values are null
-
summaryStatistics
public static EnhancedDoubleStatistics summaryStatistics(long... values)
Calculates the summary statistics for the values in the given array.
- Parameters:
values
- the values to calculate summary statistics over- Returns:
- the summary statistics of the given array
- Throws:
NullPointerException
- if the values are null
-
summaryStatistics
public static EnhancedDoubleStatistics summaryStatistics(Iterable<? extends Number> values)
Calculates the summary statistics for the values in the given iterable.
- Parameters:
values
- the values to calculate summary statistics over- Returns:
- the summary statistics of the given iterable
- Throws:
NullPointerException
- if the iterable is null
-
tryParseDouble
public static Double tryParseDouble(String string)
Try to parse a string into a double returning null if a parse error occurs- Parameters:
string
- the string to parse- Returns:
- the parsed Double value or null if not parsable
-
tryParseFloat
public static Float tryParseFloat(String string)
Try to parse a string into a float returning null if a parse error occurs- Parameters:
string
- the string to parse- Returns:
- the parsed Float value or null if not parsable
-
tryParseInteger
public static Integer tryParseInteger(String string)
Try to parse a string into a integer returning null if a parse error occurs- Parameters:
string
- the string to parse- Returns:
- the parsed Integer value or null if not parsable
-
-