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.String
ArrayRow(double[] doubleArray, int preDecimalPlaces, int postDecimalPlaces, boolean bailOnNaN)
Print the contents of the 1D array to the Specified Decimal Location as a Rowstatic int
BinaryDigitCount(int n)
Retrieve the Binary Digit Countstatic double
Bound(double dblX, double dblFloor, double dblCeiling)
Bound the input to within (floor, Ceiling), i.e., compute Min (Max (floor, X), Ceiling)static boolean
CanMeasureWater(int x, int y, int z)
You are given two jugs with capacities x and y liters.static int
DivideIntegers(int dividend, int divisor)
Divide two integers without using multiplication, division, and mod operator.static double
DoubleFactorial(int n)
Compute (2n - 1)!!static int
Factorial(int n)
This function implements Factorial N.static double
Fractional(double z)
Retrieve the Fractional Part of zstatic long
GCD(long a, long b)
Greatest Common Divisor between Two Numbersstatic double
HalfDownShiftedFactorial(int n)
Compute (n - 0.5)!static boolean
IsInteger(double z)
Indicate if z is an Integerstatic boolean
IsNegativeInteger(double z)
Indicate if z is a Negative Integerstatic boolean
IsNonNegativeInteger(double z)
Indicate if z is a Non-Negative Integerstatic boolean
IsNonPositiveInteger(double z)
Indicate if z is a Non-Positive Integerstatic boolean
IsNumberSequenceAdditive(java.lang.String s)
Additive number is a string whose digits can form additive sequence.static boolean
IsPositiveInteger(double z)
Indicate if z is a Positive Integerstatic boolean
IsValid(double dbl)
Checks if the input double is Infinite or NaNstatic boolean
IsValid(double[] adbl)
Checks if the input double array contains an Infinite or an NaNstatic boolean
IsValid(double[][] r1Grid)
Checks if the Input Matrix contains an Infinite or an NaNstatic boolean
IsValid(long l)
Check if the Input Long is MIN_VALUE or MAX_VALUEstatic boolean
IsValid(long[] al)
Check if the Input Long Array contains a MIN_VALUE or MAX_VALUEstatic double
Maximum(double[] adbl)
Retrieve the Maximum Element in the specified Arraystatic double
Minimum(double[] adbl)
Retrieve the Minimum Element in the specified Arraystatic int
NCK(int n, int k)
This function implements N choose K.static int
NextPrimePalindrome(int n)
Find the smallest prime palindrome greater than or equal to the input number.static boolean
NormalizedPositive(double[] array)
Check if the Array Elements are Normalized and Positivestatic int
NPK(int n, int k)
This function implements N Permute K.static int
NthDigit(int n)
Identify the nth Digit of the Numberstatic long
NthUglyNumber(long n, long a, long b, long c)
Find the nth ugly number.static double
PochhammerSymbol(double s, int k)
Compute the Pochhammer Symbol for the Specified s and kstatic double
Power(double x, int n)
Compute the Integer Power of xstatic double
PrimeFactorExponentTwo(int n)
Compute the Exponent 2 of Prime Factorization for a given Integerstatic boolean
Print1DArray(java.lang.String strName, double[] adblA, boolean bBailOnNaN)
Print the contents of the 1D arraystatic boolean
Print1DArray(java.lang.String strName, double[] adblA, int iNumDecimal, boolean bBailOnNaN)
Print the contents of the 1D array to the Specified Decimal Locationstatic boolean
Print2DArray(java.lang.String strName, double[][] aadblA, boolean bBailOnNaN)
Print the contents of the 2D arraystatic boolean
Print2DArrayPair(java.lang.String strLeftLabel, java.lang.String strRightLabel, double[][] aadblLeft, double[][] aadblRight, boolean bBailOnNaN)
Print the Contents of the 2D Array Pairstatic 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 Tripletstatic boolean
PrintMatrix(java.lang.String strName, double[][] aadblA)
Print the Matrix Contentsstatic int
Quotient2(int dividend, int divisor)
Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.static int
ReciprocalIntegerFloor(double z)
Retrieve the Reciprocal Integer Floor of zstatic int
ReverseInteger(int number)
Given a signed 32-bit integer number, return number with its digits reversed.static double
RisingPochhammerSymbol(double s, int k)
Compute the Rising Pochhammer Symbol for the Specified s and kstatic boolean
SameSign(double[] adbl)
Check if the specified array contains elements all of the same signstatic double
Sign(double z)
Indicate the Sign of zstatic double
SubFactorial(int n)
This function implements Sub-factorial N.static int
TrailingFactorialZeros(int n)
Estimate the Trailing Factorial Zerosstatic boolean
WithinTolerance(double dbl1, double dbl2)
Compare and checks if the two input numbers fall within a specified tolerancestatic boolean
WithinTolerance(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
-
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
-