Package jsl.utilities.math
Class JSLMath
- java.lang.Object
-
- jsl.utilities.math.JSLMath
-
public final class JSLMath extends java.lang.ObjectThis class implements additional mathematical functions and determines the parameters of the floating point representation.This is based on the DhbMath class of Didier Besset in "Object-Oriented Implementation of Numerical Methods", Morgan-Kaufmann
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static doublebinomialCoefficient(int n, int k)Computes the binomial coefficient.static booleanequal(double a, double b)Compares two numbers a and b and checks if they are within the default numerical precision of each other.static booleanequal(double a, double b, double precision)Compares two numbers a and b and checks if they are within the supplied precision of each other.static doublefactorial(int n)Returns the factorial (n!) of the numberstatic doublegetDefaultNumericalPrecision()Gets the default numerical precision.static doublegetLargestExponentialArgument()Computes the largest exponent argumentstatic doublegetLargestNumber()Gets the largest positive number that can be representedstatic doublegetMachinePrecision()Gets the largest positive number that when added to 1 yields 1static intgetMaxNumIterations()Gets the default maximum number of iterations A constant that can be used in algorithms to specify the maximum number of iterations.static doublegetNegativeMachinePrecision()Gets the largest positive number that when subtracted from 1 yield 1static intgetRadix()Gets the radix of the floating point representationstatic doublegetSmallestExponentialArgument()Computes the smallest exponent argumentstatic doublegetSmallestNumber()Gets the smallest positive number different from 0.0static doublegetSmallNumber()Returns the number that can be added to some value without noticeably changing the result of the computationstatic doublelogFactorial(int n)Computes the natural logarithm of the factorial operator.static voidprintParameters(java.io.PrintStream printStream)Computes and prints the mathematical precision parameters to the supplied PrintStreamstatic voidreset()Resets the constants.static doubleroundTo(double value, double scale)This method returns the specified value rounded to the nearest integer multiple of the specified scale.static doubleroundToScale(double value, boolean integerValued)Round the specified value upward to the next scale value.static voidsetMaxNumIterations(int iterations)Sets the default maximum number of iterations A constant that can be used in algorithms to specify the maximum number of iterations.static doublesign(double x)Get the sign of the number based on the equal() method Equal is 0.0, positive is 1.0, negative is -1.0static booleanwithin(double a, double b, double precision)Returns true if Math.abs(a-b) < precision
-
-
-
Method Detail
-
getDefaultNumericalPrecision
public static double getDefaultNumericalPrecision()
Gets the default numerical precision. This represents an estimate of the precision expected for a general numerical computation. For example, two numbers x and y can be considered equal if the relative difference between them is less than the default numerical precision. This value has been defined as the square root of the machine precision- Returns:
- the default numerical precision
-
getMaxNumIterations
public static int getMaxNumIterations()
Gets the default maximum number of iterations A constant that can be used in algorithms to specify the maximum number of iterations. This is static and thus a change will change it for any algorithm that depends on this constant- Returns:
- the maximum number of iterations
-
setMaxNumIterations
public static void setMaxNumIterations(int iterations)
Sets the default maximum number of iterations A constant that can be used in algorithms to specify the maximum number of iterations. This is static and thus a change will change it for any algorithm that depends on this constant- Parameters:
iterations- the number of iterations
-
equal
public static boolean equal(double a, double b)Compares two numbers a and b and checks if they are within the default numerical precision of each other.- Parameters:
a- doubleb- double- Returns:
- boolean true if the difference between a and b is less than the default numerical precision
-
equal
public static boolean equal(double a, double b, double precision)Compares two numbers a and b and checks if they are within the supplied precision of each other.- Parameters:
a- doubleb- doubleprecision- double- Returns:
- boolean true if the relative difference between a and b is less than precision
-
within
public static boolean within(double a, double b, double precision)Returns true if Math.abs(a-b) < precision- Parameters:
a- the first numberb- the second numberprecision- the precision to check- Returns:
- true if within the precision
-
getLargestExponentialArgument
public static double getLargestExponentialArgument()
Computes the largest exponent argument- Returns:
- the largest exponent argument
-
getSmallestExponentialArgument
public static double getSmallestExponentialArgument()
Computes the smallest exponent argument- Returns:
- the smallest exponent argument
-
getLargestNumber
public static double getLargestNumber()
Gets the largest positive number that can be represented- Returns:
- the largest positive number that can be represented
-
getMachinePrecision
public static double getMachinePrecision()
Gets the largest positive number that when added to 1 yields 1- Returns:
- the largest positive number that when added to 1 yields 1
-
getNegativeMachinePrecision
public static double getNegativeMachinePrecision()
Gets the largest positive number that when subtracted from 1 yield 1- Returns:
- the largest positive number that when subtracted from 1 yield 1
-
getRadix
public static int getRadix()
Gets the radix of the floating point representation- Returns:
- the radix of the floating point representation
-
getSmallestNumber
public static double getSmallestNumber()
Gets the smallest positive number different from 0.0- Returns:
- the smallest positive number different from 0.0
-
printParameters
public static void printParameters(java.io.PrintStream printStream)
Computes and prints the mathematical precision parameters to the supplied PrintStream- Parameters:
printStream- the stream to write to
-
reset
public static void reset()
Resets the constants. They will be recomputed at next usage
-
roundTo
public static double roundTo(double value, double scale)This method returns the specified value rounded to the nearest integer multiple of the specified scale.- Parameters:
value- number to be roundedscale- defining the rounding scale- Returns:
- rounded value
-
roundToScale
public static double roundToScale(double value, boolean integerValued)Round the specified value upward to the next scale value.- Parameters:
value- the value to be rounded.integerValued- fag specified whether integer scale are used, otherwise double scale is used.- Returns:
- a number rounded upward to the next scale value.
-
getSmallNumber
public static double getSmallNumber()
Returns the number that can be added to some value without noticeably changing the result of the computation- Returns:
- the number that can be added to some value without noticeably changing the result of the computation
-
sign
public static double sign(double x)
Get the sign of the number based on the equal() method Equal is 0.0, positive is 1.0, negative is -1.0- Parameters:
x- the number- Returns:
- the sign of the number
-
factorial
public static double factorial(int n)
Returns the factorial (n!) of the number- Parameters:
n- The number to take the factorial of- Returns:
- The factorial of the number.
-
binomialCoefficient
public static double binomialCoefficient(int n, int k)Computes the binomial coefficient. Computes the number of combinations of size k that can be formed from n distinct objects.- Parameters:
n- The total number of distinct itemsk- The number of subsets- Returns:
- the binomial coefficient
-
logFactorial
public static double logFactorial(int n)
Computes the natural logarithm of the factorial operator. ln(n!)- Parameters:
n- The value to be operated on.- Returns:
- the log of the factorial
-
-