Class MathUtils


  • public final class MathUtils
    extends Object
    Utilities for Mathematical operations.
    Since:
    1.0.0
    Author:
    Ritesh Sinha
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double ceil​(double num)
      Get the smallest whole number larger than number.
      static double floor​(double num)
      Get the largest whole number smaller than number.
      static double getAbs​(double num)
      Absolute value.
      static float getAbs​(float num)
      Absolute value.
      static int getAbs​(int num)
      Absolute value.
      static long getAbs​(long num)
      Absolute value.
      static double getArrayMaxValue​(double[] arr)
      Returns the maximum of the entries in the input array, or Double.NaN if the array is empty.
      static double getArrayMaxValue​(double[] arr, int startindex, int length)
      Returns the maximum of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty
      static double getArrayMinValue​(double[] arr)
      Returns the minimum of the entries in the input array, or Double.NaN if the array is empty.
      static double getArrayMinValue​(double[] arr, int startindex, int length)
      Returns the minimum of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty.
      static double getArrayValuesMean​(double[] arr)
      Returns the arithmetic mean of the entries in the input array, or Double.NaN if the array is empty.
      static double getArrayValuesMean​(double[] arr, int startindex, int length)  
      static double getArrayValuesProduct​(double[] arr)
      Returns the product of the entries in the input array, or Double.NaN if the array is empty.
      static double getArrayValuesProduct​(double[] arr, int startindex, int length)
      Returns the product of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty.
      static double getArrayValuesSum​(double[] arr)
      Returns the sum of the values in the input array, or Double.NaN if the array is empty.
      static double getArrayValuesSum​(double[] arr, int startindex, int length)
      Returns the sum of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty.
      static double[] getCopyOfArray​(double[] source)
      Creates a copy of source array
      static double[] getCopyOfArray​(double[] source, int length)
      Creates a copy of source array
      static int[] getCopyOfArray​(int[] source)
      Creates a copy of source array
      static int[] getCopyOfArray​(int[] source, int length)
      Creates a copy of source array.
      static long getFactorial​(int number)
      Evaluate Factorial
      static int getGcd​(int firstnumber, int secondnumber)
      Computes the greatest common divisor of the absolute value of two numbers, using a modified version of the "binary gcd" method.
      static long getGcd​(long firstnumber, long secondnumber)
      Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations
      static double getLog​(double num)
      Natural logarithm.
      static double getLog10​(double num)
      Compute the base 10 logarithm.
      static double getMax​(double firstnumber, double secondnumber)
      Compute the maximum of two values
      static int getMax​(int firstnumber, int secondnumber)
      Compute the maximum of two values
      static double getPow​(double num, double exp)
      Power function.
      static int getPow​(int num, int exp)
      Raise an int to an int power.
      static long getPow​(long num, int exp)
      Raise a long to an int power.
      static BigInteger getPow​(BigInteger num, int exp)  
      static BigInteger getPow​(BigInteger num, BigInteger exp)
      Raise a BigInteger to a BigInteger power.
      static List<Integer> getPrimeFactors​(int num)
      Prime factors decomposition
      static double getRandom​(double lowerlimit, double upperlimit)
      Generate random number
      static float getRandom​(float lowerlimit, float upperlimit)
      Generate random number
      static int getRandom​(int lowerlimit, int upperlimit)
      Generate random number
      static long getRandom​(long lowerlimit, long upperlimit)
      Generate random number
      static long getRound​(double num)
      Get the closest long to num.
      static double getRound​(double num, int place)
      Rounds the given value to the specified number of decimal places.
      static float getRound​(float num, int place)
      Rounds the given value to the specified number of decimal places.
      static double getSqrt​(double num)
      Compute the square root of a number.
      static boolean isPrimeOrNot​(int num)
      Primality test: tells if the argument is a (provable) prime or not.
      static int nextPrimeNumber​(int num)
      Return the smallest prime greater than or equal to n.
    • Method Detail

      • getPow

        public static int getPow​(int num,
                                 int exp)
        Raise an int to an int power.
        Parameters:
        num - Number to raise.
        exp - exponent (must be positive or zero)
        Returns:
        num^exp
        Throws:
        NotPositiveException - if exp is less than 0.
        ArithmeticException - if the result would overflow.
      • getPow

        public static double getPow​(double num,
                                    double exp)
        Power function. Compute num^exp.
        Parameters:
        num - a double
        exp - a double
        Returns:
        double
      • getPow

        public static long getPow​(long num,
                                  int exp)
        Raise a long to an int power.
        Parameters:
        num - Number to raise.
        exp - Exponent (must be positive or zero).
        Returns:
        num^exp
        Throws:
        NotPositiveException - if exp is less than 0.
        ArithmeticException - if the result would overflow.
      • getPow

        public static BigInteger getPow​(BigInteger num,
                                        int exp)
        Parameters:
        num - Number to raise.
        exp - Exponent (must be positive or zero).
        Returns:
        num^exp
        Throws:
        NotPositiveException - if exp is less than 0.
      • getPow

        public static BigInteger getPow​(BigInteger num,
                                        BigInteger exp)
        Raise a BigInteger to a BigInteger power.
        Parameters:
        num - Number to raise.
        exp - Exponent (must be positive or zero).
        Returns:
        num^exp
        Throws:
        NotPositiveException - if exp is less than 0.
      • getRandom

        public static int getRandom​(int lowerlimit,
                                    int upperlimit)
        Generate random number
        Parameters:
        lowerlimit - starting value
        upperlimit - maximum value
        Returns:
        random number between lowerlimit and upperlimit.
        Throws:
        NumberIsTooLargeException - -if lower is greater than or equal to upper
      • getRandom

        public static long getRandom​(long lowerlimit,
                                     long upperlimit)
        Generate random number
        Parameters:
        lowerlimit - starting value
        upperlimit - maximum value
        Returns:
        random number between lowerlimit and upperlimit.
        Throws:
        NumberIsTooLargeException - -if lower is greater than or equal to upper
      • getRandom

        public static double getRandom​(double lowerlimit,
                                       double upperlimit)
        Generate random number
        Parameters:
        lowerlimit - starting value
        upperlimit - maximum value
        Returns:
        random number between lowerlimit and upperlimit
        Throws:
        NumberIsTooLargeException - -if lower is greater than or equal to upper
        NotFiniteNumberException - if one of the bounds is infinite
        NotANumberException - if one of the bounds is NaN
      • getRandom

        public static float getRandom​(float lowerlimit,
                                      float upperlimit)
        Generate random number
        Parameters:
        lowerlimit - starting value
        upperlimit - maximum value
        Returns:
        random number between lowerlimit and upperlimit
      • getSqrt

        public static final double getSqrt​(double num)
        Compute the square root of a number.
        Parameters:
        num - number on which evaluation is done
        Returns:
        square root of num.
      • getMax

        public static final int getMax​(int firstnumber,
                                       int secondnumber)
        Compute the maximum of two values
        Parameters:
        firstnumber - first value
        secondnumber - second value
        Returns:
        secondnumber if firstnumber is lesser or equal to secondnumber, firstnumber otherwise
      • getMax

        public static final double getMax​(double firstnumber,
                                          double secondnumber)
        Compute the maximum of two values
        Parameters:
        firstnumber - first value
        secondnumber - second value
        Returns:
        secondnumber if firstnumber is lesser or equal to secondnumber, firstnumber otherwise
      • getRound

        public static final double getRound​(double num,
                                            int place)
        Rounds the given value to the specified number of decimal places.
        Parameters:
        num - Value to round.
        place - Number of digits to the right of the decimal point.
        Returns:
        the rounded value.
      • getRound

        public static final float getRound​(float num,
                                           int place)
        Rounds the given value to the specified number of decimal places.
        Parameters:
        num - Value to round.
        place - Number of digits to the right of the decimal point.
        Returns:
        the rounded value.
      • getRound

        public static final long getRound​(double num)
        Get the closest long to num.
        Parameters:
        num - number from which closest long is requested
        Returns:
        closest long to num
      • getAbs

        public static final double getAbs​(double num)
        Absolute value.
        Parameters:
        num - number from which absolute value is requested.
        Returns:
        abs(num)
      • getAbs

        public static final float getAbs​(float num)
        Absolute value.
        Parameters:
        num - number from which absolute value is requested.
        Returns:
        abs(num)
      • getAbs

        public static final long getAbs​(long num)
        Absolute value.
        Parameters:
        num - number from which absolute value is requested.
        Returns:
        abs(num)
      • getAbs

        public static final int getAbs​(int num)
        Absolute value.
        Parameters:
        num - number from which absolute value is requested.
        Returns:
        abs(num)
      • getFactorial

        public static final long getFactorial​(int number)
        Evaluate Factorial
        Parameters:
        number - argument
        Returns:
        n!
        Throws:
        NotPositiveException - -If number is not positive
        ArithmeticException - -If number is greator than 20 and result is too large to fit in long type.
      • getGcd

        public static final int getGcd​(int firstnumber,
                                       int secondnumber)
        Computes the greatest common divisor of the absolute value of two numbers, using a modified version of the "binary gcd" method.
        Parameters:
        firstnumber - Number.
        secondnumber - Number.
        Returns:
        the greatest common divisor (never negative).
        Throws:
        ArithmeticException - if the result cannot be represented as a non-negative integer value.
      • getGcd

        public static final long getGcd​(long firstnumber,
                                        long secondnumber)
        Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations
        Parameters:
        firstnumber - Number.
        secondnumber - Number.
        Returns:
        the greatest common divisor, never negative.
        Throws:
        ArithmeticException - if the result cannot be represented as a non-negative long type value.
      • getLog

        public static final double getLog​(double num)
        Natural logarithm.
        Parameters:
        num - a double
        Returns:
        log(x)
      • getLog10

        public static final double getLog10​(double num)
        Compute the base 10 logarithm.
        Parameters:
        num - a number
        Returns:
        log10(x)
      • getCopyOfArray

        public static int[] getCopyOfArray​(int[] source)
        Creates a copy of source array
        Parameters:
        source - Array to be copied.
        Returns:
        the copied array.
      • getCopyOfArray

        public static int[] getCopyOfArray​(int[] source,
                                           int length)
        Creates a copy of source array.
        Parameters:
        source - Array to be copied.
        length - Number of entries to copy. If smaller then the source length, the copy will be truncated, if larger it will padded with zeroes.
        Returns:
        the copied array.
      • getCopyOfArray

        public static double[] getCopyOfArray​(double[] source)
        Creates a copy of source array
        Parameters:
        source - Array to be copied.
        Returns:
        the copied array.
      • getCopyOfArray

        public static double[] getCopyOfArray​(double[] source,
                                              int length)
        Creates a copy of source array
        Parameters:
        source - Array to be copied.
        length - Number of entries to copy. If smaller then the source length, the copy will be truncated, if larger it will padded with zeroes.
        Returns:
        the copied array.
      • getArrayMaxValue

        public static double getArrayMaxValue​(double[] arr)
        Returns the maximum of the entries in the input array, or Double.NaN if the array is empty.
        Parameters:
        arr - the input array
        Returns:
        the maximum of the values or Double.NaN if the array is empty
        Throws:
        IllegalArgumentException - if the array is null
      • getArrayMaxValue

        public static double getArrayMaxValue​(double[] arr,
                                              int startindex,
                                              int length)
        Returns the maximum of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty
        Parameters:
        arr - the input array
        startindex - index of the first array element to include
        length - the number of elements to include
        Returns:
        the maximum of the values or Double.NaN if length = 0
        Throws:
        IllegalArgumentException - if the array is null or the array index parameters are not valid
      • getArrayMinValue

        public static double getArrayMinValue​(double[] arr)
        Returns the minimum of the entries in the input array, or Double.NaN if the array is empty.
        Parameters:
        arr - the input array
        Returns:
        the minimum of the values or Double.NaN if the array is empty
        Throws:
        IllegalArgumentException - if the array is null
      • getArrayMinValue

        public static double getArrayMinValue​(double[] arr,
                                              int startindex,
                                              int length)
        Returns the minimum of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty.
        Parameters:
        arr - the input array
        startindex - index of the first array element to include
        length - the number of elements to include
        Returns:
        the minimum of the values or Double.NaN if length = 0
        Throws:
        IllegalArgumentException - if the array is null or the array index parameters are not valid
      • getArrayValuesSum

        public static double getArrayValuesSum​(double[] arr)
        Returns the sum of the values in the input array, or Double.NaN if the array is empty.
        Parameters:
        arr - array of values to sum
        Returns:
        the sum of the values or Double.NaN if the array is empty
        Throws:
        IllegalArgumentException - if the array is null
      • getArrayValuesSum

        public static double getArrayValuesSum​(double[] arr,
                                               int startindex,
                                               int length)
        Returns the sum of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty.
        Parameters:
        arr - the input array
        startindex - index of the first array element to include
        length - the number of elements to include
        Returns:
        the sum of the values or Double.NaN if length = 0
        Throws:
        IllegalArgumentException - if the array is null or the array index parameters are not valid
      • getArrayValuesProduct

        public static double getArrayValuesProduct​(double[] arr)
        Returns the product of the entries in the input array, or Double.NaN if the array is empty.
        Parameters:
        arr - the input array
        Returns:
        the product of the values or Double.NaN if the array is empty
        Throws:
        IllegalArgumentException - if the array is null
      • getArrayValuesProduct

        public static double getArrayValuesProduct​(double[] arr,
                                                   int startindex,
                                                   int length)
        Returns the product of the entries in the specified portion of the input array, or Double.NaN if the designated subarray is empty.
        Parameters:
        arr - the input array
        startindex - index of the first array element to include
        length - the number of elements to include
        Returns:
        the product of the values or Double.NaN if length = 0
        Throws:
        IllegalArgumentException - if the array is null or the array index parameters are not valid
      • getArrayValuesMean

        public static double getArrayValuesMean​(double[] arr)
        Returns the arithmetic mean of the entries in the input array, or Double.NaN if the array is empty.
        Parameters:
        arr - the input array
        Returns:
        the mean of the values or Double.NaN if the array is empty
        Throws:
        IllegalArgumentException - if the array is null
      • getArrayValuesMean

        public static double getArrayValuesMean​(double[] arr,
                                                int startindex,
                                                int length)
        Parameters:
        arr - the input array
        startindex - index of the first array element to include
        length - the number of elements to include
        Returns:
        the mean of the values or Double.NaN if length = 0
        Throws:
        IllegalArgumentException - if the array is null or the array index parameters are not valid
      • isPrimeOrNot

        public static boolean isPrimeOrNot​(int num)
        Primality test: tells if the argument is a (provable) prime or not.
        Parameters:
        num - number to test.
        Returns:
        true if num is prime.
      • nextPrimeNumber

        public static int nextPrimeNumber​(int num)
        Return the smallest prime greater than or equal to n.
        Parameters:
        num - a positive number.
        Returns:
        the smallest prime greater than or equal to n.
        Throws:
        IllegalArgumentException - if num is less than 0.
      • getPrimeFactors

        public static List<Integer> getPrimeFactors​(int num)
        Prime factors decomposition
        Parameters:
        num - number to factorize: must be ≥ 2
        Returns:
        list of prime factors of num
        Throws:
        IllegalArgumentException - if num is less than 2.
      • ceil

        public static double ceil​(double num)
        Get the smallest whole number larger than number.
        Parameters:
        num - number from which ceil is requested
        Returns:
        a double number c such that c is an integer is greator than equal to num and num is greator than c-1.0
      • floor

        public static double floor​(double num)
        Get the largest whole number smaller than number.
        Parameters:
        num - number from which floor is requested
        Returns:
        a double number f such that f is an integer f is less than or equal to num and num is less than f + 1.0