de.erichseifert.gral.util
Class MathUtils
- java.lang.Object
-
- de.erichseifert.gral.util.MathUtils
-
public abstract class MathUtils extends java.lang.ObjectAbstract class that provides utility functions which are useful for mathematical calculations.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static booleanalmostEqual(double a, double b, double delta)Check whether two floating point values match with a given precision.static intbinarySearch(double[] a, double key)Perform a binary search on a sorted arrayato find the element with the nearest element tokey.static intbinarySearchCeil(double[] a, double key)Perform a binary search on a sorted arrayato find the element with the smallest distance tokey.static intbinarySearchFloor(double[] a, double key)Perform a binary search on a sorted arrayato find the element with the smallest distance tokey.static doubleceil(double a, double precision)Returns a rounded number larger thanawith a defined precision.static doublefloor(double a, double precision)Returns a rounded number smaller thanawith a defined precision.static booleanisCalculatable(double n)Returns whether a specified double can be used for calculations.static booleanisCalculatable(java.lang.Number n)Returns whether a specifiedjava.lang.Numberobject can be used for calculations.static doublelimit(double value, double min, double max)Clamps a double number to specified limits: ifvalueis greater thanmaxthenmaxwill be returned.static floatlimit(float value, float min, float max)Clamps a float number to specified limits: ifvalueis greater thanmaxthenmaxwill be returned.static intlimit(int value, int min, int max)Clamps a integer number to specified limits: ifvalueis greater thanmaxthenmaxwill be returned.static <T extends java.lang.Number>
Tlimit(T value, T min, T max)Clamps a number object to specified limits: ifvalueis greater thanmaxthenmaxwill be returned.static doublemagnitude(double base, double n)Returns the magnitude of the specified number.static doublenormalizeDegrees(double angle)Converts an angle in degrees so that it lies between 0.0 and 360.0.static doublequantile(java.util.List<java.lang.Double> values, double q)Utility method used to calculate arbitrary quantiles from a sorted list of values.static <T extends java.lang.Comparable<T>>
intrandomizedSelect(java.util.List<T> a, int lower, int upper, int i)Perform a randomized search on an unsorted arrayato find the ith smallest element.static doubleround(double a, double precision)Mathematically rounds a number with a defined precision.
-
-
-
Method Detail
-
almostEqual
public static boolean almostEqual(double a, double b, double delta)Check whether two floating point values match with a given precision.- Parameters:
a- First valueb- Second valuedelta- Precision- Returns:
trueif the difference of a and b is smaller or equal than delta, otherwisefalse
-
round
public static double round(double a, double precision)Mathematically rounds a number with a defined precision.- Parameters:
a- Valueprecision- Precision- Returns:
- Rounded value
-
floor
public static double floor(double a, double precision)Returns a rounded number smaller thanawith a defined precision.- Parameters:
a- Valueprecision- Precision- Returns:
- Rounded value
-
ceil
public static double ceil(double a, double precision)Returns a rounded number larger thanawith a defined precision.- Parameters:
a- Valueprecision- Precision- Returns:
- Rounded value
-
binarySearch
public static int binarySearch(double[] a, double key)Perform a binary search on a sorted arrayato find the element with the nearest element tokey.- Parameters:
a- Array with ascending valueskey- Pivot value- Returns:
- Index of the array element whose value is nearly or exactly
key
-
binarySearchFloor
public static int binarySearchFloor(double[] a, double key)Perform a binary search on a sorted arrayato find the element with the smallest distance tokey. The returned element's value is always less than or equal tokey.- Parameters:
a- Array with ascending valueskey- Pivot value- Returns:
- Index of the array element whose value is less than or equal to
key
-
binarySearchCeil
public static int binarySearchCeil(double[] a, double key)Perform a binary search on a sorted arrayato find the element with the smallest distance tokey. The returned element's value is always greater than or equal tokey.- Parameters:
a- Array with ascending valueskey- Pivot value- Returns:
- Index of the array element whose value is greater than or equal
to
key
-
limit
public static <T extends java.lang.Number> T limit(T value, T min, T max)Clamps a number object to specified limits: ifvalueis greater thanmaxthenmaxwill be returned. Ifvalueis greater thanminthenminwill be returned.- Type Parameters:
T- Numeric data type- Parameters:
value- Double value to be clampedmin- Minimummax- Maximum- Returns:
- Clamped value
-
limit
public static double limit(double value, double min, double max)Clamps a double number to specified limits: ifvalueis greater thanmaxthenmaxwill be returned. Ifvalueis greater thanminthenminwill be returned.- Parameters:
value- Double value to be clampedmin- Minimummax- Maximum- Returns:
- Clamped value
-
limit
public static float limit(float value, float min, float max)Clamps a float number to specified limits: ifvalueis greater thanmaxthenmaxwill be returned. Ifvalueis greater thanminthenminwill be returned.- Parameters:
value- Float value to be clampedmin- Minimummax- Maximum- Returns:
- Clamped value
-
limit
public static int limit(int value, int min, int max)Clamps a integer number to specified limits: ifvalueis greater thanmaxthenmaxwill be returned. Ifvalueis greater thanminthenminwill be returned.- Parameters:
value- Integer value to be clampedmin- Minimummax- Maximum- Returns:
- Clamped value
-
randomizedSelect
public static <T extends java.lang.Comparable<T>> int randomizedSelect(java.util.List<T> a, int lower, int upper, int i)Perform a randomized search on an unsorted array
ato find the ith smallest element. The array contents are be modified during the operation!See Cormen et al. (2001): Introduction to Algorithms. 2nd edition. p. 186
- Type Parameters:
T- Data type of the array- Parameters:
a- Unsorted arraylower- Starting indexupper- End indexi- Smallness rank of value to search starting at 1- Returns:
- Index of the element that is the ith smallest in array a
-
magnitude
public static double magnitude(double base, double n)Returns the magnitude of the specified number. Example for magnitude base 10:
-0.05 -0.01 0.05 0.01 3.14 1.00 54.32 10.00 123.45 100.00 - Parameters:
base- Base.n- Number.- Returns:
- Magnitude.
-
quantile
public static double quantile(java.util.List<java.lang.Double> values, double q)Utility method used to calculate arbitrary quantiles from a sorted list of values. Currently only one method is implemented: the default method that is used by R (method 7). The list must be sorted.
For more information see:
- Parameters:
values- Data values.q- Quantile in range [0, 1]- Returns:
- Quantile value
-
isCalculatable
public static boolean isCalculatable(java.lang.Number n)
Returns whether a specifiedjava.lang.Numberobject can be used for calculations.nullvalues,NaNvalues or infinite values are considered as non-calculatable.- Parameters:
n- Number object.- Returns:
- whether
ncan be used for calculations.
-
isCalculatable
public static boolean isCalculatable(double n)
Returns whether a specified double can be used for calculations.NaNvalues or infinite values are considered non-calculatable.- Parameters:
n- double value- Returns:
- whether
ncan be used for calculations.
-
normalizeDegrees
public static double normalizeDegrees(double angle)
Converts an angle in degrees so that it lies between 0.0 and 360.0.- Parameters:
angle- Arbitrary angle in degrees.- Returns:
- Angle between 0.0 and 360.0.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG