Package org.drip.numerical.common
Class NumberUtil
java.lang.Object
org.drip.numerical.common.NumberUtil
public class NumberUtil
extends java.lang.Object
NumberUtil implements number utility functions. It exposes the following functions:
- Verify number/number array validity, and closeness/sign match
- Factorial Permutation/Combination functionality
- Dump multi-dimensional array contents
- Min/Max/Bound the array entries within limits
- Module = Computational Core Module
- Library = Numerical Analysis Library
- Project = Numerical Quadrature, Differentiation, Eigenization, Linear Algebra, and Utilities
- Package = Primitives/Array Manipulate Format Display Utilities
- Author:
- Lakshmi Krishnamurthy
-
Constructor Summary
Constructors Constructor Description NumberUtil() -
Method Summary
Modifier and Type Method Description static java.lang.StringArrayRow(double[] doubleArray, int preDecimalPlaces, int postDecimalPlaces, boolean bailOnNaN)Print the contents of the 1D array to the Specified Decimal Location as a Rowstatic intBinaryDigitCount(int n)Retrieve the Binary Digit Countstatic doubleBound(double dblX, double dblFloor, double dblCeiling)Bound the input to within (floor, Ceiling), i.e., compute Min (Max (floor, X), Ceiling)static booleanCanMeasureWater(int x, int y, int z)You are given two jugs with capacities x and y liters.static intDivideIntegers(int dividend, int divisor)Divide two integers without using multiplication, division, and mod operator.static doubleDoubleFactorial(int n)Compute (2n - 1)!!static intFactorial(int n)This function implements Factorial N.static doubleFractional(double z)Retrieve the Fractional Part of zstatic longGCD(long a, long b)Greatest Common Divisor between Two Numbersstatic doubleHalfDownShiftedFactorial(int n)Compute (n - 0.5)!static booleanIsInteger(double z)Indicate if z is an Integerstatic booleanIsNegativeInteger(double z)Indicate if z is a Negative Integerstatic booleanIsNonNegativeInteger(double z)Indicate if z is a Non-Negative Integerstatic booleanIsNonPositiveInteger(double z)Indicate if z is a Non-Positive Integerstatic booleanIsNumberSequenceAdditive(java.lang.String s)Additive number is a string whose digits can form additive sequence.static booleanIsPositiveInteger(double z)Indicate if z is a Positive Integerstatic booleanIsValid(double dbl)Checks if the input double is Infinite or NaNstatic booleanIsValid(double[] adbl)Checks if the input double array contains an Infinite or an NaNstatic booleanIsValid(double[][] r1Grid)Checks if the Input Matrix contains an Infinite or an NaNstatic booleanIsValid(long l)Check if the Input Long is MIN_VALUE or MAX_VALUEstatic booleanIsValid(long[] al)Check if the Input Long Array contains a MIN_VALUE or MAX_VALUEstatic java.lang.StringMatrixToString(java.lang.String description, double[][] matrix, int leftPlaces, int rightPlaces)Stringify the Matrix by Decimal-Formatting the Contentsstatic doubleMaximum(double[] adbl)Retrieve the Maximum Element in the specified Arraystatic doubleMinimum(double[] adbl)Retrieve the Minimum Element in the specified Arraystatic intNCK(int n, int k)This function implements N choose K.static intNextPrimePalindrome(int n)Find the smallest prime palindrome greater than or equal to the input number.static booleanNormalizedPositive(double[] array)Check if the Array Elements are Normalized and Positivestatic intNPK(int n, int k)This function implements N Permute K.static intNthDigit(int n)Identify the nth Digit of the Numberstatic longNthUglyNumber(long n, long a, long b, long c)Find the nth ugly number.static doublePochhammerSymbol(double s, int k)Compute the Pochhammer Symbol for the Specified s and kstatic doublePower(double x, int n)Compute the Integer Power of xstatic doublePrimeFactorExponentTwo(int n)Compute the Exponent 2 of Prime Factorization for a given Integerstatic booleanPrint1DArray(java.lang.String strName, double[] adblA, boolean bBailOnNaN)Print the contents of the 1D arraystatic booleanPrint1DArray(java.lang.String strName, double[] adblA, int iNumDecimal, boolean bBailOnNaN)Print the contents of the 1D array to the Specified Decimal Locationstatic booleanPrint2DArray(java.lang.String strName, double[][] aadblA, boolean bBailOnNaN)Print the contents of the 2D arraystatic booleanPrint2DArrayPair(java.lang.String strLeftLabel, java.lang.String strRightLabel, double[][] aadblLeft, double[][] aadblRight, boolean bBailOnNaN)Print the Contents of the 2D Array Pairstatic booleanPrint2DArrayTriplet(java.lang.String strLeftLabel, java.lang.String strMiddleLabel, java.lang.String strRightLabel, double[][] aadblLeft, double[][] aadblMiddle, double[][] aadblRight, boolean bBailOnNaN)Print the Contents of the 2D Array Tripletstatic booleanPrintMatrix(java.lang.String strName, double[][] aadblA)Print the Matrix Contentsstatic booleanPrintMatrix(java.lang.String description, double[][] matrix, int leftPlaces, int rightPlaces)Print the Matrix by Decimal-Formatting the Contentsstatic intQuotient2(int dividend, int divisor)Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.static intReciprocalIntegerFloor(double z)Retrieve the Reciprocal Integer Floor of zstatic intReverseInteger(int number)Given a signed 32-bit integer number, return number with its digits reversed.static doubleRisingPochhammerSymbol(double s, int k)Compute the Rising Pochhammer Symbol for the Specified s and kstatic booleanSameSign(double[] adbl)Check if the specified array contains elements all of the same signstatic doubleSign(double z)Indicate the Sign of zstatic doubleSubFactorial(int n)This function implements Sub-factorial N.static intTrailingFactorialZeros(int n)Estimate the Trailing Factorial Zerosstatic booleanWithinTolerance(double dbl1, double dbl2)Compare and checks if the two input numbers fall within a specified tolerancestatic booleanWithinTolerance(double dbl1, double dbl2, double dblAbsoluteTolerance, double dblRelativeTolerance)Compare and checks if the two input numbers fall within a specified toleranceMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
NumberUtil
public NumberUtil()
-
-
Method Details
-
IsValid
public static final boolean IsValid(long l)Check if the Input Long is MIN_VALUE or MAX_VALUE- Parameters:
l- Input Long- Returns:
- TRUE - Input Long is MIN_VALUE or MAX_VALUE
-
IsValid
public static final boolean IsValid(long[] al)Check if the Input Long Array contains a MIN_VALUE or MAX_VALUE- Parameters:
al- Input Long Array- Returns:
- TRUE - Input Long Array contains a MIN_VALUE or MAX_VALUE
-
IsValid
public static final boolean IsValid(double dbl)Checks if the input double is Infinite or NaN- Parameters:
dbl- Input double- Returns:
- TRUE - Input double is Infinite or NaN
-
IsValid
public static final boolean IsValid(double[] adbl)Checks if the input double array contains an Infinite or an NaN- Parameters:
adbl- Input double array- Returns:
- TRUE - Input double contains an Infinite or an NaN
-
IsValid
public static final boolean IsValid(double[][] r1Grid)Checks if the Input Matrix contains an Infinite or an NaN- Parameters:
r1Grid- Input Matrix- Returns:
- TRUE - Input Matrix contains an Infinite or an NaN
-
WithinTolerance
public static final boolean WithinTolerance(double dbl1, double dbl2, double dblAbsoluteTolerance, double dblRelativeTolerance)Compare and checks if the two input numbers fall within a specified tolerance- Parameters:
dbl1- Number #1dbl2- Number #2dblAbsoluteTolerance- Absolute TolerancedblRelativeTolerance- Relative Tolerance- Returns:
- TRUE if they fall within the tolerance
-
WithinTolerance
public static final boolean WithinTolerance(double dbl1, double dbl2)Compare and checks if the two input numbers fall within a specified tolerance- Parameters:
dbl1- Number #1dbl2- Number #2- Returns:
- TRUE if they fall within the tolerance
-
Factorial
public static final int Factorial(int n)This function implements Factorial N.- Parameters:
n- N- Returns:
- Factorial N
-
SubFactorial
public static final double SubFactorial(int n)This function implements Sub-factorial N.- Parameters:
n- N- Returns:
- The Sub-factorial N
-
NPK
public static final int NPK(int n, int k)This function implements N Permute K.- Parameters:
n- Nk- K- Returns:
- N Permute K
-
NCK
public static final int NCK(int n, int k)This function implements N choose K.- Parameters:
n- Nk- K- Returns:
- N choose K
-
Bound
public static final double Bound(double dblX, double dblFloor, double dblCeiling) throws java.lang.ExceptionBound the input to within (floor, Ceiling), i.e., compute Min (Max (floor, X), Ceiling)- Parameters:
dblX- Input NumberdblFloor- FloordblCeiling- Ceiling- Returns:
- Min (Max (floor, X), Ceiling)
- Throws:
java.lang.Exception- Thrown if the inputs are invalid
-
Minimum
public static final double Minimum(double[] adbl) throws java.lang.ExceptionRetrieve the Minimum Element in the specified Array- Parameters:
adbl- Array of elements- Returns:
- The Minimum Element
- Throws:
java.lang.Exception- Thrown if the Inputs are invalid
-
Maximum
public static final double Maximum(double[] adbl) throws java.lang.ExceptionRetrieve the Maximum Element in the specified Array- Parameters:
adbl- Array of elements- Returns:
- The Maximum Element
- Throws:
java.lang.Exception- Thrown if the Inputs are invalid
-
SameSign
public static final boolean SameSign(double[] adbl) throws java.lang.ExceptionCheck if the specified array contains elements all of the same sign- Parameters:
adbl- Array of elements- Returns:
- TRUE - Same Sign
- Throws:
java.lang.Exception- Thrown if the Inputs are invalid
-
Print1DArray
public static final boolean Print1DArray(java.lang.String strName, double[] adblA, boolean bBailOnNaN)Print the contents of the 1D array- Parameters:
strName- Label NameadblA- The 1D arraybBailOnNaN- Bail on encountering an NaN- Returns:
- TRUE - Print Successful
-
Print1DArray
public static final boolean Print1DArray(java.lang.String strName, double[] adblA, int iNumDecimal, boolean bBailOnNaN)Print the contents of the 1D array to the Specified Decimal Location- Parameters:
strName- Label NameadblA- The 1D arrayiNumDecimal- Number of Decimal Places to DisplaybBailOnNaN- Bail on encountering an NaN- Returns:
- TRUE - Print Successful
-
ArrayRow
public static final java.lang.String ArrayRow(double[] doubleArray, int preDecimalPlaces, int postDecimalPlaces, boolean bailOnNaN)Print the contents of the 1D array to the Specified Decimal Location as a Row- Parameters:
doubleArray- The 1D arraypreDecimalPlaces- Number of Decimal Places to DisplaypostDecimalPlaces- Number of Post Decimal Places to DisplaybailOnNaN- TRUE - Bail on encountering an NaN- Returns:
- Contents of the 1D array to the Specified Decimal Location as a Row
-
Print2DArray
public static final boolean Print2DArray(java.lang.String strName, double[][] aadblA, boolean bBailOnNaN)Print the contents of the 2D array- Parameters:
strName- Label NameaadblA- The 2D arraybBailOnNaN- Bail on encountering an NaN- Returns:
- TRUE - Print Successful
-
Print2DArrayPair
public static final boolean Print2DArrayPair(java.lang.String strLeftLabel, java.lang.String strRightLabel, double[][] aadblLeft, double[][] aadblRight, boolean bBailOnNaN)Print the Contents of the 2D Array Pair- Parameters:
strLeftLabel- Left LabelstrRightLabel- Right LabelaadblLeft- The Left 2D arrayaadblRight- The Right 2D arraybBailOnNaN- Bail on encountering an NaN- Returns:
- TRUE - Print Successful
-
Print2DArrayTriplet
public static final boolean Print2DArrayTriplet(java.lang.String strLeftLabel, java.lang.String strMiddleLabel, java.lang.String strRightLabel, double[][] aadblLeft, double[][] aadblMiddle, double[][] aadblRight, boolean bBailOnNaN)Print the Contents of the 2D Array Triplet- Parameters:
strLeftLabel- Left LabelstrMiddleLabel- Middle LabelstrRightLabel- Right LabelaadblLeft- The Left 2D arrayaadblMiddle- The Middle 2D arrayaadblRight- The Right 2D arraybBailOnNaN- Bail on encountering an NaN- Returns:
- TRUE - Print Successful
-
PrintMatrix
public static final boolean PrintMatrix(java.lang.String strName, double[][] aadblA)Print the Matrix Contents- Parameters:
strName- Name of the MatrixaadblA- Matrix- Returns:
- TRUE - Matrix Contents Successfully printed
-
PrintMatrix
public static final boolean PrintMatrix(java.lang.String description, double[][] matrix, int leftPlaces, int rightPlaces)Print the Matrix by Decimal-Formatting the Contents- Parameters:
description- Row Descriptionmatrix- Matrix to be PrintedleftPlaces- Number of Digits to the Left of the DecimalrightPlaces- Number of Digits to the Right of the Decimal- Returns:
- TRUE => Matrix successfully printed
-
MatrixToString
public static final java.lang.String MatrixToString(java.lang.String description, double[][] matrix, int leftPlaces, int rightPlaces)Stringify the Matrix by Decimal-Formatting the Contents- Parameters:
description- Row Descriptionmatrix- Matrix to be PrintedleftPlaces- Number of Digits to the Left of the DecimalrightPlaces- Number of Digits to the Right of the Decimal- Returns:
- Formatted Matrix
-
HalfDownShiftedFactorial
public static final double HalfDownShiftedFactorial(int n) throws java.lang.ExceptionCompute (n - 0.5)!- Parameters:
n- n- Returns:
- (n - 0.5)! Value
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
DoubleFactorial
public static final double DoubleFactorial(int n) throws java.lang.ExceptionCompute (2n - 1)!!- Parameters:
n- n- Returns:
- (2n - 1)!!
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
RisingPochhammerSymbol
public static final double RisingPochhammerSymbol(double s, int k) throws java.lang.ExceptionCompute the Rising Pochhammer Symbol for the Specified s and k- Parameters:
s- sk- k- Returns:
- The Rising Pochhammer Symbol
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
PochhammerSymbol
public static final double PochhammerSymbol(double s, int k) throws java.lang.ExceptionCompute the Pochhammer Symbol for the Specified s and k- Parameters:
s- sk- k- Returns:
- Pochhammer Symbol
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
IsInteger
public static final boolean IsInteger(double z) throws java.lang.ExceptionIndicate if z is an Integer- Parameters:
z- Z- Returns:
- TRUE - z is an Integer
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
IsPositiveInteger
public static final boolean IsPositiveInteger(double z) throws java.lang.ExceptionIndicate if z is a Positive Integer- Parameters:
z- Z- Returns:
- TRUE - z is a Positive Integer
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
IsNonPositiveInteger
public static final boolean IsNonPositiveInteger(double z) throws java.lang.ExceptionIndicate if z is a Non-Positive Integer- Parameters:
z- Z- Returns:
- TRUE - z is a Non-Positive Integer
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
IsNegativeInteger
public static final boolean IsNegativeInteger(double z) throws java.lang.ExceptionIndicate if z is a Negative Integer- Parameters:
z- Z- Returns:
- TRUE - z is a Negative Integer
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
IsNonNegativeInteger
public static final boolean IsNonNegativeInteger(double z) throws java.lang.ExceptionIndicate if z is a Non-Negative Integer- Parameters:
z- Z- Returns:
- TRUE - z is a Non-Negative Integer
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
Sign
public static final double Sign(double z) throws java.lang.ExceptionIndicate the Sign of z- Parameters:
z- Z- Returns:
- Sign of z
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
NormalizedPositive
public static final boolean NormalizedPositive(double[] array)Check if the Array Elements are Normalized and Positive- Parameters:
array- Array- Returns:
- TRUE - The Array Elements are Normalized and Positive
-
Fractional
public static final double Fractional(double z) throws java.lang.ExceptionRetrieve the Fractional Part of z- Parameters:
z- Z- Returns:
- The Fractional Part of z
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
ReciprocalIntegerFloor
public static final int ReciprocalIntegerFloor(double z) throws java.lang.ExceptionRetrieve the Reciprocal Integer Floor of z- Parameters:
z- Z- Returns:
- The Reciprocal Integer Floor of z
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
PrimeFactorExponentTwo
public static final double PrimeFactorExponentTwo(int n) throws java.lang.ExceptionCompute the Exponent 2 of Prime Factorization for a given Integer- Parameters:
n- The Integer- Returns:
- Exponent 2 of Prime Factorization for a given Integer
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
BinaryDigitCount
public static final int BinaryDigitCount(int n)Retrieve the Binary Digit Count- Parameters:
n- N- Returns:
- The Binary Digit Count
-
DivideIntegers
public static final int DivideIntegers(int dividend, int divisor) throws java.lang.ExceptionDivide two integers without using multiplication, division, and mod operator. The integer division should truncate toward zero, which means losing its fractional part. For example, truncate(8.345) = 8 and truncate(-2.7335) = -2.- Parameters:
dividend- Dividenddivisor- Divisor- Returns:
- The quotient after dividing dividend by divisor
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
Power
public static final double Power(double x, int n) throws java.lang.ExceptionCompute the Integer Power of x- Parameters:
x- xn- n- Returns:
- Integer Power of x
- Throws:
java.lang.Exception- Thrown if the Inputs are Invalid
-
NextPrimePalindrome
public static final int NextPrimePalindrome(int n)Find the smallest prime palindrome greater than or equal to the input number. Recall that a number is prime if it's only divisors are 1 and itself, and it is greater than 1. For example, 2,3,5,7,11 and 13 are primes. Recall that a number is a palindrome if it reads the same from left to right as it does from right to left. For example, 12321 is a palindrome.- Parameters:
n- The Number- Returns:
- The Next Prime Palindrome
-
GCD
public static final long GCD(long a, long b)Greatest Common Divisor between Two Numbers- Parameters:
a- First Numberb- Second Number- Returns:
- The GCD
-
NthUglyNumber
public static final long NthUglyNumber(long n, long a, long b, long c)Find the nth ugly number. Ugly numbers are positive integers which are divisible by a or b or c.- Parameters:
n- na- Ab- Bc- C- Returns:
- The nth Ugly Number
-
IsNumberSequenceAdditive
public static final boolean IsNumberSequenceAdditive(java.lang.String s)Additive number is a string whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two. Given a string containing only digits '0'-'9', write a function to determine if it's an additive number. Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.- Parameters:
s- The Input String- Returns:
- TRUE - Input is an Additive Number
-
TrailingFactorialZeros
public static final int TrailingFactorialZeros(int n)Estimate the Trailing Factorial Zeros- Parameters:
n- Factorial NUmber- Returns:
- Number of Training Factor Zeros
-
CanMeasureWater
public static final boolean CanMeasureWater(int x, int y, int z)You are given two jugs with capacities x and y liters. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z liters using these two jugs. If z liters of water is measurable, you must have z liters of water contained within one or both buckets by the end. Operations allowed: Fill any of the jugs completely with water. Empty any of the jugs. Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.- Parameters:
x- xy- yz- z- Returns:
- TRUE - The Measurement can be successfully carried out
-
NthDigit
public static final int NthDigit(int n)Identify the nth Digit of the Number- Parameters:
n- The Number- Returns:
- The nth Digit
-
Quotient2
public static final int Quotient2(int dividend, int divisor)Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator. Return the quotient after dividing dividend by divisor. The integer division should truncate toward zero, which means losing its fractional part. For example, truncate(8.345) = 8 and truncate(-2.7335) = -2. Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For this problem, assume that your function returns 231 − 1 when the division result overflows.- Parameters:
dividend- Dividenddivisor- Divisor- Returns:
- Quotient
-
ReverseInteger
public static final int ReverseInteger(int number)Given a signed 32-bit integer number, return number with its digits reversed. If reversing number causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.- Parameters:
number- The Given Number- Returns:
- The Reverse
-