Package io.mosip.kernel.core.util
Class MathUtils
- java.lang.Object
-
- io.mosip.kernel.core.util.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 doubleceil(double num)Get the smallest whole number larger than number.static doublefloor(double num)Get the largest whole number smaller than number.static doublegetAbs(double num)Absolute value.static floatgetAbs(float num)Absolute value.static intgetAbs(int num)Absolute value.static longgetAbs(long num)Absolute value.static doublegetArrayMaxValue(double[] arr)Returns the maximum of the entries in the input array, orDouble.NaNif the array is empty.static doublegetArrayMaxValue(double[] arr, int startindex, int length)Returns the maximum of the entries in the specified portion of the input array, orDouble.NaNif the designated subarray is emptystatic doublegetArrayMinValue(double[] arr)Returns the minimum of the entries in the input array, orDouble.NaNif the array is empty.static doublegetArrayMinValue(double[] arr, int startindex, int length)Returns the minimum of the entries in the specified portion of the input array, orDouble.NaNif the designated subarray is empty.static doublegetArrayValuesMean(double[] arr)Returns the arithmetic mean of the entries in the input array, orDouble.NaNif the array is empty.static doublegetArrayValuesMean(double[] arr, int startindex, int length)static doublegetArrayValuesProduct(double[] arr)Returns the product of the entries in the input array, orDouble.NaNif the array is empty.static doublegetArrayValuesProduct(double[] arr, int startindex, int length)Returns the product of the entries in the specified portion of the input array, orDouble.NaNif the designated subarray is empty.static doublegetArrayValuesSum(double[] arr)Returns the sum of the values in the input array, orDouble.NaNif the array is empty.static doublegetArrayValuesSum(double[] arr, int startindex, int length)Returns the sum of the entries in the specified portion of the input array, orDouble.NaNif the designated subarray is empty.static double[]getCopyOfArray(double[] source)Creates a copy of source arraystatic double[]getCopyOfArray(double[] source, int length)Creates a copy of source arraystatic int[]getCopyOfArray(int[] source)Creates a copy of source arraystatic int[]getCopyOfArray(int[] source, int length)Creates a copy of source array.static longgetFactorial(int number)Evaluate Factorialstatic intgetGcd(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 longgetGcd(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 operationsstatic doublegetLog(double num)Natural logarithm.static doublegetLog10(double num)Compute the base 10 logarithm.static doublegetMax(double firstnumber, double secondnumber)Compute the maximum of two valuesstatic intgetMax(int firstnumber, int secondnumber)Compute the maximum of two valuesstatic doublegetPow(double num, double exp)Power function.static intgetPow(int num, int exp)Raise an int to an int power.static longgetPow(long num, int exp)Raise a long to an int power.static BigIntegergetPow(BigInteger num, int exp)static BigIntegergetPow(BigInteger num, BigInteger exp)Raise a BigInteger to a BigInteger power.static List<Integer>getPrimeFactors(int num)Prime factors decompositionstatic doublegetRandom(double lowerlimit, double upperlimit)Generate random numberstatic floatgetRandom(float lowerlimit, float upperlimit)Generate random numberstatic intgetRandom(int lowerlimit, int upperlimit)Generate random numberstatic longgetRandom(long lowerlimit, long upperlimit)Generate random numberstatic longgetRound(double num)Get the closest long to num.static doublegetRound(double num, int place)Rounds the given value to the specified number of decimal places.static floatgetRound(float num, int place)Rounds the given value to the specified number of decimal places.static doublegetSqrt(double num)Compute the square root of a number.static booleanisPrimeOrNot(int num)Primality test: tells if the argument is a (provable) prime or not.static intnextPrimeNumber(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 doubleexp- 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 valueupperlimit- 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 valueupperlimit- 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 valueupperlimit- maximum value- Returns:
- random number between lowerlimit and upperlimit
- Throws:
NumberIsTooLargeException- -if lower is greater than or equal to upperNotFiniteNumberException- if one of the bounds is infiniteNotANumberException- if one of the bounds is NaN
-
getRandom
public static float getRandom(float lowerlimit, float upperlimit)Generate random number- Parameters:
lowerlimit- starting valueupperlimit- 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 valuesecondnumber- 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 valuesecondnumber- 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 positiveArithmeticException- -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, orDouble.NaNif 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, orDouble.NaNif the designated subarray is empty- Parameters:
arr- the input arraystartindex- index of the first array element to includelength- 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, orDouble.NaNif 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, orDouble.NaNif the designated subarray is empty.- Parameters:
arr- the input arraystartindex- index of the first array element to includelength- 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, orDouble.NaNif the array is empty.- Parameters:
arr- array of values to sum- Returns:
- the sum of the values or
Double.NaNif 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, orDouble.NaNif the designated subarray is empty.- Parameters:
arr- the input arraystartindex- index of the first array element to includelength- 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, orDouble.NaNif 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, orDouble.NaNif the designated subarray is empty.- Parameters:
arr- the input arraystartindex- index of the first array element to includelength- 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, orDouble.NaNif 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 arraystartindex- index of the first array element to includelength- 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
-
-