Class 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>>
      T
      clampNumber​(T value, T lowerBound, T upperBound)
      Clamp a number between its lower and upper bound.
      static long findClosestDivisibleInteger​(int dividend, int divisor)
      Find the closest value to a number which can be divided by the divisor.
      static double fitGaussian​(double gaussian, double newStd, double newMean)  
      static double getFractionalPart​(double d)
      Return the fractional part of the number
      static int getLowerShiftBitMask​(int mask)
      Get the right shift offset until the first masked bit
      static boolean isDoubleEquals​(double needle, double target, double epsilon)
      Compare two doubles.
      static boolean isNumeric​(Object var)
      Check if the supplied variable represents a numeric value
      static double log​(double value, double base)
      Returns the logarithm of an arbitrary base of a double value.
      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.
      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.
      static int triangularNumber​(int n)
      Calculate the triangular number.
    • Constructor Detail

      • MathUtil

        public MathUtil()
    • 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 value
        lowerBound - the lower bound
        upperBound - 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 condition
         
        If two numbers are exactly the same distance away one of them will returned.
        Parameters:
        dividend - the dividend
        divisor - 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 needle
        target - the target
        epsilon - 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 0
        newStd - new standard deviation
        newMean - 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 fit
        observedMin - the minimum value of the current dataset
        observedMax - the maximum value of the current dataset
        newMin - the lower end of the newly fitted range
        newMax - 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.
         
         [observedMin <= value <= observedMax] 
         				---> 
         [newMin <= transformed <= newMax]
         
         
        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 normalizeValue(double, double, double, double, double);
        Parameters:
        value - The value to fit
        observedRange - the observedMax - observedMin
        observedMax - the maximum value of the current dataset
        newRange - the newMax - newMin
        newMax - the upper end of the newly fitted range
        dummy - 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 logarithm
        base - 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 + ... + n The 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