Package dev.brachtendorf
Class MathUtil
- java.lang.Object
-
- dev.brachtendorf.MathUtil
-
public class MathUtil extends Object
- Author:
- Kilian
-
-
Constructor Summary
Constructors Constructor Description MathUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends Number & Comparable<T>>
TclampNumber(T value, T lowerBound, T upperBound)Clamp a number between its lower and upper bound.static longfindClosestDivisibleInteger(int dividend, int divisor)Find the closest value to a number which can be divided by the divisor.static doublefitGaussian(double gaussian, double newStd, double newMean)static doublegetFractionalPart(double d)Return the fractional part of the numberstatic intgetLowerShiftBitMask(int mask)Get the right shift offset until the first masked bitstatic booleanisDoubleEquals(double needle, double target, double epsilon)Compare two doubles.static booleanisNumeric(Object var)Check if the supplied variable represents a numeric valuestatic doublelog(double value, double base)Returns the logarithm of an arbitrary base of a double value.static doublenormalizeValue(double value, double observedMin, double observedMax, double newMin, double newMax)Linearly fit/transform a value from a given to a new range.static doublenormalizeValue(double value, double observedRange, double observedMax, double newRange, double newMax, boolean dummy)Linearly fit/transform a value from a given to a new range.static inttriangularNumber(int n)Calculate the triangular number.
-
-
-
Method Detail
-
getLowerShiftBitMask
public static int getLowerShiftBitMask(int mask)
Get the right shift offset until the first masked bit- Parameters:
mask- bit mask to compute the offset for- Returns:
- the number off bits a number needs to be shifted to be caught by the mask or -1 if the mask == 0
-
clampNumber
public static <T extends Number & Comparable<T>> T clampNumber(T value, T lowerBound, T upperBound)
Clamp a number between its lower and upper bound. If x > upper bound return upper bound. If x < lower bound return lower bound- Type Parameters:
T- The type of the input and return value- Parameters:
value- the input valuelowerBound- the lower boundupperBound- the upper bound- Returns:
- the original value if it's between bounds or the bound
- Since:
- 1.0.0 com.github.kilianB
-
findClosestDivisibleInteger
public static long findClosestDivisibleInteger(int dividend, int divisor)Find the closest value to a number which can be divided by the divisor.if dividend%divisor == 0 return dividend if dividend%divisor != 0 return the value closest to dividend which fulfills the conditionIf two numbers are exactly the same distance away one of them will returned.- Parameters:
dividend- the dividenddivisor- the divisor- Returns:
- the nearest number to dividend which can be divided by divisor
- Throws:
ArithmeticException- if divisor is zero- Since:
- 1.0.0 com.github.kilianB
-
isDoubleEquals
public static boolean isDoubleEquals(double needle, double target, double epsilon)Compare two doubles. Necessary due to non exact floating point arithmetics- Parameters:
needle- the needletarget- the targetepsilon- the amount the values may differ to still consider a match- Returns:
- true if numbers are considered equal, false otherwise
- Since:
- 1.0.0 com.github.kilianB
-
getFractionalPart
public static double getFractionalPart(double d)
Return the fractional part of the number- Parameters:
d- a double- Returns:
- the fractional part of the number. If the base number is negative the returned fraction will also be negative.
- Since:
- 1.0.0 com.github.kilianB
-
fitGaussian
public static double fitGaussian(double gaussian, double newStd, double newMean)- Parameters:
gaussian- A gaussian with std 1 and mean 0newStd- new standard deviationnewMean- new mean- Returns:
- a value fitted to the new mean and std
- Since:
- 1.0.0 com.github.kilianB
-
normalizeValue
public static double normalizeValue(double value, double observedMin, double observedMax, double newMin, double newMax)Linearly fit/transform a value from a given to a new range.[observedMin <= value <= observedMax] ---> [newMin <= transformed <= newMax]- Parameters:
value- The value to fitobservedMin- the minimum value of the current datasetobservedMax- the maximum value of the current datasetnewMin- the lower end of the newly fitted rangenewMax- the upper end of the newly fitted range- Returns:
- the transformed value
-
normalizeValue
public static double normalizeValue(double value, double observedRange, double observedMax, double newRange, double newMax, boolean dummy)Linearly fit/transform a value from a given to a new range.
This method uses a the pre computed range instead of the single range bounds to minimize repetitive calculations in case this method gets called multiple times. For more convenient arguments take a look at[observedMin <= value <= observedMax] ---> [newMin <= transformed <= newMax]normalizeValue(double, double, double, double, double);- Parameters:
value- The value to fitobservedRange- the observedMax - observedMinobservedMax- the maximum value of the current datasetnewRange- the newMax - newMinnewMax- the upper end of the newly fitted rangedummy- dummy variables used to prevent ambiguous method signatures- Returns:
- the transformed value
-
isNumeric
public static boolean isNumeric(Object var)
Check if the supplied variable represents a numeric value- Parameters:
var- the variable to check- Returns:
- true if the variable is a number, false otherwise
- Since:
- 1.2.0 com.github.kilianB
-
log
public static double log(double value, double base)Returns the logarithm of an arbitrary base of a double value. Special cases:- If the argument is NaN or less than zero, then the result is NaN.
- If the argument is positive infinity, then the result is positive infinity.
- If the argument is positive zero or negative zero, then the result is negative infinity.
- If the base is zero or NaN the result is NaN
- Parameters:
value- the value of the logarithmbase- the base of the logarithm- Returns:
- the log_base(value)
- Since:
- 1.3.2 com.github.kilianB
-
triangularNumber
public static int triangularNumber(int n)
Calculate the triangular number. The triangular sum is the sum of all positive natural number up to n. The triangular number (n-1) represents the number of distinct 2 element sets that can be constructed given n individuals.1 + 2 + 3 + 4 + ... + nThe method is undefined for n < 1- Parameters:
n- the number up to which the numbers are summed up- Returns:
- the triangular number
- Throws:
ArithmeticException- if n is 0- Since:
- 1.4.1 com.github.kilianB
-
-